From 561c76a754364e048de87b8dc9aeac07200813e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Tue, 26 Nov 2024 13:28:35 +0100 Subject: [PATCH] test: deliver delegate events --- halo/evmstaking2/keeper/keeper.go | 26 ++-- .../keeper/keeper_internal_test.go | 117 ++++++++++++++++-- halo/evmstaking2/module/module.go | 9 +- halo/evmstaking2/types/interfaces.go | 28 +++++ 4 files changed, 150 insertions(+), 30 deletions(-) create mode 100644 halo/evmstaking2/types/interfaces.go diff --git a/halo/evmstaking2/keeper/keeper.go b/halo/evmstaking2/keeper/keeper.go index c01e1d7ea..c812468bb 100644 --- a/halo/evmstaking2/keeper/keeper.go +++ b/halo/evmstaking2/keeper/keeper.go @@ -20,9 +20,6 @@ import ( "cosmossdk.io/math" "cosmossdk.io/orm/model/ormdb" sdk "github.com/cosmos/cosmos-sdk/types" - akeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - bkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - skeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -35,21 +32,23 @@ var ( // Keeper also implements the evmenginetypes.EvmEventProcessor interface. type Keeper struct { eventsTable EVMEventTable - ethCl ethclient.Client + ethCl ethclient.EngineClient address common.Address contract *bindings.Staking - aKeeper akeeper.AccountKeeperI - bKeeper bkeeper.Keeper - sKeeper *skeeper.Keeper + aKeeper types.AuthKeeper + bKeeper types.BankKeeper + sKeeper types.StakingKeeper + msgServer types.StakingMsgServer submissionDelay int64 } func NewKeeper( storeService store.KVStoreService, - ethCl ethclient.Client, - aKeeper akeeper.AccountKeeperI, - bKeeper bkeeper.Keeper, - sKeeper *skeeper.Keeper, + ethCl ethclient.EngineClient, + aKeeper types.AuthKeeper, + bKeeper types.BankKeeper, + sKeeper types.StakingKeeper, + msgServer types.StakingMsgServer, submissionDelay int64, ) (*Keeper, error) { schema := &ormv1alpha1.ModuleSchemaDescriptor{SchemaFile: []*ormv1alpha1.ModuleSchemaDescriptor_FileEntry{ @@ -78,6 +77,7 @@ func NewKeeper( aKeeper: aKeeper, bKeeper: bKeeper, sKeeper: sKeeper, + msgServer: msgServer, address: address, contract: contract, submissionDelay: submissionDelay, @@ -259,7 +259,7 @@ func (k Keeper) deliverDelegate(ctx context.Context, ev *bindings.StakingDelegat // Validator already exists, add deposit to self delegation msg := stypes.NewMsgDelegate(delAddr.String(), valAddr.String(), amountCoin) - _, err := skeeper.NewMsgServerImpl(k.sKeeper).Delegate(ctx, msg) + _, err := k.msgServer.Delegate(ctx, msg) if err != nil { return errors.Wrap(err, "delegate") } @@ -320,7 +320,7 @@ func (k Keeper) deliverCreateValidator(ctx context.Context, ev *bindings.Staking return errors.Wrap(err, "create validator message") } - _, err = skeeper.NewMsgServerImpl(k.sKeeper).CreateValidator(ctx, msg) + _, err = k.msgServer.CreateValidator(ctx, msg) if err != nil { return errors.Wrap(err, "create validator") } diff --git a/halo/evmstaking2/keeper/keeper_internal_test.go b/halo/evmstaking2/keeper/keeper_internal_test.go index df62edf0a..9b411786c 100644 --- a/halo/evmstaking2/keeper/keeper_internal_test.go +++ b/halo/evmstaking2/keeper/keeper_internal_test.go @@ -1,10 +1,15 @@ package keeper import ( + context "context" "testing" + "github.com/omni-network/omni/halo/evmstaking2/types" + "github.com/omni-network/omni/lib/ethclient" "github.com/omni-network/omni/lib/netconf" - types "github.com/omni-network/omni/octane/evmengine/types" + evmenginetypes "github.com/omni-network/omni/octane/evmengine/types" + + k1 "github.com/cometbft/cometbft/crypto/secp256k1" "github.com/ethereum/go-ethereum/common" @@ -12,9 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/runtime" sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" - akeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - bkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" - skeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + stypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" ) @@ -22,13 +25,13 @@ import ( func TestInsertAndDeleteEVMEvents(t *testing.T) { tests := []struct { name string - event types.EVMEvent + event evmenginetypes.EVMEvent insertedID uint64 height int64 }{ { name: "Insert event with address [1,2,3]", - event: types.EVMEvent{ + event: evmenginetypes.EVMEvent{ Address: []byte{1, 2, 3}, }, insertedID: 1, @@ -36,7 +39,7 @@ func TestInsertAndDeleteEVMEvents(t *testing.T) { }, { name: "Insert event with address [2,3,4]", - event: types.EVMEvent{ + event: evmenginetypes.EVMEvent{ Address: []byte{2, 3, 4}, }, insertedID: 2, @@ -46,7 +49,7 @@ func TestInsertAndDeleteEVMEvents(t *testing.T) { submissionDelay := int64(5) - keeper, ctx := setupKeeper(t, submissionDelay, nil, nil, nil) + keeper, ctx := setupKeeper(t, submissionDelay, nil, nil, nil, nil, nil) for _, test := range tests { t.Run(test.name, func(t *testing.T) { @@ -87,12 +90,58 @@ func TestInsertAndDeleteEVMEvents(t *testing.T) { } } +func TestDeliverDelegate(t *testing.T) { + t.Parallel() + + submissionDelay := int64(3) + ethClientMock, err := ethclient.NewEngineMock( + ethclient.WithPortalRegister(netconf.SimnetNetwork()), + ethclient.WithMockSelfDelegation(k1.GenPrivKey().PubKey(), 1), + ) + require.NoError(t, err) + + msgServer := &msgServerMock{} + + keeper, ctx := setupKeeper(t, submissionDelay, ethClientMock, authKeeperMock{}, bankKeeperMock{}, stakingKeeperMock{}, msgServer) + + var hash common.Hash + events, err := keeper.Prepare(ctx, hash) + require.NoError(t, err) + + require.Len(t, events, 1) + + for _, event := range events { + err := keeper.Deliver(ctx, hash, event) + require.NoError(t, err) + } + + // Make sure the event was persisted. + insertedID := uint64(1) + found, err := keeper.eventsTable.Has(ctx, insertedID) + require.NoError(t, err) + require.True(t, found) + + ctx = ctx.WithBlockHeight(submissionDelay) + err = keeper.EndBlock(ctx) + require.NoError(t, err) + + // Make sure the event was deleted. + found, err = keeper.eventsTable.Has(ctx, insertedID) + require.NoError(t, err) + require.False(t, found) + + // Assert that the message was delivered to the msg server. + require.Len(t, msgServer.delegateMsgBuffer, 1) +} + func setupKeeper( t *testing.T, submissionDelay int64, - aKeeper akeeper.AccountKeeperI, - bKeeper bkeeper.Keeper, - sKeeper *skeeper.Keeper, + ethCl ethclient.EngineClient, + aKeeper types.AuthKeeper, + bKeeper types.BankKeeper, + sKeeper types.StakingKeeper, + msgServer types.StakingMsgServer, ) (*Keeper, sdk.Context) { t.Helper() @@ -104,13 +153,57 @@ func setupKeeper( k, err := NewKeeper( storeSvc, - nil, + ethCl, aKeeper, bKeeper, sKeeper, + msgServer, submissionDelay, ) require.NoError(t, err, "new keeper") return k, ctx } + +type stakingKeeperMock struct{} + +func (stakingKeeperMock) GetValidator(context.Context, sdk.ValAddress) (stypes.Validator, error) { + return stypes.Validator{}, nil +} + +type authKeeperMock struct{} + +func (authKeeperMock) HasAccount(context.Context, sdk.AccAddress) bool { + return true +} + +func (authKeeperMock) NewAccountWithAddress(context.Context, sdk.AccAddress) sdk.AccountI { + return nil +} + +func (authKeeperMock) SetAccount(context.Context, sdk.AccountI) {} + +type bankKeeperMock struct{} + +func (bankKeeperMock) MintCoins(context.Context, string, sdk.Coins) error { + return nil +} + +func (bankKeeperMock) SendCoinsFromModuleToAccount(context.Context, string, sdk.AccAddress, sdk.Coins) error { + return nil +} + +type msgServerMock struct { + createValidatorMsgBuffer []*stypes.MsgCreateValidator + delegateMsgBuffer []*stypes.MsgDelegate +} + +func (srv *msgServerMock) CreateValidator(_ context.Context, msg *stypes.MsgCreateValidator) (*stypes.MsgCreateValidatorResponse, error) { + srv.createValidatorMsgBuffer = append(srv.createValidatorMsgBuffer, msg) + return nil, nil //nolint:nilnil // API requires nil-nil return +} + +func (srv *msgServerMock) Delegate(_ context.Context, msg *stypes.MsgDelegate) (*stypes.MsgDelegateResponse, error) { + srv.delegateMsgBuffer = append(srv.delegateMsgBuffer, msg) + return nil, nil //nolint:nilnil // API requires nil-nil return +} diff --git a/halo/evmstaking2/module/module.go b/halo/evmstaking2/module/module.go index 7e4b553ce..56bd6c7bf 100644 --- a/halo/evmstaking2/module/module.go +++ b/halo/evmstaking2/module/module.go @@ -14,8 +14,6 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cdctypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/types/module" - accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" - bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "github.com/grpc-ecosystem/grpc-gateway/runtime" ) @@ -115,9 +113,9 @@ type ModuleInputs struct { depinject.In StoreService store.KVStoreService - EthCl ethclient.Client - AKeeper accountkeeper.AccountKeeperI - BKeeper bankkeeper.Keeper + EthCl ethclient.EngineClient + AKeeper types.AuthKeeper + BKeeper types.BankKeeper SKeeper *stakingkeeper.Keeper Cdc codec.Codec Config *Module @@ -137,6 +135,7 @@ func ProvideModule(in ModuleInputs) (ModuleOutputs, error) { in.AKeeper, in.BKeeper, in.SKeeper, + stakingkeeper.NewMsgServerImpl(in.SKeeper), in.Config.GetDeliverInterval(), ) if err != nil { diff --git a/halo/evmstaking2/types/interfaces.go b/halo/evmstaking2/types/interfaces.go new file mode 100644 index 000000000..21c8951d7 --- /dev/null +++ b/halo/evmstaking2/types/interfaces.go @@ -0,0 +1,28 @@ +package types + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + stypes "github.com/cosmos/cosmos-sdk/x/staking/types" +) + +type AuthKeeper interface { + HasAccount(_ context.Context, _ sdk.AccAddress) bool + NewAccountWithAddress(_ context.Context, _ sdk.AccAddress) sdk.AccountI + SetAccount(_ context.Context, _ sdk.AccountI) +} + +type BankKeeper interface { + MintCoins(_ context.Context, _ string, _ sdk.Coins) error + SendCoinsFromModuleToAccount(_ context.Context, _ string, _ sdk.AccAddress, _ sdk.Coins) error +} + +type StakingKeeper interface { + GetValidator(_ context.Context, _ sdk.ValAddress) (stypes.Validator, error) +} + +type StakingMsgServer interface { + CreateValidator(_ context.Context, _ *stypes.MsgCreateValidator) (*stypes.MsgCreateValidatorResponse, error) + Delegate(_ context.Context, _ *stypes.MsgDelegate) (*stypes.MsgDelegateResponse, error) +}