From 015b0ac8613741da346d8e7852c89f38da22cce7 Mon Sep 17 00:00:00 2001 From: lbqds Date: Fri, 15 Mar 2024 14:11:18 +0800 Subject: [PATCH] Retrieve the latest event index from the database when restarting --- explorer-backend/main.go | 14 ++------------ explorer-backend/transactions/evm.go | 16 +++++++++++----- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/explorer-backend/main.go b/explorer-backend/main.go index d693ca55..a1d5206d 100644 --- a/explorer-backend/main.go +++ b/explorer-backend/main.go @@ -346,18 +346,13 @@ func run(cmd *cobra.Command, args []string) { return err } - ethEventIndex, err := watcher.GetLatestEventIndexEth(ctx) - if err != nil { - logger.Error("failed to get latest event index", zap.Uint16("chainId", uint16(vaa.ChainIDEthereum)), zap.Error(err)) - return err - } ethWatcher, err := transactions.NewEVMWatcher( logger, ctx, *ethRpcUrl, vaa.ChainIDEthereum, bridgeConfig.Ethereum, - *ethEventIndex, + watcher.GetLatestEventIndexEth, blockTxsC, ) if err != nil { @@ -369,18 +364,13 @@ func run(cmd *cobra.Command, args []string) { } if *bscRpcUrl != "" && *network == "devnet" { - bscEventIndex, err := watcher.GetLatestEventIndexBsc(ctx) - if err != nil { - logger.Error("failed to get latest event index", zap.Uint16("chainId", uint16(vaa.ChainIDBSC)), zap.Error(err)) - return err - } bscWatcher, err := transactions.NewEVMWatcher( logger, ctx, *bscRpcUrl, vaa.ChainIDBSC, bridgeConfig.Bsc, - *bscEventIndex, + watcher.GetLatestEventIndexBsc, blockTxsC, ) if err != nil { diff --git a/explorer-backend/transactions/evm.go b/explorer-backend/transactions/evm.go index 967f92f1..8d95baa4 100644 --- a/explorer-backend/transactions/evm.go +++ b/explorer-backend/transactions/evm.go @@ -23,7 +23,7 @@ type EVMWatcher struct { chainConfig *common.ChainConfig connector *ethereum.EthereumConnector contractAddress *ethCommon.Address - fromHeight uint32 + getEventIndex func(context.Context) (*uint32, error) blockTxsC chan<- []*BlockTransactions logger *zap.Logger } @@ -34,7 +34,7 @@ func NewEVMWatcher( rpcUrl string, chainId vaa.ChainID, chainConfig *common.ChainConfig, - fromHeight uint32, + getEventIndex func(context.Context) (*uint32, error), blockTxsC chan<- []*BlockTransactions, ) (*EVMWatcher, error) { contractAddress := ethCommon.HexToAddress(chainConfig.Contracts.Governance) @@ -48,7 +48,7 @@ func NewEVMWatcher( chainConfig: chainConfig, contractAddress: &contractAddress, connector: connector, - fromHeight: fromHeight, + getEventIndex: getEventIndex, blockTxsC: blockTxsC, logger: namedLogger, }, nil @@ -69,7 +69,13 @@ func (w *EVMWatcher) Run() func(ctx context.Context) error { } func (w *EVMWatcher) fetchEvents(ctx context.Context, errC chan<- error) { - w.logger.Info("evm watcher started", zap.Uint32("fromHeight", w.fromHeight)) + fromHeight, err := w.getEventIndex(ctx) + if err != nil { + w.logger.Error("failed to get latest event index", zap.Error(err), zap.Uint16("chainId", uint16(w.chainId))) + errC <- err + return + } + w.logger.Info("evm watcher started", zap.Uint32("fromHeight", *fromHeight)) blockC := make(chan *ethereum.NewBlock, 64) subscription, err := w.connector.SubscribeForBlocks(ctx, blockC) @@ -92,7 +98,7 @@ func (w *EVMWatcher) fetchEvents(ctx context.Context, errC chan<- error) { w.logger.Info("received new block", zap.Uint64("height", block.Number.Uint64()), zap.String("hash", block.Hash.Hex())) if isFirstBlock { isFirstBlock = false - if err := w.fetchEventsFromBlockRange(ctx, w.fromHeight, uint32(block.Number.Uint64())); err != nil { + if err := w.fetchEventsFromBlockRange(ctx, *fromHeight, uint32(block.Number.Uint64())); err != nil { w.logger.Error("failed to fetch events by block range", zap.Error(err)) errC <- err return