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
use forc_pkg as pkg;
use fuel_tx as tx;
use fuel_vm::{self as vm, prelude::Opcode};
use rand::{Rng, SeedableRng};
#[derive(Debug)]
pub enum Tested {
Package(Box<TestedPackage>),
Workspace,
}
#[derive(Debug)]
pub struct TestedPackage {
pub built: Box<pkg::BuiltPackage>,
pub tests: Vec<TestResult>,
}
#[derive(Debug)]
pub struct TestResult {
pub name: String,
pub state: vm::state::ProgramState,
pub duration: std::time::Duration,
}
pub struct BuiltTests {
built_pkg: Box<pkg::BuiltPackage>,
}
#[derive(Default)]
pub struct Opts {
pub pkg: pkg::PkgOpts,
pub print: pkg::PrintOpts,
pub minify: pkg::MinifyOpts,
pub binary_outfile: Option<String>,
pub debug_outfile: Option<String>,
pub build_profile: Option<String>,
pub release: bool,
pub time_phases: bool,
}
impl Opts {
pub fn into_build_opts(self) -> pkg::BuildOpts {
pkg::BuildOpts {
pkg: self.pkg,
print: self.print,
minify: self.minify,
binary_outfile: self.binary_outfile,
debug_outfile: self.debug_outfile,
build_profile: self.build_profile,
release: self.release,
time_phases: self.time_phases,
tests: true,
}
}
}
impl TestResult {
pub fn passed(&self) -> bool {
!matches!(self.state, vm::state::ProgramState::Revert(_))
}
}
impl BuiltTests {
pub fn test_count(&self) -> usize {
self.built_pkg
.entries
.iter()
.filter(|e| e.is_test())
.count()
}
pub fn run(self) -> anyhow::Result<Tested> {
run_tests(self)
}
}
pub fn build(opts: Opts) -> anyhow::Result<BuiltTests> {
let build_opts = opts.into_build_opts();
let built_pkg = match pkg::build_with_options(build_opts)? {
pkg::Built::Package(pkg) => pkg,
pkg::Built::Workspace => anyhow::bail!("testing workspaces not yet supported"),
};
Ok(BuiltTests { built_pkg })
}
fn run_tests(built: BuiltTests) -> anyhow::Result<Tested> {
let BuiltTests { built_pkg } = built;
let tests = built_pkg
.entries
.iter()
.filter(|entry| entry.is_test())
.map(|entry| {
let offset = u32::try_from(entry.imm).expect("test instruction offset out of range");
let name = entry.fn_name.clone();
let (state, duration) = exec_test(&built_pkg.bytecode, offset);
TestResult {
name,
state,
duration,
}
})
.collect();
let built = built_pkg;
let tested_pkg = TestedPackage { built, tests };
let tested = Tested::Package(Box::new(tested_pkg));
Ok(tested)
}
fn patch_test_bytecode(bytecode: &[u8], test_offset: u32) -> std::borrow::Cow<[u8]> {
const PROGRAM_START_INST_OFFSET: u32 = 6;
const PROGRAM_START_BYTE_OFFSET: usize = PROGRAM_START_INST_OFFSET as usize * Opcode::LEN;
if test_offset == PROGRAM_START_INST_OFFSET {
return std::borrow::Cow::Borrowed(bytecode);
}
let ji = Opcode::JI(test_offset);
let ji_bytes = ji.to_bytes();
let start = PROGRAM_START_BYTE_OFFSET;
let end = start + ji_bytes.len();
let mut patched = bytecode.to_vec();
patched.splice(start..end, ji_bytes);
std::borrow::Cow::Owned(patched)
}
fn exec_test(bytecode: &[u8], test_offset: u32) -> (vm::state::ProgramState, std::time::Duration) {
let bytecode = patch_test_bytecode(bytecode, test_offset).into_owned();
let script_input_data = vec![];
let mut rng = rand::rngs::StdRng::seed_from_u64(0x7E57u64);
let maturity = 1;
let block_height = (u32::MAX >> 1) as u64;
let secret_key = rng.gen();
let utxo_id = rng.gen();
let amount = 1;
let asset_id = Default::default();
let tx_ptr = rng.gen();
let params = tx::ConsensusParameters::default();
let tx = tx::TransactionBuilder::script(bytecode, script_input_data)
.add_unsigned_coin_input(secret_key, utxo_id, amount, asset_id, tx_ptr, 0)
.gas_limit(tx::ConsensusParameters::DEFAULT.max_gas_per_tx)
.maturity(maturity)
.finalize_checked(block_height as tx::Word, ¶ms);
let storage = vm::storage::MemoryStorage::default();
let mut interpreter = vm::interpreter::Interpreter::with_storage(storage, params);
let start = std::time::Instant::now();
let transition = interpreter.transact(tx).unwrap();
let duration = start.elapsed();
let state = *transition.state();
(state, duration)
}