Skip to content

Commit 6fab9e2

Browse files
ws4charlielumtis
andauthored
fix: removed maxHeightDiff to let observer scan Bitcoin block from where it left off (#2222)
* removed maxHeightDiff to let observer scan Bitcoin block from where it left off * added changelog entry * renamed LoadLastBlock -> LoadLastScannedBlock; added more comments * run format --------- Co-authored-by: Lucas Bertrand <[email protected]>
1 parent 9df63f8 commit 6fab9e2

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
* [1484](https://github.com/zeta-chain/node/issues/1484) - replaced hard-coded `MaxLookaheadNonce` with a default lookback factor
4242
* [2125](https://github.com/zeta-chain/node/pull/2125) - fix develop upgrade test
43+
* [2222](https://github.com/zeta-chain/node/pull/2222) - removed `maxHeightDiff` to let observer scan from Bitcoin height where it left off
4344

4445
### CI
4546

zetaclient/chains/bitcoin/observer/observer.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ import (
3939
)
4040

4141
const (
42-
// maxHeightDiff contains the max height diff in case the last block is too old when the observer starts
43-
maxHeightDiff = 10000
44-
4542
// btcBlocksPerDay represents Bitcoin blocks per days for LRU block cache size
4643
btcBlocksPerDay = 144
4744

@@ -684,37 +681,35 @@ func (ob *Observer) BuildBroadcastedTxMap() error {
684681
return nil
685682
}
686683

687-
func (ob *Observer) LoadLastBlock() error {
684+
// LoadLastScannedBlock loads last scanned block from database
685+
// The last scanned block is the height from which the observer should continue scanning for inbound transactions
686+
func (ob *Observer) LoadLastScannedBlock() error {
688687
// Get the latest block number from node
689688
bn, err := ob.rpcClient.GetBlockCount()
690689
if err != nil {
691690
return err
692691
}
693692
if bn < 0 {
694-
return fmt.Errorf("LoadLastBlock: negative block number %d", bn)
693+
return fmt.Errorf("LoadLastScannedBlock: negative block number %d", bn)
695694
}
696695

697696
//Load persisted block number
698697
var lastBlockNum clienttypes.LastBlockSQLType
699698
if err := ob.db.First(&lastBlockNum, clienttypes.LastBlockNumID).Error; err != nil {
700-
ob.logger.Chain.Info().Msg("LastBlockNum not found in DB, scan from latest")
699+
ob.logger.Chain.Info().Msg("LoadLastScannedBlock: last scanned block not found in DB, scan from latest")
701700
ob.SetLastBlockHeightScanned(bn)
702701
} else {
703702
// #nosec G701 always in range
704703
lastBN := int64(lastBlockNum.Num)
705704
ob.SetLastBlockHeightScanned(lastBN)
706-
707-
//If persisted block number is too low, use the latest height
708-
if (bn - lastBN) > maxHeightDiff {
709-
ob.logger.Chain.Info().Msgf("LastBlockNum too low: %d, scan from latest", lastBlockNum.Num)
710-
ob.SetLastBlockHeightScanned(bn)
711-
}
712705
}
713706

714-
if ob.chain.ChainId == 18444 { // bitcoin regtest: start from block 100
707+
// bitcoin regtest starts from block 100
708+
if chains.IsBitcoinRegnet(ob.chain.ChainId) {
715709
ob.SetLastBlockHeightScanned(100)
716710
}
717-
ob.logger.Chain.Info().Msgf("%s: start scanning from block %d", ob.chain.String(), ob.GetLastBlockHeightScanned())
711+
ob.logger.Chain.Info().
712+
Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())
718713

719714
return nil
720715
}
@@ -812,8 +807,8 @@ func (ob *Observer) loadDB(dbpath string) error {
812807
return err
813808
}
814809

815-
//Load last block
816-
err = ob.LoadLastBlock()
810+
// Load last scanned block
811+
err = ob.LoadLastScannedBlock()
817812
if err != nil {
818813
return err
819814
}

zetaclient/chains/evm/observer/observer.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,17 @@ func (ob *Observer) BlockByNumber(blockNumber int) (*ethrpc.Block, error) {
570570
return block, nil
571571
}
572572

573-
func (ob *Observer) BuildLastBlock() error {
574-
logger := ob.logger.Chain.With().Str("module", "BuildBlockIndex").Logger()
573+
// LoadLastScannedBlock loads last scanned block from specified height or from database
574+
// The last scanned block is the height from which the observer should continue scanning for inbound transactions
575+
func (ob *Observer) LoadLastScannedBlock() error {
576+
// get environment variable
575577
envvar := ob.chain.ChainName.String() + "_SCAN_FROM"
576578
scanFromBlock := os.Getenv(envvar)
579+
580+
// load from environment variable if set
577581
if scanFromBlock != "" {
578-
logger.Info().Msgf("BuildLastBlock: envvar %s is set; scan from block %s", envvar, scanFromBlock)
582+
ob.logger.Chain.Info().
583+
Msgf("LoadLastScannedBlock: envvar %s is set; scan from block %s", envvar, scanFromBlock)
579584
if scanFromBlock == clienttypes.EnvVarLatest {
580585
header, err := ob.evmClient.HeaderByNumber(context.Background(), nil)
581586
if err != nil {
@@ -589,22 +594,26 @@ func (ob *Observer) BuildLastBlock() error {
589594
}
590595
ob.SetLastBlockHeightScanned(scanFromBlockInt)
591596
}
592-
} else { // last observed block
593-
var lastBlockNum clienttypes.LastBlockSQLType
594-
if err := ob.db.First(&lastBlockNum, clienttypes.LastBlockNumID).Error; err != nil {
595-
logger.Info().Msgf("BuildLastBlock: db PosKey does not exist; read from external chain %s", ob.chain.String())
597+
} else {
598+
// load from DB otherwise
599+
var lastBlock clienttypes.LastBlockSQLType
600+
if err := ob.db.First(&lastBlock, clienttypes.LastBlockNumID).Error; err != nil {
601+
ob.logger.Chain.Info().Msg("LoadLastScannedBlock: last scanned block not found in DB, scan from latest")
596602
header, err := ob.evmClient.HeaderByNumber(context.Background(), nil)
597603
if err != nil {
598604
return err
599605
}
600606
ob.SetLastBlockHeightScanned(header.Number.Uint64())
601607
if dbc := ob.db.Save(clienttypes.ToLastBlockSQLType(ob.GetLastBlockHeightScanned())); dbc.Error != nil {
602-
logger.Error().Err(dbc.Error).Msgf("BuildLastBlock: error writing lastBlockScanned %d to db", ob.GetLastBlockHeightScanned())
608+
ob.logger.Chain.Error().Err(dbc.Error).Msgf("LoadLastScannedBlock: error writing last scanned block %d to DB", ob.GetLastBlockHeightScanned())
603609
}
604610
} else {
605-
ob.SetLastBlockHeightScanned(lastBlockNum.Num)
611+
ob.SetLastBlockHeightScanned(lastBlock.Num)
606612
}
607613
}
614+
ob.logger.Chain.Info().
615+
Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())
616+
608617
return nil
609618
}
610619

@@ -635,7 +644,7 @@ func (ob *Observer) LoadDB(dbPath string, chain chains.Chain) error {
635644
}
636645

637646
ob.db = db
638-
err = ob.BuildLastBlock()
647+
err = ob.LoadLastScannedBlock()
639648
if err != nil {
640649
return err
641650
}

0 commit comments

Comments
 (0)