Skip to content

Commit

Permalink
renamed LoadLastBlock -> LoadLastScannedBlock; added more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed May 21, 2024
1 parent 7981347 commit 09acf94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
15 changes: 8 additions & 7 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L667 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 674 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L674 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 680 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

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

Added line #L680 was not covered by tests
ob.SetLastBlockHeightScanned(bn)
} else {
// #nosec G701 always in range
Expand All @@ -688,7 +689,7 @@ func (ob *Observer) LoadLastBlock() error {
if chains.IsBitcoinRegnet(ob.chain.ChainId) {

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L692 was not covered by tests

return nil
}
Expand Down Expand Up @@ -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()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L791 was not covered by tests
if err != nil {
return err
}
Expand Down
21 changes: 11 additions & 10 deletions zetaclient/chains/evm/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L556 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 != "" {
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)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L563 was not covered by tests
if scanFromBlock == clienttypes.EnvVarLatest {
header, err := ob.evmClient.HeaderByNumber(context.Background(), nil)
if err != nil {
Expand All @@ -575,22 +576,22 @@ func (ob *Observer) LoadLastBlock() error {
}
} else {

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/evm/observer/observer.go#L579-L581

Added lines #L579 - L581 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 {
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())

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

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

0 comments on commit 09acf94

Please sign in to comment.