1
- extern crate ewasm_api;
2
- extern crate vm;
3
- extern crate evm;
4
1
extern crate ethereum_types;
2
+ extern crate evm;
3
+ extern crate ewasm_api;
5
4
extern crate parity_bytes as bytes;
5
+ extern crate vm;
6
6
7
- use std:: sync:: Arc ;
8
- use std:: ops:: Deref ;
9
7
use std:: cmp;
8
+ use std:: ops:: Deref ;
9
+ use std:: sync:: Arc ;
10
10
11
- use self :: ethereum_types:: { U128 , U256 , H256 , H160 , Address } ;
11
+ use self :: ethereum_types:: { Address , H160 , H256 , U128 , U256 } ;
12
12
13
13
use self :: bytes:: Bytes ;
14
14
15
15
use self :: evm:: Factory ;
16
16
17
17
use self :: vm:: {
18
- EnvInfo , CreateContractAddress , ReturnData , CleanDustMode , ActionParams ,
19
- ActionValue , Schedule , ContractCreateResult , MessageCallResult , CallType ,
20
- Result , GasLeft
18
+ ActionParams , ActionValue , CallType , CleanDustMode , ContractCreateResult ,
19
+ CreateContractAddress , EnvInfo , GasLeft , MessageCallResult , Result , ReturnData , Schedule ,
21
20
} ;
22
21
23
22
// For some explanation see ethcore/vm/src/tests.rs::FakeExt
@@ -26,7 +25,7 @@ use self::vm::{
26
25
struct EwasmExt {
27
26
pub info : EnvInfo ,
28
27
pub schedule : Schedule ,
29
- pub selfdestruct_address : Option < Address >
28
+ pub selfdestruct_address : Option < Address > ,
30
29
}
31
30
32
31
impl vm:: Ext for EwasmExt {
@@ -70,8 +69,10 @@ impl vm::Ext for EwasmExt {
70
69
/// Returns address balance.
71
70
fn balance ( & self , address : & Address ) -> Result < U256 > {
72
71
// FIXME: this type should just implement the From trait for the underlying type
73
- let address: [ u8 ; 20 ] = address. 0 ;
74
- Ok ( U256 :: from ( U128 :: from ( ewasm_api:: external_balance ( & address) ) ) )
72
+ let address: [ u8 ; 20 ] = address. 0 ;
73
+ Ok ( U256 :: from ( U128 :: from ( ewasm_api:: external_balance (
74
+ & address,
75
+ ) ) ) )
75
76
}
76
77
77
78
/// Returns the hash of one of the 256 most recent complete blocks.
@@ -83,7 +84,13 @@ impl vm::Ext for EwasmExt {
83
84
/// Creates new contract.
84
85
///
85
86
/// Returns gas_left and contract address if contract creation was succesfull.
86
- fn create ( & mut self , gas : & U256 , value : & U256 , code : & [ u8 ] , address : CreateContractAddress ) -> ContractCreateResult {
87
+ fn create (
88
+ & mut self ,
89
+ gas : & U256 ,
90
+ value : & U256 ,
91
+ code : & [ u8 ] ,
92
+ address : CreateContractAddress ,
93
+ ) -> ContractCreateResult {
87
94
// FIXME: implement
88
95
unimplemented ! ( )
89
96
// ContractCreateResult::Failed
@@ -94,15 +101,16 @@ impl vm::Ext for EwasmExt {
94
101
/// Returns Err, if we run out of gas.
95
102
/// Otherwise returns call_result which contains gas left
96
103
/// and true if subcall was successfull.
97
- fn call ( & mut self ,
98
- gas : & U256 ,
99
- sender_address : & Address ,
100
- receive_address : & Address ,
101
- value : Option < U256 > ,
102
- data : & [ u8 ] ,
103
- code_address : & Address ,
104
- output : & mut [ u8 ] ,
105
- call_type : CallType
104
+ fn call (
105
+ & mut self ,
106
+ gas : & U256 ,
107
+ sender_address : & Address ,
108
+ receive_address : & Address ,
109
+ value : Option < U256 > ,
110
+ data : & [ u8 ] ,
111
+ code_address : & Address ,
112
+ output : & mut [ u8 ] ,
113
+ call_type : CallType ,
106
114
) -> MessageCallResult {
107
115
// FIXME: set this properly
108
116
//let gas_limit = u64::from(gas);
@@ -112,14 +120,24 @@ impl vm::Ext for EwasmExt {
112
120
let gas_start = ewasm_api:: gas_left ( ) ;
113
121
114
122
// FIXME: this type should just implement the From trait for the underlying type
115
- let receive_address: [ u8 ; 20 ] = receive_address. 0 ;
123
+ let receive_address: [ u8 ; 20 ] = receive_address. 0 ;
116
124
117
125
let call_result = match call_type {
118
- CallType :: Call => ewasm_api:: call_mutable ( gas_limit, & receive_address, & U128 :: from ( value. unwrap_or_default ( ) ) . into ( ) , & data) ,
119
- CallType :: CallCode => ewasm_api:: call_code ( gas_limit, & receive_address, & U128 :: from ( value. unwrap_or_default ( ) ) . into ( ) , & data) ,
126
+ CallType :: Call => ewasm_api:: call_mutable (
127
+ gas_limit,
128
+ & receive_address,
129
+ & U128 :: from ( value. unwrap_or_default ( ) ) . into ( ) ,
130
+ & data,
131
+ ) ,
132
+ CallType :: CallCode => ewasm_api:: call_code (
133
+ gas_limit,
134
+ & receive_address,
135
+ & U128 :: from ( value. unwrap_or_default ( ) ) . into ( ) ,
136
+ & data,
137
+ ) ,
120
138
CallType :: DelegateCall => ewasm_api:: call_delegate ( gas_limit, & receive_address, & data) ,
121
139
CallType :: StaticCall => ewasm_api:: call_static ( gas_limit, & receive_address, & data) ,
122
- _ => panic ! ( )
140
+ _ => panic ! ( ) ,
123
141
} ;
124
142
125
143
// FIXME: might not be good enough
@@ -137,7 +155,7 @@ impl vm::Ext for EwasmExt {
137
155
138
156
let ret_len = ret. len ( ) ;
139
157
MessageCallResult :: Success ( gas_used, ReturnData :: new ( ret, 0 , ret_len) )
140
- } ,
158
+ }
141
159
ewasm_api:: CallResult :: Failure => MessageCallResult :: Failed ,
142
160
ewasm_api:: CallResult :: Revert => {
143
161
// Retrieve the entire returndata as it needs to be returned
@@ -223,13 +241,22 @@ impl vm::Ext for EwasmExt {
223
241
}
224
242
225
243
/// Decide if any more operations should be traced. Passthrough for the VM trace.
226
- fn trace_next_instruction ( & mut self , _pc : usize , _instruction : u8 , _current_gas : U256 ) -> bool { false }
244
+ fn trace_next_instruction ( & mut self , _pc : usize , _instruction : u8 , _current_gas : U256 ) -> bool {
245
+ false
246
+ }
227
247
228
248
/// Prepare to trace an operation. Passthrough for the VM trace.
229
249
fn trace_prepare_execute ( & mut self , _pc : usize , _instruction : u8 , _gas_cost : U256 ) { }
230
250
231
251
/// Trace the finalised execution of a single instruction.
232
- fn trace_executed ( & mut self , _gas_used : U256 , _stack_push : & [ U256 ] , _mem_diff : Option < ( usize , & [ u8 ] ) > , _store_diff : Option < ( U256 , U256 ) > ) { }
252
+ fn trace_executed (
253
+ & mut self ,
254
+ _gas_used : U256 ,
255
+ _stack_push : & [ U256 ] ,
256
+ _mem_diff : Option < ( usize , & [ u8 ] ) > ,
257
+ _store_diff : Option < ( U256 , U256 ) > ,
258
+ ) {
259
+ }
233
260
234
261
/// Check if running in static context.
235
262
fn is_static ( & self ) -> bool {
@@ -239,7 +266,7 @@ impl vm::Ext for EwasmExt {
239
266
}
240
267
241
268
#[ no_mangle]
242
- pub extern fn main ( ) {
269
+ pub extern "C" fn main ( ) {
243
270
// It is fine using U256::zero() here because the main point of the
244
271
// factory is to determine if gas is 64bit or not. In ewasm it is always 64bit.
245
272
let mut instance = Factory :: default ( ) . create ( & U256 :: zero ( ) ) ;
@@ -263,14 +290,18 @@ pub extern fn main() {
263
290
match result {
264
291
Ok ( GasLeft :: Known ( gas_left) ) => {
265
292
if ext. selfdestruct_address . is_some ( ) {
266
- let beneficiary: [ u8 ; 20 ] = ext. selfdestruct_address . unwrap ( ) . into ( ) ;
293
+ let beneficiary: [ u8 ; 20 ] = ext. selfdestruct_address . unwrap ( ) . into ( ) ;
267
294
ewasm_api:: selfdestruct ( & beneficiary)
268
295
} else {
269
296
ewasm_api:: finish ( )
270
297
}
271
- } ,
272
- Ok ( GasLeft :: NeedsReturn { gas_left, data, apply_state} ) => ewasm_api:: finish_data ( & data. deref ( ) ) ,
298
+ }
299
+ Ok ( GasLeft :: NeedsReturn {
300
+ gas_left,
301
+ data,
302
+ apply_state,
303
+ } ) => ewasm_api:: finish_data ( & data. deref ( ) ) ,
273
304
// FIXME: add support for pushing the error message as revert data
274
- Err ( err) => ewasm_api:: revert ( )
305
+ Err ( err) => ewasm_api:: revert ( ) ,
275
306
}
276
307
}
0 commit comments