From f4b877f6f0bb8b9796d8f2dac425b3e81c461805 Mon Sep 17 00:00:00 2001 From: ClaytonNorthey92 Date: Wed, 28 Aug 2024 21:04:51 -0400 Subject: [PATCH] cache bitcoin height from process btc block (#236) * cache bitcoin height from process btc block * skipping irrelevant test * check for notification --- e2e/e2e_ext_test.go | 12 ++++++++++++ service/bfg/bfg.go | 33 ++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/e2e/e2e_ext_test.go b/e2e/e2e_ext_test.go index ea5127a8..bd810688 100644 --- a/e2e/e2e_ext_test.go +++ b/e2e/e2e_ext_test.go @@ -1059,6 +1059,7 @@ func TestBFGPublicErrorCases(t *testing.T) { {}, }, electrumx: false, + skip: true, }, { name: "bitcoin utxos electrumx error", @@ -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 { @@ -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) } diff --git a/service/bfg/bfg.go b/service/bfg/bfg.go index e96d2992..758d022c 100644 --- a/service/bfg/bfg.go +++ b/service/bfg/bfg.go @@ -136,6 +136,8 @@ type Server struct { checkForInvalidBlocks chan struct{} l2keystonesCache []hemi.L2Keystone + + btcHeightCache uint64 } // metrics stores prometheus metrics. @@ -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, @@ -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?