From 5eba01b6ac0e8adf61dedcc3d57d12672760c279 Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 29 May 2024 00:51:52 +0800 Subject: [PATCH] Fix updating with current beefy state --- relayer/relays/beefy/main.go | 10 ++++++---- relayer/relays/beefy/polkadot-listener.go | 16 +++++++++++----- relayer/relays/beefy/scanner.go | 4 ---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/relayer/relays/beefy/main.go b/relayer/relays/beefy/main.go index b0c7590680..92aaa48710 100644 --- a/relayer/relays/beefy/main.go +++ b/relayer/relays/beefy/main.go @@ -37,13 +37,15 @@ func NewRelay(config *Config, ethereumKeypair *secp256k1.Keypair) (*Relay, error log.Info("Beefy relay created") - return &Relay{ + relayer := Relay{ config: config, relaychainConn: relaychainConn, ethereumConn: ethereumConn, polkadotListener: polkadotListener, ethereumWriter: ethereumWriter, - }, nil + } + polkadotListener.relayer = &relayer + return &relayer, nil } func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error { @@ -57,7 +59,7 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error { return fmt.Errorf("create ethereum connection: %w", err) } - currentState, err := relay.getCurrentState(ctx) + currentState, err := relay.CurrentState(ctx) if err != nil { return fmt.Errorf("fetch BeefyClient current state: %w", err) } @@ -78,7 +80,7 @@ func (relay *Relay) Start(ctx context.Context, eg *errgroup.Group) error { return nil } -func (relay *Relay) getCurrentState(ctx context.Context) (BeefyState, error) { +func (relay *Relay) CurrentState(ctx context.Context) (BeefyState, error) { var currentState BeefyState address := common.HexToAddress(relay.config.Sink.Contracts.BeefyClient) beefyClient, err := contracts.NewBeefyClient(address, relay.ethereumConn.Client()) diff --git a/relayer/relays/beefy/polkadot-listener.go b/relayer/relays/beefy/polkadot-listener.go index 323b694df1..da2c491e28 100644 --- a/relayer/relays/beefy/polkadot-listener.go +++ b/relayer/relays/beefy/polkadot-listener.go @@ -17,6 +17,7 @@ type PolkadotListener struct { config *SourceConfig conn *relaychain.Connection beefyAuthoritiesKey types.StorageKey + relayer *Relay } func NewPolkadotListener( @@ -96,9 +97,9 @@ func (li *PolkadotListener) scanCommitments( if err != nil { return fmt.Errorf("fetch beefy authorities at block %v: %w", result.BlockHash, err) } - currentAuthoritySet, err := li.queryBeefyAuthoritySet(result.BlockHash) + nextAuthoritySet, err := li.queryBeefyNextAuthoritySet(result.BlockHash) if err != nil { - return fmt.Errorf("fetch beefy authoritie set at block %v: %w", result.BlockHash, err) + return fmt.Errorf("fetch beefy authorities set at block %v: %w", result.BlockHash, err) } task := Request{ Validators: validators, @@ -106,8 +107,13 @@ func (li *PolkadotListener) scanCommitments( Proof: result.MMRProof, } + currentState, err = li.relayer.CurrentState(ctx) + if err != nil { + return fmt.Errorf("get current state from beefy LC %w", err) + } + if validatorSetID > currentValidatorSet { - if currentAuthoritySet.Root == currentState.NextValidatorSetRoot && committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod { + if nextAuthoritySet.Root == currentState.NextValidatorSetRoot && committedBeefyBlock < lastSyncedBeefyBlock+li.config.UpdatePeriod { logEntry.Info("Discarded commitment with beefy authorities not change") continue } @@ -162,7 +168,7 @@ func (li *PolkadotListener) queryBeefyAuthorities(blockHash types.Hash) ([]subst func (li *PolkadotListener) queryBeefyNextAuthoritySet(blockHash types.Hash) (types.BeefyNextAuthoritySet, error) { var nextAuthoritySet types.BeefyNextAuthoritySet - storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "MmrLeaf", "BeefyNextAuthorities", nil, nil) + storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "BeefyMmrLeaf", "BeefyNextAuthorities", nil, nil) ok, err := li.conn.API().RPC.State.GetStorage(storageKey, &nextAuthoritySet, blockHash) if err != nil { return nextAuthoritySet, err @@ -178,7 +184,7 @@ type BeefyAuthoritySet = types.BeefyNextAuthoritySet func (li *PolkadotListener) queryBeefyAuthoritySet(blockHash types.Hash) (BeefyAuthoritySet, error) { var authoritySet BeefyAuthoritySet - storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "MmrLeaf", "BeefyAuthorities", nil, nil) + storageKey, err := types.CreateStorageKey(li.conn.Metadata(), "BeefyMmrLeaf", "BeefyAuthorities", nil, nil) ok, err := li.conn.API().RPC.State.GetStorage(storageKey, &authoritySet, blockHash) if err != nil { return authoritySet, err diff --git a/relayer/relays/beefy/scanner.go b/relayer/relays/beefy/scanner.go index 16cce67a3d..12a42ebfd3 100644 --- a/relayer/relays/beefy/scanner.go +++ b/relayer/relays/beefy/scanner.go @@ -128,10 +128,6 @@ func scanCommitments(ctx context.Context, api *gsrpc.SubstrateAPI, startBlock ui sendError(fmt.Errorf("fetch block: %w", err)) return } - log.WithFields(log.Fields{ - "blockNumber": result.BlockNumber, - "depth": result.Depth, - }).Info("fetch block") var commitment *types.SignedCommitment for j := range block.Justifications {