21
21
#![ cfg_attr( not( feature = "std" ) , no_std) ]
22
22
23
23
mod backend;
24
+ mod tests;
24
25
25
26
pub use crate :: backend:: { Account , Log , Vicinity , Backend } ;
26
27
@@ -144,7 +145,7 @@ pub trait Trait: frame_system::Trait + pallet_timestamp::Trait {
144
145
/// Precompiles associated with this EVM engine.
145
146
type Precompiles : Precompiles ;
146
147
/// Chain ID of EVM.
147
- type ChainId : Get < U256 > ;
148
+ type ChainId : Get < u64 > ;
148
149
149
150
/// EVM config used in the module.
150
151
fn config ( ) -> & ' static Config {
@@ -201,6 +202,12 @@ decl_event! {
201
202
Log ( Log ) ,
202
203
/// A contract has been created at given address.
203
204
Created ( H160 ) ,
205
+ /// A contract was attempted to be created, but the execution failed.
206
+ CreatedFailed ( H160 ) ,
207
+ /// A contract has been executed successfully with states applied.
208
+ Executed ( H160 ) ,
209
+ /// A contract has been executed with errors. States are reverted with only gas fees applied.
210
+ ExecutedFailed ( H160 ) ,
204
211
/// A deposit has been made at a given address.
205
212
BalanceDeposit ( AccountId , H160 , U256 ) ,
206
213
/// A withdrawal has been made from a given address.
@@ -220,12 +227,6 @@ decl_error! {
220
227
WithdrawFailed ,
221
228
/// Gas price is too low.
222
229
GasPriceTooLow ,
223
- /// Call failed
224
- ExitReasonFailed ,
225
- /// Call reverted
226
- ExitReasonRevert ,
227
- /// Call returned VM fatal error
228
- ExitReasonFatal ,
229
230
/// Nonce is invalid
230
231
InvalidNonce ,
231
232
}
@@ -300,15 +301,24 @@ decl_module! {
300
301
let sender = ensure_signed( origin) ?;
301
302
let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
302
303
303
- Self :: execute_call(
304
+ match Self :: execute_call(
304
305
source,
305
306
target,
306
307
input,
307
308
value,
308
309
gas_limit,
309
310
gas_price,
310
311
nonce,
311
- ) . map_err( Into :: into)
312
+ ) ? {
313
+ ExitReason :: Succeed ( _) => {
314
+ Module :: <T >:: deposit_event( Event :: <T >:: Executed ( target) ) ;
315
+ } ,
316
+ ExitReason :: Error ( _) | ExitReason :: Revert ( _) | ExitReason :: Fatal ( _) => {
317
+ Module :: <T >:: deposit_event( Event :: <T >:: ExecutedFailed ( target) ) ;
318
+ } ,
319
+ }
320
+
321
+ Ok ( ( ) )
312
322
}
313
323
314
324
/// Issue an EVM create operation. This is similar to a contract creation transaction in
@@ -327,16 +337,22 @@ decl_module! {
327
337
let sender = ensure_signed( origin) ?;
328
338
let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
329
339
330
- let create_address = Self :: execute_create(
340
+ match Self :: execute_create(
331
341
source,
332
342
init,
333
343
value,
334
344
gas_limit,
335
345
gas_price,
336
346
nonce
337
- ) ?;
347
+ ) ? {
348
+ ( create_address, ExitReason :: Succeed ( _) ) => {
349
+ Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
350
+ } ,
351
+ ( create_address, _) => {
352
+ Module :: <T >:: deposit_event( Event :: <T >:: CreatedFailed ( create_address) ) ;
353
+ } ,
354
+ }
338
355
339
- Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
340
356
Ok ( ( ) )
341
357
}
342
358
@@ -356,17 +372,23 @@ decl_module! {
356
372
let sender = ensure_signed( origin) ?;
357
373
let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
358
374
359
- let create_address = Self :: execute_create2(
375
+ match Self :: execute_create2(
360
376
source,
361
377
init,
362
378
salt,
363
379
value,
364
380
gas_limit,
365
381
gas_price,
366
382
nonce
367
- ) ?;
383
+ ) ? {
384
+ ( create_address, ExitReason :: Succeed ( _) ) => {
385
+ Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
386
+ } ,
387
+ ( create_address, _) => {
388
+ Module :: <T >:: deposit_event( Event :: <T >:: CreatedFailed ( create_address) ) ;
389
+ } ,
390
+ }
368
391
369
- Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
370
392
Ok ( ( ) )
371
393
}
372
394
}
@@ -413,7 +435,7 @@ impl<T: Trait> Module<T> {
413
435
gas_limit : u32 ,
414
436
gas_price : U256 ,
415
437
nonce : Option < U256 >
416
- ) -> Result < H160 , Error < T > > {
438
+ ) -> Result < ( H160 , ExitReason ) , Error < T > > {
417
439
Self :: execute_evm (
418
440
source,
419
441
value,
@@ -442,7 +464,7 @@ impl<T: Trait> Module<T> {
442
464
gas_limit : u32 ,
443
465
gas_price : U256 ,
444
466
nonce : Option < U256 >
445
- ) -> Result < H160 , Error < T > > {
467
+ ) -> Result < ( H160 , ExitReason ) , Error < T > > {
446
468
let code_hash = H256 :: from_slice ( Keccak256 :: digest ( & init) . as_slice ( ) ) ;
447
469
Self :: execute_evm (
448
470
source,
@@ -473,8 +495,8 @@ impl<T: Trait> Module<T> {
473
495
gas_limit : u32 ,
474
496
gas_price : U256 ,
475
497
nonce : Option < U256 > ,
476
- ) -> Result < ( ) , Error < T > > {
477
- Self :: execute_evm (
498
+ ) -> Result < ExitReason , Error < T > > {
499
+ Ok ( Self :: execute_evm (
478
500
source,
479
501
value,
480
502
gas_limit,
@@ -487,7 +509,7 @@ impl<T: Trait> Module<T> {
487
509
input,
488
510
gas_limit as usize ,
489
511
) ) ,
490
- )
512
+ ) ? . 1 )
491
513
}
492
514
493
515
/// Execute an EVM operation.
@@ -498,7 +520,7 @@ impl<T: Trait> Module<T> {
498
520
gas_price : U256 ,
499
521
nonce : Option < U256 > ,
500
522
f : F ,
501
- ) -> Result < R , Error < T > > where
523
+ ) -> Result < ( R , ExitReason ) , Error < T > > where
502
524
F : FnOnce ( & mut StackExecutor < Backend < T > > ) -> ( R , ExitReason ) ,
503
525
{
504
526
let vicinity = Vicinity {
@@ -527,19 +549,12 @@ impl<T: Trait> Module<T> {
527
549
528
550
let ( retv, reason) = f ( & mut executor) ;
529
551
530
- let ret = match reason {
531
- ExitReason :: Succeed ( _) => Ok ( retv) ,
532
- ExitReason :: Error ( _) => Err ( Error :: < T > :: ExitReasonFailed ) ,
533
- ExitReason :: Revert ( _) => Err ( Error :: < T > :: ExitReasonRevert ) ,
534
- ExitReason :: Fatal ( _) => Err ( Error :: < T > :: ExitReasonFatal ) ,
535
- } ;
536
-
537
552
let actual_fee = executor. fee ( gas_price) ;
538
553
executor. deposit ( source, total_fee. saturating_sub ( actual_fee) ) ;
539
554
540
555
let ( values, logs) = executor. deconstruct ( ) ;
541
556
backend. apply ( values, logs, true ) ;
542
557
543
- ret
558
+ Ok ( ( retv , reason ) )
544
559
}
545
560
}
0 commit comments