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

chore(btcscanner): unexport fields #41

Merged
merged 1 commit into from
Sep 11, 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
2 changes: 1 addition & 1 deletion e2etest/reporter_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestHandleReorgAfterRestart(t *testing.T) {
// // we will start from block before tip and submit 2 new block this should trigger rollback
tm.GenerateAndSubmitBlockNBlockStartingFromDepth(t, 2, 1)

btcClient := initBTCClientWithSubscriber(t, tm.Config) //current tm.BtcClient already has an active zmq subscription, would panic
btcClient := initBTCClientWithSubscriber(t, tm.Config) //current tm.btcClient already has an active zmq subscription, would panic
defer btcClient.Stop()

// Start new reporter
Expand Down
14 changes: 7 additions & 7 deletions monitor/btcscanner/block_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (bs *BtcScanner) bootstrapAndBlockEventHandler() {
bs.Bootstrap()

var blockEpoch *chainntnfs.BlockEpoch
bestKnownBlock := bs.UnconfirmedBlockCache.Tip()
bestKnownBlock := bs.unconfirmedBlockCache.Tip()
if bestKnownBlock != nil {
hash := bestKnownBlock.BlockHash()
blockEpoch = &chainntnfs.BlockEpoch{
Expand All @@ -34,7 +34,7 @@ func (bs *BtcScanner) bootstrapAndBlockEventHandler() {
for {
select {
case <-bs.quit:
bs.BtcClient.Stop()
bs.btcClient.Stop()
return
case epoch, open := <-blockNotifier.Epochs:
if !open {
Expand All @@ -55,7 +55,7 @@ func (bs *BtcScanner) bootstrapAndBlockEventHandler() {
// if new confirmed blocks are found, send them through the channel
func (bs *BtcScanner) handleNewBlock(height int32, header *wire.BlockHeader) error {
// get cache tip
cacheTip := bs.UnconfirmedBlockCache.Tip()
cacheTip := bs.unconfirmedBlockCache.Tip()
if cacheTip == nil {
return errors.New("no unconfirmed blocks found")
}
Expand All @@ -81,21 +81,21 @@ func (bs *BtcScanner) handleNewBlock(height int32, header *wire.BlockHeader) err

// get the block from hash
blockHash := header.BlockHash()
ib, _, err := bs.BtcClient.GetBlockByHash(&blockHash)
ib, _, err := bs.btcClient.GetBlockByHash(&blockHash)
if err != nil {
// failing to request the block, which means a bug
panic(fmt.Errorf("failed to request block by hash: %s", blockHash.String()))
}

// otherwise, add the block to the cache
bs.UnconfirmedBlockCache.Add(ib)
bs.unconfirmedBlockCache.Add(ib)

// still unconfirmed
if bs.UnconfirmedBlockCache.Size() <= bs.K {
if bs.unconfirmedBlockCache.Size() <= bs.k {
return nil
}

confirmedBlocks := bs.UnconfirmedBlockCache.TrimConfirmedBlocks(int(bs.K))
confirmedBlocks := bs.unconfirmedBlockCache.TrimConfirmedBlocks(int(bs.k))
if confirmedBlocks == nil {
return nil
}
Expand Down
89 changes: 62 additions & 27 deletions monitor/btcscanner/btc_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ type BtcScanner struct {
logger *zap.SugaredLogger

// connect to BTC node
BtcClient btcclient.BTCClient
btcClient btcclient.BTCClient
btcNotifier notifier.ChainNotifier

// the BTC height the scanner starts
BaseHeight uint64
baseHeight uint64
// the BTC confirmation depth
K uint64
k uint64

confirmedTipBlock *types.IndexedBlock
ConfirmedBlocksChan chan *types.IndexedBlock
confirmedBlocksChan chan *types.IndexedBlock

// cache of a sequence of checkpoints
ckptCache *types.CheckpointCache
// cache of a sequence of unconfirmed blocks
UnconfirmedBlockCache *types.BTCCache
unconfirmedBlockCache *types.BTCCache

// communicate with the monitor
blockHeaderChan chan *wire.BlockHeader
checkpointsChan chan *types.CheckpointRecord

wg sync.WaitGroup
Started *atomic.Bool
started *atomic.Bool
quit chan struct{}
}

Expand All @@ -75,29 +75,29 @@ func New(

return &BtcScanner{
logger: parentLogger.With(zap.String("module", "btcscanner")).Sugar(),
BtcClient: btcClient,
btcClient: btcClient,
btcNotifier: btcNotifier,
K: monitorCfg.BtcConfirmationDepth,
k: monitorCfg.BtcConfirmationDepth,
ckptCache: ckptCache,
UnconfirmedBlockCache: unconfirmedBlockCache,
ConfirmedBlocksChan: confirmedBlocksChan,
unconfirmedBlockCache: unconfirmedBlockCache,
confirmedBlocksChan: confirmedBlocksChan,
blockHeaderChan: headersChan,
checkpointsChan: ckptsChan,
Started: atomic.NewBool(false),
started: atomic.NewBool(false),
quit: make(chan struct{}),
}, nil
}

// Start starts the scanning process from curBTCHeight to tipHeight
func (bs *BtcScanner) Start(startHeight uint64) {
if bs.Started.Load() {
if bs.started.Load() {
bs.logger.Info("the BTC scanner is already started")
return
}

bs.SetBaseHeight(startHeight)

bs.Started.Store(true)
bs.started.Store(true)
bs.logger.Info("the BTC scanner is started")

if err := bs.btcNotifier.Start(); err != nil {
Expand All @@ -109,11 +109,11 @@ func (bs *BtcScanner) Start(startHeight uint64) {
bs.wg.Add(1)
go bs.bootstrapAndBlockEventHandler()

for bs.Started.Load() {
for bs.started.Load() {
select {
case <-bs.quit:
bs.Started.Store(false)
case block := <-bs.ConfirmedBlocksChan:
bs.started.Store(false)
case block := <-bs.confirmedBlocksChan:
bs.logger.Debugf("found a confirmed BTC block at height %d", block.Height)
// send the header to the Monitor for consistency check
bs.blockHeaderChan <- block.Header
Expand Down Expand Up @@ -150,17 +150,17 @@ func (bs *BtcScanner) Bootstrap() {
bs.logger.Infof("the bootstrapping starts at %d", firstUnconfirmedHeight)

// clear all the blocks in the cache to avoid forks
bs.UnconfirmedBlockCache.RemoveAll()
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))
}

bestConfirmedHeight := bestHeight - bs.K
bestConfirmedHeight := bestHeight - bs.k
// process confirmed blocks
for i := firstUnconfirmedHeight; i <= bestConfirmedHeight; i++ {
ib, _, err := bs.BtcClient.GetBlockByHeight(i)
ib, _, err := bs.btcClient.GetBlockByHeight(i)
if err != nil {
panic(err)
}
Expand All @@ -181,21 +181,21 @@ func (bs *BtcScanner) Bootstrap() {

// add unconfirmed blocks into the cache
for i := bestConfirmedHeight + 1; i <= bestHeight; i++ {
ib, _, err := bs.BtcClient.GetBlockByHeight(i)
ib, _, err := bs.btcClient.GetBlockByHeight(i)
if err != nil {
panic(err)
}

// the unconfirmed blocks must follow the canonical chain
tipCache := bs.UnconfirmedBlockCache.Tip()
tipCache := bs.unconfirmedBlockCache.Tip()
if tipCache != nil {
tipHash := tipCache.BlockHash()
if !tipHash.IsEqual(&ib.Header.PrevBlock) {
panic("invalid canonical chain")
}
}

bs.UnconfirmedBlockCache.Add(ib)
bs.unconfirmedBlockCache.Add(ib)
}

bs.logger.Infof("bootstrapping is finished at the best confirmed height: %d", bestConfirmedHeight)
Expand All @@ -206,12 +206,12 @@ func (bs *BtcScanner) SetLogger(logger *zap.SugaredLogger) {
}

func (bs *BtcScanner) GetConfirmedBlocksChan() chan *types.IndexedBlock {
return bs.ConfirmedBlocksChan
return bs.confirmedBlocksChan
}

func (bs *BtcScanner) sendConfirmedBlocksToChan(blocks []*types.IndexedBlock) {
for i := 0; i < len(blocks); i++ {
bs.ConfirmedBlocksChan <- blocks[i]
bs.confirmedBlocksChan <- blocks[i]
bs.confirmedTipBlock = blocks[i]
}
}
Expand Down Expand Up @@ -286,11 +286,46 @@ func (bs *BtcScanner) Stop() {
func (bs *BtcScanner) GetBaseHeight() uint64 {
bs.mu.Lock()
defer bs.mu.Unlock()
return bs.BaseHeight
return bs.baseHeight
}

func (bs *BtcScanner) SetBaseHeight(h uint64) {
bs.mu.Lock()
defer bs.mu.Unlock()
bs.BaseHeight = h
bs.baseHeight = h
}

// GetBtcClient returns the btcClient.
func (bs *BtcScanner) GetBtcClient() btcclient.BTCClient {
return bs.btcClient
}

// SetBtcClient sets the btcClient.
func (bs *BtcScanner) SetBtcClient(c btcclient.BTCClient) {
bs.btcClient = c
}

// GetK returns the value of k - confirmation depth
func (bs *BtcScanner) GetK() uint64 {
return bs.k
}

// SetK sets the value of k - confirmation depth
func (bs *BtcScanner) SetK(k uint64) {
bs.k = k
}

// SetConfirmedBlocksChan sets the confirmedBlocksChan.
func (bs *BtcScanner) SetConfirmedBlocksChan(ch chan *types.IndexedBlock) {
bs.confirmedBlocksChan = ch
}

// GetUnconfirmedBlockCache returns the unconfirmedBlockCache.
func (bs *BtcScanner) GetUnconfirmedBlockCache() *types.BTCCache {
return bs.unconfirmedBlockCache
}

// SetUnconfirmedBlockCache sets the unconfirmedBlockCache.
func (bs *BtcScanner) SetUnconfirmedBlockCache(c *types.BTCCache) {
bs.unconfirmedBlockCache = c
}
16 changes: 8 additions & 8 deletions monitor/btcscanner/btc_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ func FuzzBootStrap(f *testing.F) {

cache, err := types.NewBTCCache(numBlocks)
require.NoError(t, err)
btcScanner := &btcscanner.BtcScanner{
BtcClient: mockBtcClient,
BaseHeight: uint64(baseHeight),
K: k,
ConfirmedBlocksChan: make(chan *types.IndexedBlock),
UnconfirmedBlockCache: cache,
}
var btcScanner btcscanner.BtcScanner
btcScanner.SetBtcClient(mockBtcClient)
btcScanner.SetBaseHeight(uint64(baseHeight))
btcScanner.SetK(k)
btcScanner.SetConfirmedBlocksChan(make(chan *types.IndexedBlock))
btcScanner.SetUnconfirmedBlockCache(cache)

logger, err := config.NewRootLogger("auto", "debug")
require.NoError(t, err)
btcScanner.SetLogger(logger.Sugar())

go func() {
for i := 0; i < len(confirmedBlocks); i++ {
b := <-btcScanner.ConfirmedBlocksChan
b := <-btcScanner.GetConfirmedBlocksChan()
require.Equal(t, confirmedBlocks[i].BlockHash(), b.BlockHash())
}
}()
Expand Down
Loading