Skip to content

Commit

Permalink
test: validator creation event delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
chmllr committed Nov 26, 2024
1 parent d2d4959 commit 94b5543
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
50 changes: 34 additions & 16 deletions halo/evmstaking2/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
context "context"
"errors"
"testing"

"github.com/omni-network/omni/halo/evmstaking2/types"
Expand Down Expand Up @@ -90,48 +91,53 @@ func TestInsertAndDeleteEVMEvents(t *testing.T) {
}
}

func TestDeliverDelegate(t *testing.T) {
func TestDelivery(t *testing.T) {
t.Parallel()

submissionDelay := int64(3)
ethClientMock, err := ethclient.NewEngineMock(
ethclient.WithPortalRegister(netconf.SimnetNetwork()),
ethclient.WithMockSelfDelegation(k1.GenPrivKey().PubKey(), 1),
ethclient.WithMockValidatorCreation(k1.GenPrivKey().PubKey()),
)
require.NoError(t, err)

msgServer := &msgServerMock{}

keeper, ctx := setupKeeper(t, submissionDelay, ethClientMock, authKeeperMock{}, bankKeeperMock{}, stakingKeeperMock{}, msgServer)
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)
require.Len(t, events, 2)

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)
// Make sure the events were persisted.
for id := uint64(1); id < 3; id++ {
found, err := keeper.eventsTable.Has(ctx, id)
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)
// Make sure the events were deleted.
for id := uint64(1); id < 3; id++ {
found, err := keeper.eventsTable.Has(ctx, id)
require.NoError(t, err)
require.False(t, found)
}

// Assert that the message was delivered to the msg server.
// Assert that the events were delivered to the msg server.
require.Len(t, msgServer.delegateMsgBuffer, 1)
require.Len(t, msgServer.createValidatorMsgBuffer, 1)
}

func setupKeeper(
Expand Down Expand Up @@ -165,10 +171,22 @@ func setupKeeper(
return k, ctx
}

type stakingKeeperMock struct{}
type stakingKeeperMock struct {
// calls is the number of calls to GetValidator
calls uint32
}

// GetValidator returns no errors on the first call, because it is called on delegation
// event delivery for the first time and the validator should be available.
// Second time it is called on a validator creation event and it should return an error
// on the pubkey of the new validator.
func (m *stakingKeeperMock) GetValidator(context.Context, sdk.ValAddress) (stypes.Validator, error) {
if m.calls == 0 {
m.calls++
return stypes.Validator{}, nil
}

func (stakingKeeperMock) GetValidator(context.Context, sdk.ValAddress) (stypes.Validator, error) {
return stypes.Validator{}, nil
return stypes.Validator{}, errors.New("validator exists")
}

type authKeeperMock struct{}
Expand Down
41 changes: 37 additions & 4 deletions lib/ethclient/enginemock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ type payloadArgs struct {

//nolint:gochecknoglobals // This is a static mapping.
var (
delegateEvent = mustGetABI(bindings.StakingMetaData).Events["Delegate"]
portalRegEvent = mustGetABI(bindings.PortalRegistryMetaData).Events["PortalRegistered"]
planUpgradeEvent = mustGetABI(bindings.UpgradeMetaData).Events["PlanUpgrade"]
delegateEvent = mustGetABI(bindings.StakingMetaData).Events["Delegate"]
portalRegEvent = mustGetABI(bindings.PortalRegistryMetaData).Events["PortalRegistered"]
planUpgradeEvent = mustGetABI(bindings.UpgradeMetaData).Events["PlanUpgrade"]
createValidatorEvent = mustGetABI(bindings.StakingMetaData).Events["CreateValidator"]
)

var _ EngineClient = (*engineMock)(nil)
Expand All @@ -60,6 +61,38 @@ type engineMock struct {
payloads map[engine.PayloadID]payloadArgs
}

// ValidatorCreation returns an option to add a validator creation event to the mock.
func WithMockValidatorCreation(pubkey crypto.PubKey) func(*engineMock) {
return func(mock *engineMock) {
mock.mu.Lock()
defer mock.mu.Unlock()

valAddr, err := k1util.PubKeyToAddress(pubkey)
if err != nil {
panic(errors.Wrap(err, "pubkey to address"))
}

wei := new(big.Int).Mul(big.NewInt(1), big.NewInt(params.Ether))
data, err := createValidatorEvent.Inputs.NonIndexed().Pack(pubkey.Bytes(), wei)
if err != nil {
panic(errors.Wrap(err, "pack create validator"))
}

contractAddr := common.HexToAddress(predeploys.Staking)
eventLog := types.Log{
Address: contractAddr,
Topics: []common.Hash{
createValidatorEvent.ID,
common.HexToHash(valAddr.Hex()), // validator
// common.HexToHash(string(pubkey.Bytes())), // pubkey
},
Data: data,
}

mock.pendingLogs[contractAddr] = append(mock.pendingLogs[contractAddr], eventLog)
}
}

// WithMockSelfDelegation returns an option to add a self-delegation Delegate event to the mock.
func WithMockSelfDelegation(pubkey crypto.PubKey, ether int64) func(*engineMock) {
return func(mock *engineMock) {
Expand Down Expand Up @@ -89,7 +122,7 @@ func WithMockSelfDelegation(pubkey crypto.PubKey, ether int64) func(*engineMock)
Data: data,
}

mock.pendingLogs[contractAddr] = []types.Log{eventLog}
mock.pendingLogs[contractAddr] = append(mock.pendingLogs[contractAddr], eventLog)
}
}

Expand Down

0 comments on commit 94b5543

Please sign in to comment.