@@ -604,3 +604,67 @@ pub fn selfdestruct(address: &Address) -> ! {
604604 native:: ethereum_selfDestruct ( address. bytes . as_ptr ( ) as * const u32 ) ;
605605 }
606606}
607+
608+ pub trait EwasmAPI : Send + Sync {
609+ fn consume_gas ( & self , amount : u64 ) ;
610+ fn gas_left ( & self ) -> u64 ;
611+ fn current_address ( & self ) -> [ u8 ; 20 ] ;
612+ }
613+
614+ #[ derive( Debug ) ]
615+ pub struct NativeImpl ;
616+
617+ #[ derive( Debug ) ]
618+ pub struct TestImpl {
619+ gas : u64 ,
620+ }
621+
622+ impl Default for TestImpl {
623+ fn default ( ) -> TestImpl {
624+ TestImpl { gas : 0 }
625+ }
626+ }
627+
628+ /*
629+ trait TestSetter {
630+ fn set_gas(mut self, amount: u64);
631+ }
632+
633+ impl TestSetter for TestImpl {
634+ fn set_gas(mut self, amount: u64) {
635+ self.gas = amount
636+ }
637+ }
638+ */
639+
640+ impl EwasmAPI for NativeImpl {
641+ fn consume_gas ( & self , amount : u64 ) { }
642+ fn gas_left ( & self ) -> u64 {
643+ gas_left ( )
644+ }
645+ fn current_address ( & self ) -> [ u8 ; 20 ] {
646+ [ 0u8 ; 20 ]
647+ }
648+ }
649+
650+ impl EwasmAPI for TestImpl {
651+ fn consume_gas ( & self , amount : u64 ) {
652+ }
653+ fn gas_left ( & self ) -> u64 {
654+ self . gas
655+ }
656+ fn current_address ( & self ) -> [ u8 ; 20 ] {
657+ [ 0u8 ; 20 ]
658+ }
659+ }
660+
661+ #[ cfg( test) ]
662+ mod tests {
663+ use super :: { EwasmAPI , TestImpl } ;
664+
665+ #[ test]
666+ fn consume_gas_func ( ) {
667+ assert_eq ! ( 0 , EwasmAPI :: gas_left( & <TestImpl >:: default ( ) ) ) ;
668+ assert_eq ! ( TestImpl :: default ( ) . gas_left( ) , 0 ) ;
669+ }
670+ }
0 commit comments