diff --git a/e2etest/reporter_e2e_test.go b/e2etest/reporter_e2e_test.go index d558cb3..dfdaad0 100644 --- a/e2etest/reporter_e2e_test.go +++ b/e2etest/reporter_e2e_test.go @@ -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 diff --git a/monitor/btcscanner/block_handler.go b/monitor/btcscanner/block_handler.go index 704d1b9..abce39c 100644 --- a/monitor/btcscanner/block_handler.go +++ b/monitor/btcscanner/block_handler.go @@ -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{ @@ -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 { @@ -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") } @@ -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 } diff --git a/monitor/btcscanner/btc_scanner.go b/monitor/btcscanner/btc_scanner.go index 7145989..fa50387 100644 --- a/monitor/btcscanner/btc_scanner.go +++ b/monitor/btcscanner/btc_scanner.go @@ -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{} } @@ -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 { @@ -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 @@ -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) } @@ -181,13 +181,13 @@ 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) { @@ -195,7 +195,7 @@ func (bs *BtcScanner) Bootstrap() { } } - bs.UnconfirmedBlockCache.Add(ib) + bs.unconfirmedBlockCache.Add(ib) } bs.logger.Infof("bootstrapping is finished at the best confirmed height: %d", bestConfirmedHeight) @@ -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] } } @@ -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 } diff --git a/monitor/btcscanner/btc_scanner_test.go b/monitor/btcscanner/btc_scanner_test.go index cb91d70..0916cd1 100644 --- a/monitor/btcscanner/btc_scanner_test.go +++ b/monitor/btcscanner/btc_scanner_test.go @@ -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()) } }()