Skip to content

Commit

Permalink
refactor(octane/evmengine): simplify event proc interface (#2757)
Browse files Browse the repository at this point in the history
Simplify the `evmengine.EvmEventProcessor` interface to return filter
query params, instead of doing fetch logic. This reduces lots of
duplication and removes EthCl dependency from event procs

issue: #2106
  • Loading branch information
corverroos authored Jan 9, 2025
1 parent c289c89 commit e568a2d
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 238 deletions.
3 changes: 0 additions & 3 deletions halo/evmslashing/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package evmslashing

import (
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
evmenginetypes "github.com/omni-network/omni/octane/evmengine/types"

"cosmossdk.io/depinject"
Expand All @@ -11,7 +10,6 @@ import (

type DIInputs struct {
depinject.In
EthCl ethclient.Client
SlashingKeeper slashingkeeper.Keeper
}

Expand All @@ -23,7 +21,6 @@ type DIOutputs struct {

func DIProvide(input DIInputs) (DIOutputs, error) {
proc, err := New(
input.EthCl,
input.SlashingKeeper,
)
if err != nil {
Expand Down
40 changes: 5 additions & 35 deletions halo/evmslashing/evmslashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/halo/genutil/evm/predeploys"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/log"
evmenginetypes "github.com/omni-network/omni/octane/evmengine/types"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -34,60 +32,32 @@ var (
// EventProcessor implements the evmenginetypes.EvmEventProcessor interface.
type EventProcessor struct {
contract *bindings.Slashing
ethCl ethclient.Client
address common.Address
sKeeper skeeper.Keeper
}

// New returns a new EventProcessor.
func New(ethCl ethclient.Client, sKeeper skeeper.Keeper) (EventProcessor, error) {
func New(sKeeper skeeper.Keeper) (EventProcessor, error) {
address := common.HexToAddress(predeploys.Slashing)
contract, err := bindings.NewSlashing(address, ethCl)
contract, err := bindings.NewSlashing(address, nil) // Passing nil backend if safe since only Parse functions are used.
if err != nil {
return EventProcessor{}, errors.Wrap(err, "new staking")
}

return EventProcessor{
contract: contract,
ethCl: ethCl,
address: address,
sKeeper: sKeeper,
}, nil
}

// Prepare returns all omni stake contract EVM event logs from the provided block hash.
func (p EventProcessor) Prepare(ctx context.Context, blockHash common.Hash) ([]evmenginetypes.EVMEvent, error) {
logs, err := p.ethCl.FilterLogs(ctx, ethereum.FilterQuery{
BlockHash: &blockHash,
Addresses: p.Addresses(),
Topics: [][]common.Hash{{unjailEvent.ID}},
})
if err != nil {
return nil, errors.Wrap(err, "filter logs")
}

resp := make([]evmenginetypes.EVMEvent, 0, len(logs))
for _, l := range logs {
topics := make([][]byte, 0, len(l.Topics))
for _, t := range l.Topics {
topics = append(topics, t.Bytes())
}
resp = append(resp, evmenginetypes.EVMEvent{
Address: l.Address.Bytes(),
Topics: topics,
Data: l.Data,
})
}

return resp, nil
}

func (EventProcessor) Name() string {
return ModuleName
}

func (p EventProcessor) Addresses() []common.Address {
return []common.Address{p.address}
// FilterParams defines the matching EVM log events, see github.com/ethereum/go-ethereum#FilterQuery.
func (p EventProcessor) FilterParams() ([]common.Address, [][]common.Hash) {
return []common.Address{p.address}, [][]common.Hash{{unjailEvent.ID}}
}

// Deliver processes a omni deposit log event, which must be one of:
Expand Down
3 changes: 0 additions & 3 deletions halo/evmstaking/depinject.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package evmstaking

import (
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
evmenginetypes "github.com/omni-network/omni/octane/evmengine/types"

"cosmossdk.io/depinject"
Expand All @@ -13,7 +12,6 @@ import (

type DIInputs struct {
depinject.In
EthCl ethclient.Client
StakingKeeper *stakingkeeper.Keeper
BankKeeper bankkeeper.Keeper
AccountKeeper accountkeeper.AccountKeeper
Expand All @@ -27,7 +25,6 @@ type DIOutputs struct {

func DIProvide(input DIInputs) (DIOutputs, error) {
proc, err := New(
input.EthCl,
input.StakingKeeper,
input.BankKeeper,
input.AccountKeeper,
Expand Down
43 changes: 5 additions & 38 deletions halo/evmstaking/evmstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/halo/genutil/evm/predeploys"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/feature"
"github.com/omni-network/omni/lib/k1util"
"github.com/omni-network/omni/lib/log"
evmenginetypes "github.com/omni-network/omni/octane/evmengine/types"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -42,7 +40,6 @@ var (
// EventProcessor implements the evmenginetypes.EvmEventProcessor interface.
type EventProcessor struct {
contract *bindings.Staking
ethCl ethclient.Client
address common.Address
sKeeper *skeeper.Keeper
bKeeper bkeeper.Keeper
Expand All @@ -51,63 +48,33 @@ type EventProcessor struct {

// New returns a new EventProcessor.
func New(
ethCl ethclient.Client,
sKeeper *skeeper.Keeper,
bKeeper bkeeper.Keeper,
aKeeper akeeper.AccountKeeper,
) (EventProcessor, error) {
address := common.HexToAddress(predeploys.Staking)
contract, err := bindings.NewStaking(address, ethCl)
contract, err := bindings.NewStaking(address, nil) // Passing nil backend if safe since only Parse functions are used.
if err != nil {
return EventProcessor{}, errors.Wrap(err, "new staking")
}

return EventProcessor{
contract: contract,
ethCl: ethCl,
address: address,
sKeeper: sKeeper,
bKeeper: bKeeper,
aKeeper: aKeeper,
}, nil
}

// Prepare returns all omni stake contract EVM event logs from the provided block hash.
func (p EventProcessor) Prepare(ctx context.Context, blockHash common.Hash) ([]evmenginetypes.EVMEvent, error) {
if feature.FlagEVMStakingModule.Enabled(ctx) {
return nil, errors.New("unexpected code path [BUG]")
}
logs, err := p.ethCl.FilterLogs(ctx, ethereum.FilterQuery{
BlockHash: &blockHash,
Addresses: p.Addresses(),
Topics: [][]common.Hash{{createValidatorEvent.ID, delegateEvent.ID}},
})
if err != nil {
return nil, errors.Wrap(err, "filter logs")
}

resp := make([]evmenginetypes.EVMEvent, 0, len(logs))
for _, l := range logs {
topics := make([][]byte, 0, len(l.Topics))
for _, t := range l.Topics {
topics = append(topics, t.Bytes())
}
resp = append(resp, evmenginetypes.EVMEvent{
Address: l.Address.Bytes(),
Topics: topics,
Data: l.Data,
})
}

return resp, nil
}

// Name returns the name of the EventProcessor.
func (EventProcessor) Name() string {
return ModuleName
}

func (p EventProcessor) Addresses() []common.Address {
return []common.Address{p.address}
// FilterParams defines the matching EVM log events, see github.com/ethereum/go-ethereum#FilterQuery.
func (p EventProcessor) FilterParams() ([]common.Address, [][]common.Hash) {
return []common.Address{p.address}, [][]common.Hash{{createValidatorEvent.ID, delegateEvent.ID}}
}

// Deliver processes a omni deposit log event, which must be one of:
Expand Down
44 changes: 5 additions & 39 deletions halo/evmstaking2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"github.com/omni-network/omni/halo/evmstaking2/types"
"github.com/omni-network/omni/halo/genutil/evm/predeploys"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/feature"
"github.com/omni-network/omni/lib/k1util"
"github.com/omni-network/omni/lib/log"
evmenginetypes "github.com/omni-network/omni/octane/evmengine/types"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"

ormv1alpha1 "cosmossdk.io/api/cosmos/orm/v1alpha1"
Expand All @@ -33,7 +31,6 @@ var (
// Keeper also implements the evmenginetypes.EvmEventProcessor interface.
type Keeper struct {
eventsTable EVMEventTable
ethCl ethclient.Client
address common.Address
contract *bindings.Staking
aKeeper types.AuthKeeper
Expand All @@ -45,7 +42,6 @@ type Keeper struct {

func NewKeeper(
storeService store.KVStoreService,
ethCl ethclient.Client,
aKeeper types.AuthKeeper,
bKeeper types.BankKeeper,
sKeeper types.StakingKeeper,
Expand All @@ -67,14 +63,13 @@ func NewKeeper(
}

address := common.HexToAddress(predeploys.Staking)
contract, err := bindings.NewStaking(address, ethCl)
contract, err := bindings.NewStaking(address, nil) // Passing nil backend if safe since only Parse functions are used.
if err != nil {
return &Keeper{}, errors.Wrap(err, "new staking")
}

return &Keeper{
eventsTable: evmstakingStore.EVMEventTable(),
ethCl: ethCl,
aKeeper: aKeeper,
bKeeper: bKeeper,
sKeeper: sKeeper,
Expand All @@ -86,7 +81,7 @@ func NewKeeper(
}

// EndBlock delivers all pending EVM events on every `k.deliverInterval`'th block.
func (k *Keeper) EndBlock(ctx context.Context) error {
func (k Keeper) EndBlock(ctx context.Context) error {
if !feature.FlagEVMStakingModule.Enabled(ctx) {
return errors.New("unexpected code path [BUG]")
}
Expand Down Expand Up @@ -125,42 +120,13 @@ func (k *Keeper) EndBlock(ctx context.Context) error {
return nil
}

// Prepare returns all omni stake contract EVM event logs from the provided block hash.
func (k Keeper) Prepare(ctx context.Context, blockHash common.Hash) ([]evmenginetypes.EVMEvent, error) {
if !feature.FlagEVMStakingModule.Enabled(ctx) {
return nil, errors.New("unexpected code path [BUG]")
}
logs, err := k.ethCl.FilterLogs(ctx, ethereum.FilterQuery{
BlockHash: &blockHash,
Addresses: k.Addresses(),
Topics: [][]common.Hash{{createValidatorEvent.ID, delegateEvent.ID}},
})
if err != nil {
return nil, errors.Wrap(err, "filter logs")
}

resp := make([]evmenginetypes.EVMEvent, 0, len(logs))
for _, l := range logs {
topics := make([][]byte, 0, len(l.Topics))
for _, t := range l.Topics {
topics = append(topics, t.Bytes())
}
resp = append(resp, evmenginetypes.EVMEvent{
Address: l.Address.Bytes(),
Topics: topics,
Data: l.Data,
})
}

return resp, nil
}

func (Keeper) Name() string {
return types.ModuleName
}

func (k Keeper) Addresses() []common.Address {
return []common.Address{k.address}
// FilterParams defines the matching EVM log events, see github.com/ethereum/go-ethereum#FilterQuery.
func (k Keeper) FilterParams() ([]common.Address, [][]common.Hash) {
return []common.Address{k.address}, [][]common.Hash{{createValidatorEvent.ID, delegateEvent.ID}}
}

// Deliver processes a omni deposit log event, which must be one of:
Expand Down
17 changes: 8 additions & 9 deletions halo/evmstaking2/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/feature"
"github.com/omni-network/omni/lib/netconf"
evmengkeeper "github.com/omni-network/omni/octane/evmengine/keeper"
etypes "github.com/omni-network/omni/octane/evmengine/types"

k1 "github.com/cometbft/cometbft/crypto/secp256k1"
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestInsertAndDeleteEVMEvents(t *testing.T) {

deliverInterval := int64(5)

keeper, ctx := setupKeeper(t, deliverInterval, nil, nil)
keeper, ctx := setupKeeper(t, deliverInterval, nil)

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down Expand Up @@ -111,10 +112,10 @@ func TestDeliveryWithBrokenServer(t *testing.T) {
sServerMock.EXPECT().CreateValidator(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, err)
sServerMock.EXPECT().Delegate(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, err)

keeper, ctx := setupKeeper(t, deliverInterval, ethClientMock, sServerMock)
keeper, ctx := setupKeeper(t, deliverInterval, sServerMock)

var hash common.Hash
events, err := keeper.Prepare(ctx, hash)
events, err := evmengkeeper.FetchProcEvents(ctx, ethClientMock, keeper, hash)
require.NoError(t, err)

expectDelegates := 1
Expand Down Expand Up @@ -148,10 +149,10 @@ func TestDeliveryOfInvalidEvents(t *testing.T) {
sServerMock.EXPECT().CreateValidator(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil)
sServerMock.EXPECT().Delegate(gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil)

keeper, ctx := setupKeeper(t, deliverInterval, ethClientMock, sServerMock)
keeper, ctx := setupKeeper(t, deliverInterval, sServerMock)

var hash common.Hash
events, err := keeper.Prepare(ctx, hash)
events, err := evmengkeeper.FetchProcEvents(ctx, ethClientMock, keeper, hash)
require.NoError(t, err)

expectDelegates := 1
Expand Down Expand Up @@ -228,10 +229,10 @@ func TestHappyPathDelivery(t *testing.T) {
}).
Return(new(stypes.MsgDelegateResponse), nil)

keeper, ctx := setupKeeper(t, deliverInterval, ethClientMock, sServerMock)
keeper, ctx := setupKeeper(t, deliverInterval, sServerMock)

var hash common.Hash
events, err := keeper.Prepare(ctx, hash)
events, err := evmengkeeper.FetchProcEvents(ctx, ethClientMock, keeper, hash)
require.NoError(t, err)

expectDelegates := 1
Expand Down Expand Up @@ -296,7 +297,6 @@ func assertNotContains(t *testing.T, ctx context.Context, keeper *Keeper, eventI
func setupKeeper(
t *testing.T,
deliverInterval int64,
ethCl ethclient.EngineClient,
sServer types.StakingMsgServer,
) (*Keeper, sdk.Context) {
t.Helper()
Expand Down Expand Up @@ -338,7 +338,6 @@ func setupKeeper(

k, err := NewKeeper(
storeSvc,
ethCl,
authKeeperMock,
bKeeperMock,
sKeeperMock,
Expand Down
Loading

0 comments on commit e568a2d

Please sign in to comment.