Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lowest published
  • Loading branch information
ClaytonNorthey92 committed May 27, 2025
commit 6becfbcb31f9ff3d6bb1642e3d4e916f3bc7386e
56 changes: 43 additions & 13 deletions database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
// note that this is not paginated, so we may have many results but
// we should be able to handle it
q := `
SELECT DISTINCT(l2_block_number) FROM pop_basis
SELECT DISTINCT(l2_keystones.l2_keystone_abrev_hash), l2_block_number FROM pop_basis
INNER JOIN l2_keystones ON pop_basis.l2_keystone_abrev_hash = l2_keystones.l2_keystone_abrev_hash
WHERE btc_block_hash = $1
AND l2_block_number < $2
Expand All @@ -407,36 +407,66 @@
}

l2BlockNumbers := []uint32{}
l2BlockHashes := [][]byte{}

for rows.Next() {
var l2BlockNumber uint32
if err := rows.Scan(&l2BlockNumber); err != nil {
var l2KeystoneAbrevHash []byte
if err := rows.Scan(&l2KeystoneAbrevHash, &l2BlockNumber); err != nil {
return fmt.Errorf("scan btc block keystones: %w", err)
}

l2BlockNumbers = append(l2BlockNumbers, l2BlockNumber)
l2BlockHashes = append(l2BlockHashes, l2KeystoneAbrevHash)

log.Tracef("found l2 block number: %d for btc block height: %d", l2BlockNumber, btcBlockHeight)
}

rows.Close()

for _, l2BlockNumber := range l2BlockNumbers {

Check failure on line 427 in database/bfgd/postgres/postgres.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

// set the effective height for all keystones occuring at this l2 block
// or before to this btc block height if this is the lowest btc block number
u := `
UPDATE l2_keystones
SET lowest_btc_block_height = $1
WHERE l2_block_number <= $2
AND (
lowest_btc_block_height = 0
OR lowest_btc_block_height > $1
)
`
UPDATE l2_keystones
SET lowest_btc_block_effective_height = $1
WHERE l2_block_number <= $2
AND (
lowest_btc_block_effective_height = 0
OR lowest_btc_block_effective_height > $1
)
`

_, err = p.db.ExecContext(ctx, u, btcBlockHeight, l2BlockNumber)
if err != nil {
return fmt.Errorf("update l2 keystone lowest btc block height: %w", err)
}
}
for _, l2KeystoneAbrevHash := range l2BlockHashes {
// set the lowest btc block hash for this keystone equal to this block
// if it's null or this block is lower than the one that is set
u := `
UPDATE l2_keystones
SET lowest_btc_block_hash = $1
WHERE l2_keystone_abrev_hash = $2
AND (
lowest_btc_block_hash IS NULL
OR (
(SELECT height FROM btc_blocks WHERE hash = $1)
< COALESCE((
SELECT height FROM btc_blocks WHERE hash = lowest_btc_block_hash
), 0)

)
)
`

_, err = p.db.ExecContext(ctx, u, btcBlockHash[:], l2KeystoneAbrevHash)
if err != nil {
return fmt.Errorf("update l2 keystone lowest btc block height: %w", err)
}
}

return nil
}
Expand Down Expand Up @@ -771,7 +801,7 @@
sql := `
SELECT
hash,
lowest_btc_block_height,
COALESCE(height, 0),
l2_keystone_abrev_hash,
l1_block_number,
l2_block_number,
Expand All @@ -780,12 +810,12 @@
state_root,
ep_hash,
version,
lowest_btc_block_height,
lowest_btc_block_effective_height,
COALESCE((SELECT height FROM btc_blocks ORDER BY height DESC LIMIT 1),0)

FROM l2_keystones

LEFT JOIN btc_blocks ON btc_blocks.height = lowest_btc_block_height
LEFT JOIN btc_blocks ON btc_blocks.hash = lowest_btc_block_hash

WHERE l2_block_number <= $2
AND l2_keystone_abrev_hash = ANY($1)
Expand Down
3 changes: 2 additions & 1 deletion database/bfgd/scripts/0018.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ UPDATE version SET version = 18;

-- height is unique in BFG as of now; we use electrs as our "source-of-truth"
-- for the canonical chain
ALTER TABLE l2_keystones ADD COLUMN lowest_btc_block_height INT NOT NULL DEFAULT 0;
ALTER TABLE l2_keystones ADD COLUMN lowest_btc_block_effective_height INT NOT NULL DEFAULT 0;
ALTER TABLE l2_keystones ADD COLUMN lowest_btc_block_hash BYTEA NULL;

COMMIT;

Loading