Skip to content

Commit

Permalink
cache bitcoin height from process btc block (#236)
Browse files Browse the repository at this point in the history
* cache bitcoin height from process btc block

* skipping irrelevant test

* check for notification
  • Loading branch information
ClaytonNorthey92 authored Aug 29, 2024
1 parent 87a37c1 commit f4b877f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 12 additions & 0 deletions e2e/e2e_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ func TestBFGPublicErrorCases(t *testing.T) {
{},
},
electrumx: false,
skip: true,
},
{
name: "bitcoin utxos electrumx error",
Expand Down Expand Up @@ -1326,6 +1327,8 @@ func TestBitcoinInfo(t *testing.T) {
t.Fatal(err)
}

time.Sleep(5 * time.Second)

if err := bfgapi.Write(
ctx, bws.conn, "someid", &bfgapi.BitcoinInfoRequest{},
); err != nil {
Expand Down Expand Up @@ -2460,6 +2463,15 @@ func TestGetFinalitiesByL2KeystoneBSS(t *testing.T) {
t.Fatal(err)
}

// there is a chance we get notifications from the L2KeystonesInsert
// call above, if they haven't been broadcast yet. ignore those.
if v.Header.Command == bfgapi.CmdL2KeystonesNotification {
err = wsjson.Read(ctx, c, &v)
if err != nil {
t.Fatal(err)
}
}

if v.Header.Command != bssapi.CmdBTCFinalityByKeystonesResponse {
t.Fatalf("received unexpected command: %s", v.Header.Command)
}
Expand Down
33 changes: 26 additions & 7 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ type Server struct {
checkForInvalidBlocks chan struct{}

l2keystonesCache []hemi.L2Keystone

btcHeightCache uint64
}

// metrics stores prometheus metrics.
Expand Down Expand Up @@ -468,17 +470,31 @@ func (s *Server) handleBitcoinBroadcast(ctx context.Context, bbr *bfgapi.Bitcoin
return &bfgapi.BitcoinBroadcastResponse{TXID: hash[:]}, nil
}

func (s *Server) updateBtcHeightCache(height uint64) {
log.Tracef("updateBtcHeightCache")
defer log.Tracef("updateBtcHeightCache exit")

s.mtx.Lock()
defer s.mtx.Unlock()

s.btcHeightCache = height
}

func (s *Server) getBtcHeightCache() uint64 {
log.Tracef("getBtcHeightCache")
defer log.Tracef("getBtcHeightCache exit")

s.mtx.Lock()
defer s.mtx.Unlock()

return s.btcHeightCache
}

func (s *Server) handleBitcoinInfo(ctx context.Context, bir *bfgapi.BitcoinInfoRequest) (any, error) {
log.Tracef("handleBitcoinInfo")
defer log.Tracef("handleBitcoinInfo exit")

height, err := s.btcClient.Height(ctx)
if err != nil {
e := protocol.NewInternalErrorf("bitcoin height: %w", err)
return &bfgapi.BitcoinInfoResponse{
Error: e.ProtocolError(),
}, e
}
height := s.getBtcHeightCache()

return &bfgapi.BitcoinInfoResponse{
Height: height,
Expand Down Expand Up @@ -755,6 +771,9 @@ func (s *Server) trackBitcoin(ctx context.Context) {
}
continue
}

s.updateBtcHeightCache(btcHeight)

printMsg = true
if s.btcHeight > btcHeight {
// XXX do we need this check?
Expand Down

0 comments on commit f4b877f

Please sign in to comment.