Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(halo/evmstaking2): implement Prepare #2554

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions halo/evmstaking2/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package keeper
import (
"context"

"github.com/omni-network/omni/contracts/bindings"
"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"
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"

ormv1alpha1 "cosmossdk.io/api/cosmos/orm/v1alpha1"
Expand All @@ -15,14 +21,23 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
stakingABI = mustGetABI(bindings.StakingMetaData)
createValidatorEvent = mustGetEvent(stakingABI, "CreateValidator")
delegateEvent = mustGetEvent(stakingABI, "Delegate")
)

// Keeper also implements the evmenginetypes.EvmEventProcessor interface.
type Keeper struct {
eventsTable EVMEventTable
ethCl ethclient.Client
address common.Address
submissionDelay int64
}

func NewKeeper(
storeService store.KVStoreService,
ethCl ethclient.Client,
submissionDelay int64,
) (*Keeper, error) {
schema := &ormv1alpha1.ModuleSchemaDescriptor{SchemaFile: []*ormv1alpha1.ModuleSchemaDescriptor_FileEntry{
Expand All @@ -39,8 +54,12 @@ func NewKeeper(
return nil, errors.Wrap(err, "create valsync store")
}

address := common.HexToAddress(predeploys.Staking)

return &Keeper{
eventsTable: evmstakingStore.EVMEventTable(),
ethCl: ethCl,
address: address,
submissionDelay: submissionDelay,
}, nil
}
Expand Down Expand Up @@ -75,16 +94,38 @@ func (k *Keeper) EndBlock(ctx context.Context) error {
}

// Prepare returns all omni stake contract EVM event logs from the provided block hash.
func (Keeper) Prepare(context.Context, common.Hash) ([]evmenginetypes.EVMEvent, error) {
return nil, nil
func (k Keeper) Prepare(ctx context.Context, blockHash common.Hash) ([]evmenginetypes.EVMEvent, error) {
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 (Keeper) Addresses() []common.Address {
return nil
func (k Keeper) Addresses() []common.Address {
return []common.Address{k.address}
}

// Deliver processes a omni deposit log event, which must be one of:
Expand All @@ -107,3 +148,25 @@ func (k Keeper) Deliver(ctx context.Context, _ common.Hash, elog evmenginetypes.
// parseAndDeliver parses the provided event and tries to deliver it on a state branch.
// If the delivery fails, the error will be logged and the state branch will be discarded.
func parseAndDeliver(context.Context, *evmenginetypes.EVMEvent) {}

// mustGetABI returns the metadata's ABI as an abi.ABI type.
// It panics on error.
func mustGetABI(metadata *bind.MetaData) *abi.ABI {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can move these to helpers.go maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

abi, err := metadata.GetAbi()
if err != nil {
panic(err)
}

return abi
}

// mustGetEvent returns the event with the given name from the ABI.
// It panics if the event is not found.
func mustGetEvent(abi *abi.ABI, name string) abi.Event {
event, ok := abi.Events[name]
if !ok {
panic("event not found")
}

return event
}
2 changes: 1 addition & 1 deletion halo/evmstaking2/keeper/keeper_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func setupKeeper(t *testing.T, submissionDelay int64) (*Keeper, sdk.Context) {
ctx = ctx.WithBlockHeight(1)
ctx = ctx.WithChainID(netconf.Simnet.Static().OmniConsensusChainIDStr())

k, err := NewKeeper(storeSvc, submissionDelay)
k, err := NewKeeper(storeSvc, nil, submissionDelay)
require.NoError(t, err, "new keeper")

return k, ctx
Expand Down
3 changes: 3 additions & 0 deletions halo/evmstaking2/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/omni-network/omni/halo/evmstaking2/keeper"
"github.com/omni-network/omni/halo/evmstaking2/types"
"github.com/omni-network/omni/lib/ethclient"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
Expand Down Expand Up @@ -111,6 +112,7 @@ type ModuleInputs struct {
depinject.In

StoreService store.KVStoreService
EthCl ethclient.Client
Cdc codec.Codec
Config *Module
}
Expand All @@ -125,6 +127,7 @@ type ModuleOutputs struct {
func ProvideModule(in ModuleInputs) (ModuleOutputs, error) {
k, err := keeper.NewKeeper(
in.StoreService,
in.EthCl,
in.Config.GetDeliverInterval(),
)
if err != nil {
Expand Down
Loading