Skip to content

Commit

Permalink
combine booststrap and blockevent handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Aug 27, 2024
1 parent df198b3 commit e47d580
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
33 changes: 23 additions & 10 deletions monitor/btcscanner/block_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@ import (
"github.com/lightningnetwork/lnd/chainntnfs"
)

// blockEventHandler handles connected and disconnected blocks from the BTC client.
func (bs *BtcScanner) blockEventHandler(blockNotifier *chainntnfs.BlockEpochEvent) {
// bootstrapAndBlockEventHandler handles connected and disconnected blocks from the BTC client.
func (bs *BtcScanner) bootstrapAndBlockEventHandler() {
defer bs.wg.Done()

bs.Bootstrap()

var blockEpoch *chainntnfs.BlockEpoch
bestKnownBlock := bs.UnconfirmedBlockCache.Tip()
if bestKnownBlock != nil {
hash := bestKnownBlock.BlockHash()
blockEpoch = &chainntnfs.BlockEpoch{
Hash: &hash,
Height: bestKnownBlock.Height,
BlockHeader: bestKnownBlock.Header,
}
}
// register the notifier with the best known tip
blockNotifier, err := bs.btcNotifier.RegisterBlockEpochNtfn(blockEpoch)
if err != nil {
bs.logger.Errorf("Failed registering block epoch notifier")
return
}
defer blockNotifier.Cancel()

for {
Expand All @@ -26,9 +45,7 @@ func (bs *BtcScanner) blockEventHandler(blockNotifier *chainntnfs.BlockEpochEven
if err := bs.handleNewBlock(epoch.Height, epoch.BlockHeader); err != nil {
bs.logger.Warnf("failed to handle block at height %d: %s, "+
"need to restart the bootstrapping process", epoch.Height, err.Error())
if bs.Synced.Swap(false) {
bs.Bootstrap()
}
bs.Bootstrap()
}
}
}
Expand All @@ -37,10 +54,6 @@ func (bs *BtcScanner) blockEventHandler(blockNotifier *chainntnfs.BlockEpochEven
// handleNewBlock handles blocks from the BTC client
// if new confirmed blocks are found, send them through the channel
func (bs *BtcScanner) handleNewBlock(height int32, header *wire.BlockHeader) error {
if !bs.Synced.Load() {
return errors.New("the btc scanner is not synced")
}

// get cache tip
cacheTip := bs.UnconfirmedBlockCache.Tip()
if cacheTip == nil {
Expand Down Expand Up @@ -71,7 +84,7 @@ func (bs *BtcScanner) handleNewBlock(height int32, header *wire.BlockHeader) err
ib, _, err := bs.BtcClient.GetBlockByHash(&blockHash)
if err != nil {
// failing to request the block, which means a bug
panic(err)
panic(fmt.Errorf("failed to request block by hash: %s", blockHash.String()))
}

// otherwise, add the block to the cache
Expand Down
20 changes: 1 addition & 19 deletions monitor/btcscanner/btc_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ type BtcScanner struct {
blockHeaderChan chan *wire.BlockHeader
checkpointsChan chan *types.CheckpointRecord

Synced *atomic.Bool

wg sync.WaitGroup
Started *atomic.Bool
quit chan struct{}
Expand Down Expand Up @@ -75,7 +73,6 @@ func New(
ConfirmedBlocksChan: confirmedBlocksChan,
blockHeaderChan: headersChan,
checkpointsChan: ckptsChan,
Synced: atomic.NewBool(false),
Started: atomic.NewBool(false),
quit: make(chan struct{}),
}, nil
Expand All @@ -88,9 +85,6 @@ func (bs *BtcScanner) Start() {
return
}

// the bootstrapping should not block the main thread
go bs.Bootstrap()

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

Expand All @@ -99,15 +93,9 @@ func (bs *BtcScanner) Start() {
return
}

blockNotifier, err := bs.btcNotifier.RegisterBlockEpochNtfn(nil)
if err != nil {
bs.logger.Errorf("Failed registering block epoch notifier")
return
}

// start handling new blocks
bs.wg.Add(1)
go bs.blockEventHandler(blockNotifier)
go bs.bootstrapAndBlockEventHandler()

for bs.Started.Load() {
select {
Expand Down Expand Up @@ -141,12 +129,6 @@ func (bs *BtcScanner) Bootstrap() {
err error
)

if bs.Synced.Load() {
// the scanner is already synced
return
}
defer bs.Synced.Store(true)

if bs.confirmedTipBlock != nil {
firstUnconfirmedHeight = uint64(bs.confirmedTipBlock.Height + 1)
} else {
Expand Down
8 changes: 2 additions & 6 deletions monitor/btcscanner/btc_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"testing"

"github.com/babylonlabs-io/babylon/testutil/datagen"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"

"github.com/babylonlabs-io/vigilante/config"
"github.com/babylonlabs-io/vigilante/monitor/btcscanner"
vdatagen "github.com/babylonlabs-io/vigilante/testutil/datagen"
"github.com/babylonlabs-io/vigilante/testutil/mocks"
"github.com/babylonlabs-io/vigilante/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)

func FuzzBootStrap(f *testing.F) {
Expand Down Expand Up @@ -44,7 +42,6 @@ func FuzzBootStrap(f *testing.F) {
K: k,
ConfirmedBlocksChan: make(chan *types.IndexedBlock),
UnconfirmedBlockCache: cache,
Synced: atomic.NewBool(false),
}
logger, err := config.NewRootLogger("auto", "debug")
require.NoError(t, err)
Expand All @@ -57,6 +54,5 @@ func FuzzBootStrap(f *testing.F) {
}
}()
btcScanner.Bootstrap()
require.True(t, btcScanner.Synced.Load())
})
}

0 comments on commit e47d580

Please sign in to comment.