Skip to content

Commit

Permalink
squashme
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Jan 14, 2025
1 parent 4902083 commit 1fa33d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 48 deletions.
6 changes: 0 additions & 6 deletions database/bfgd/scripts/0015.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ DROP FUNCTION refresh_btc_blocks_can();
-- no longer have BFG responsible for the canonical chain
DROP MATERIALIZED VIEW btc_blocks_can;

-- IMPORTANT
-- rebuild these tables, since their functionality change so much
-- we can keep l2_keystones
DELETE FROM pop_basis;
DELETE FROM btc_blocks;

-- we now trust electrs/bitcoind to maintain the best chain, ensure these
-- three columns are unique together
ALTER TABLE btc_blocks ADD UNIQUE (height, hash, header);
Expand Down
50 changes: 8 additions & 42 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ type Server struct {
bfgCmdCh chan bfgCmd // commands to send to bfg

btcPrivateKey *secp256k1.PrivateKey

walkChainFrom chan uint64
}

// metrics stores prometheus metrics.
Expand Down Expand Up @@ -264,7 +262,6 @@ func NewServer(cfg *Config) (*Server, error) {
holdoffTimeout: 6 * time.Second,
bfgCallTimeout: 20 * time.Second,
bfgCmdCh: make(chan bfgCmd),
walkChainFrom: make(chan uint64, 1),
}
for range cfg.RequestLimit {
s.requestLimiter <- true
Expand Down Expand Up @@ -714,10 +711,16 @@ func (s *Server) trackBitcoin(ctx context.Context) {
log.Tracef("trackBitcoin")
defer log.Tracef("trackBitcoin exit")

// upon startup we walk every block between the tip and our
// configured start block. IMPORTANT NOTE: we ONLY process
// transactions in blocks that we have not seen, so whilst we
// walk quite a few blocks, most will be essentially no-ops
// except for when you have an empty database table
initialWalk := true

btcInterval := 5 * time.Second
ticker := time.NewTicker(btcInterval)

for {
select {
case <-ctx.Done():
Expand All @@ -738,39 +741,8 @@ func (s *Server) trackBitcoin(ctx context.Context) {
log.Errorf("could not walk chain: %s", err)
}

initialWalk = false
}
}
}

func (s *Server) queueChainWalk(btcHeight uint64) {
select {
case s.walkChainFrom <- btcHeight:
default:
}
}

func (s *Server) chainWalker(ctx context.Context) {
defer s.wg.Done()

// on startup, process all stored blocks. this seems like a lot, but
// if we have already seen the block we don't process its transactions
// so this will be quick
initialWalk := true

for {
select {
case <-ctx.Done():
return
case tip := <-s.walkChainFrom:
log.Infof("received tip to walk back from: %d", tip)
if err := s.walkChain(ctx, tip, !initialWalk); err != nil {
log.Errorf("could not walk chain: %s", err)
}

// after we have walked once, then going forward we only walk
// to blocks that we have seen before. if those haven't changed,
// nothing lower should have changed
// after we have done the initial walk, in the future we only
// walk back until a block that we've seen
initialWalk = false
}
}
Expand Down Expand Up @@ -1706,9 +1678,6 @@ func (s *Server) connectBFG(pctx context.Context) error {
s.bfgWG.Add(1)
go s.handleBFGWebsocketReadUnauth(ctx, conn)

s.bfgWG.Add(1)
go s.chainWalker(ctx)

// Wait for exit
s.bfgWG.Wait()

Expand Down Expand Up @@ -1970,9 +1939,6 @@ func (s *Server) Run(pctx context.Context) error {
s.wg.Add(1)
go s.trackBitcoin(ctx)

s.wg.Add(1)
go s.chainWalker(ctx)

select {
case <-ctx.Done():
err = ctx.Err()
Expand Down

0 comments on commit 1fa33d2

Please sign in to comment.