-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitizen.ts
338 lines (307 loc) · 10.1 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
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 {
Batch,
ContractsByEpoch,
SimnetPlan,
Transaction,
} from "./citizen.types";
/**
* Prepares the simnet environment and assures the target contract is treated
* as a first-class citizen, by combining it with its tests. 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 sutContractName - The target contract name.
* @returns The initialized simnet instance with all contracts deployed, with
* the target contract treated as a first-class citizen.
*/
export const issueFirstClassCitizenship = async (
manifestDir: string,
sutContractName: string
): Promise<Simnet> => {
const manifestPath = join(manifestDir, "Clarinet.toml");
// 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);
await simnet.initEmptySession();
// Combine the target contract with its tests into a single contract. The
// resulting contract will replace the target contract in the simnet. This
// map stores the contract name and its corresponding 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
)
);
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
);
};
/**
* Deploy 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: Record<
EpochString,
Record<string, { path: string; clarity_version: 1 | 2 | 3 }>[]
>,
getContractSourceFn: (
name: string,
props: {
path: string;
clarity_version: 1 | 2 | 3;
}
) => 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,
{ path: string; clarity_version: 1 | 2 | 3 }
] = 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 retrieve the contract source based on whether the contract is
* a SUT contract or not.
* @param sutContractNames - The list of SUT contract names.
* @param rendezvousMap - The rendezvous map.
* @param contractName - The contract name.
* @param contractProps - The contract properties.
* @param manifestDir - The relative path to the manifest directory.
* @returns The contract source.
*/
export const getContractSource = (
sutContractNames: string[],
rendezvousMap: Map<string, string>,
contractName: string,
contractProps: {
path: string;
clarity_version: 1 | 2 | 3;
},
manifestDir: string
): string => {
if (sutContractNames.includes(contractName)) {
const contractSource = rendezvousMap.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",
});
}
};
/**
* Build 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 an object. The returned object
* 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 (e: any) {
throw new Error(
`Error processing "${contractName}" contract: ${e.message}`
);
}
};
/**
* Get the contract source code from 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();
};
/**
* Get 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 (e: any) {
throw new Error(
`Error retrieving the corresponding test contract for the "${sutContractName}" contract. ${e.message}`
);
}
};
/**
* Schedule a Rendezvous between the System Under Test (`SUT`) and the
* invariants.
* @param sutContractSource The SUT contract source code.
* @param invariants The invariants contract source code.
* @returns The Rendezvous source code.
*/
export function scheduleRendezvous(
sutContractSource: string,
invariants: 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 `${sutContractSource}\n\n${context}\n\n${invariants}`;
}