Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removed maxHeightDiff to let observer scan Bitcoin block from where it left off #2222

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
)

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 @@
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 {

Check warning on line 686 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L686

Added line #L686 was not covered by tests
// 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)

Check warning on line 693 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L693

Added line #L693 was not covered by tests
}

//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")

Check warning on line 699 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L699

Added line #L699 was not covered by tests
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 {
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved
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) {

Check warning on line 708 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L708

Added line #L708 was not covered by tests
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())

Check warning on line 712 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L711-L712

Added lines #L711 - L712 were not covered by tests

return nil
}
Expand Down Expand Up @@ -812,8 +807,8 @@
return err
}

//Load last block
err = ob.LoadLastBlock()
// Load last scanned block
err = ob.LoadLastScannedBlock()

Check warning on line 811 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L811

Added line #L811 was not covered by tests
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 @@
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 {

Check warning on line 575 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L575

Added line #L575 was not covered by tests
// get environment variable
envvar := ob.chain.ChainName.String() + "_SCAN_FROM"
scanFromBlock := os.Getenv(envvar)
ws4charlie marked this conversation as resolved.
Show resolved Hide resolved

// 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)

Check warning on line 583 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L582-L583

Added lines #L582 - L583 were not covered by tests
if scanFromBlock == clienttypes.EnvVarLatest {
header, err := ob.evmClient.HeaderByNumber(context.Background(), nil)
if err != nil {
Expand All @@ -589,22 +594,26 @@
}
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 {

Check warning on line 597 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L597

Added line #L597 was not covered by tests
// 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")

Check warning on line 601 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L599-L601

Added lines #L599 - L601 were not covered by tests
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())

Check warning on line 608 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L608

Added line #L608 was not covered by tests
}
} else {
ob.SetLastBlockHeightScanned(lastBlockNum.Num)
ob.SetLastBlockHeightScanned(lastBlock.Num)

Check warning on line 611 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L611

Added line #L611 was not covered by tests
}
}
ob.logger.Chain.Info().
Msgf("LoadLastScannedBlock: chain %d starts scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())

Check warning on line 615 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L614-L615

Added lines #L614 - L615 were not covered by tests

return nil
}

Expand Down Expand Up @@ -635,7 +644,7 @@
}

ob.db = db
err = ob.BuildLastBlock()
err = ob.LoadLastScannedBlock()

Check warning on line 647 in zetaclient/chains/evm/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L647

Added line #L647 was not covered by tests
if err != nil {
return err
}
Expand Down
Loading