2121#![ cfg_attr( not( feature = "std" ) , no_std) ]
2222
2323mod backend;
24+ mod tests;
2425
2526pub use crate :: backend:: { Account , Log , Vicinity , Backend } ;
2627
@@ -144,7 +145,7 @@ pub trait Trait: frame_system::Trait + pallet_timestamp::Trait {
144145 /// Precompiles associated with this EVM engine.
145146 type Precompiles : Precompiles ;
146147 /// Chain ID of EVM.
147- type ChainId : Get < U256 > ;
148+ type ChainId : Get < u64 > ;
148149
149150 /// EVM config used in the module.
150151 fn config ( ) -> & ' static Config {
@@ -201,6 +202,12 @@ decl_event! {
201202 Log ( Log ) ,
202203 /// A contract has been created at given address.
203204 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 ) ,
204211 /// A deposit has been made at a given address.
205212 BalanceDeposit ( AccountId , H160 , U256 ) ,
206213 /// A withdrawal has been made from a given address.
@@ -220,12 +227,6 @@ decl_error! {
220227 WithdrawFailed ,
221228 /// Gas price is too low.
222229 GasPriceTooLow ,
223- /// Call failed
224- ExitReasonFailed ,
225- /// Call reverted
226- ExitReasonRevert ,
227- /// Call returned VM fatal error
228- ExitReasonFatal ,
229230 /// Nonce is invalid
230231 InvalidNonce ,
231232 }
@@ -300,15 +301,24 @@ decl_module! {
300301 let sender = ensure_signed( origin) ?;
301302 let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
302303
303- Self :: execute_call(
304+ match Self :: execute_call(
304305 source,
305306 target,
306307 input,
307308 value,
308309 gas_limit,
309310 gas_price,
310311 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 ( ( ) )
312322 }
313323
314324 /// Issue an EVM create operation. This is similar to a contract creation transaction in
@@ -327,16 +337,22 @@ decl_module! {
327337 let sender = ensure_signed( origin) ?;
328338 let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
329339
330- let create_address = Self :: execute_create(
340+ match Self :: execute_create(
331341 source,
332342 init,
333343 value,
334344 gas_limit,
335345 gas_price,
336346 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+ }
338355
339- Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
340356 Ok ( ( ) )
341357 }
342358
@@ -356,17 +372,23 @@ decl_module! {
356372 let sender = ensure_signed( origin) ?;
357373 let source = T :: ConvertAccountId :: convert_account_id( & sender) ;
358374
359- let create_address = Self :: execute_create2(
375+ match Self :: execute_create2(
360376 source,
361377 init,
362378 salt,
363379 value,
364380 gas_limit,
365381 gas_price,
366382 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+ }
368391
369- Module :: <T >:: deposit_event( Event :: <T >:: Created ( create_address) ) ;
370392 Ok ( ( ) )
371393 }
372394 }
@@ -413,7 +435,7 @@ impl<T: Trait> Module<T> {
413435 gas_limit : u32 ,
414436 gas_price : U256 ,
415437 nonce : Option < U256 >
416- ) -> Result < H160 , Error < T > > {
438+ ) -> Result < ( H160 , ExitReason ) , Error < T > > {
417439 Self :: execute_evm (
418440 source,
419441 value,
@@ -442,7 +464,7 @@ impl<T: Trait> Module<T> {
442464 gas_limit : u32 ,
443465 gas_price : U256 ,
444466 nonce : Option < U256 >
445- ) -> Result < H160 , Error < T > > {
467+ ) -> Result < ( H160 , ExitReason ) , Error < T > > {
446468 let code_hash = H256 :: from_slice ( Keccak256 :: digest ( & init) . as_slice ( ) ) ;
447469 Self :: execute_evm (
448470 source,
@@ -473,8 +495,8 @@ impl<T: Trait> Module<T> {
473495 gas_limit : u32 ,
474496 gas_price : U256 ,
475497 nonce : Option < U256 > ,
476- ) -> Result < ( ) , Error < T > > {
477- Self :: execute_evm (
498+ ) -> Result < ExitReason , Error < T > > {
499+ Ok ( Self :: execute_evm (
478500 source,
479501 value,
480502 gas_limit,
@@ -487,7 +509,7 @@ impl<T: Trait> Module<T> {
487509 input,
488510 gas_limit as usize ,
489511 ) ) ,
490- )
512+ ) ? . 1 )
491513 }
492514
493515 /// Execute an EVM operation.
@@ -498,7 +520,7 @@ impl<T: Trait> Module<T> {
498520 gas_price : U256 ,
499521 nonce : Option < U256 > ,
500522 f : F ,
501- ) -> Result < R , Error < T > > where
523+ ) -> Result < ( R , ExitReason ) , Error < T > > where
502524 F : FnOnce ( & mut StackExecutor < Backend < T > > ) -> ( R , ExitReason ) ,
503525 {
504526 let vicinity = Vicinity {
@@ -527,19 +549,12 @@ impl<T: Trait> Module<T> {
527549
528550 let ( retv, reason) = f ( & mut executor) ;
529551
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-
537552 let actual_fee = executor. fee ( gas_price) ;
538553 executor. deposit ( source, total_fee. saturating_sub ( actual_fee) ) ;
539554
540555 let ( values, logs) = executor. deconstruct ( ) ;
541556 backend. apply ( values, logs, true ) ;
542557
543- ret
558+ Ok ( ( retv , reason ) )
544559 }
545560}
0 commit comments