From 0dd7724e2f2e8734bdd69c515472d10683658f20 Mon Sep 17 00:00:00 2001 From: Denys S <150304777+dssei@users.noreply.github.com> Date: Tue, 5 Nov 2024 11:06:13 -0800 Subject: [PATCH] Use msg server send in bank precompile (#1922) * use msg server send in bank precompile * self review tweak --- app/app.go | 1 + precompiles/bank/bank.go | 18 ++++++++++++++++-- precompiles/bank/bank_test.go | 11 ++++++----- precompiles/common/expected_keepers.go | 4 ++++ precompiles/setup.go | 5 +++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/app.go b/app/app.go index 00aa5fe32..04aeddce0 100644 --- a/app/app.go +++ b/app/app.go @@ -697,6 +697,7 @@ func New( false, &app.EvmKeeper, app.BankKeeper, + bankkeeper.NewMsgServerImpl(app.BankKeeper), wasmkeeper.NewDefaultPermissionKeeper(app.WasmKeeper), app.WasmKeeper, stakingkeeper.NewMsgServerImpl(app.StakingKeeper), diff --git a/precompiles/bank/bank.go b/precompiles/bank/bank.go index 88cb90a8a..cfdd0aae6 100644 --- a/precompiles/bank/bank.go +++ b/precompiles/bank/bank.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" @@ -39,6 +40,7 @@ var f embed.FS type PrecompileExecutor struct { accountKeeper pcommon.AccountKeeper bankKeeper pcommon.BankKeeper + bankMsgServer pcommon.BankMsgServer evmKeeper pcommon.EVMKeeper address common.Address @@ -57,10 +59,11 @@ type CoinBalance struct { Denom string } -func NewPrecompile(bankKeeper pcommon.BankKeeper, evmKeeper pcommon.EVMKeeper, accountKeeper pcommon.AccountKeeper) (*pcommon.Precompile, error) { +func NewPrecompile(bankKeeper pcommon.BankKeeper, bankMsgServer pcommon.BankMsgServer, evmKeeper pcommon.EVMKeeper, accountKeeper pcommon.AccountKeeper) (*pcommon.Precompile, error) { newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ bankKeeper: bankKeeper, + bankMsgServer: bankMsgServer, evmKeeper: evmKeeper, accountKeeper: accountKeeper, address: common.HexToAddress(BankAddress), @@ -150,7 +153,18 @@ func (p PrecompileExecutor) send(ctx sdk.Context, caller common.Address, method return nil, err } - if err := p.bankKeeper.SendCoins(ctx, senderSeiAddr, receiverSeiAddr, sdk.NewCoins(sdk.NewCoin(denom, sdk.NewIntFromBigInt(amount)))); err != nil { + msg := &banktypes.MsgSend{ + FromAddress: senderSeiAddr.String(), + ToAddress: receiverSeiAddr.String(), + Amount: sdk.NewCoins(sdk.NewCoin(denom, sdk.NewIntFromBigInt(amount))), + } + + err = msg.ValidateBasic() + if err != nil { + return nil, err + } + + if _, err = p.bankMsgServer.Send(sdk.WrapSDKContext(ctx), msg); err != nil { return nil, err } diff --git a/precompiles/bank/bank_test.go b/precompiles/bank/bank_test.go index 41ffee15f..6c2ab19b0 100644 --- a/precompiles/bank/bank_test.go +++ b/precompiles/bank/bank_test.go @@ -4,6 +4,7 @@ import ( "embed" "encoding/hex" "fmt" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" "math/big" "strings" "testing" @@ -65,7 +66,7 @@ func TestRun(t *testing.T) { // Setup receiving addresses seiAddr, evmAddr := testkeeper.MockAddressPair() k.SetAddressMapping(ctx, seiAddr, evmAddr) - p, err := bank.NewPrecompile(k.BankKeeper(), k, k.AccountKeeper()) + p, err := bank.NewPrecompile(k.BankKeeper(), bankkeeper.NewMsgServerImpl(k.BankKeeper()), k, k.AccountKeeper()) require.Nil(t, err) statedb := state.NewDBImpl(ctx, k, true) evm := vm.EVM{ @@ -272,7 +273,7 @@ func TestSendForUnlinkedReceiver(t *testing.T) { // Setup receiving addresses - NOT linked _, evmAddr := testkeeper.MockAddressPair() - p, err := bank.NewPrecompile(k.BankKeeper(), k, k.AccountKeeper()) + p, err := bank.NewPrecompile(k.BankKeeper(), bankkeeper.NewMsgServerImpl(k.BankKeeper()), k, k.AccountKeeper()) require.Nil(t, err) statedb := state.NewDBImpl(ctx, k, true) evm := vm.EVM{ @@ -342,7 +343,7 @@ func TestMetadata(t *testing.T) { k := &testkeeper.EVMTestApp.EvmKeeper ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{}).WithBlockTime(time.Now()) k.BankKeeper().SetDenomMetaData(ctx, banktypes.Metadata{Name: "SEI", Symbol: "usei", Base: "usei"}) - p, err := bank.NewPrecompile(k.BankKeeper(), k, k.AccountKeeper()) + p, err := bank.NewPrecompile(k.BankKeeper(), bankkeeper.NewMsgServerImpl(k.BankKeeper()), k, k.AccountKeeper()) require.Nil(t, err) statedb := state.NewDBImpl(ctx, k, true) evm := vm.EVM{ @@ -381,7 +382,7 @@ func TestMetadata(t *testing.T) { func TestRequiredGas(t *testing.T) { k := &testkeeper.EVMTestApp.EvmKeeper - p, err := bank.NewPrecompile(k.BankKeeper(), k, k.AccountKeeper()) + p, err := bank.NewPrecompile(k.BankKeeper(), bankkeeper.NewMsgServerImpl(k.BankKeeper()), k, k.AccountKeeper()) require.Nil(t, err) balanceRequiredGas := p.RequiredGas(p.GetExecutor().(*bank.PrecompileExecutor).BalanceID) require.Equal(t, uint64(1000), balanceRequiredGas) @@ -391,7 +392,7 @@ func TestRequiredGas(t *testing.T) { func TestAddress(t *testing.T) { k := &testkeeper.EVMTestApp.EvmKeeper - p, err := bank.NewPrecompile(k.BankKeeper(), k, k.AccountKeeper()) + p, err := bank.NewPrecompile(k.BankKeeper(), bankkeeper.NewMsgServerImpl(k.BankKeeper()), k, k.AccountKeeper()) require.Nil(t, err) require.Equal(t, common.HexToAddress(bank.BankAddress), p.Address()) } diff --git a/precompiles/common/expected_keepers.go b/precompiles/common/expected_keepers.go index bb98ffbef..bd7f3e687 100644 --- a/precompiles/common/expected_keepers.go +++ b/precompiles/common/expected_keepers.go @@ -32,6 +32,10 @@ type BankKeeper interface { SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins } +type BankMsgServer interface { + Send(goCtx context.Context, msg *banktypes.MsgSend) (*banktypes.MsgSendResponse, error) +} + type EVMKeeper interface { GetSeiAddress(sdk.Context, common.Address) (sdk.AccAddress, bool) GetSeiAddressOrDefault(ctx sdk.Context, evmAddress common.Address) sdk.AccAddress // only used for getting precompile Sei addresses diff --git a/precompiles/setup.go b/precompiles/setup.go index 56c6ce3e4..c04741f35 100644 --- a/precompiles/setup.go +++ b/precompiles/setup.go @@ -42,6 +42,7 @@ func InitializePrecompiles( dryRun bool, evmKeeper common.EVMKeeper, bankKeeper common.BankKeeper, + bankSender common.BankMsgServer, wasmdKeeper common.WasmdKeeper, wasmdViewKeeper common.WasmdViewKeeper, stakingKeeper common.StakingKeeper, @@ -60,7 +61,7 @@ func InitializePrecompiles( if Initialized { panic("precompiles already initialized") } - bankp, err := bank.NewPrecompile(bankKeeper, evmKeeper, accountKeeper) + bankp, err := bank.NewPrecompile(bankKeeper, bankSender, evmKeeper, accountKeeper) if err != nil { return err } @@ -135,7 +136,7 @@ func InitializePrecompiles( func GetPrecompileInfo(name string) PrecompileInfo { if !Initialized { // Precompile Info does not require any keeper state - _ = InitializePrecompiles(true, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) + _ = InitializePrecompiles(true, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) } i, ok := PrecompileNamesToInfo[name] if !ok {