@@ -770,71 +770,72 @@ func ExamplePrecompileEnvironment() {
770770
771771func TestStateMutability (t * testing.T ) {
772772 rng := ethtest .NewPseudoRand (0 )
773- precompile := rng .Address ()
773+ precompileAddr := rng .Address ()
774774 chainID := rng .BigUint64 ()
775775
776776 const precompileReturn = "precompile executed"
777- hooks := & hookstest.Stub {
778- PrecompileOverrides : map [common.Address ]libevm.PrecompiledContract {
779- precompile : vm .NewStatefulPrecompile (func (env vm.PrecompileEnvironment , input []byte ) (ret []byte , err error ) {
777+ precompile := vm .NewStatefulPrecompile (func (env vm.PrecompileEnvironment , input []byte ) (ret []byte , err error ) {
778+ tests := []struct {
779+ name string
780+ env vm.PrecompileEnvironment
781+ want vm.StateMutability
782+ }{
783+ {
784+ name : "incoming argument" ,
785+ env : env ,
786+ want : vm .MutableState ,
787+ },
788+ {
789+ name : "AsReadOnly()" ,
790+ env : env .AsReadOnly (),
791+ want : vm .ReadOnlyState ,
792+ },
793+ {
794+ name : "AsPure()" ,
795+ env : env .AsPure (),
796+ want : vm .Pure ,
797+ },
798+ {
799+ name : "AsPure().AsReadOnly() is still pure" ,
800+ env : env .AsPure ().AsReadOnly (),
801+ want : vm .Pure ,
802+ },
803+ }
780804
781- tests := []struct {
782- name string
783- env vm.PrecompileEnvironment
784- want vm.StateMutability
785- }{
786- {
787- name : "incoming argument" ,
788- env : env ,
789- want : vm .MutableState ,
790- },
791- {
792- name : "AsReadOnly()" ,
793- env : env .AsReadOnly (),
794- want : vm .ReadOnlyState ,
795- },
796- {
797- name : "AsPure()" ,
798- env : env .AsPure (),
799- want : vm .Pure ,
800- },
801- {
802- name : "AsPure().AsReadOnly() is still pure" ,
803- env : env .AsPure ().AsReadOnly (),
804- want : vm .Pure ,
805- },
806- }
805+ for _ , tt := range tests {
806+ t .Run (tt .name , func (t * testing.T ) {
807+ env := tt .env // deliberately shadow the incoming arg
808+ t .Run ("mutability_and_access" , func (t * testing.T ) {
809+ assert .Equal (t , tt .want , env .StateMutability (), "env.StateMutability()" )
810+ assert .Equal (t , env .StateDB () != nil , tt .want == vm .MutableState , "env.StateDB() != nil i.f.f. MutableState" )
811+ assert .Equal (t , env .ReadOnlyState () != nil , tt .want != vm .Pure , "env.ReadOnlyState() != nil i.f.f !Pure" )
812+ })
813+
814+ t .Run ("environment_unmodified" , func (t * testing.T ) {
815+ // Each of these demonstrate that the underlying
816+ // copy of the environment propagates everything but
817+ // mutability.
818+ assert .Equal (t , chainID , env .ChainConfig ().ChainID , "Chain ID preserved" )
819+ assert .Equalf (t , precompileAddr , env .Addresses ().Self , "%T preserved" , env .Addresses ())
820+ assert .Equalf (t , vm .Call , env .IncomingCallType (), "%T preserved" , env .IncomingCallType ())
821+ })
822+ })
823+ }
807824
808- for _ , tt := range tests {
809- t .Run (tt .name , func (t * testing.T ) {
810- env := tt .env // deliberately shadow the incoming arg
811- t .Run ("mutability_and_access" , func (t * testing.T ) {
812- assert .Equal (t , tt .want , env .StateMutability (), "env.StateMutability()" )
813- assert .Equal (t , env .StateDB () != nil , tt .want == vm .MutableState , "env.StateDB() != nil i.f.f. MutableState" )
814- assert .Equal (t , env .ReadOnlyState () != nil , tt .want != vm .Pure , "env.ReadOnlyState() != nil i.f.f !Pure" )
815- })
816-
817- t .Run ("environment_unmodified" , func (t * testing.T ) {
818- // Each of these demonstrate that the underlying
819- // copy of the environment propagates everything but
820- // mutability.
821- assert .Equal (t , chainID , env .ChainConfig ().ChainID , "Chain ID preserved" )
822- assert .Equalf (t , precompile , env .Addresses ().Self , "%T preserved" , env .Addresses ())
823- assert .Equalf (t , vm .Call , env .IncomingCallType (), "%T preserved" , env .IncomingCallType ())
824- })
825- })
826- }
825+ return []byte (precompileReturn ), nil
826+ })
827827
828- return []byte (precompileReturn ), nil
829- }),
828+ hooks := & hookstest.Stub {
829+ PrecompileOverrides : map [common.Address ]libevm.PrecompiledContract {
830+ precompileAddr : precompile ,
830831 },
831832 }
832833 hooks .Register (t )
833834
834835 _ , evm := ethtest .NewZeroEVM (t , ethtest .WithChainConfig (& params.ChainConfig {
835836 ChainID : chainID ,
836837 }))
837- got , _ , err := evm .Call (vm.AccountRef {}, precompile , nil , 0 , uint256 .NewInt (0 ))
838+ got , _ , err := evm .Call (vm.AccountRef {}, precompileAddr , nil , 0 , uint256 .NewInt (0 ))
838839 if got , want := string (got ), precompileReturn ; err != nil || got != want {
839840 t .Errorf ("%T.Call([precompile]) got {%q, %v}; want {%q, nil}" , evm , got , err , want )
840841 }
0 commit comments