@@ -303,7 +303,7 @@ fn create() {
303303 & FakeCall {
304304 call_type: FakeCallType :: Create ,
305305 create_scheme: Some ( CreateContractAddress :: FromSenderAndCodeHash ) ,
306- gas: U256 :: from( 52_017 ) ,
306+ gas: U256 :: from( 49_674 ) ,
307307 sender_address: None ,
308308 receive_address: None ,
309309 value: Some ( ( 1_000_000_000 / 2 ) . into( ) ) ,
@@ -315,15 +315,15 @@ fn create() {
315315 & FakeCall {
316316 call_type: FakeCallType :: Create ,
317317 create_scheme: Some ( CreateContractAddress :: FromSenderSaltAndCodeHash ( H256 :: from( [ 5u8 ] . as_ref( ) ) ) ) ,
318- gas: U256 :: from( 10_740 ) ,
318+ gas: U256 :: from( 6039 ) ,
319319 sender_address: None ,
320320 receive_address: None ,
321321 value: Some ( ( 1_000_000_000 / 2 ) . into( ) ) ,
322322 data: vec![ 0u8 , 2 , 4 , 8 , 16 , 32 , 64 , 128 ] ,
323323 code_address: None ,
324324 }
325325 ) ) ;
326- assert_eq ! ( gas_left, U256 :: from( 10_675 ) ) ;
326+ assert_eq ! ( gas_left, U256 :: from( 5974 ) ) ;
327327}
328328
329329#[ test]
@@ -371,6 +371,54 @@ fn call_msg() {
371371 assert_eq ! ( gas_left, U256 :: from( 91_672 ) ) ;
372372}
373373
374+ // The same as `call_msg`, but send a `pwasm_ethereum::gasleft`
375+ // value as `gas` argument to the inner pwasm_ethereum::call
376+ #[ test]
377+ fn call_msg_gasleft ( ) {
378+ :: ethcore_logger:: init_log ( ) ;
379+
380+ let sender: Address = "01030507090b0d0f11131517191b1d1f21232527" . parse ( ) . unwrap ( ) ;
381+ let receiver: Address = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" . parse ( ) . unwrap ( ) ;
382+ let contract_address: Address = "0d461d4174b4ae35775c4a342f1e5e1e4e6c4db5" . parse ( ) . unwrap ( ) ;
383+
384+ let mut params = ActionParams :: default ( ) ;
385+ params. sender = sender. clone ( ) ;
386+ params. address = receiver. clone ( ) ;
387+ params. code_address = contract_address. clone ( ) ;
388+ params. gas = U256 :: from ( 100_000 ) ;
389+ params. code = Some ( Arc :: new ( load_sample ! ( "call_gasleft.wasm" ) ) ) ;
390+ params. data = Some ( Vec :: new ( ) ) ;
391+
392+ let mut ext = FakeExt :: new ( ) . with_wasm ( ) ;
393+ ext. schedule . wasm . as_mut ( ) . unwrap ( ) . have_gasleft = true ;
394+ ext. balances . insert ( receiver. clone ( ) , U256 :: from ( 10000000000u64 ) ) ;
395+
396+ let gas_left = {
397+ let mut interpreter = wasm_interpreter ( ) ;
398+ let result = interpreter. exec ( params, & mut ext) . expect ( "Interpreter to execute without any errors" ) ;
399+ match result {
400+ GasLeft :: Known ( gas_left) => gas_left,
401+ GasLeft :: NeedsReturn { .. } => { panic ! ( "Call test should not return payload" ) ; } ,
402+ }
403+ } ;
404+
405+ trace ! ( target: "wasm" , "fake_calls: {:?}" , & ext. calls) ;
406+ assert ! ( ext. calls. contains(
407+ & FakeCall {
408+ call_type: FakeCallType :: Call ,
409+ create_scheme: None ,
410+ gas: U256 :: from( 91_165 ) ,
411+ sender_address: Some ( receiver) ,
412+ receive_address: Some ( Address :: from( [ 99 , 88 , 77 , 66 , 55 , 44 , 33 , 22 , 11 , 0 , 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 0 ] ) ) ,
413+ value: Some ( 1000000000 . into( ) ) ,
414+ data: vec![ 129u8 , 123 , 113 , 107 , 101 , 97 ] ,
415+ code_address: Some ( Address :: from( [ 99 , 88 , 77 , 66 , 55 , 44 , 33 , 22 , 11 , 0 , 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 0 ] ) ) ,
416+ }
417+ ) ) ;
418+
419+ assert_eq ! ( gas_left, U256 :: from( 91_671 ) ) ;
420+ }
421+
374422#[ test]
375423fn call_code ( ) {
376424 :: ethcore_logger:: init_log ( ) ;
@@ -591,7 +639,7 @@ fn math_add() {
591639 U256 :: from_dec_str( "1888888888888888888888888888887" ) . unwrap( ) ,
592640 ( & result[ ..] ) . into( )
593641 ) ;
594- assert_eq ! ( gas_left, U256 :: from( 92_095 ) ) ;
642+ assert_eq ! ( gas_left, U256 :: from( 92_072 ) ) ;
595643}
596644
597645// multiplication
@@ -613,7 +661,7 @@ fn math_mul() {
613661 U256 :: from_dec_str( "888888888888888888888888888887111111111111111111111111111112" ) . unwrap( ) ,
614662 ( & result[ ..] ) . into( )
615663 ) ;
616- assert_eq ! ( gas_left, U256 :: from( 91_423 ) ) ;
664+ assert_eq ! ( gas_left, U256 :: from( 91_400 ) ) ;
617665}
618666
619667// subtraction
@@ -635,7 +683,7 @@ fn math_sub() {
635683 U256 :: from_dec_str( "111111111111111111111111111111" ) . unwrap( ) ,
636684 ( & result[ ..] ) . into( )
637685 ) ;
638- assert_eq ! ( gas_left, U256 :: from( 92_095 ) ) ;
686+ assert_eq ! ( gas_left, U256 :: from( 92_072 ) ) ;
639687}
640688
641689// subtraction with overflow
@@ -677,7 +725,7 @@ fn math_div() {
677725 U256 :: from_dec_str( "1125000" ) . unwrap( ) ,
678726 ( & result[ ..] ) . into( )
679727 ) ;
680- assert_eq ! ( gas_left, U256 :: from( 87_379 ) ) ;
728+ assert_eq ! ( gas_left, U256 :: from( 85_700 ) ) ;
681729}
682730
683731#[ test]
@@ -705,7 +753,7 @@ fn storage_metering() {
705753 } ;
706754
707755 // 0 -> not 0
708- assert_eq ! ( gas_left, U256 :: from( 72_395 ) ) ;
756+ assert_eq ! ( gas_left, U256 :: from( 72_164 ) ) ;
709757
710758 // #2
711759
@@ -724,7 +772,7 @@ fn storage_metering() {
724772 } ;
725773
726774 // not 0 -> not 0
727- assert_eq ! ( gas_left, U256 :: from( 87_395 ) ) ;
775+ assert_eq ! ( gas_left, U256 :: from( 87_164 ) ) ;
728776}
729777
730778// This test checks the ability of wasm contract to invoke
@@ -815,6 +863,47 @@ fn externs() {
815863 assert_eq ! ( gas_left, U256 :: from( 90_428 ) ) ;
816864}
817865
866+ // This test checks the ability of wasm contract to invoke gasleft
867+ #[ test]
868+ fn gasleft ( ) {
869+ :: ethcore_logger:: init_log ( ) ;
870+
871+ let mut params = ActionParams :: default ( ) ;
872+ params. gas = U256 :: from ( 100_000 ) ;
873+ params. code = Some ( Arc :: new ( load_sample ! ( "gasleft.wasm" ) ) ) ;
874+
875+ let mut ext = FakeExt :: new ( ) . with_wasm ( ) ;
876+ ext. schedule . wasm . as_mut ( ) . unwrap ( ) . have_gasleft = true ;
877+
878+ let mut interpreter = wasm_interpreter ( ) ;
879+ let result = interpreter. exec ( params, & mut ext) . expect ( "Interpreter to execute without any errors" ) ;
880+ match result {
881+ GasLeft :: Known ( _) => { } ,
882+ GasLeft :: NeedsReturn { gas_left, data, .. } => {
883+ let gas = LittleEndian :: read_u64 ( data. as_ref ( ) ) ;
884+ assert_eq ! ( gas, 93_423 ) ;
885+ assert_eq ! ( gas_left, U256 :: from( 93_349 ) ) ;
886+ } ,
887+ }
888+ }
889+
890+ // This test should fail because
891+ // ext.schedule.wasm.as_mut().unwrap().have_gasleft = false;
892+ #[ test]
893+ fn gasleft_fail ( ) {
894+ :: ethcore_logger:: init_log ( ) ;
895+
896+ let mut params = ActionParams :: default ( ) ;
897+ params. gas = U256 :: from ( 100_000 ) ;
898+ params. code = Some ( Arc :: new ( load_sample ! ( "gasleft.wasm" ) ) ) ;
899+ let mut ext = FakeExt :: new ( ) . with_wasm ( ) ;
900+ let mut interpreter = wasm_interpreter ( ) ;
901+ match interpreter. exec ( params, & mut ext) {
902+ Err ( _) => { } ,
903+ Ok ( _) => panic ! ( "interpreter.exec should return Err if ext.schedule.wasm.have_gasleft = false" )
904+ }
905+ }
906+
818907#[ test]
819908fn embedded_keccak ( ) {
820909 :: ethcore_logger:: init_log ( ) ;
@@ -873,7 +962,7 @@ fn events() {
873962 assert_eq ! ( & log_entry. data, b"gnihtemos" ) ;
874963
875964 assert_eq ! ( & result, b"gnihtemos" ) ;
876- assert_eq ! ( gas_left, U256 :: from( 83_158 ) ) ;
965+ assert_eq ! ( gas_left, U256 :: from( 83_161 ) ) ;
877966}
878967
879968#[ test]
0 commit comments