Skip to content

Commit

Permalink
fix query + debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Dec 20, 2024
1 parent eef7536 commit 9545857
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 6 additions & 4 deletions database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,10 @@ func (p *pgdb) L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2Keyston
}

sql := fmt.Sprintf(`
SELECT
SELECT` +
// DISTINCT ON returns the first result for each unique l2 block number
`
DISTINCT ON (l2_block_number)
btc_blocks_can.hash,
COALESCE(btc_blocks_can.height, 0),
l2_keystones.l2_keystone_abrev_hash,
Expand All @@ -635,18 +638,17 @@ func (p *pgdb) L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2Keyston
l2_keystones.version,
COALESCE((SELECT height FROM btc_blocks_can ORDER BY height DESC LIMIT 1),0)
` +
// for each l2_keystone <-> pop_basis pair (i.e. every mining)
// give me each pop mining
`
FROM l2_keystones
INNER JOIN pop_basis ON l2_keystones.l2_keystone_abrev_hash = pop_basis.l2_keystone_abrev_hash
` +
// give me the lowest block this was mined in on the canonical chain
// give me the lowest mined btc block for that keystone
`
LEFT JOIN LATERAL (
SELECT * FROM btc_blocks_can WHERE hash = pop_basis.btc_block_hash
ORDER BY height ASC LIMIT 1
) btc_blocks_can ON TRUE
WHERE l2_keystones.l2_keystone_abrev_hash = ANY($1)
Expand Down
23 changes: 23 additions & 0 deletions service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,29 @@ func (s *Server) trackBitcoin(ctx context.Context) {
case <-ticker.C:
log.Tracef("Checking BTC height...")

log.Infof("getting finalities")
finalities, err := s.db.L2BTCFinalityMostRecent(ctx, 100)
if err != nil {
log.Errorf("error getting finalities, %s", err)
}
log.Infof("done getting finalities")

apiFinalities := make([]hemi.L2BTCFinality, 0, len(finalities))
for _, finality := range finalities {
apiFinality, err := hemi.L2BTCFinalityFromBfgd(
&finality,
finality.BTCTipHeight,
finality.EffectiveHeight,
)
if err != nil {
log.Errorf("could not convert finality: %s", err)
} else {
apiFinalities = append(apiFinalities, *apiFinality)
}
}

log.Infof("finalities are %s", spew.Sdump(apiFinalities))

btcHeight, err := s.btcClient.Height(ctx)
if err != nil {
if printMsg {
Expand Down

0 comments on commit 9545857

Please sign in to comment.