Skip to content

Commit

Permalink
chore(btcclient): reduce rpc calls (#51)
Browse files Browse the repository at this point in the history
Reduces the number of RPC calls in methods by combining them.

[References issue](babylonchain/vigilante#218)
  • Loading branch information
Lazar955 authored Sep 16, 2024
1 parent 358f7a7 commit 49e8cb8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion btcclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type BTCClient interface {
Stop()
WaitForShutdown()
GetBestBlock() (*chainhash.Hash, uint64, error)
GetBestBlock() (uint64, error)
GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock, *wire.MsgBlock, error)
FindTailBlocksByHeight(height uint64) ([]*types.IndexedBlock, error)
GetBlockByHeight(height uint64) (*types.IndexedBlock, *wire.MsgBlock, error)
Expand Down
41 changes: 29 additions & 12 deletions btcclient/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@ import (
"github.com/babylonlabs-io/vigilante/types"
)

// GetBestBlock provides similar functionality with the btcd.rpcclient.GetBestBlock function
// We implement this, because this function is only provided by btcd.
// TODO: replace two rpc calls with only one c.GetBlockCount
func (c *Client) GetBestBlock() (*chainhash.Hash, uint64, error) {
btcLatestBlockHash, err := c.getBestBlockHashWithRetry()
// GetBestBlock returns the height of the best block
func (c *Client) GetBestBlock() (uint64, error) {
height, err := c.getBlockCountWithRetry()
if err != nil {
return nil, 0, err
return 0, err
}
btcLatestBlock, err := c.getBlockVerboseWithRetry(btcLatestBlockHash)
if err != nil {
return nil, 0, err
}
btcLatestBlockHeight := uint64(btcLatestBlock.Height)
return btcLatestBlockHash, btcLatestBlockHeight, nil

return uint64(height), nil
}

func (c *Client) GetBlockByHash(blockHash *chainhash.Hash) (*types.IndexedBlock, *wire.MsgBlock, error) {
Expand Down Expand Up @@ -213,3 +207,26 @@ func (c *Client) FindTailBlocksByHeight(baseHeight uint64) ([]*types.IndexedBloc

return c.getChainBlocks(baseHeight, tipIb)
}

func (c *Client) getBlockCountWithRetry() (int64, error) {
var (
height int64
err error
)

if err = retry.Do(func() error {
height, err = c.GetBlockCount()
if err != nil {
return err
}
return nil
},
retry.Delay(c.retrySleepTime),
retry.MaxDelay(c.maxRetrySleepTime),
retry.Attempts(c.maxRetryTimes),
); err != nil {
c.logger.Debug("failed to query get block count", zap.Error(err))
}

return height, nil
}
2 changes: 1 addition & 1 deletion monitor/btcscanner/btc_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (bs *BtcScanner) Bootstrap() {
// clear all the blocks in the cache to avoid forks
bs.unconfirmedBlockCache.RemoveAll()

_, bestHeight, err := bs.btcClient.GetBestBlock()
bestHeight, err := bs.btcClient.GetBestBlock()
if err != nil {
panic(fmt.Errorf("cannot get the best BTC block %w", err))
}
Expand Down
2 changes: 1 addition & 1 deletion monitor/btcscanner/btc_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func FuzzBootStrap(f *testing.F) {
defer ctl.Finish()
mockBtcClient := mocks.NewMockBTCClient(ctl)
confirmedBlocks := chainIndexedBlocks[:numBlocks-k]
mockBtcClient.EXPECT().GetBestBlock().Return(nil, uint64(bestHeight), nil)
mockBtcClient.EXPECT().GetBestBlock().Return(uint64(bestHeight), nil)
for i := 0; i < int(numBlocks); i++ {
mockBtcClient.EXPECT().GetBlockByHeight(gomock.Eq(uint64(chainIndexedBlocks[i].Height))).
Return(chainIndexedBlocks[i], nil, nil).AnyTimes()
Expand Down
4 changes: 2 additions & 2 deletions reporter/bootstrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (r *Reporter) waitUntilBTCSync() error {
)

// Retrieve hash/height of the latest block in BTC
btcLatestBlockHash, btcLatestBlockHeight, err = r.btcClient.GetBestBlock()
btcLatestBlockHeight, err = r.btcClient.GetBestBlock()
if err != nil {
return err
}
Expand Down Expand Up @@ -264,7 +264,7 @@ func (r *Reporter) waitUntilBTCSync() error {
// When BTC catches up, break and continue the bootstrapping process
ticker := time.NewTicker(5 * time.Second) // TODO: parameterise the polling interval
for range ticker.C {
_, btcLatestBlockHeight, err = r.btcClient.GetBestBlock()
btcLatestBlockHeight, err = r.btcClient.GetBestBlock()
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions testutil/mocks/btcclient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49e8cb8

Please sign in to comment.