Skip to content

Commit

Permalink
test: deliver delegate events
Browse files Browse the repository at this point in the history
  • Loading branch information
chmllr committed Nov 26, 2024
1 parent 873fb3b commit 561c76a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 30 deletions.
26 changes: 13 additions & 13 deletions halo/evmstaking2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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{
Expand Down Expand Up @@ -78,6 +77,7 @@ func NewKeeper(
aKeeper: aKeeper,
bKeeper: bKeeper,
sKeeper: sKeeper,
msgServer: msgServer,
address: address,
contract: contract,
submissionDelay: submissionDelay,
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
117 changes: 105 additions & 12 deletions halo/evmstaking2/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
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"

storetypes "cosmossdk.io/store/types"
"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"
)

//nolint:paralleltest // Asserting insertion ids of sequential writes
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,
height: 0,
},
{
name: "Insert event with address [2,3,4]",
event: types.EVMEvent{
event: evmenginetypes.EVMEvent{
Address: []byte{2, 3, 4},
},
insertedID: 2,
Expand All @@ -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) {
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
9 changes: 4 additions & 5 deletions halo/evmstaking2/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions halo/evmstaking2/types/interfaces.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 561c76a

Please sign in to comment.