@stacks/rendezvous 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +127 -0
- package/dist/app.js +116 -0
- package/dist/citizen.js +231 -0
- package/dist/citizen.types.js +2 -0
- package/dist/heatstroke.js +76 -0
- package/dist/invariant.js +239 -0
- package/dist/invariant.types.js +2 -0
- package/dist/package.json +42 -0
- package/dist/property.js +247 -0
- package/dist/property.types.js +3 -0
- package/dist/shared.js +285 -0
- package/dist/shared.types.js +2 -0
- package/package.json +42 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.initializeClarityContext = exports.initializeLocalContext = exports.checkInvariants = void 0;
|
|
7
|
+
const shared_1 = require("./shared");
|
|
8
|
+
const transactions_1 = require("@stacks/transactions");
|
|
9
|
+
const heatstroke_1 = require("./heatstroke");
|
|
10
|
+
const fast_check_1 = __importDefault(require("fast-check"));
|
|
11
|
+
const ansicolor_1 = require("ansicolor");
|
|
12
|
+
const checkInvariants = (simnet, sutContractName, rendezvousList, rendezvousAllFunctions, seed, path, runs, radio) => {
|
|
13
|
+
// A map where the keys are the Rendezvous identifiers and the values are
|
|
14
|
+
// arrays of their SUT (System Under Test) functions. This map will be used
|
|
15
|
+
// to access the SUT functions for each Rendezvous contract afterwards.
|
|
16
|
+
const rendezvousSutFunctions = filterSutFunctions(rendezvousAllFunctions);
|
|
17
|
+
// A map where the keys are the Rendezvous identifiers and the values are
|
|
18
|
+
// arrays of their invariant functions. This map will be used to access the
|
|
19
|
+
// invariant functions for each Rendezvous contract afterwards.
|
|
20
|
+
const rendezvousInvariantFunctions = filterInvariantFunctions(rendezvousAllFunctions);
|
|
21
|
+
// Set up local context to track SUT function call counts.
|
|
22
|
+
const localContext = (0, exports.initializeLocalContext)(rendezvousSutFunctions);
|
|
23
|
+
// Set up context in simnet by initializing state for SUT.
|
|
24
|
+
(0, exports.initializeClarityContext)(simnet, rendezvousSutFunctions);
|
|
25
|
+
radio.emit("logMessage", `\nStarting invariant testing type for the ${sutContractName} contract...\n`);
|
|
26
|
+
const simnetAccounts = simnet.getAccounts();
|
|
27
|
+
const eligibleAccounts = new Map([...simnetAccounts].filter(([key]) => key !== "faucet"));
|
|
28
|
+
const simnetAddresses = Array.from(simnetAccounts.values());
|
|
29
|
+
// The Rendezvous identifier is the first one in the list. Only one contract
|
|
30
|
+
// can be fuzzed at a time.
|
|
31
|
+
const rendezvousContractId = rendezvousList[0];
|
|
32
|
+
const functions = (0, shared_1.getFunctionsListForContract)(rendezvousSutFunctions, rendezvousContractId);
|
|
33
|
+
const invariantFunctions = (0, shared_1.getFunctionsListForContract)(rendezvousInvariantFunctions, rendezvousContractId);
|
|
34
|
+
if ((functions === null || functions === void 0 ? void 0 : functions.length) === 0) {
|
|
35
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No public functions found for the "${sutContractName}" contract. Without public functions, no state transitions can happen inside the contract, and the invariant test is not meaningful.\n`));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if ((invariantFunctions === null || invariantFunctions === void 0 ? void 0 : invariantFunctions.length) === 0) {
|
|
39
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No invariant functions found for the "${sutContractName}" contract. Beware, for your contract may be exposed to unforeseen issues.\n`));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const eligibleFunctions = functions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
43
|
+
const eligibleInvariants = invariantFunctions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
44
|
+
if (eligibleFunctions.length === 0) {
|
|
45
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible public functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (eligibleInvariants.length === 0) {
|
|
49
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible invariant functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const radioReporter = (runDetails) => {
|
|
53
|
+
(0, heatstroke_1.reporter)(runDetails, radio, "invariant");
|
|
54
|
+
};
|
|
55
|
+
fast_check_1.default.assert(fast_check_1.default.property(fast_check_1.default
|
|
56
|
+
.record({
|
|
57
|
+
// The target contract identifier. It is a constant value equal
|
|
58
|
+
// to the first contract in the list. The arbitrary is still needed,
|
|
59
|
+
// being used for reporting purposes in `heatstroke.ts`.
|
|
60
|
+
rendezvousContractId: fast_check_1.default.constant(rendezvousContractId),
|
|
61
|
+
sutCaller: fast_check_1.default.constantFrom(...eligibleAccounts.entries()),
|
|
62
|
+
invariantCaller: fast_check_1.default.constantFrom(...eligibleAccounts.entries()),
|
|
63
|
+
canMineBlocks: fast_check_1.default.boolean(),
|
|
64
|
+
})
|
|
65
|
+
.chain((r) => fast_check_1.default
|
|
66
|
+
.record({
|
|
67
|
+
selectedFunction: fast_check_1.default.constantFrom(...eligibleFunctions),
|
|
68
|
+
selectedInvariant: fast_check_1.default.constantFrom(...eligibleInvariants),
|
|
69
|
+
})
|
|
70
|
+
.map((selectedFunctions) => (Object.assign(Object.assign({}, r), selectedFunctions))))
|
|
71
|
+
.chain((r) => fast_check_1.default
|
|
72
|
+
.record({
|
|
73
|
+
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedFunction, simnetAddresses)),
|
|
74
|
+
invariantArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedInvariant, simnetAddresses)),
|
|
75
|
+
})
|
|
76
|
+
.map((args) => (Object.assign(Object.assign({}, r), args))))
|
|
77
|
+
.chain((r) => fast_check_1.default
|
|
78
|
+
.record({
|
|
79
|
+
burnBlocks: r.canMineBlocks
|
|
80
|
+
? // This arbitrary produces integers with a maximum value
|
|
81
|
+
// inversely proportional to the number of runs:
|
|
82
|
+
// - Fewer runs result in a higher maximum burn blocks,
|
|
83
|
+
// allowing more blocks to be mined.
|
|
84
|
+
// - More runs result in a lower maximum burn blocks, as more
|
|
85
|
+
// blocks are mined overall.
|
|
86
|
+
fast_check_1.default.integer({
|
|
87
|
+
min: 1,
|
|
88
|
+
max: Math.ceil(100000 / (runs || 100)),
|
|
89
|
+
})
|
|
90
|
+
: fast_check_1.default.constant(0),
|
|
91
|
+
})
|
|
92
|
+
.map((burnBlocks) => (Object.assign(Object.assign({}, r), burnBlocks)))), (r) => {
|
|
93
|
+
const selectedFunctionArgs = (0, shared_1.argsToCV)(r.selectedFunction, r.functionArgsArb);
|
|
94
|
+
const selectedInvariantArgs = (0, shared_1.argsToCV)(r.selectedInvariant, r.invariantArgsArb);
|
|
95
|
+
const printedFunctionArgs = r.functionArgsArb
|
|
96
|
+
.map((arg) => {
|
|
97
|
+
try {
|
|
98
|
+
return typeof arg === "object"
|
|
99
|
+
? JSON.stringify(arg)
|
|
100
|
+
: arg.toString();
|
|
101
|
+
}
|
|
102
|
+
catch (_a) {
|
|
103
|
+
return "[Circular]";
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
.join(" ");
|
|
107
|
+
const [sutCallerWallet, sutCallerAddress] = r.sutCaller;
|
|
108
|
+
try {
|
|
109
|
+
const { result: functionCallResult } = simnet.callPublicFn(r.rendezvousContractId, r.selectedFunction.name, selectedFunctionArgs, sutCallerAddress);
|
|
110
|
+
const functionCallResultJson = (0, transactions_1.cvToJSON)(functionCallResult);
|
|
111
|
+
if (functionCallResultJson.success) {
|
|
112
|
+
localContext[r.rendezvousContractId][r.selectedFunction.name]++;
|
|
113
|
+
simnet.callPublicFn(r.rendezvousContractId, "update-context", [
|
|
114
|
+
transactions_1.Cl.stringAscii(r.selectedFunction.name),
|
|
115
|
+
transactions_1.Cl.uint(localContext[r.rendezvousContractId][r.selectedFunction.name]),
|
|
116
|
+
], simnet.deployer);
|
|
117
|
+
radio.emit("logMessage", `₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
118
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
119
|
+
(0, ansicolor_1.dim)(`${sutCallerWallet} `) +
|
|
120
|
+
`${sutContractName} ` +
|
|
121
|
+
`${(0, ansicolor_1.underline)(r.selectedFunction.name)} ` +
|
|
122
|
+
printedFunctionArgs);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
radio.emit("logMessage", (0, ansicolor_1.dim)(`₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
126
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
127
|
+
`${sutCallerWallet} ` +
|
|
128
|
+
`${sutContractName} ` +
|
|
129
|
+
`${(0, ansicolor_1.underline)(r.selectedFunction.name)} ` +
|
|
130
|
+
printedFunctionArgs));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
// If the function call fails with a runtime error, log a dimmed
|
|
135
|
+
// message. Since the public function result is ignored, there's
|
|
136
|
+
// no need to throw an error.
|
|
137
|
+
radio.emit("logMessage", (0, ansicolor_1.dim)(`₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
138
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
139
|
+
`${sutCallerWallet} ` +
|
|
140
|
+
`${sutContractName} ` +
|
|
141
|
+
`${(0, ansicolor_1.underline)(r.selectedFunction.name)} ` +
|
|
142
|
+
printedFunctionArgs));
|
|
143
|
+
}
|
|
144
|
+
const printedInvariantArgs = r.invariantArgsArb
|
|
145
|
+
.map((arg) => {
|
|
146
|
+
try {
|
|
147
|
+
return typeof arg === "object"
|
|
148
|
+
? JSON.stringify(arg)
|
|
149
|
+
: arg.toString();
|
|
150
|
+
}
|
|
151
|
+
catch (_a) {
|
|
152
|
+
return "[Circular]";
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
.join(" ");
|
|
156
|
+
const [invariantCallerWallet, invariantCallerAddress] = r.invariantCaller;
|
|
157
|
+
try {
|
|
158
|
+
const { result: invariantCallResult } = simnet.callReadOnlyFn(r.rendezvousContractId, r.selectedInvariant.name, selectedInvariantArgs, invariantCallerAddress);
|
|
159
|
+
const invariantCallResultJson = (0, transactions_1.cvToJSON)(invariantCallResult);
|
|
160
|
+
if (invariantCallResultJson.value === true) {
|
|
161
|
+
radio.emit("logMessage", `₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
162
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
163
|
+
`${(0, ansicolor_1.dim)(invariantCallerWallet)} ` +
|
|
164
|
+
`${(0, ansicolor_1.green)("[PASS]")} ` +
|
|
165
|
+
`${sutContractName} ` +
|
|
166
|
+
`${(0, ansicolor_1.underline)(r.selectedInvariant.name)} ` +
|
|
167
|
+
printedInvariantArgs);
|
|
168
|
+
}
|
|
169
|
+
if (!invariantCallResultJson.value) {
|
|
170
|
+
throw new Error(`Invariant failed for ${sutContractName} contract: "${r.selectedInvariant.name}" returned ${invariantCallResultJson.value}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
// Handle both negative results from the invariant function and
|
|
175
|
+
// general runtime failures. Focus is on capturing the invariant
|
|
176
|
+
// function's result, including any runtime errors it caused.
|
|
177
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
178
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
179
|
+
`${invariantCallerWallet} ` +
|
|
180
|
+
`[FAIL] ` +
|
|
181
|
+
`${sutContractName} ` +
|
|
182
|
+
`${(0, ansicolor_1.underline)(r.selectedInvariant.name)} ` +
|
|
183
|
+
printedInvariantArgs));
|
|
184
|
+
// Re-throw the error for fast-check to catch and process.
|
|
185
|
+
throw error;
|
|
186
|
+
}
|
|
187
|
+
if (r.canMineBlocks) {
|
|
188
|
+
simnet.mineEmptyBurnBlocks(r.burnBlocks);
|
|
189
|
+
}
|
|
190
|
+
}), {
|
|
191
|
+
verbose: true,
|
|
192
|
+
reporter: radioReporter,
|
|
193
|
+
seed: seed,
|
|
194
|
+
path: path,
|
|
195
|
+
numRuns: runs,
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
exports.checkInvariants = checkInvariants;
|
|
199
|
+
/**
|
|
200
|
+
* Initialize the local context, setting the number of times each function
|
|
201
|
+
* has been called to zero.
|
|
202
|
+
* @param rendezvousSutFunctions The Rendezvous functions.
|
|
203
|
+
* @returns The initialized local context.
|
|
204
|
+
*/
|
|
205
|
+
const initializeLocalContext = (rendezvousSutFunctions) => Object.fromEntries(Array.from(rendezvousSutFunctions.entries()).map(([contractId, functions]) => [
|
|
206
|
+
contractId,
|
|
207
|
+
Object.fromEntries(functions.map((f) => [f.name, 0])),
|
|
208
|
+
]));
|
|
209
|
+
exports.initializeLocalContext = initializeLocalContext;
|
|
210
|
+
const initializeClarityContext = (simnet, rendezvousSutFunctions) => rendezvousSutFunctions.forEach((fns, contractId) => {
|
|
211
|
+
fns.forEach((fn) => {
|
|
212
|
+
const { result: initialize } = simnet.callPublicFn(contractId, "update-context", [transactions_1.Cl.stringAscii(fn.name), transactions_1.Cl.uint(0)], simnet.deployer);
|
|
213
|
+
const jsonResult = (0, transactions_1.cvToJSON)(initialize);
|
|
214
|
+
if (!jsonResult.value || !jsonResult.success) {
|
|
215
|
+
throw new Error(`Failed to initialize the context for function: ${fn.name}.`);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
exports.initializeClarityContext = initializeClarityContext;
|
|
220
|
+
/**
|
|
221
|
+
* Filter the System Under Test (`SUT`) functions from the map of all
|
|
222
|
+
* contract functions.
|
|
223
|
+
*
|
|
224
|
+
* The SUT functions are the ones that have `public` access since they are
|
|
225
|
+
* capable of changing the contract state, and they are not test functions.
|
|
226
|
+
* @param allFunctionsMap The map containing all the functions for each
|
|
227
|
+
* contract.
|
|
228
|
+
* @returns A map containing only the SUT functions for each contract.
|
|
229
|
+
*/
|
|
230
|
+
const filterSutFunctions = (allFunctionsMap) => new Map(Array.from(allFunctionsMap, ([contractId, functions]) => [
|
|
231
|
+
contractId,
|
|
232
|
+
functions.filter((f) => f.access === "public" &&
|
|
233
|
+
f.name !== "update-context" &&
|
|
234
|
+
!f.name.startsWith("test-")),
|
|
235
|
+
]));
|
|
236
|
+
const filterInvariantFunctions = (allFunctionsMap) => new Map(Array.from(allFunctionsMap, ([contractId, functions]) => [
|
|
237
|
+
contractId,
|
|
238
|
+
functions.filter((f) => f.access === "read_only" && f.name.startsWith("invariant-")),
|
|
239
|
+
]));
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stacks/rendezvous",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Meet your contract's vulnerabilities head-on.",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"rv": "./dist/app.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"postinstall": "npx -p typescript tsc --project tsconfig.json && node -e \"if (process.platform !== 'win32') require('fs').chmodSync('./dist/app.js', 0o755);\"",
|
|
11
|
+
"test": "npx tsc --project tsconfig.json && npx jest",
|
|
12
|
+
"test:coverage": "npx tsc --project tsconfig.json && npx jest --coverage"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"stacks",
|
|
16
|
+
"clarity",
|
|
17
|
+
"fuzz",
|
|
18
|
+
"testing"
|
|
19
|
+
],
|
|
20
|
+
"author": "Radu Bahmata, Nikos Baxevanis",
|
|
21
|
+
"license": "GPL-3.0-only",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist/",
|
|
24
|
+
"package.json",
|
|
25
|
+
"README.md",
|
|
26
|
+
"LICENSE"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hirosystems/clarinet-sdk": "2.12.0",
|
|
30
|
+
"@stacks/transactions": "^6.16.1",
|
|
31
|
+
"ansicolor": "^2.0.3",
|
|
32
|
+
"fast-check": "^3.20.0",
|
|
33
|
+
"yaml": "^2.6.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@hirosystems/clarinet-sdk-wasm": "2.12.0",
|
|
37
|
+
"@types/jest": "^29.5.12",
|
|
38
|
+
"jest": "^29.7.0",
|
|
39
|
+
"ts-jest": "^29.2.3",
|
|
40
|
+
"typescript": "^5.5.4"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/dist/property.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isReturnTypeBoolean = exports.isParamsMatch = exports.isTestDiscardedInPlace = exports.checkProperties = void 0;
|
|
7
|
+
const fast_check_1 = __importDefault(require("fast-check"));
|
|
8
|
+
const transactions_1 = require("@stacks/transactions");
|
|
9
|
+
const heatstroke_1 = require("./heatstroke");
|
|
10
|
+
const shared_1 = require("./shared");
|
|
11
|
+
const ansicolor_1 = require("ansicolor");
|
|
12
|
+
const checkProperties = (simnet, sutContractName, rendezvousList, rendezvousAllFunctions, seed, path, runs, radio) => {
|
|
13
|
+
// A map where the keys are the test contract identifiers and the values are
|
|
14
|
+
// arrays of their test functions. This map will be used to access the test
|
|
15
|
+
// functions for each test contract in the property-based testing routine.
|
|
16
|
+
const testContractsTestFunctions = filterTestFunctions(rendezvousAllFunctions);
|
|
17
|
+
radio.emit("logMessage", `\nStarting property testing type for the ${sutContractName} contract...\n`);
|
|
18
|
+
// Search for discard functions, for each test function. This map will
|
|
19
|
+
// be used to pair the test functions with their corresponding discard
|
|
20
|
+
// functions.
|
|
21
|
+
const testContractsDiscardFunctions = new Map(Array.from(rendezvousAllFunctions, ([contractId, functions]) => [
|
|
22
|
+
contractId,
|
|
23
|
+
functions.filter((f) => f.access === "read_only" && f.name.startsWith("can-")),
|
|
24
|
+
]));
|
|
25
|
+
// Pair each test function with its corresponding discard function. When a
|
|
26
|
+
// test function is selected, Rendezvous will first call its discard
|
|
27
|
+
// function, to allow or prevent the property test from running.
|
|
28
|
+
const testContractsPairedFunctions = new Map(Array.from(testContractsTestFunctions, ([contractId, functions]) => [
|
|
29
|
+
contractId,
|
|
30
|
+
new Map(functions.map((f) => {
|
|
31
|
+
var _a;
|
|
32
|
+
const discardFunction = (_a = testContractsDiscardFunctions
|
|
33
|
+
.get(contractId)) === null || _a === void 0 ? void 0 : _a.find((pf) => pf.name === `can-${f.name}`);
|
|
34
|
+
return [f.name, discardFunction === null || discardFunction === void 0 ? void 0 : discardFunction.name];
|
|
35
|
+
})),
|
|
36
|
+
]));
|
|
37
|
+
const hasDiscardFunctionErrors = Array.from(testContractsPairedFunctions).some(([contractId, pairedMap]) => Array.from(pairedMap).some(([testFunctionName, discardFunctionName]) => discardFunctionName
|
|
38
|
+
? !validateDiscardFunction(contractId, discardFunctionName, testFunctionName, testContractsDiscardFunctions, testContractsTestFunctions, radio)
|
|
39
|
+
: false));
|
|
40
|
+
if (hasDiscardFunctionErrors) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const simnetAccounts = simnet.getAccounts();
|
|
44
|
+
const eligibleAccounts = new Map([...simnetAccounts].filter(([key]) => key !== "faucet"));
|
|
45
|
+
const simnetAddresses = Array.from(simnetAccounts.values());
|
|
46
|
+
const testContractId = rendezvousList[0];
|
|
47
|
+
const testFunctions = (0, shared_1.getFunctionsListForContract)(testContractsTestFunctions, testContractId);
|
|
48
|
+
if ((testFunctions === null || testFunctions === void 0 ? void 0 : testFunctions.length) === 0) {
|
|
49
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No test functions found for the "${sutContractName}" contract.\n`));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const eligibleTestFunctions = testFunctions.filter((fn) => !(0, shared_1.isTraitReferenceFunction)(fn));
|
|
53
|
+
if (eligibleTestFunctions.length === 0) {
|
|
54
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`No eligible test functions found for the "${sutContractName}" contract. Note: trait references are not supported.\n`));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const radioReporter = (runDetails) => {
|
|
58
|
+
(0, heatstroke_1.reporter)(runDetails, radio, "test");
|
|
59
|
+
};
|
|
60
|
+
fast_check_1.default.assert(fast_check_1.default.property(fast_check_1.default
|
|
61
|
+
.record({
|
|
62
|
+
testContractId: fast_check_1.default.constant(testContractId),
|
|
63
|
+
testCaller: fast_check_1.default.constantFrom(...eligibleAccounts.entries()),
|
|
64
|
+
canMineBlocks: fast_check_1.default.boolean(),
|
|
65
|
+
})
|
|
66
|
+
.chain((r) => fast_check_1.default
|
|
67
|
+
.record({
|
|
68
|
+
selectedTestFunction: fast_check_1.default.constantFrom(...eligibleTestFunctions),
|
|
69
|
+
})
|
|
70
|
+
.map((selectedTestFunction) => (Object.assign(Object.assign({}, r), selectedTestFunction))))
|
|
71
|
+
.chain((r) => fast_check_1.default
|
|
72
|
+
.record({
|
|
73
|
+
functionArgsArb: fast_check_1.default.tuple(...(0, shared_1.functionToArbitrary)(r.selectedTestFunction, simnetAddresses)),
|
|
74
|
+
})
|
|
75
|
+
.map((args) => (Object.assign(Object.assign({}, r), args))))
|
|
76
|
+
.chain((r) => fast_check_1.default
|
|
77
|
+
.record({
|
|
78
|
+
burnBlocks: r.canMineBlocks
|
|
79
|
+
? // This arbitrary produces integers with a maximum value
|
|
80
|
+
// inversely proportional to the number of runs:
|
|
81
|
+
// - Fewer runs result in a higher maximum burn blocks,
|
|
82
|
+
// allowing more blocks to be mined.
|
|
83
|
+
// - More runs result in a lower maximum burn blocks, as more
|
|
84
|
+
// blocks are mined overall.
|
|
85
|
+
fast_check_1.default.integer({
|
|
86
|
+
min: 1,
|
|
87
|
+
max: Math.ceil(100000 / (runs || 100)),
|
|
88
|
+
})
|
|
89
|
+
: fast_check_1.default.constant(0),
|
|
90
|
+
})
|
|
91
|
+
.map((burnBlocks) => (Object.assign(Object.assign({}, r), burnBlocks)))), (r) => {
|
|
92
|
+
const selectedTestFunctionArgs = (0, shared_1.argsToCV)(r.selectedTestFunction, r.functionArgsArb);
|
|
93
|
+
const printedTestFunctionArgs = r.functionArgsArb
|
|
94
|
+
.map((arg) => {
|
|
95
|
+
try {
|
|
96
|
+
return typeof arg === "object"
|
|
97
|
+
? JSON.stringify(arg)
|
|
98
|
+
: arg.toString();
|
|
99
|
+
}
|
|
100
|
+
catch (_a) {
|
|
101
|
+
return "[Circular]";
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
.join(" ");
|
|
105
|
+
const [testCallerWallet, testCallerAddress] = r.testCaller;
|
|
106
|
+
const discardFunctionName = testContractsPairedFunctions
|
|
107
|
+
.get(r.testContractId)
|
|
108
|
+
.get(r.selectedTestFunction.name);
|
|
109
|
+
const discarded = isTestDiscarded(discardFunctionName, selectedTestFunctionArgs, r.testContractId, simnet, testCallerAddress);
|
|
110
|
+
if (discarded) {
|
|
111
|
+
radio.emit("logMessage", `₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
112
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
113
|
+
`${(0, ansicolor_1.dim)(testCallerWallet)} ` +
|
|
114
|
+
`${(0, ansicolor_1.yellow)("[WARN]")} ` +
|
|
115
|
+
`${sutContractName} ` +
|
|
116
|
+
`${(0, ansicolor_1.underline)(r.selectedTestFunction.name)} ` +
|
|
117
|
+
(0, ansicolor_1.dim)(printedTestFunctionArgs));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
try {
|
|
121
|
+
// If the function call results in a runtime error, the error will
|
|
122
|
+
// be caught and logged as a test failure in the catch block.
|
|
123
|
+
const { result: testFunctionCallResult } = simnet.callPublicFn(r.testContractId, r.selectedTestFunction.name, selectedTestFunctionArgs, testCallerAddress);
|
|
124
|
+
const testFunctionCallResultJson = (0, transactions_1.cvToJSON)(testFunctionCallResult);
|
|
125
|
+
const discardedInPlace = (0, exports.isTestDiscardedInPlace)(testFunctionCallResultJson);
|
|
126
|
+
if (discardedInPlace) {
|
|
127
|
+
radio.emit("logMessage", `₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
128
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
129
|
+
`${(0, ansicolor_1.dim)(testCallerWallet)} ` +
|
|
130
|
+
`${(0, ansicolor_1.yellow)("[WARN]")} ` +
|
|
131
|
+
`${sutContractName} ` +
|
|
132
|
+
`${(0, ansicolor_1.underline)(r.selectedTestFunction.name)} ` +
|
|
133
|
+
(0, ansicolor_1.dim)(printedTestFunctionArgs));
|
|
134
|
+
}
|
|
135
|
+
else if (!discardedInPlace &&
|
|
136
|
+
testFunctionCallResultJson.success &&
|
|
137
|
+
testFunctionCallResultJson.value.value === true) {
|
|
138
|
+
radio.emit("logMessage", `₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
139
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
140
|
+
`${(0, ansicolor_1.dim)(testCallerWallet)} ` +
|
|
141
|
+
`${(0, ansicolor_1.green)("[PASS]")} ` +
|
|
142
|
+
`${sutContractName} ` +
|
|
143
|
+
`${(0, ansicolor_1.underline)(r.selectedTestFunction.name)} ` +
|
|
144
|
+
printedTestFunctionArgs);
|
|
145
|
+
if (r.canMineBlocks) {
|
|
146
|
+
simnet.mineEmptyBurnBlocks(r.burnBlocks);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new Error(`Test failed for ${sutContractName} contract: "${r.selectedTestFunction.name}" returned ${testFunctionCallResultJson.value.value}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
// Capture the error and log the test failure.
|
|
155
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`₿ ${simnet.burnBlockHeight.toString().padStart(8)} ` +
|
|
156
|
+
`Ӿ ${simnet.blockHeight.toString().padStart(8)} ` +
|
|
157
|
+
`${testCallerWallet} ` +
|
|
158
|
+
`[FAIL] ` +
|
|
159
|
+
`${sutContractName} ` +
|
|
160
|
+
`${(0, ansicolor_1.underline)(r.selectedTestFunction.name)} ` +
|
|
161
|
+
printedTestFunctionArgs));
|
|
162
|
+
// Re-throw the error for fast-check to catch and process.
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}), {
|
|
167
|
+
verbose: true,
|
|
168
|
+
reporter: radioReporter,
|
|
169
|
+
seed: seed,
|
|
170
|
+
path: path,
|
|
171
|
+
numRuns: runs,
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
exports.checkProperties = checkProperties;
|
|
175
|
+
const filterTestFunctions = (allFunctionsMap) => new Map(Array.from(allFunctionsMap, ([contractId, functions]) => [
|
|
176
|
+
contractId,
|
|
177
|
+
functions.filter((f) => f.access === "public" && f.name.startsWith("test-")),
|
|
178
|
+
]));
|
|
179
|
+
const isTestDiscardedInPlace = (testFunctionCallResultJson) => testFunctionCallResultJson.success === true &&
|
|
180
|
+
testFunctionCallResultJson.value.value === false;
|
|
181
|
+
exports.isTestDiscardedInPlace = isTestDiscardedInPlace;
|
|
182
|
+
/**
|
|
183
|
+
* Check if the test function has to be discarded.
|
|
184
|
+
* @param discardFunctionName The discard function name.
|
|
185
|
+
* @param selectedTestFunctionArgs The generated test function arguments.
|
|
186
|
+
* @param contractId The contract identifier.
|
|
187
|
+
* @param simnet The simnet instance.
|
|
188
|
+
* @param selectedCaller The selected caller.
|
|
189
|
+
* @returns A boolean indicating if the test function has to be discarded.
|
|
190
|
+
*/
|
|
191
|
+
const isTestDiscarded = (discardFunctionName, selectedTestFunctionArgs, contractId, simnet, selectedCaller) => {
|
|
192
|
+
if (!discardFunctionName)
|
|
193
|
+
return false;
|
|
194
|
+
const { result: discardFunctionCallResult } = simnet.callReadOnlyFn(contractId, discardFunctionName, selectedTestFunctionArgs, selectedCaller);
|
|
195
|
+
const jsonDiscardFunctionCallResult = (0, transactions_1.cvToJSON)(discardFunctionCallResult);
|
|
196
|
+
return jsonDiscardFunctionCallResult.value === false;
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* Validate the discard function, ensuring that its parameters match the test
|
|
200
|
+
* function's parameters and that its return type is boolean.
|
|
201
|
+
* @param contractId The contract identifier.
|
|
202
|
+
* @param discardFunctionName The discard function name.
|
|
203
|
+
* @param testFunctionName The test function name.
|
|
204
|
+
* @param testContractsDiscardFunctions The discard functions map.
|
|
205
|
+
* @param testContractsTestFunctions The test functions map.
|
|
206
|
+
* @param radio The radio event emitter.
|
|
207
|
+
* @returns A boolean indicating if the discard function passes the checks.
|
|
208
|
+
*/
|
|
209
|
+
const validateDiscardFunction = (contractId, discardFunctionName, testFunctionName, testContractsDiscardFunctions, testContractsTestFunctions, radio) => {
|
|
210
|
+
var _a, _b;
|
|
211
|
+
const testFunction = (_a = testContractsTestFunctions
|
|
212
|
+
.get(contractId)) === null || _a === void 0 ? void 0 : _a.find((f) => f.name === testFunctionName);
|
|
213
|
+
const discardFunction = (_b = testContractsDiscardFunctions
|
|
214
|
+
.get(contractId)) === null || _b === void 0 ? void 0 : _b.find((f) => f.name === discardFunctionName);
|
|
215
|
+
if (!testFunction || !discardFunction)
|
|
216
|
+
return false;
|
|
217
|
+
if (!(0, exports.isParamsMatch)(testFunction, discardFunction)) {
|
|
218
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`\nError: Parameter mismatch for discard function "${discardFunctionName}" in contract "${(0, shared_1.getContractNameFromContractId)(contractId)}".\n`));
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
if (!(0, exports.isReturnTypeBoolean)(discardFunction)) {
|
|
222
|
+
radio.emit("logMessage", (0, ansicolor_1.red)(`\nError: Return type must be boolean for discard function "${discardFunctionName}" in contract "${(0, shared_1.getContractNameFromContractId)(contractId)}".\n`));
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Verify if the test function parameters match the discard function
|
|
229
|
+
* parameters.
|
|
230
|
+
* @param testFunction The test function's interface.
|
|
231
|
+
* @param discardFunction The discard function's interface.
|
|
232
|
+
* @returns A boolean indicating if the parameters match.
|
|
233
|
+
*/
|
|
234
|
+
const isParamsMatch = (testFunction, discardFunction) => {
|
|
235
|
+
const sortedTestFunctionArgs = [...testFunction.args].sort((a, b) => a.name.localeCompare(b.name));
|
|
236
|
+
const sortedDiscardFunctionArgs = [...discardFunction.args].sort((a, b) => a.name.localeCompare(b.name));
|
|
237
|
+
return (JSON.stringify(sortedTestFunctionArgs) ===
|
|
238
|
+
JSON.stringify(sortedDiscardFunctionArgs));
|
|
239
|
+
};
|
|
240
|
+
exports.isParamsMatch = isParamsMatch;
|
|
241
|
+
/**
|
|
242
|
+
* Verify if the discard function's return type is boolean.
|
|
243
|
+
* @param discardFunction The discard function's interface.
|
|
244
|
+
* @returns A boolean indicating if the return type is boolean.
|
|
245
|
+
*/
|
|
246
|
+
const isReturnTypeBoolean = (discardFunction) => discardFunction.outputs.type === "bool";
|
|
247
|
+
exports.isReturnTypeBoolean = isReturnTypeBoolean;
|