From 09acf94869bdd3ea5d92504b90958e38e928b468 Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Tue, 21 May 2024 15:17:28 -0500 Subject: [PATCH] renamed LoadLastBlock -> LoadLastScannedBlock; added more comments --- .../chains/bitcoin/observer/observer.go | 15 ++++++------- zetaclient/chains/evm/observer/observer.go | 21 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/zetaclient/chains/bitcoin/observer/observer.go b/zetaclient/chains/bitcoin/observer/observer.go index dd52de0b2b..2fb22bccb6 100644 --- a/zetaclient/chains/bitcoin/observer/observer.go +++ b/zetaclient/chains/bitcoin/observer/observer.go @@ -662,21 +662,22 @@ func (ob *Observer) BuildBroadcastedTxMap() error { return nil } -// LoadLastBlock loads last scanned block from DB -func (ob *Observer) LoadLastBlock() error { +// LoadLastScannedBlock loads last scanned block from database +// The last scanned block is the height from which the observer should continue scanning for inbound transactions +func (ob *Observer) LoadLastScannedBlock() error { // Get the latest block number from node bn, err := ob.rpcClient.GetBlockCount() if err != nil { return err } if bn < 0 { - return fmt.Errorf("LoadLastBlock: negative block number %d", bn) + return fmt.Errorf("LoadLastScannedBlock: negative block number %d", bn) } //Load persisted block number var lastBlockNum clienttypes.LastBlockSQLType if err := ob.db.First(&lastBlockNum, clienttypes.LastBlockNumID).Error; err != nil { - ob.logger.Chain.Info().Msg("LastBlockNum not found in DB, scan from latest") + ob.logger.Chain.Info().Msg("LoadLastScannedBlock: last scanned block not found in DB, scan from latest") ob.SetLastBlockHeightScanned(bn) } else { // #nosec G701 always in range @@ -688,7 +689,7 @@ func (ob *Observer) LoadLastBlock() error { if chains.IsBitcoinRegnet(ob.chain.ChainId) { ob.SetLastBlockHeightScanned(100) } - ob.logger.Chain.Info().Msgf("chain %d: start scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned()) + ob.logger.Chain.Info().Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned()) return nil } @@ -786,8 +787,8 @@ func (ob *Observer) loadDB(dbpath string) error { return err } - //Load last block - err = ob.LoadLastBlock() + // Load last scanned block + err = ob.LoadLastScannedBlock() if err != nil { return err } diff --git a/zetaclient/chains/evm/observer/observer.go b/zetaclient/chains/evm/observer/observer.go index 60436f4f9e..3b7dcd1113 100644 --- a/zetaclient/chains/evm/observer/observer.go +++ b/zetaclient/chains/evm/observer/observer.go @@ -551,15 +551,16 @@ func (ob *Observer) BlockByNumber(blockNumber int) (*ethrpc.Block, error) { return block, nil } -// LoadLastBlock loads last scanned block from specified height or from DB -func (ob *Observer) LoadLastBlock() error { +// LoadLastScannedBlock loads last scanned block from specified height or from database +// The last scanned block is the height from which the observer should continue scanning for inbound transactions +func (ob *Observer) LoadLastScannedBlock() error { // get environment variable envvar := ob.chain.ChainName.String() + "_SCAN_FROM" scanFromBlock := os.Getenv(envvar) // load from environment variable if set if scanFromBlock != "" { - ob.logger.Chain.Info().Msgf("BuildLastBlock: envvar %s is set; scan from block %s", envvar, scanFromBlock) + ob.logger.Chain.Info().Msgf("LoadLastScannedBlock: envvar %s is set; scan from block %s", envvar, scanFromBlock) if scanFromBlock == clienttypes.EnvVarLatest { header, err := ob.evmClient.HeaderByNumber(context.Background(), nil) if err != nil { @@ -575,22 +576,22 @@ func (ob *Observer) LoadLastBlock() error { } } else { // load from DB otherwise - var lastBlockNum clienttypes.LastBlockSQLType - if err := ob.db.First(&lastBlockNum, clienttypes.LastBlockNumID).Error; err != nil { - ob.logger.Chain.Info().Msgf("BuildLastBlock: db PosKey does not exist; read from external chain %s", ob.chain.String()) + var lastBlock clienttypes.LastBlockSQLType + if err := ob.db.First(&lastBlock, clienttypes.LastBlockNumID).Error; err != nil { + ob.logger.Chain.Info().Msg("LoadLastScannedBlock: last scanned block not found in DB, scan from latest") header, err := ob.evmClient.HeaderByNumber(context.Background(), nil) if err != nil { return err } ob.SetLastBlockHeightScanned(header.Number.Uint64()) if dbc := ob.db.Save(clienttypes.ToLastBlockSQLType(ob.GetLastBlockHeightScanned())); dbc.Error != nil { - ob.logger.Chain.Error().Err(dbc.Error).Msgf("BuildLastBlock: error writing lastBlockScanned %d to db", ob.GetLastBlockHeightScanned()) + ob.logger.Chain.Error().Err(dbc.Error).Msgf("LoadLastScannedBlock: error writing last scanned block %d to DB", ob.GetLastBlockHeightScanned()) } } else { - ob.SetLastBlockHeightScanned(lastBlockNum.Num) + ob.SetLastBlockHeightScanned(lastBlock.Num) } } - ob.logger.Chain.Info().Msgf("chain %d: start scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned()) + ob.logger.Chain.Info().Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned()) return nil } @@ -620,7 +621,7 @@ func (ob *Observer) LoadDB(dbPath string, chain chains.Chain) error { } ob.db = db - err = ob.LoadLastBlock() + err = ob.LoadLastScannedBlock() if err != nil { return err }