-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
168 lines (147 loc) · 4.72 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
#!/usr/bin/env node
import { join } from "path";
import { EventEmitter } from "events";
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 } from "ansicolor";
const logger = (log: string, logLevel: "log" | "error" | "info" = "log") => {
console[logLevel](log);
};
const helpMessage = `
rv v${version}
Usage: ./rv <path-to-clarinet-project> <contract-name> <type> [--seed=<seed>] [--path=<path>] [--runs=<runs>]
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.
--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 `Clarinet.toml`. */
const manifestPath = join(manifestDir, "Clarinet.toml");
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}`);
}
const simnet = await issueFirstClassCitizenship(manifestDir, sutContractName);
/**
* The list of contract IDs for the SUT contract names, as per the simnet.
*/
const rendezvousList = Array.from(
getSimnetDeployerContractsInterfaces(simnet).keys()
).filter((deployedContract) =>
[sutContractName].includes(getContractNameFromContractId(deployedContract))
);
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": {
checkInvariants(
simnet,
sutContractName,
rendezvousList,
rendezvousAllFunctions,
seed,
path,
runs,
radio
);
break;
}
case "test": {
checkProperties(
simnet,
sutContractName,
rendezvousList,
rendezvousAllFunctions,
seed,
path,
runs,
radio
);
break;
}
}
}
if (require.main === module) {
main();
}