Skip to content

Commit

Permalink
fix: removed maxHeightDiff to let observer scan Bitcoin block from wh…
Browse files Browse the repository at this point in the history
…ere 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]>
  • Loading branch information
ws4charlie and lumtis authored May 22, 2024
1 parent 9df63f8 commit 6fab9e2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

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

### CI

Expand Down
27 changes: 11 additions & 16 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ import (
)

const (
// maxHeightDiff contains the max height diff in case the last block is too old when the observer starts
maxHeightDiff = 10000

// btcBlocksPerDay represents Bitcoin blocks per days for LRU block cache size
btcBlocksPerDay = 144

Expand Down Expand Up @@ -684,37 +681,35 @@ func (ob *Observer) BuildBroadcastedTxMap() error {
return nil
}

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
lastBN := int64(lastBlockNum.Num)
ob.SetLastBlockHeightScanned(lastBN)

//If persisted block number is too low, use the latest height
if (bn - lastBN) > maxHeightDiff {
ob.logger.Chain.Info().Msgf("LastBlockNum too low: %d, scan from latest", lastBlockNum.Num)
ob.SetLastBlockHeightScanned(bn)
}
}

if ob.chain.ChainId == 18444 { // bitcoin regtest: start from block 100
// bitcoin regtest starts from block 100
if chains.IsBitcoinRegnet(ob.chain.ChainId) {
ob.SetLastBlockHeightScanned(100)
}
ob.logger.Chain.Info().Msgf("%s: start scanning from block %d", ob.chain.String(), ob.GetLastBlockHeightScanned())
ob.logger.Chain.Info().
Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())

return nil
}
Expand Down Expand Up @@ -812,8 +807,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
}
Expand Down
29 changes: 19 additions & 10 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,12 +570,17 @@ func (ob *Observer) BlockByNumber(blockNumber int) (*ethrpc.Block, error) {
return block, nil
}

func (ob *Observer) BuildLastBlock() error {
logger := ob.logger.Chain.With().Str("module", "BuildBlockIndex").Logger()
// 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 != "" {
logger.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 {
Expand All @@ -589,22 +594,26 @@ func (ob *Observer) BuildLastBlock() error {
}
ob.SetLastBlockHeightScanned(scanFromBlockInt)
}
} else { // last observed block
var lastBlockNum clienttypes.LastBlockSQLType
if err := ob.db.First(&lastBlockNum, clienttypes.LastBlockNumID).Error; err != nil {
logger.Info().Msgf("BuildLastBlock: db PosKey does not exist; read from external chain %s", ob.chain.String())
} else {
// load from DB otherwise
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 {
logger.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("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())

return nil
}

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

ob.db = db
err = ob.BuildLastBlock()
err = ob.LoadLastScannedBlock()
if err != nil {
return err
}
Expand Down

0 comments on commit 6fab9e2

Please sign in to comment.