diff --git a/go.mod b/go.mod index b967ff0ea..5d794e01b 100644 --- a/go.mod +++ b/go.mod @@ -125,7 +125,6 @@ require ( github.com/fzipp/gocyclo v0.5.1 // indirect github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect - github.com/ghodss/yaml v1.0.0 // indirect github.com/go-critic/go-critic v0.6.3 // indirect github.com/go-kit/kit v0.12.0 // indirect github.com/go-kit/log v0.2.1 // indirect @@ -173,7 +172,6 @@ require ( github.com/gostaticanalysis/nilerr v0.1.1 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-bexpr v0.1.10 // indirect @@ -348,10 +346,10 @@ require ( replace ( github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.2.4 github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0 - github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.36 + github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.3.37-0.20240923023912-aa7a702d42cc github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.2.0 github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.3.2 - github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-22 + github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240923025222-815b87dde97b github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/sei-protocol/sei-db => github.com/sei-protocol/sei-db v0.0.44 // Latest goleveldb is broken, we have to stick to this version diff --git a/go.sum b/go.sum index 98428104b..38d948197 100644 --- a/go.sum +++ b/go.sum @@ -457,7 +457,6 @@ github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2Gihuqh github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc= github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= github.com/ghemawat/stream v0.0.0-20171120220530-696b145b53b9/go.mod h1:106OIgooyS7OzLDOpUGgm9fA3bQENb/cFSyyBmMoJDs= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -766,8 +765,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqCisSPWTxCZ7sBRjU6iH9c= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjwqUPTYmYuemVOx+Ys= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= github.com/guptarohit/asciigraph v0.5.5/go.mod h1:dYl5wwK4gNsnFf9Zp+l06rFiDZ5YtXM6x7SRWZ3KGag= @@ -1346,12 +1343,12 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod github.com/securego/gosec/v2 v2.11.0 h1:+PDkpzR41OI2jrw1q6AdXZCbsNGNGT7pQjal0H0cArI= github.com/securego/gosec/v2 v2.11.0/go.mod h1:SX8bptShuG8reGC0XS09+a4H2BoWSJi+fscA+Pulbpo= github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY= -github.com/sei-protocol/go-ethereum v1.13.5-sei-22 h1:t/m1qXER+DEMrcpqgoYmUxifkAdmz5T8xpL9KRVgLtw= -github.com/sei-protocol/go-ethereum v1.13.5-sei-22/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ= +github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240923025222-815b87dde97b h1:mVNX02TN7XlxkZliA752umeOB5MegtMroeIzdFGbgBk= +github.com/sei-protocol/go-ethereum v1.13.5-sei-9.0.20240923025222-815b87dde97b/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ= github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA= github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8= -github.com/sei-protocol/sei-cosmos v0.3.36 h1:mIqZd+MqwCoUJD5cSofzcMudf2t9JaEv6rUW88jG3P8= -github.com/sei-protocol/sei-cosmos v0.3.36/go.mod h1:ZwWxF/69WlcLEn4BzVjPPToTFkE2sjPanU8PNNyKoOk= +github.com/sei-protocol/sei-cosmos v0.3.37-0.20240923023912-aa7a702d42cc h1:srWLbsoS0NYBIl8OjZOFuPmIeqf+fJTkfsK39MmG3+k= +github.com/sei-protocol/sei-cosmos v0.3.37-0.20240923023912-aa7a702d42cc/go.mod h1:ZwWxF/69WlcLEn4BzVjPPToTFkE2sjPanU8PNNyKoOk= github.com/sei-protocol/sei-db v0.0.44 h1:HMgcyDTQlmXdJysHJxmIo66EKeXn1CSQT9qXDnxjJgI= github.com/sei-protocol/sei-db v0.0.44/go.mod h1:F/ZKZA8HJPcUzSZPA8yt6pfwlGriJ4RDR4eHKSGLStI= github.com/sei-protocol/sei-iavl v0.2.0 h1:OisPjXiDT+oe+aeckzDEFgkZCYuUjHgs/PP8DPicN+I= diff --git a/precompiles/addr/addr_test.go b/precompiles/addr/addr_test.go index c43f94834..9f5400dc6 100644 --- a/precompiles/addr/addr_test.go +++ b/precompiles/addr/addr_test.go @@ -141,7 +141,7 @@ func TestAssociatePubKey(t *testing.T) { require.Nil(t, err) // Make the call to associate. - ret, err := p.Run(tt.args.evm, tt.args.caller, tt.args.caller, append(p.GetExecutor().(*addr.PrecompileExecutor).AssociatePubKeyID, inputs...), tt.args.value, tt.args.readOnly) + ret, err := p.Run(tt.args.evm, tt.args.caller, tt.args.caller, append(p.GetExecutor().(*addr.PrecompileExecutor).AssociatePubKeyID, inputs...), tt.args.value, tt.args.readOnly, false) if (err != nil) != tt.wantErr { t.Errorf("Run() error = %v, wantErr %v %v", err, tt.wantErr, string(ret)) return @@ -331,7 +331,7 @@ func TestAssociate(t *testing.T) { require.Nil(t, err) // Make the call to associate. - ret, err := p.Run(tt.args.evm, tt.args.caller, tt.args.caller, append(p.GetExecutor().(*addr.PrecompileExecutor).AssociateID, inputs...), tt.args.value, tt.args.readOnly) + ret, err := p.Run(tt.args.evm, tt.args.caller, tt.args.caller, append(p.GetExecutor().(*addr.PrecompileExecutor).AssociateID, inputs...), tt.args.value, tt.args.readOnly, false) if (err != nil) != tt.wantErr { t.Errorf("Run() error = %v, wantErr %v %v", err, tt.wantErr, string(ret)) return diff --git a/precompiles/bank/bank.go b/precompiles/bank/bank.go index efc137090..88cb90a8a 100644 --- a/precompiles/bank/bank.go +++ b/precompiles/bank/bank.go @@ -161,7 +161,7 @@ func (p PrecompileExecutor) sendNative(ctx sdk.Context, method *abi.Method, args if readOnly { return nil, errors.New("cannot call sendNative from staticcall") } - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, errors.New("cannot delegatecall sendNative") } if err := pcommon.ValidateArgsLength(args, 1); err != nil { diff --git a/precompiles/bank/bank_test.go b/precompiles/bank/bank_test.go index 931898452..3ad24ed56 100644 --- a/precompiles/bank/bank_test.go +++ b/precompiles/bank/bank_test.go @@ -78,7 +78,7 @@ func TestRun(t *testing.T) { require.Nil(t, err) args, err := send.Inputs.Pack(senderEVMAddr, evmAddr, "usei", big.NewInt(25)) require.Nil(t, err) - _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendID, args...), nil, false) // should error because address is not whitelisted + _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendID, args...), nil, false, false) // should error because address is not whitelisted require.NotNil(t, err) // Precompile sendNative test error @@ -88,19 +88,19 @@ func TestRun(t *testing.T) { argsNativeError, err := sendNative.Inputs.Pack(seiAddrString) require.Nil(t, err) // 0 amount disallowed - _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(0), false) + _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(0), false, false) require.NotNil(t, err) argsNativeError, err = sendNative.Inputs.Pack("") require.Nil(t, err) - _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false) + _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false, false) require.NotNil(t, err) argsNativeError, err = sendNative.Inputs.Pack("invalidaddr") require.Nil(t, err) - _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false) + _, err = p.Run(&evm, senderEVMAddr, senderEVMAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false, false) require.NotNil(t, err) argsNativeError, err = sendNative.Inputs.Pack(senderAddr.String()) require.Nil(t, err) - _, err = p.Run(&evm, evmAddr, evmAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false) + _, err = p.Run(&evm, evmAddr, evmAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNativeError...), big.NewInt(100), false, false) require.NotNil(t, err) // Send native 10_000_000_000_100, split into 10 usei 100wei @@ -191,7 +191,7 @@ func TestRun(t *testing.T) { require.Nil(t, err) args, err = balance.Inputs.Pack(evmAddr, "usei") require.Nil(t, err) - precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false) + precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false, false) require.Nil(t, err) is, err := balance.Outputs.Unpack(precompileRes) require.Nil(t, err) @@ -205,7 +205,7 @@ func TestRun(t *testing.T) { argsNewAccount, err := sendNative.Inputs.Pack(newAddr.String()) require.Nil(t, err) require.Nil(t, k.BankKeeper().SendCoins(ctx, seiAddr, k.GetSeiAddressOrDefault(ctx, p.Address()), sdk.NewCoins(sdk.NewCoin("usei", sdk.OneInt())))) - _, err = p.Run(&evm, evmAddr, evmAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNewAccount...), big.NewInt(1), false) + _, err = p.Run(&evm, evmAddr, evmAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendNativeID, argsNewAccount...), big.NewInt(1), false, false) require.Nil(t, err) // should create account if not exists require.NotNil(t, k.AccountKeeper().GetAccount(statedb.Ctx(), newAddr)) @@ -215,7 +215,7 @@ func TestRun(t *testing.T) { require.Nil(t, err) args, err = allBalances.Inputs.Pack(senderEVMAddr) require.Nil(t, err) - precompileRes, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).AllBalancesID, args...), nil, false) + precompileRes, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).AllBalancesID, args...), nil, false, false) require.Nil(t, err) balances, err := allBalances.Outputs.Unpack(precompileRes) require.Nil(t, err) @@ -236,15 +236,15 @@ func TestRun(t *testing.T) { }, bank.CoinBalance(parsedBalances[1])) // Verify errors properly raised on bank balance calls with incorrect inputs - _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args[:1]...), nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args[:1]...), nil, false, false) require.NotNil(t, err) args, err = balance.Inputs.Pack(evmAddr, "") require.Nil(t, err) - _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false, false) require.NotNil(t, err) // invalid input - _, err = p.Run(&evm, common.Address{}, common.Address{}, []byte{1, 2, 3, 4}, nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, []byte{1, 2, 3, 4}, nil, false, false) require.NotNil(t, err) } @@ -285,7 +285,7 @@ func TestSendForUnlinkedReceiver(t *testing.T) { require.Nil(t, err) args, err := send.Inputs.Pack(senderEVMAddr, evmAddr, "ufoo", big.NewInt(100)) require.Nil(t, err) - _, err = p.Run(&evm, pointerAddr, pointerAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendID, args...), nil, false) // should not error + _, err = p.Run(&evm, pointerAddr, pointerAddr, append(p.GetExecutor().(*bank.PrecompileExecutor).SendID, args...), nil, false, false) // should not error require.Nil(t, err) // Use precompile balance to verify sendNative usei amount succeeded @@ -293,7 +293,7 @@ func TestSendForUnlinkedReceiver(t *testing.T) { require.Nil(t, err) args, err = balance.Inputs.Pack(evmAddr, "ufoo") require.Nil(t, err) - precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false) + precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false, false) require.Nil(t, err) is, err := balance.Outputs.Unpack(precompileRes) require.Nil(t, err) @@ -305,7 +305,7 @@ func TestSendForUnlinkedReceiver(t *testing.T) { require.Nil(t, err) args, err = allBalances.Inputs.Pack(senderEVMAddr) require.Nil(t, err) - precompileRes, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).AllBalancesID, args...), nil, false) + precompileRes, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).AllBalancesID, args...), nil, false, false) require.Nil(t, err) balances, err := allBalances.Outputs.Unpack(precompileRes) require.Nil(t, err) @@ -326,15 +326,15 @@ func TestSendForUnlinkedReceiver(t *testing.T) { }, bank.CoinBalance(parsedBalances[1])) // Verify errors properly raised on bank balance calls with incorrect inputs - _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args[:1]...), nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args[:1]...), nil, false, false) require.NotNil(t, err) args, err = balance.Inputs.Pack(evmAddr, "") require.Nil(t, err) - _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID, args...), nil, false, false) require.NotNil(t, err) // invalid input - _, err = p.Run(&evm, common.Address{}, common.Address{}, []byte{1, 2, 3, 4}, nil, false) + _, err = p.Run(&evm, common.Address{}, common.Address{}, []byte{1, 2, 3, 4}, nil, false, false) require.NotNil(t, err) } @@ -352,7 +352,7 @@ func TestMetadata(t *testing.T) { require.Nil(t, err) args, err := name.Inputs.Pack("usei") require.Nil(t, err) - res, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).NameID, args...), nil, false) + res, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).NameID, args...), nil, false, false) require.Nil(t, err) outputs, err := name.Outputs.Unpack(res) require.Nil(t, err) @@ -362,7 +362,7 @@ func TestMetadata(t *testing.T) { require.Nil(t, err) args, err = symbol.Inputs.Pack("usei") require.Nil(t, err) - res, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).SymbolID, args...), nil, false) + res, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).SymbolID, args...), nil, false, false) require.Nil(t, err) outputs, err = symbol.Outputs.Unpack(res) require.Nil(t, err) @@ -372,7 +372,7 @@ func TestMetadata(t *testing.T) { require.Nil(t, err) args, err = decimal.Inputs.Pack("usei") require.Nil(t, err) - res, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).DecimalsID, args...), nil, false) + res, err = p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*bank.PrecompileExecutor).DecimalsID, args...), nil, false, false) require.Nil(t, err) outputs, err = decimal.Outputs.Unpack(res) require.Nil(t, err) diff --git a/precompiles/common/precompiles.go b/precompiles/common/precompiles.go index 006d90074..fc9858761 100644 --- a/precompiles/common/precompiles.go +++ b/precompiles/common/precompiles.go @@ -56,7 +56,7 @@ func (p Precompile) RequiredGas(input []byte) uint64 { return p.executor.RequiredGas(input[4:], method) } -func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool) (bz []byte, err error) { +func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool, isFromDelegateCall bool) (bz []byte, err error) { operation := fmt.Sprintf("%s_unknown", p.name) defer func() { HandlePrecompileError(err, evm, operation) @@ -73,6 +73,7 @@ func (p Precompile) Run(evm *vm.EVM, caller common.Address, callingContract comm operation = method.Name em := ctx.EventManager() ctx = ctx.WithEventManager(sdk.NewEventManager()) + ctx = ctx.WithEVMPrecompileCalledFromDelegateCall(isFromDelegateCall) bz, err = p.executor.Execute(ctx, method, caller, callingContract, args, value, readOnly, evm) if err != nil { return bz, err @@ -146,7 +147,7 @@ func NewDynamicGasPrecompile(a abi.ABI, executor DynamicGasPrecompileExecutor, a return &DynamicGasPrecompile{Precompile: NewPrecompile(a, nil, address, name), executor: executor} } -func (d DynamicGasPrecompile) RunAndCalculateGas(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, suppliedGas uint64, value *big.Int, hooks *tracing.Hooks, readOnly bool) (ret []byte, remainingGas uint64, err error) { +func (d DynamicGasPrecompile) RunAndCalculateGas(evm *vm.EVM, caller common.Address, callingContract common.Address, input []byte, suppliedGas uint64, value *big.Int, hooks *tracing.Hooks, readOnly bool, isFromDelegateCall bool) (ret []byte, remainingGas uint64, err error) { operation := fmt.Sprintf("%s_unknown", d.name) defer func() { HandlePrecompileError(err, evm, operation) @@ -164,6 +165,7 @@ func (d DynamicGasPrecompile) RunAndCalculateGas(evm *vm.EVM, caller common.Addr operation = method.Name em := ctx.EventManager() ctx = ctx.WithEventManager(sdk.NewEventManager()) + ctx = ctx.WithEVMPrecompileCalledFromDelegateCall(isFromDelegateCall) ret, remainingGas, err = d.executor.Execute(ctx, method, caller, callingContract, args, value, readOnly, evm, suppliedGas) if err != nil { return ret, remainingGas, err diff --git a/precompiles/common/precompiles_test.go b/precompiles/common/precompiles_test.go index eae5ad7ab..c1143bec3 100644 --- a/precompiles/common/precompiles_test.go +++ b/precompiles/common/precompiles_test.go @@ -77,13 +77,13 @@ func TestPrecompileRun(t *testing.T) { require.Nil(t, err) precompile := common.NewPrecompile(newAbi, &MockPrecompileExecutor{throw: false}, ethcommon.Address{}, "test") stateDB := state.NewDBImpl(ctx, k, false) - res, err := precompile.Run(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, big.NewInt(0), false) + res, err := precompile.Run(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, big.NewInt(0), false, false) require.Equal(t, []byte("success"), res) require.Nil(t, err) require.NotEmpty(t, stateDB.Ctx().EventManager().Events()) stateDB.WithCtx(ctx.WithEventManager(sdk.NewEventManager())) precompile = common.NewPrecompile(newAbi, &MockPrecompileExecutor{throw: true}, ethcommon.Address{}, "test") - res, err = precompile.Run(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, big.NewInt(0), false) + res, err = precompile.Run(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, big.NewInt(0), false, false) require.NotNil(t, res) require.NotNil(t, err) // should not emit any event @@ -118,13 +118,13 @@ func TestDynamicGasPrecompileRun(t *testing.T) { require.Nil(t, err) precompile := common.NewDynamicGasPrecompile(newAbi, &MockDynamicGasPrecompileExecutor{throw: false, evmKeeper: k}, ethcommon.Address{}, "test") stateDB := state.NewDBImpl(ctx, k, false) - res, _, err := precompile.RunAndCalculateGas(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, 0, big.NewInt(0), nil, false) + res, _, err := precompile.RunAndCalculateGas(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, 0, big.NewInt(0), nil, false, false) require.Equal(t, []byte("success"), res) require.Nil(t, err) require.NotEmpty(t, stateDB.Ctx().EventManager().Events()) stateDB.WithCtx(ctx.WithEventManager(sdk.NewEventManager())) precompile = common.NewDynamicGasPrecompile(newAbi, &MockDynamicGasPrecompileExecutor{throw: true, evmKeeper: k}, ethcommon.Address{}, "test") - res, _, err = precompile.RunAndCalculateGas(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, 0, big.NewInt(0), nil, false) + res, _, err = precompile.RunAndCalculateGas(&vm.EVM{StateDB: stateDB}, ethcommon.Address{}, ethcommon.Address{}, input, 0, big.NewInt(0), nil, false, false) require.NotNil(t, res) require.NotNil(t, err) // should not emit any event diff --git a/precompiles/distribution/distribution.go b/precompiles/distribution/distribution.go index d24c63efc..ad49427ee 100644 --- a/precompiles/distribution/distribution.go +++ b/precompiles/distribution/distribution.go @@ -70,7 +70,7 @@ func NewPrecompile(distrKeeper pcommon.DistributionKeeper, evmKeeper pcommon.EVM } func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, 0, errors.New("cannot delegatecall distr") } switch method.Name { diff --git a/precompiles/distribution/distribution_test.go b/precompiles/distribution/distribution_test.go index f45919ebb..087b0a7c8 100644 --- a/precompiles/distribution/distribution_test.go +++ b/precompiles/distribution/distribution_test.go @@ -366,13 +366,14 @@ func TestPrecompile_RunAndCalculateGas_WithdrawDelegationRewards(t *testing.T) { WithdrawMultipleDelegationRewardsID []byte } type args struct { - evm *vm.EVM - caller common.Address - callingContract common.Address - validator string - suppliedGas uint64 - value *big.Int - readOnly bool + evm *vm.EVM + caller common.Address + callingContract common.Address + validator string + suppliedGas uint64 + value *big.Int + readOnly bool + isFromDelegateCall bool } tests := []struct { name string @@ -431,13 +432,14 @@ func TestPrecompile_RunAndCalculateGas_WithdrawDelegationRewards(t *testing.T) { wantErrMsg: "{ReadFlat}", }, { - name: "fails if caller != callingContract", + name: "fails if caller == callingContract, delegatecall", fields: fields{}, args: args{ - caller: notAssociatedCallerEvmAddress, - callingContract: contractEvmAddress, - validator: validatorAddress, - suppliedGas: uint64(1000000), + caller: contractEvmAddress, + callingContract: contractEvmAddress, + validator: validatorAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -445,13 +447,14 @@ func TestPrecompile_RunAndCalculateGas_WithdrawDelegationRewards(t *testing.T) { wantErrMsg: "cannot delegatecall distr", }, { - name: "fails if caller != callingContract and callingContract not set", + name: "fails if caller != callingContract and callingContract not set, from a delegatecall", fields: fields{}, args: args{ - caller: notAssociatedCallerEvmAddress, - callingContract: contractEvmAddress, - validator: validatorAddress, - suppliedGas: uint64(1000000), + caller: notAssociatedCallerEvmAddress, + callingContract: contractEvmAddress, + validator: validatorAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -488,7 +491,7 @@ func TestPrecompile_RunAndCalculateGas_WithdrawDelegationRewards(t *testing.T) { require.Nil(t, err) inputs, err := withdraw.Inputs.Pack(tt.args.validator) require.Nil(t, err) - gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).WithdrawDelegationRewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly) + gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).WithdrawDelegationRewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("RunAndCalculateGas() error = %v, wantErr %v", err, tt.wantErr) return @@ -521,13 +524,14 @@ func TestPrecompile_RunAndCalculateGas_WithdrawMultipleDelegationRewards(t *test WithdrawMultipleDelegationRewardsID []byte } type args struct { - evm *vm.EVM - caller common.Address - callingContract common.Address - validators []string - suppliedGas uint64 - value *big.Int - readOnly bool + evm *vm.EVM + caller common.Address + callingContract common.Address + validators []string + suppliedGas uint64 + value *big.Int + readOnly bool + isFromDelegateCall bool } tests := []struct { name string @@ -589,10 +593,11 @@ func TestPrecompile_RunAndCalculateGas_WithdrawMultipleDelegationRewards(t *test name: "fails if caller != callingContract", fields: fields{}, args: args{ - caller: notAssociatedCallerEvmAddress, - callingContract: contractEvmAddress, - validators: validatorAddresses, - suppliedGas: uint64(1000000), + caller: notAssociatedCallerEvmAddress, + callingContract: contractEvmAddress, + validators: validatorAddresses, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -603,9 +608,10 @@ func TestPrecompile_RunAndCalculateGas_WithdrawMultipleDelegationRewards(t *test name: "fails if caller != callingContract and callingContract not set", fields: fields{}, args: args{ - caller: notAssociatedCallerEvmAddress, - validators: validatorAddresses, - suppliedGas: uint64(1000000), + caller: notAssociatedCallerEvmAddress, + validators: validatorAddresses, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -642,7 +648,7 @@ func TestPrecompile_RunAndCalculateGas_WithdrawMultipleDelegationRewards(t *test require.Nil(t, err) inputs, err := withdraw.Inputs.Pack(tt.args.validators) require.Nil(t, err) - gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).WithdrawMultipleDelegationRewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly) + gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).WithdrawMultipleDelegationRewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("RunAndCalculateGas() error = %v, wantErr %v", err, tt.wantErr) return @@ -675,13 +681,14 @@ func TestPrecompile_RunAndCalculateGas_SetWithdrawAddress(t *testing.T) { WithdrawMultipleDelegationRewardsID []byte } type args struct { - evm *vm.EVM - addressToSet common.Address - caller common.Address - callingContract common.Address - suppliedGas uint64 - value *big.Int - readOnly bool + evm *vm.EVM + addressToSet common.Address + caller common.Address + callingContract common.Address + suppliedGas uint64 + value *big.Int + readOnly bool + isFromDelegateCall bool } tests := []struct { name string @@ -757,10 +764,11 @@ func TestPrecompile_RunAndCalculateGas_SetWithdrawAddress(t *testing.T) { name: "fails if caller != callingContract", fields: fields{}, args: args{ - addressToSet: common.Address{}, - caller: callerEvmAddress, - callingContract: contractEvmAddress, - suppliedGas: uint64(1000000), + addressToSet: common.Address{}, + caller: callerEvmAddress, + callingContract: contractEvmAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -771,9 +779,10 @@ func TestPrecompile_RunAndCalculateGas_SetWithdrawAddress(t *testing.T) { name: "fails if caller != callingContract with callingContract not set", fields: fields{}, args: args{ - addressToSet: common.Address{}, - caller: callerEvmAddress, - suppliedGas: uint64(1000000), + addressToSet: common.Address{}, + caller: callerEvmAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRet: nil, wantRemainingGas: 0, @@ -812,7 +821,7 @@ func TestPrecompile_RunAndCalculateGas_SetWithdrawAddress(t *testing.T) { require.Nil(t, err) inputs, err := setAddress.Inputs.Pack(tt.args.addressToSet) require.Nil(t, err) - gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).SetWithdrawAddrID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly) + gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).SetWithdrawAddrID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("RunAndCalculateGas() error = %v, wantErr %v", err, tt.wantErr) return @@ -947,13 +956,14 @@ func TestPrecompile_RunAndCalculateGas_Rewards(t *testing.T) { WithdrawMultipleDelegationRewardsID []byte } type args struct { - evm *vm.EVM - delegatorAddress common.Address - caller common.Address - callingContract common.Address - suppliedGas uint64 - value *big.Int - readOnly bool + evm *vm.EVM + delegatorAddress common.Address + caller common.Address + callingContract common.Address + suppliedGas uint64 + value *big.Int + readOnly bool + isFromDelegateCall bool } tests := []struct { name string @@ -1015,10 +1025,11 @@ func TestPrecompile_RunAndCalculateGas_Rewards(t *testing.T) { distrKeeper: &TestDistributionKeeper{}, }, args: args{ - delegatorAddress: common.Address{}, - caller: callerEvmAddress, - callingContract: contractEvmAddress, - suppliedGas: uint64(1000000), + delegatorAddress: common.Address{}, + caller: callerEvmAddress, + callingContract: contractEvmAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRemainingGas: 0, wantErr: true, @@ -1030,9 +1041,10 @@ func TestPrecompile_RunAndCalculateGas_Rewards(t *testing.T) { distrKeeper: &TestDistributionKeeper{}, }, args: args{ - delegatorAddress: common.Address{}, - caller: callerEvmAddress, - suppliedGas: uint64(1000000), + delegatorAddress: common.Address{}, + caller: callerEvmAddress, + suppliedGas: uint64(1000000), + isFromDelegateCall: true, }, wantRemainingGas: 0, wantErr: true, @@ -1087,7 +1099,7 @@ func TestPrecompile_RunAndCalculateGas_Rewards(t *testing.T) { require.Nil(t, err) inputs, err := rewards.Inputs.Pack(tt.args.delegatorAddress) require.Nil(t, err) - gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).RewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly) + gotRet, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*distribution.PrecompileExecutor).RewardsID, inputs...), tt.args.suppliedGas, tt.args.value, nil, tt.args.readOnly, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("RunAndCalculateGas() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/precompiles/gov/gov.go b/precompiles/gov/gov.go index e3be07b76..a092b64be 100644 --- a/precompiles/gov/gov.go +++ b/precompiles/gov/gov.go @@ -77,7 +77,7 @@ func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller if readOnly { return nil, errors.New("cannot call gov precompile from staticcall") } - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, errors.New("cannot delegatecall gov") } diff --git a/precompiles/ibc/ibc.go b/precompiles/ibc/ibc.go index 160ab8cdb..a7e24d4ae 100644 --- a/precompiles/ibc/ibc.go +++ b/precompiles/ibc/ibc.go @@ -76,7 +76,7 @@ func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller if readOnly { return nil, 0, errors.New("cannot call IBC precompile from staticcall") } - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, 0, errors.New("cannot delegatecall IBC") } diff --git a/precompiles/ibc/ibc_test.go b/precompiles/ibc/ibc_test.go index 7b5e735a4..08a231377 100644 --- a/precompiles/ibc/ibc_test.go +++ b/precompiles/ibc/ibc_test.go @@ -68,11 +68,12 @@ func TestPrecompile_Run(t *testing.T) { memo string } type args struct { - caller common.Address - callingContract common.Address - input *input - suppliedGas uint64 - value *big.Int + caller common.Address + callingContract common.Address + input *input + suppliedGas uint64 + value *big.Int + isFromDelegateCall bool } commonArgs := args{ @@ -120,7 +121,7 @@ func TestPrecompile_Run(t *testing.T) { { name: "failed transfer: caller not whitelisted", fields: fields{transferKeeper: &MockTransferKeeper{}}, - args: args{caller: senderEvmAddress, callingContract: common.Address{}, input: commonArgs.input, suppliedGas: 1000000, value: nil}, + args: args{caller: senderEvmAddress, callingContract: common.Address{}, input: commonArgs.input, suppliedGas: 1000000, value: nil, isFromDelegateCall: true}, wantBz: nil, wantErr: true, wantErrMsg: "cannot delegatecall IBC", @@ -278,7 +279,7 @@ func TestPrecompile_Run(t *testing.T) { tt.args.input.revisionNumber, tt.args.input.revisionHeight, tt.args.input.timeoutTimestamp, tt.args.input.memo) require.Nil(t, err) - gotBz, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*ibc.PrecompileExecutor).TransferID, inputs...), tt.args.suppliedGas, tt.args.value, nil, false) + gotBz, gotRemainingGas, err := p.RunAndCalculateGas(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*ibc.PrecompileExecutor).TransferID, inputs...), tt.args.suppliedGas, tt.args.value, nil, false, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) return @@ -317,11 +318,12 @@ func TestTransferWithDefaultTimeoutPrecompile_Run(t *testing.T) { memo string } type args struct { - caller common.Address - callingContract common.Address - input *input - suppliedGas uint64 - value *big.Int + caller common.Address + callingContract common.Address + input *input + suppliedGas uint64 + value *big.Int + isFromDelegateCall bool } commonArgs := args{ @@ -350,7 +352,7 @@ func TestTransferWithDefaultTimeoutPrecompile_Run(t *testing.T) { { name: "failed transfer: caller not whitelisted", fields: fields{transferKeeper: &MockTransferKeeper{}}, - args: args{caller: senderEvmAddress, callingContract: common.Address{}, input: commonArgs.input, suppliedGas: 1000000, value: nil}, + args: args{caller: senderEvmAddress, callingContract: common.Address{}, input: commonArgs.input, suppliedGas: 1000000, value: nil, isFromDelegateCall: true}, wantBz: nil, wantErr: true, wantErrMsg: "cannot delegatecall IBC", @@ -465,7 +467,9 @@ func TestTransferWithDefaultTimeoutPrecompile_Run(t *testing.T) { tt.args.suppliedGas, tt.args.value, nil, - false) + false, + tt.args.isFromDelegateCall, + ) if (err != nil) != tt.wantErr { t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/precompiles/json/json_test.go b/precompiles/json/json_test.go index fd86125fe..ccb598930 100644 --- a/precompiles/json/json_test.go +++ b/precompiles/json/json_test.go @@ -41,7 +41,7 @@ func TestExtractAsBytes(t *testing.T) { args, err := method.Inputs.Pack(test.body, "key") require.Nil(t, err) input := append(p.GetExecutor().(*json.PrecompileExecutor).ExtractAsBytesID, args...) - res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true) + res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true, false) require.Nil(t, err) output, err := method.Outputs.Unpack(res) require.Nil(t, err) @@ -79,7 +79,7 @@ func TestExtractAsBytesList(t *testing.T) { args, err := method.Inputs.Pack(test.body, "key") require.Nil(t, err) input := append(p.GetExecutor().(*json.PrecompileExecutor).ExtractAsBytesListID, args...) - res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true) + res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true, false) require.Nil(t, err) output, err := method.Outputs.Unpack(res) require.Nil(t, err) @@ -113,7 +113,7 @@ func TestExtractAsUint256(t *testing.T) { args, err := method.Inputs.Pack(test.body, "key") require.Nil(t, err) input := append(p.GetExecutor().(*json.PrecompileExecutor).ExtractAsUint256ID, args...) - res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true) + res, err := p.Run(evm, common.Address{}, common.Address{}, input, nil, true, false) require.Nil(t, err) output, err := method.Outputs.Unpack(res) require.Nil(t, err) diff --git a/precompiles/oracle/oracle_test.go b/precompiles/oracle/oracle_test.go index 0cbecf2b2..c537dde0e 100644 --- a/precompiles/oracle/oracle_test.go +++ b/precompiles/oracle/oracle_test.go @@ -38,7 +38,7 @@ func TestGetExchangeRate(t *testing.T) { query, err := p.ABI.MethodById(p.GetExecutor().(*oracle.PrecompileExecutor).GetExchangeRatesId) require.Nil(t, err) - precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, p.GetExecutor().(*oracle.PrecompileExecutor).GetExchangeRatesId, nil, true) + precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, p.GetExecutor().(*oracle.PrecompileExecutor).GetExchangeRatesId, nil, true, false) require.Nil(t, err) exchangeRates, err := query.Outputs.Unpack(precompileRes) require.Nil(t, err) @@ -114,7 +114,7 @@ func TestGetOracleTwaps(t *testing.T) { require.Nil(t, err) args, err := query.Inputs.Pack(uint64(3600)) require.Nil(t, err) - precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*oracle.PrecompileExecutor).GetOracleTwapsId, args...), nil, true) + precompileRes, err := p.Run(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*oracle.PrecompileExecutor).GetOracleTwapsId, args...), nil, true, false) require.Nil(t, err) twap, err := query.Outputs.Unpack(precompileRes) require.Nil(t, err) diff --git a/precompiles/pointer/pointer.go b/precompiles/pointer/pointer.go index c939a47b4..6d1502cd1 100644 --- a/precompiles/pointer/pointer.go +++ b/precompiles/pointer/pointer.go @@ -67,7 +67,7 @@ func (p PrecompileExecutor) Execute(ctx sdk.Context, method *ethabi.Method, call if readOnly { return nil, 0, errors.New("cannot call pointer precompile from staticcall") } - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, 0, errors.New("cannot delegatecall pointer") } diff --git a/precompiles/pointer/pointer_test.go b/precompiles/pointer/pointer_test.go index be5f10b46..d3fe14b00 100644 --- a/precompiles/pointer/pointer_test.go +++ b/precompiles/pointer/pointer_test.go @@ -35,7 +35,7 @@ func TestAddNative(t *testing.T) { statedb := state.NewDBImpl(ctx, &testApp.EvmKeeper, true) blockCtx, _ := testApp.EvmKeeper.GetVMBlockContext(ctx, core.GasPool(suppliedGas)) evm := vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}) - _, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false) + _, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.NotNil(t, statedb.GetPrecompileError()) require.Equal(t, uint64(0), g) @@ -55,7 +55,7 @@ func TestAddNative(t *testing.T) { }) statedb = state.NewDBImpl(ctx, &testApp.EvmKeeper, false) evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}) - ret, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false) + ret, g, err := p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) require.Equal(t, uint64(8888494), g) outputs, err := m.Outputs.Unpack(ret) @@ -84,7 +84,7 @@ func TestAddNative(t *testing.T) { testApp.EvmKeeper.SetERC20NativePointerWithVersion(statedb.Ctx(), "test", pointerAddr, version-1) statedb = state.NewDBImpl(statedb.Ctx(), &testApp.EvmKeeper, true) evm = vm.NewEVM(*blockCtx, vm.TxContext{}, statedb, cfg, vm.Config{}) - _, _, err = p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(evm, caller, caller, append(p.GetExecutor().(*pointer.PrecompileExecutor).AddNativePointerID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) require.Nil(t, statedb.GetPrecompileError()) newAddr, _, exists := testApp.EvmKeeper.GetERC20NativePointer(statedb.Ctx(), "test") diff --git a/precompiles/staking/staking.go b/precompiles/staking/staking.go index 443d4fef8..1e27bda25 100644 --- a/precompiles/staking/staking.go +++ b/precompiles/staking/staking.go @@ -86,7 +86,7 @@ func (p PrecompileExecutor) RequiredGas(input []byte, method *abi.Method) uint64 } func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool, evm *vm.EVM) (bz []byte, err error) { - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { return nil, errors.New("cannot delegatecall staking") } switch method.Name { diff --git a/precompiles/staking/staking_test.go b/precompiles/staking/staking_test.go index 81eeae979..ae7a2c4ad 100644 --- a/precompiles/staking/staking_test.go +++ b/precompiles/staking/staking_test.go @@ -308,13 +308,14 @@ func TestPrecompile_Run_Delegation(t *testing.T) { evmKeeper pcommon.EVMKeeper } type args struct { - evm *vm.EVM - delegatorAddress common.Address - validatorAddress string - caller common.Address - callingContract common.Address - value *big.Int - readOnly bool + evm *vm.EVM + delegatorAddress common.Address + validatorAddress string + caller common.Address + callingContract common.Address + value *big.Int + readOnly bool + isFromDelegateCall bool } tests := []struct { @@ -349,11 +350,12 @@ func TestPrecompile_Run_Delegation(t *testing.T) { }, }, args: args{ - caller: callerEvmAddress, - callingContract: contractEvmAddress, - delegatorAddress: callerEvmAddress, - validatorAddress: validatorAddress, - value: big.NewInt(100), + caller: callerEvmAddress, + callingContract: contractEvmAddress, + delegatorAddress: callerEvmAddress, + validatorAddress: validatorAddress, + value: big.NewInt(100), + isFromDelegateCall: true, }, wantErr: true, wantErrMsg: "cannot delegatecall staking", @@ -456,7 +458,7 @@ func TestPrecompile_Run_Delegation(t *testing.T) { require.Nil(t, err) inputs, err := delegation.Inputs.Pack(tt.args.delegatorAddress, tt.args.validatorAddress) require.Nil(t, err) - gotRet, err := p.Run(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*staking.PrecompileExecutor).DelegationID, inputs...), tt.args.value, tt.args.readOnly) + gotRet, err := p.Run(&evm, tt.args.caller, tt.args.callingContract, append(p.GetExecutor().(*staking.PrecompileExecutor).DelegationID, inputs...), tt.args.value, tt.args.readOnly, tt.args.isFromDelegateCall) if (err != nil) != tt.wantErr { t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/precompiles/wasmd/wasmd.go b/precompiles/wasmd/wasmd.go index d28f00684..a01b9d56c 100644 --- a/precompiles/wasmd/wasmd.go +++ b/precompiles/wasmd/wasmd.go @@ -104,7 +104,7 @@ func (p PrecompileExecutor) EVMKeeper() pcommon.EVMKeeper { return p.evmKeeper } -func (p PrecompileExecutor) instantiate(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool) (ret []byte, remainingGas uint64, rerr error) { +func (p PrecompileExecutor) instantiate(ctx sdk.Context, method *abi.Method, caller common.Address, _ common.Address, args []interface{}, value *big.Int, readOnly bool) (ret []byte, remainingGas uint64, rerr error) { defer func() { if err := recover(); err != nil { ret = nil @@ -121,7 +121,7 @@ func (p PrecompileExecutor) instantiate(ctx sdk.Context, method *abi.Method, cal rerr = err return } - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { rerr = errors.New("cannot delegatecall instantiate") return } @@ -254,7 +254,7 @@ func (p PrecompileExecutor) executeBatch(ctx sdk.Context, method *abi.Method, ca // type assertion will always succeed because it's already validated in p.Prepare call in Run() contractAddrStr := executeMsg.ContractAddress - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { erc20pointer, _, erc20exists := p.evmKeeper.GetERC20CW20Pointer(ctx, contractAddrStr) erc721pointer, _, erc721exists := p.evmKeeper.GetERC721CW721Pointer(ctx, contractAddrStr) if (!erc20exists || erc20pointer.Cmp(callingContract) != 0) && (!erc721exists || erc721pointer.Cmp(callingContract) != 0) { @@ -347,7 +347,7 @@ func (p PrecompileExecutor) execute(ctx sdk.Context, method *abi.Method, caller // type assertion will always succeed because it's already validated in p.Prepare call in Run() contractAddrStr := args[0].(string) - if caller.Cmp(callingContract) != 0 { + if ctx.EVMPrecompileCalledFromDelegateCall() { erc20pointer, _, erc20exists := p.evmKeeper.GetERC20CW20Pointer(ctx, contractAddrStr) erc721pointer, _, erc721exists := p.evmKeeper.GetERC721CW721Pointer(ctx, contractAddrStr) if (!erc20exists || erc20pointer.Cmp(callingContract) != 0) && (!erc721exists || erc721pointer.Cmp(callingContract) != 0) { diff --git a/precompiles/wasmd/wasmd_test.go b/precompiles/wasmd/wasmd_test.go index 1e6670c51..3fa0a285d 100644 --- a/precompiles/wasmd/wasmd_test.go +++ b/precompiles/wasmd/wasmd_test.go @@ -61,7 +61,7 @@ func TestInstantiate(t *testing.T) { } testApp.BankKeeper.SendCoins(ctx, mockAddr, testApp.EvmKeeper.GetSeiAddressOrDefault(ctx, common.HexToAddress(wasmd.WasmdAddress)), amts) suppliedGas := uint64(1000000) - res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.Nil(t, err) outputs, err := instantiateMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -84,7 +84,7 @@ func TestInstantiate(t *testing.T) { evm = vm.EVM{ StateDB: statedb, } - res, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, nil, nil, false) + res, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) outputs, err = instantiateMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -101,7 +101,7 @@ func TestInstantiate(t *testing.T) { "test", amtsbz, ) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.NotNil(t, statedb.GetPrecompileError()) require.Equal(t, uint64(0), g) @@ -109,12 +109,12 @@ func TestInstantiate(t *testing.T) { // bad inputs badArgs, _ := instantiateMethod.Inputs.Pack(codeID, "not bech32", []byte("{}"), "test", amtsbz) statedb.SetPrecompileError(nil) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, badArgs...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, badArgs...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.NotNil(t, statedb.GetPrecompileError()) badArgs, _ = instantiateMethod.Inputs.Pack(codeID, mockAddr.String(), []byte("{}"), "test", []byte("bad coins")) statedb.SetPrecompileError(nil) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, badArgs...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).InstantiateID, badArgs...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.NotNil(t, statedb.GetPrecompileError()) } @@ -155,11 +155,11 @@ func TestExecute(t *testing.T) { // circular interop statedb.WithCtx(statedb.Ctx().WithIsEVM(false)) testApp.EvmKeeper.SetCode(statedb.Ctx(), mockEVMAddr, []byte{1, 2, 3}) - res, _, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + res, _, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.Equal(t, "sei does not support CW->EVM->CW call pattern", string(res)) require.Equal(t, vm.ErrExecutionReverted, err) statedb.WithCtx(statedb.Ctx().WithIsEVM(true)) - res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.Nil(t, err) outputs, err := executeMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -172,38 +172,38 @@ func TestExecute(t *testing.T) { require.Nil(t, err) args, err = executeMethod.Inputs.Pack(contractAddr.String(), []byte("{\"echo\":{\"message\":\"test msg\"}}"), amtsbz) require.Nil(t, err) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.NotNil(t, err) // used coins instead of `value` to send usei to the contract args, err = executeMethod.Inputs.Pack(contractAddr.String(), []byte("{\"echo\":{\"message\":\"test msg\"}}"), amtsbz) require.Nil(t, err) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.NotNil(t, err) amtsbz, err = sdk.NewCoins().MarshalJSON() require.Nil(t, err) args, err = executeMethod.Inputs.Pack(contractAddr.String(), []byte("{\"echo\":{\"message\":\"test msg\"}}"), amtsbz) require.Nil(t, err) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.NotNil(t, err) // allowed delegatecall contractAddrAllowed := common.BytesToAddress([]byte("contractA")) testApp.EvmKeeper.SetERC20CW20Pointer(ctx, contractAddr.String(), contractAddrAllowed) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) // disallowed delegatecall contractAddrDisallowed := common.BytesToAddress([]byte("contractB")) statedb.SetPrecompileError(nil) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, true) require.NotNil(t, err) require.NotNil(t, statedb.GetPrecompileError()) // bad contract address args, _ = executeMethod.Inputs.Pack(mockAddr.String(), []byte("{\"echo\":{\"message\":\"test msg\"}}"), amtsbz) statedb.SetPrecompileError(nil) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) require.NotNil(t, statedb.GetPrecompileError()) @@ -211,13 +211,13 @@ func TestExecute(t *testing.T) { // bad inputs args, _ = executeMethod.Inputs.Pack("not bech32", []byte("{\"echo\":{\"message\":\"test msg\"}}"), amtsbz) statedb.SetPrecompileError(nil) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) require.NotNil(t, statedb.GetPrecompileError()) args, _ = executeMethod.Inputs.Pack(contractAddr.String(), []byte("{\"echo\":{\"message\":\"test msg\"}}"), []byte("bad coins")) statedb.SetPrecompileError(nil) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) require.NotNil(t, statedb.GetPrecompileError()) @@ -248,7 +248,7 @@ func TestQuery(t *testing.T) { StateDB: statedb, } suppliedGas := uint64(1000000) - res, g, err := p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).QueryID, args...), suppliedGas, nil, nil, false) + res, g, err := p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).QueryID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) outputs, err := queryMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -258,17 +258,17 @@ func TestQuery(t *testing.T) { // bad contract address args, _ = queryMethod.Inputs.Pack(mockAddr.String(), []byte("{\"info\":{}}")) - _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) // bad input args, _ = queryMethod.Inputs.Pack("not bech32", []byte("{\"info\":{}}")) - _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) args, _ = queryMethod.Inputs.Pack(contractAddr.String(), []byte("{\"bad\":{}}")) - _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, common.Address{}, common.Address{}, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) } @@ -311,7 +311,7 @@ func TestExecuteBatchOneMessage(t *testing.T) { } suppliedGas := uint64(1000000) testApp.BankKeeper.SendCoins(ctx, mockAddr, testApp.EvmKeeper.GetSeiAddressOrDefault(ctx, common.HexToAddress(wasmd.WasmdAddress)), amts) - res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.Nil(t, err) outputs, err := executeMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -329,18 +329,18 @@ func TestExecuteBatchOneMessage(t *testing.T) { } args, err = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsg}) require.Nil(t, err) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.NotNil(t, err) // value and amounts not equal // allowed delegatecall contractAddrAllowed := common.BytesToAddress([]byte("contractA")) testApp.EvmKeeper.SetERC20CW20Pointer(ctx, contractAddr.String(), contractAddrAllowed) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) // disallowed delegatecall contractAddrDisallowed := common.BytesToAddress([]byte("contractB")) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, true) require.NotNil(t, err) // bad contract address @@ -350,7 +350,7 @@ func TestExecuteBatchOneMessage(t *testing.T) { Coins: amtsbz, } args, _ = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsg}) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) @@ -361,7 +361,7 @@ func TestExecuteBatchOneMessage(t *testing.T) { Coins: amtsbz, } args, _ = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsg}) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) executeMsg = wasmd.ExecuteMsg{ @@ -370,7 +370,7 @@ func TestExecuteBatchOneMessage(t *testing.T) { Coins: []byte("bad coins"), } args, _ = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsg}) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) } @@ -415,7 +415,7 @@ func TestExecuteBatchValueImmutability(t *testing.T) { testApp.BankKeeper.SendCoins(ctx, mockAddr, testApp.EvmKeeper.GetSeiAddressOrDefault(ctx, common.HexToAddress(wasmd.WasmdAddress)), amts) value := big.NewInt(1000_000_000_000_000) valueCopy := new(big.Int).Set(value) - p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, value, nil, false) + p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, value, nil, false, false) require.Equal(t, valueCopy, value) } @@ -459,7 +459,7 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { require.Nil(t, err) args, err := executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsgWithCoinsAmt, executeMsgWithCoinsAmt, executeMsgWithCoinsAmt}) require.Nil(t, err) - res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false) + res, g, err := p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false, false) require.Nil(t, err) outputs, err := executeMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -486,7 +486,7 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { require.Nil(t, err) args, err = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsgWithNoCoins, executeMsgWithCoinsAmt, executeMsgWithNoCoins}) require.Nil(t, err) - res, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false) + res, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(1000_000_000_000_000), nil, false, false) require.Nil(t, err) outputs, err = executeMethod.Outputs.Unpack(res) require.Nil(t, err) @@ -503,12 +503,12 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { require.Nil(t, err) contractAddrAllowed := common.BytesToAddress([]byte("contractA")) testApp.EvmKeeper.SetERC20CW20Pointer(ctx, contractAddr.String(), contractAddrAllowed) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrAllowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, false) require.Nil(t, err) // disallowed delegatecall contractAddrDisallowed := common.BytesToAddress([]byte("contractB")) - _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false) + _, _, err = p.RunAndCalculateGas(&evm, mockEVMAddr, contractAddrDisallowed, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, nil, nil, false, true) require.NotNil(t, err) // bad contract address @@ -525,7 +525,7 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { require.Nil(t, err) args, err = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsgWithCoinsAmt, executeMsgBadContract, executeMsgWithCoinsAmt}) require.Nil(t, err) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) @@ -542,7 +542,7 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { err = testApp.BankKeeper.SendCoins(ctx, mockAddr, testApp.EvmKeeper.GetSeiAddressOrDefault(ctx, common.HexToAddress(wasmd.WasmdAddress)), largeAmts) require.Nil(t, err) args, _ = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsgWithCoinsAmt, executeMsgBadInputs, executeMsgWithCoinsAmt}) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) executeMsgBadInputCoins := wasmd.ExecuteMsg{ @@ -557,7 +557,7 @@ func TestExecuteBatchMultipleMessages(t *testing.T) { err = testApp.BankKeeper.SendCoins(ctx, mockAddr, testApp.EvmKeeper.GetSeiAddressOrDefault(ctx, common.HexToAddress(wasmd.WasmdAddress)), largeAmts) require.Nil(t, err) args, _ = executeMethod.Inputs.Pack([]wasmd.ExecuteMsg{executeMsgWithCoinsAmt, executeMsgBadInputCoins, executeMsgWithCoinsAmt}) - _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false) + _, g, err = p.RunAndCalculateGas(&evm, mockEVMAddr, mockEVMAddr, append(p.GetExecutor().(*wasmd.PrecompileExecutor).ExecuteBatchID, args...), suppliedGas, big.NewInt(3000_000_000_000_000), nil, false, false) require.NotNil(t, err) require.Equal(t, uint64(0), g) }