Skip to content

Commit

Permalink
Use msg server send in bank precompile (#1922)
Browse files Browse the repository at this point in the history
* use msg server send in bank precompile

* self review tweak
  • Loading branch information
dssei authored Nov 5, 2024
1 parent ddcb1e6 commit 0dd7724
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
18 changes: 16 additions & 2 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand All @@ -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),
Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 6 additions & 5 deletions precompiles/bank/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"encoding/hex"
"fmt"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"math/big"
"strings"
"testing"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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())
}
4 changes: 4 additions & 0 deletions precompiles/common/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions precompiles/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0dd7724

Please sign in to comment.