-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.ts
281 lines (244 loc) · 8.2 KB
/
app.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
#!/usr/bin/env node
import { join, resolve } from "path";
import { EventEmitter } from "events";
import toml from "toml";
import { checkProperties } from "./property";
import { checkInvariants } from "./invariant";
import {
getContractNameFromContractId,
getFunctionsFromContractInterfaces,
getSimnetDeployerContractsInterfaces,
} from "./shared";
import { issueFirstClassCitizenship } from "./citizen";
import { version } from "./package.json";
import { red, yellow } from "ansicolor";
import { existsSync, readFileSync } from "fs";
import { DialerRegistry } from "./dialer";
import { RemoteDataSettings } from "./app.types";
const logger = (log: string, logLevel: "log" | "error" | "info" = "log") => {
console[logLevel](log);
};
/**
* The object used to initialize an empty simnet session with, when no remote
* data is enabled in the `Clarinet.toml` file.
*/
export const noRemoteData = {
enabled: false,
api_url: "",
initial_height: 1,
};
export const invalidRemoteDataWarningMessage = `\nRemote data settings existing in Clarinet.toml, but remote data feature will not be used! To use remote data, please make sure the following fields are set:
- enabled = true
- api_url = <stacks-api-url>
- initial_height = <stacks-block-height-to-fork-from>
under the "repl.remote_data" section in the Clarinet.toml file.`;
/**
* Gets the manifest file name for a Clarinet project.
* If a custom manifest exists (`Clarinet-<contract-name>.toml`), it is used.
* Otherwise, the default `Clarinet.toml` is returned.
* @param manifestDir The relative path to the Clarinet project directory.
* @param targetContractName The target contract name.
* @returns The manifest file name.
*/
export const getManifestFileName = (
manifestDir: string,
targetContractName: string
) => {
const isCustomManifest = existsSync(
resolve(manifestDir, `Clarinet-${targetContractName}.toml`)
);
if (isCustomManifest) {
return `Clarinet-${targetContractName}.toml`;
}
return "Clarinet.toml";
};
export const tryParseRemoteDataSettings = (
manifestPath: string,
radio: EventEmitter
): RemoteDataSettings => {
const clarinetToml = toml.parse(readFileSync(resolve(manifestPath), "utf-8"));
const remoteDataUserSettings =
clarinetToml.repl?.["remote_data"] ?? undefined;
const invalidRemoteDataSetup =
!remoteDataUserSettings?.["api_url"] ||
!remoteDataUserSettings?.["enabled"] ||
!remoteDataUserSettings?.["initial_height"];
if (remoteDataUserSettings !== undefined && invalidRemoteDataSetup) {
radio.emit("logMessage", yellow(invalidRemoteDataWarningMessage));
} else if (remoteDataUserSettings) {
radio.emit(
"logMessage",
yellow(
"\nUsing remote data. Setting up the environment can take up to a minute..."
)
);
}
if (!remoteDataUserSettings || invalidRemoteDataSetup) {
return noRemoteData;
}
return remoteDataUserSettings;
};
const helpMessage = `
rv v${version}
Usage: rv <path-to-clarinet-project> <contract-name> <type> [--seed=<seed>] [--path=<path>] [--runs=<runs>] [--dial=<path-to-dialers-file>] [--help]
Positional arguments:
path-to-clarinet-project - The path to the Clarinet project.
contract-name - The name of the contract to be fuzzed.
type - The type to use for exercising the contracts. Possible values: test, invariant.
Options:
--seed - The seed to use for the replay functionality.
--path - The path to use for the replay functionality.
--runs - The runs to use for iterating over the tests. Default: 100.
--dial – The path to a JavaScript file containing custom pre- and post-execution functions (dialers).
--help - Show the help message.
`;
const parseOptionalArgument = (argName: string) => {
return process.argv
.find(
(arg, idx) => idx >= 4 && arg.toLowerCase().startsWith(`--${argName}`)
)
?.split("=")[1];
};
export async function main() {
const radio = new EventEmitter();
radio.on("logMessage", (log) => logger(log));
radio.on("logFailure", (log) => logger(red(log), "error"));
const args = process.argv;
if (args.includes("--help")) {
radio.emit("logMessage", helpMessage);
return;
}
/** The relative path to the Clarinet project. */
const manifestDir = args[2];
if (!manifestDir || manifestDir.startsWith("--")) {
radio.emit(
"logMessage",
red(
"\nNo path to Clarinet project provided. Supply it immediately or face the relentless scrutiny of your contract's vulnerabilities."
)
);
radio.emit("logMessage", helpMessage);
return;
}
/** The target contract name. */
const sutContractName = args[3];
if (!sutContractName || sutContractName.startsWith("--")) {
radio.emit(
"logMessage",
red(
"\nNo target contract name provided. Please provide the contract name to be fuzzed."
)
);
radio.emit("logMessage", helpMessage);
return;
}
const type = args[4]?.toLowerCase();
if (!type || type.startsWith("--") || !["test", "invariant"].includes(type)) {
radio.emit(
"logMessage",
red(
"\nInvalid type provided. Please provide the type of test to be executed. Possible values: test, invariant."
)
);
radio.emit("logMessage", helpMessage);
return;
}
/**
* The relative path to the manifest file, either `Clarinet.toml` or
* `Clarinet-<contract-name>.toml`. If the latter exists, it is used.
*/
const manifestPath = join(
manifestDir,
getManifestFileName(manifestDir, sutContractName)
);
radio.emit("logMessage", `Using manifest path: ${manifestPath}`);
radio.emit("logMessage", `Target contract: ${sutContractName}`);
const seed = parseInt(parseOptionalArgument("seed")!, 10) || undefined;
if (seed !== undefined) {
radio.emit("logMessage", `Using seed: ${seed}`);
}
const path = parseOptionalArgument("path") || undefined;
if (path !== undefined) {
radio.emit("logMessage", `Using path: ${path}`);
}
const runs = parseInt(parseOptionalArgument("runs")!, 10) || undefined;
if (runs !== undefined) {
radio.emit("logMessage", `Using runs: ${runs}`);
}
/**
* The path to the dialer file. The dialer file allows the user to register
* custom pre and post-execution JavaScript functions to be executed before
* and after the public function calls during invariant testing.
*/
const dialPath = parseOptionalArgument("dial") || undefined;
if (dialPath !== undefined) {
radio.emit("logMessage", `Using dial path: ${dialPath}`);
}
/**
* The dialer registry, which is used to keep track of all the custom dialers
* registered by the user using the `--dial` flag.
*/
const dialerRegistry =
dialPath !== undefined ? new DialerRegistry(dialPath) : undefined;
if (dialerRegistry !== undefined) {
dialerRegistry.registerDialers();
}
const remoteDataSettings = tryParseRemoteDataSettings(manifestPath, radio);
const simnet = await issueFirstClassCitizenship(
manifestDir,
manifestPath,
remoteDataSettings,
sutContractName
);
/**
* The list of contract IDs for the SUT contract names, as per the simnet.
*/
const rendezvousList = Array.from(
getSimnetDeployerContractsInterfaces(simnet).keys()
).filter(
(deployedContract) =>
getContractNameFromContractId(deployedContract) === sutContractName
);
const rendezvousAllFunctions = getFunctionsFromContractInterfaces(
new Map(
Array.from(getSimnetDeployerContractsInterfaces(simnet)).filter(
([contractId]) => rendezvousList.includes(contractId)
)
)
);
// Select the testing routine based on `type`.
// If "invariant", call `checkInvariants` to verify contract invariants.
// If "test", call `checkProperties` for property-based testing.
switch (type) {
case "invariant": {
await checkInvariants(
simnet,
sutContractName,
rendezvousList,
rendezvousAllFunctions,
seed,
path,
runs,
dialerRegistry,
radio
);
break;
}
case "test": {
checkProperties(
simnet,
sutContractName,
rendezvousList,
rendezvousAllFunctions,
seed,
path,
runs,
radio
);
break;
}
}
}
if (require.main === module) {
main();
}