-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathusability.rs
More file actions
198 lines (173 loc) · 5.38 KB
/
Copy pathusability.rs
File metadata and controls
198 lines (173 loc) · 5.38 KB
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
use evm_interpreter::{
Capture, Context, Control, Etable, ExitError, ExitSucceed, Log, Machine, Opcode,
RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, RuntimeState, TransactionContext,
};
use primitive_types::{H160, H256, U256};
use std::rc::Rc;
const CODE1: &str = "60e060020a6000350480632839e92814601e57806361047ff414603457005b602a6004356024356047565b8060005260206000f35b603d6004356099565b8060005260206000f35b600082600014605457605e565b8160010190506093565b81600014606957607b565b60756001840360016047565b90506093565b609060018403608c85600186036047565b6047565b90505b92915050565b6000816000148060a95750816001145b60b05760b7565b81905060cf565b60c1600283036099565b60cb600184036099565b0190505b91905056";
const DATA1: &str = "2839e92800000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001";
const RET1: &str = "000000000000000000000000000000000000000000000000000000000000000d";
#[test]
fn etable_wrap() {
let code = hex::decode(&CODE1).unwrap();
let data = hex::decode(&DATA1).unwrap();
let wrapped_etable = Etable::<_, _, Opcode>::core().wrap(|f, opcode_t| {
move |machine, handle, opcode, position| {
assert_eq!(opcode_t, opcode);
println!("opcode: {:?}", opcode);
f(machine, handle, opcode, position)
}
});
let mut vm = Machine::new(Rc::new(code), Rc::new(data), 1024, 10000, ());
let result = vm.run(&mut (), &wrapped_etable);
assert_eq!(result, Capture::Exit(Ok(ExitSucceed::Returned.into())));
assert_eq!(vm.retval, hex::decode(&RET1).unwrap());
}
#[test]
fn etable_wrap2() {
let code = hex::decode(&CODE1).unwrap();
let data = hex::decode(&DATA1).unwrap();
let wrapped_etable = Etable::core().wrap(
|f, opcode_t| -> Box<dyn Fn(&mut Machine<()>, &mut (), Opcode, usize) -> Control<Opcode>> {
if opcode_t != Opcode(0x50) {
Box::new(move |machine, handle, opcode, position| {
assert_eq!(opcode_t, opcode);
println!("opcode: {:?}", opcode);
f(machine, handle, opcode, position)
})
} else {
Box::new(|_machine, _handle, opcode, _position| {
println!("disabled!");
Control::Trap(opcode)
})
}
},
);
let mut vm = Machine::new(Rc::new(code), Rc::new(data), 1024, 10000, ());
let result = vm.run(&mut (), &wrapped_etable);
assert_eq!(result, Capture::Trap(Opcode(0x50)));
}
pub struct UnimplementedHandler;
impl RuntimeEnvironment for UnimplementedHandler {
fn block_hash(&self, _number: U256) -> H256 {
unimplemented!()
}
fn block_number(&self) -> U256 {
unimplemented!()
}
fn block_coinbase(&self) -> H160 {
unimplemented!()
}
fn block_timestamp(&self) -> U256 {
unimplemented!()
}
fn block_difficulty(&self) -> U256 {
unimplemented!()
}
fn block_randomness(&self) -> Option<H256> {
unimplemented!()
}
fn block_gas_limit(&self) -> U256 {
unimplemented!()
}
fn block_base_fee_per_gas(&self) -> U256 {
unimplemented!()
}
fn chain_id(&self) -> U256 {
unimplemented!()
}
}
impl<'a> RuntimeBaseBackend for UnimplementedHandler {
fn balance(&self, _address: H160) -> U256 {
unimplemented!()
}
fn code_size(&self, _address: H160) -> U256 {
unimplemented!()
}
fn code_hash(&self, _address: H160) -> H256 {
unimplemented!()
}
fn code(&self, _address: H160) -> Vec<u8> {
unimplemented!()
}
fn storage(&self, _address: H160, _index: H256) -> H256 {
unimplemented!()
}
fn exists(&self, _address: H160) -> bool {
unimplemented!()
}
fn nonce(&self, _address: H160) -> U256 {
unimplemented!()
}
}
impl<'a> RuntimeBackend for UnimplementedHandler {
fn original_storage(&self, _address: H160, _index: H256) -> H256 {
unimplemented!()
}
fn deleted(&self, _address: H160) -> bool {
unimplemented!()
}
fn is_cold(&self, _address: H160, _index: Option<H256>) -> bool {
unimplemented!()
}
fn mark_hot(&mut self, _address: H160, _index: Option<H256>) {
unimplemented!()
}
fn set_storage(&mut self, _address: H160, _index: H256, _value: H256) -> Result<(), ExitError> {
unimplemented!()
}
fn log(&mut self, _log: Log) -> Result<(), ExitError> {
unimplemented!()
}
fn mark_delete(&mut self, _address: H160) {
unimplemented!()
}
fn reset_storage(&mut self, _address: H160) {
unimplemented!()
}
fn set_code(&mut self, _address: H160, _code: Vec<u8>) -> Result<(), ExitError> {
unimplemented!()
}
fn reset_balance(&mut self, _address: H160) {
unimplemented!()
}
fn deposit(&mut self, _address: H160, _value: U256) {
unimplemented!()
}
fn withdrawal(&mut self, _address: H160, _value: U256) -> Result<(), ExitError> {
unimplemented!()
}
fn inc_nonce(&mut self, _address: H160) -> Result<(), ExitError> {
unimplemented!()
}
}
static RUNTIME_ETABLE: Etable<RuntimeState, UnimplementedHandler, Opcode> = Etable::runtime();
#[test]
fn etable_runtime() {
let code = hex::decode(&CODE1).unwrap();
let data = hex::decode(&DATA1).unwrap();
let mut handler = UnimplementedHandler;
let mut vm = Machine::new(
Rc::new(code),
Rc::new(data),
1024,
10000,
RuntimeState {
context: Context {
address: H160::default(),
caller: H160::default(),
apparent_value: U256::default(),
},
transaction_context: TransactionContext {
gas_price: U256::default(),
origin: H160::default(),
}
.into(),
retbuf: Vec::new(),
gas: U256::zero(),
},
);
let res = vm.run(&mut handler, &RUNTIME_ETABLE).exit().unwrap();
assert_eq!(res, Ok(ExitSucceed::Returned.into()));
assert_eq!(vm.retval, hex::decode(&RET1).unwrap());
}