-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcitizen.ts
500 lines (451 loc) · 15.2 KB
/
citizen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import { readFileSync } from "fs";
import { join } from "path";
import yaml from "yaml";
import { initSimnet, Simnet } from "@hirosystems/clarinet-sdk";
import { EpochString } from "@hirosystems/clarinet-sdk-wasm";
import {
BufferCV,
Cl,
ClarityType,
cvToJSON,
cvToValue,
hexToCV,
OptionalCV,
} from "@stacks/transactions";
import {
Batch,
ContractDeploymentProperties,
ContractsByEpoch,
SimnetPlan,
Transaction,
} from "./citizen.types";
import { RemoteDataSettings } from "./app.types";
/**
* Prepares the simnet instance and assures the target contract's corresponding
* test contract is treated as a first-class citizen, relying on their
* concatenation. This function handles:
* - Contract sorting by epoch based on the deployment plan.
* - Combining the target contract with its tests and deploying all contracts
* to the simnet.
*
* @param manifestDir The relative path to the manifest directory.
* @param manifestPath The absolute path to the manifest file.
* @param remoteDataSettings The remote data settings.
* @param sutContractName The target contract name.
* @returns The initialized simnet instance with all contracts deployed, with
* the test contract treated as a first-class citizen of the target contract.
*/
export const issueFirstClassCitizenship = async (
manifestDir: string,
manifestPath: string,
remoteDataSettings: RemoteDataSettings,
sutContractName: string
): Promise<Simnet> => {
// Initialize the simnet, to generate the simnet plan and instance. The empty
// session will be set up, and contracts will be deployed in the correct
// order based on the simnet plan a few lines below.
const simnet = await initSimnet(manifestPath);
const simnetPlan = yaml.parse(
readFileSync(join(manifestDir, "deployments", "default.simnet-plan.yaml"), {
encoding: "utf-8",
})
);
const sortedContractsByEpoch =
groupContractsByEpochFromSimnetPlan(simnetPlan);
const simnetAddresses = [...simnet.getAccounts().values()];
const stxBalancesMap = new Map(
simnetAddresses.map((address) => {
const balanceHex = simnet.runSnippet(`(stx-get-balance '${address})`);
return [address, cvToValue(hexToCV(balanceHex))];
})
);
const sbtcBalancesMap = new Map(
simnetAddresses.map((address) => {
try {
const { result: getBalanceResult } = simnet.callReadOnlyFn(
"SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token",
"get-balance",
[Cl.principal(address)],
address
);
// If the previous read-only call works, the user is working with
// sBTC. This means we can proceed with restoring sBTC balances.
const sbtcBalance = cvToJSON(getBalanceResult).value.value;
return [address, sbtcBalance];
} catch (e) {
return [address, 0];
}
})
);
await simnet.initEmptySession(remoteDataSettings);
simnetAddresses.forEach((address) => {
simnet.mintSTX(address, stxBalancesMap.get(address)!);
});
// Combine the target contract with its tests into a single contract. The
// resulting contract will replace the target contract in the simnet.
/** The contract names mapped to the concatenated source code. */
const rendezvousSources = new Map(
[sutContractName]
.map((contractName) =>
buildRendezvousData(simnetPlan, contractName, manifestDir)
)
.map((rendezvousContractData) => [
rendezvousContractData.rendezvousContractName,
rendezvousContractData.rendezvousSource,
])
);
// Deploy the contracts to the simnet in the correct order.
await deployContracts(simnet, sortedContractsByEpoch, (name, props) =>
getContractSource(
[sutContractName],
rendezvousSources,
name,
props,
manifestDir
)
);
// Filter out addresses with zero balance. They do not need to be restored.
const sbtcBalancesToRestore: Map<string, number> = new Map(
[...sbtcBalancesMap.entries()].filter(([_, balance]) => balance !== 0)
);
// After all the contracts and requirements are deployed, if the test wallets
// had sBTC balances previously, restore them. If no test wallet previously
// owned sBTC, skip this step.
if ([...sbtcBalancesToRestore.keys()].length > 0) {
restoreSbtcBalances(simnet, sbtcBalancesToRestore);
}
return simnet;
};
/**
* Groups contracts by epoch from the simnet plan.
* @param simnetPlan The simnet plan.
* @returns A record of contracts grouped by epoch. The record key is the epoch
* string, and the value is an array of contracts. Each contract is represented
* as a record with the contract name as the key and a record containing the
* contract path and clarity version as the value.
*/
export const groupContractsByEpochFromSimnetPlan = (
simnetPlan: SimnetPlan
): ContractsByEpoch => {
return simnetPlan.plan.batches.reduce(
(acc: ContractsByEpoch, batch: Batch) => {
const epoch = batch.epoch;
const contracts = batch.transactions
.filter((tx) => tx["emulated-contract-publish"])
.map((tx) => {
const contract = tx["emulated-contract-publish"]!;
return {
[contract["contract-name"]]: {
path: contract.path,
clarity_version: contract["clarity-version"],
},
};
});
if (contracts.length > 0) {
acc[epoch] = (acc[epoch] || []).concat(contracts);
}
return acc;
},
{} as ContractsByEpoch
);
};
/**
* Deploys the contracts to the simnet in the correct order.
* @param simnet The simnet instance.
* @param contractsByEpoch The record of contracts by epoch.
* @param getContractSourceFn The function to retrieve the contract source.
*/
const deployContracts = async (
simnet: Simnet,
contractsByEpoch: ContractsByEpoch,
getContractSourceFn: (
name: string,
props: ContractDeploymentProperties
) => string
): Promise<void> => {
for (const [epoch, contracts] of Object.entries(contractsByEpoch)) {
// Move to the next epoch and deploy the contracts in the correct order.
simnet.setEpoch(epoch as EpochString);
for (const contract of contracts.flatMap(Object.entries)) {
const [name, props]: [string, ContractDeploymentProperties] = contract;
const source = getContractSourceFn(name, props);
// For requirement contracts, use the original sender. The sender address
// is included in the path:
// "./.cache/requirements/<address>.contract-name.clar".
const sender = props.path.includes(".cache")
? props.path.split("requirements")[1].slice(1).split(".")[0]
: simnet.deployer;
simnet.deployContract(
name,
source,
{ clarityVersion: props.clarity_version },
sender
);
}
}
};
/**
* Conditionally retrieves the contract source based on whether the contract is
* a SUT contract or not.
* @param targetContractNames The list of target contract names.
* @param rendezvousSourcesMap The contract names mapped to the concatenated
* source code.
* @param contractName The contract name.
* @param contractProps The contract deployment properties.
* @param manifestDir The relative path to the manifest directory.
* @returns The contract source code.
*/
export const getContractSource = (
targetContractNames: string[],
rendezvousSourcesMap: Map<string, string>,
contractName: string,
contractProps: ContractDeploymentProperties,
manifestDir: string
): string => {
if (targetContractNames.includes(contractName)) {
const contractSource = rendezvousSourcesMap.get(contractName);
if (!contractSource) {
throw new Error(`Contract source not found for ${contractName}`);
}
return contractSource;
} else {
return readFileSync(join(manifestDir, contractProps.path), {
encoding: "utf-8",
});
}
};
/**
* Builds the Rendezvous data.
* @param simnetPlan The parsed simnet plan.
* @param contractName The contract name.
* @param manifestDir The relative path to the manifest directory.
* @returns The Rendezvous data representing a record. The returned record
* contains the Rendezvous source code and the Rendezvous contract name.
*/
export const buildRendezvousData = (
simnetPlan: SimnetPlan,
contractName: string,
manifestDir: string
) => {
try {
const sutContractSource = getSimnetPlanContractSource(
simnetPlan,
manifestDir,
contractName
);
const testContractSource = getTestContractSource(
simnetPlan,
contractName,
manifestDir
);
const rendezvousSource = scheduleRendezvous(
sutContractSource!,
testContractSource
);
return {
rendezvousSource: rendezvousSource,
rendezvousContractName: contractName,
};
} catch (error: any) {
throw new Error(
`Error processing "${contractName}" contract: ${error.message}`
);
}
};
/**
* Retrieves the contract source code using the simnet plan.
* @param simnetPlan The parsed simnet plan.
* @param manifestDir The relative path to the manifest directory.
* @param sutContractName The target contract name.
* @returns The contract source code.
*/
const getSimnetPlanContractSource = (
simnetPlan: SimnetPlan,
manifestDir: string,
sutContractName: string
) => {
// Filter for transactions that contain "emulated-contract-publish".
const contractInfo = simnetPlan.plan.batches
.flatMap((batch: Batch) => batch.transactions)
.find(
(transaction: Transaction) =>
transaction["emulated-contract-publish"] &&
transaction["emulated-contract-publish"]["contract-name"] ===
sutContractName
)?.["emulated-contract-publish"];
if (contractInfo == undefined) {
throw new Error(
`"${sutContractName}" contract not found in Clarinet.toml.`
);
}
return readFileSync(join(manifestDir, contractInfo.path), {
encoding: "utf-8",
}).toString();
};
/**
* Retrieves the test contract source code.
* @param simnetPlan The parsed simnet plan.
* @param sutContractName The target contract name.
* @param manifestDir The relative path to the manifest directory.
* @returns The test contract source code.
*/
export const getTestContractSource = (
simnetPlan: SimnetPlan,
sutContractName: string,
manifestDir: string
): string => {
const contractInfo = simnetPlan.plan.batches
.flatMap((batch: Batch) => batch.transactions)
.find(
(transaction: Transaction) =>
transaction["emulated-contract-publish"] &&
transaction["emulated-contract-publish"]["contract-name"] ===
sutContractName
)?.["emulated-contract-publish"];
const sutContractPath = contractInfo!.path;
const extension = ".clar";
if (!sutContractPath.endsWith(extension)) {
throw new Error(
`Invalid contract extension for the "${sutContractName}" contract.`
);
}
const testContractPath = sutContractPath.replace(
extension,
`.tests${extension}`
);
try {
return readFileSync(join(manifestDir, testContractPath), {
encoding: "utf-8",
}).toString();
} catch (error: any) {
throw new Error(
`Error retrieving the corresponding test contract for the "${sutContractName}" contract. ${error.message}`
);
}
};
/**
* Schedules a Rendezvous between the System Under Test (`SUT`) and the test
* contract.
* @param targetContractSource The target contract source code.
* @param tests The corresponding test contract source code.
* @returns The Rendezvous source code.
*/
export function scheduleRendezvous(
targetContractSource: string,
tests: string
): string {
/**
* The `context` map tracks how many times each function has been called.
* This data can be useful for invariant tests to check behavior over time.
*/
const context = `(define-map context (string-ascii 100) {
called: uint
;; other data
})
(define-public (update-context (function-name (string-ascii 100)) (called uint))
(ok (map-set context function-name {called: called})))`;
return `${targetContractSource}\n\n${context}\n\n${tests}`;
}
/**
* Utility function that restores the test wallets' initial sBTC balances in
* the re-initialized first-class citizenship simnet.
*
* @param simnet The simnet instance.
* @param sbtcBalancesMap A map containing the test wallets' balances to be
* restored.
*/
const restoreSbtcBalances = (
simnet: Simnet,
sbtcBalancesMap: Map<string, number>
) => {
// For each address present in the balances map, restore the balance.
[...sbtcBalancesMap.entries()]
// Re-assure the map does not contain nil balances.
.filter(([_, balance]) => balance !== 0)
.forEach(([address, balance]) => {
// To deposit sBTC, one needs a txId and a sweep txId. A deposit transaction
// must have a unique txId and sweep txId.
const txId = getUniqueHex();
const sweepTxId = getUniqueHex();
mintSbtc(simnet, balance, address, txId, sweepTxId);
});
};
/**
* Utility function to deposit an amount of sBTC to a Stacks address.
*
* @param simnet The simnet instance.
* @param amountSats The amount to mint in sats.
* @param recipient The Stacks address to mint sBTC to.
* @param txId A unique hex to use for the deposit.
* @param sweepTxId A unique hex to use for the deposit.
*/
const mintSbtc = (
simnet: Simnet,
amountSats: number,
recipient: string,
txId: string,
sweepTxId: string
) => {
// Calling `get-burn-header` only works for past block heights. We mine one
// empty Stacks block if the initial height is 0.
if (simnet.blockHeight === 0) {
simnet.mineEmptyBlock();
}
const previousStacksHeight = simnet.blockHeight - 1;
const burnHash = simnet.callReadOnlyFn(
"SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-deposit",
"get-burn-header",
[
// (height uint)
Cl.uint(previousStacksHeight),
],
simnet.deployer
).result as OptionalCV<BufferCV>;
if (burnHash === null || burnHash.type === ClarityType.OptionalNone) {
throw new Error("Something went wrong trying to retrieve the burn header.");
}
const completeDepositTx = cvToJSON(
simnet.callPublicFn(
"SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-deposit",
"complete-deposit-wrapper",
[
// (txid (buff 32))
Cl.bufferFromHex(txId),
// (vout-index uint)
Cl.uint(1),
// (amount uint)
Cl.uint(amountSats),
// (recipient principal)
Cl.principal(recipient),
// (burn-hash (buff 32))
burnHash.value,
// (burn-height uint)
Cl.uint(previousStacksHeight),
// (sweep-txid (buff 32))
Cl.bufferFromHex(sweepTxId),
],
"SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4"
).result
);
// If the deposit transaction fails, an unexpected outcome can happen. Throw
// an error if the transaction is not successful.
if (!completeDepositTx.success) {
throw new Error("Something went wrong trying to restore sBTC balances.");
}
};
/**
* Utility function that generates a random, unique hex to be used as txId in
* `mintSbtc`.
*
* @returns A random hex string.
*/
const getUniqueHex = (): string => {
let hex: string;
// Generate a 32-byte (64 character) random hex string.
const bytes = new Uint8Array(32);
crypto.getRandomValues(bytes);
hex = Array.from(bytes)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
return hex;
};