Skip to content

Commit

Permalink
removed maxHeightDiff to let observer scan Bitcoin block from where i…
Browse files Browse the repository at this point in the history
…t left off
  • Loading branch information
ws4charlie committed May 20, 2024
1 parent 47c8205 commit 906824a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
15 changes: 4 additions & 11 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,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 @@ -679,6 +676,7 @@ func (ob *Observer) BuildBroadcastedTxMap() error {
return nil
}

// LoadLastBlock loads last scanned block from DB
func (ob *Observer) LoadLastBlock() error {
bn, err := ob.rpcClient.GetBlockCount()
if err != nil {
Expand All @@ -694,18 +692,13 @@ func (ob *Observer) LoadLastBlock() error {
// #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) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L698 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("chain %d: start scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L701 was not covered by tests

return nil
}
Expand Down
20 changes: 13 additions & 7 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,15 @@ 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()
// LoadLastBlock loads last scanned block from specified height or from DB
func (ob *Observer) LoadLastBlock() error {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L562 was not covered by tests
// 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("BuildLastBlock: envvar %s is set; scan from block %s", envvar, scanFromBlock)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L569 was not covered by tests
if scanFromBlock == clienttypes.EnvVarLatest {
header, err := ob.evmClient.HeaderByNumber(context.Background(), nil)
if err != nil {
Expand All @@ -577,22 +580,25 @@ func (ob *Observer) BuildLastBlock() error {
}
ob.SetLastBlockHeightScanned(scanFromBlockInt)
}
} else { // last observed block
} else {

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#L583

Added line #L583 was not covered by tests
// load from DB otherwise
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())
ob.logger.Chain.Info().Msgf("BuildLastBlock: db PosKey does not exist; read from external chain %s", ob.chain.String())

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L587 was 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("BuildLastBlock: error writing lastBlockScanned %d to db", ob.GetLastBlockHeightScanned())

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L594 was not covered by tests
}
} else {
ob.SetLastBlockHeightScanned(lastBlockNum.Num)
}
}
ob.logger.Chain.Info().Msgf("chain %d: start scanning from block %d", ob.chain.ChainId, ob.GetLastBlockHeightScanned())

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L600 was not covered by tests

return nil
}

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

0 comments on commit 906824a

Please sign in to comment.