Skip to content

Commit

Permalink
make effective height sql more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Jan 8, 2025
1 parent a110579 commit e5a6083
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@ const (
verbose = false
)

const effectiveHeightSql = `
COALESCE((SELECT height
FROM
(
SELECT height FROM btc_blocks_can
INNER JOIN pop_basis ON pop_basis.btc_block_hash
= btc_blocks_can.hash
INNER JOIN l2_keystones ll ON ll.l2_keystone_abrev_hash
= pop_basis.l2_keystone_abrev_hash
WHERE ll.l2_block_number >= l2_keystones.l2_block_number
AND height > (SELECT height FROM btc_blocks_can ORDER BY height DESC LIMIT 1) - 100
ORDER BY height ASC LIMIT 1
)), 0)
`

var log = loggo.GetLogger("bfgpostgres")

func init() {
Expand Down Expand Up @@ -668,7 +651,7 @@ func (p *pgdb) L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2Keyston
limit = 100
}

sql := fmt.Sprintf(`
sql := `
SELECT
btc_block_hash,
COALESCE(btc_block_height, 0),
Expand All @@ -680,7 +663,18 @@ func (p *pgdb) L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2Keyston
l2_keystones.state_root,
l2_keystones.ep_hash,
l2_keystones.version,
%s,
COALESCE((SELECT height
FROM
(
SELECT height FROM btc_blocks_can
INNER JOIN l2_keystones_lowest_btc_block lll
ON lll.btc_block_hash = btc_blocks_can.hash
INNER JOIN l2_keystones ll
ON ll.l2_keystone_abrev_hash = lll.l2_keystone_abrev_hash
WHERE ll.l2_block_number >= l2_keystones.l2_block_number
AND height > (SELECT height FROM btc_blocks_can ORDER BY height DESC LIMIT 1) - 100
ORDER BY height ASC LIMIT 1
)), 0),
COALESCE((SELECT height FROM btc_blocks_can ORDER BY height DESC LIMIT 1),0)
FROM l2_keystones
Expand All @@ -694,7 +688,7 @@ func (p *pgdb) L2BTCFinalityByL2KeystoneAbrevHash(ctx context.Context, l2Keyston
OFFSET $2
LIMIT $3
`, effectiveHeightSql)
`

l2KeystoneAbrevHashesStr := [][]byte{}
for _, l := range l2KeystoneAbrevHashes {
Expand Down

0 comments on commit e5a6083

Please sign in to comment.