Skip to content

Commit f1db7c9

Browse files
authored
Merge pull request #1596 from lightninglabs/supply-leaf-height-query
universe+tapdb: add new `block_height` field to `universe_leaves` use that to implement block height based supply tree queries
2 parents 58c3b8f + 4e7a385 commit f1db7c9

21 files changed

+596
-39
lines changed

tapdb/burn_tree.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,17 @@ func insertBurnsInternal(ctx context.Context, db BaseUniverseStore,
117117
IsBurn: true,
118118
}
119119

120+
var blockHeight lfn.Option[uint32]
121+
height := burnLeaf.BurnProof.BlockHeight
122+
if height > 0 {
123+
blockHeight = lfn.Some(height)
124+
}
125+
120126
// Call the generic upsert function for the burn sub-tree to
121127
// update DB records. MetaReveal is nil for burns.
122128
_, err = universeUpsertProofLeaf(
123129
ctx, db, subNs, supplycommit.BurnTreeType.String(),
124-
groupKey, leafKey, leaf, nil,
130+
groupKey, leafKey, leaf, nil, blockHeight,
125131
)
126132
if err != nil {
127133
return nil, fmt.Errorf("unable to upsert burn "+

tapdb/ignore_tree.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ func addTuplesInternal(ctx context.Context, db BaseUniverseStore,
113113
"universe root: %w", err)
114114
}
115115

116+
var blockHeight lfn.Option[uint32]
117+
height := tup.IgnoreTuple.Val.BlockHeight
118+
if height > 0 {
119+
blockHeight = lfn.Some(height)
120+
}
121+
122+
sqlBlockHeight := lfn.MapOptionZ(
123+
blockHeight, func(num uint32) sql.NullInt32 {
124+
return sql.NullInt32{
125+
Int32: int32(num),
126+
Valid: true,
127+
}
128+
},
129+
)
130+
116131
scriptKey := ignoreTup.ScriptKey
117132
err = db.UpsertUniverseLeaf(ctx, UpsertUniverseLeaf{
118133
AssetGenesisID: assetGenID,
@@ -121,6 +136,7 @@ func addTuplesInternal(ctx context.Context, db BaseUniverseStore,
121136
LeafNodeKey: smtKey[:],
122137
LeafNodeNamespace: namespace,
123138
MintingPoint: ignorePointBytes,
139+
BlockHeight: sqlBlockHeight,
124140
})
125141
if err != nil {
126142
return nil, fmt.Errorf("failed to upsert ignore "+

tapdb/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
// daemon.
2525
//
2626
// NOTE: This MUST be updated when a new migration is added.
27-
LatestMigrationVersion = 41
27+
LatestMigrationVersion = 42
2828
)
2929

3030
// DatabaseBackend is an interface that contains all methods our different

tapdb/multiverse.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,11 +776,16 @@ func (b *MultiverseStore) UpsertProofLeaf(ctx context.Context,
776776

777777
execTxFunc := func(dbTx BaseMultiverseStore) error {
778778
// Register issuance in the asset (group) specific universe
779-
// tree.
780-
var err error
779+
// tree. We don't need to decode the whole proof, we just
780+
// need the block height.
781+
blockHeight, err := SparseDecodeBlockHeight(leaf.RawProof)
782+
if err != nil {
783+
return err
784+
}
785+
781786
uniProof, err = universeUpsertProofLeaf(
782787
ctx, dbTx, id.String(), id.ProofType.String(),
783-
id.GroupKey, key, leaf, metaReveal,
788+
id.GroupKey, key, leaf, metaReveal, blockHeight,
784789
)
785790
if err != nil {
786791
return fmt.Errorf("failed universe upsert: %w", err)
@@ -847,13 +852,22 @@ func (b *MultiverseStore) UpsertProofLeafBatch(ctx context.Context,
847852
for idx := range items {
848853
item := items[idx]
849854

855+
// We don't need to decode the whole proof, we
856+
// just need the block height.
857+
blockHeight, err := SparseDecodeBlockHeight(
858+
item.Leaf.RawProof,
859+
)
860+
if err != nil {
861+
return err
862+
}
863+
850864
// Upsert into the specific universe tree to
851865
// start with.
852866
uniProof, err := universeUpsertProofLeaf(
853867
ctx, store, item.ID.String(),
854868
item.ID.ProofType.String(),
855869
item.ID.GroupKey, item.Key, item.Leaf,
856-
item.MetaReveal,
870+
item.MetaReveal, blockHeight,
857871
)
858872
if err != nil {
859873
return fmt.Errorf("failed universe "+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE universe_leaves DROP COLUMN block_height;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE universe_leaves ADD COLUMN block_height INTEGER;

tapdb/sqlc/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tapdb/sqlc/queries/supply_tree.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,25 @@ JOIN universe_supply_roots r
4343
WHERE r.id = @supply_root_id AND
4444
(l.sub_tree_type = sqlc.narg('sub_tree_type') OR sqlc.narg('sub_tree_type') IS NULL);
4545

46+
-- name: QuerySupplyLeavesByHeight :many
47+
SELECT
48+
leaves.script_key_bytes,
49+
gen.gen_asset_id,
50+
nodes.value AS supply_leaf_bytes,
51+
nodes.sum AS sum_amt,
52+
gen.asset_id,
53+
leaves.block_height
54+
FROM universe_leaves AS leaves
55+
JOIN mssmt_nodes AS nodes
56+
ON leaves.leaf_node_key = nodes.key
57+
AND leaves.leaf_node_namespace = nodes.namespace
58+
JOIN genesis_info_view AS gen
59+
ON leaves.asset_genesis_id = gen.gen_asset_id
60+
WHERE
61+
leaves.leaf_node_namespace = @namespace AND
62+
(leaves.block_height >= sqlc.narg('start_height') OR sqlc.narg('start_height') IS NULL) AND
63+
(leaves.block_height <= sqlc.narg('end_height') OR sqlc.narg('end_height') IS NULL);
64+
4665
-- name: DeleteUniverseSupplyLeaves :exec
4766
DELETE FROM universe_supply_leaves
4867
WHERE supply_root_id = (

tapdb/sqlc/queries/universe.sql

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ WHERE namespace_root = @namespace_root;
3838

3939
-- name: UpsertUniverseLeaf :exec
4040
INSERT INTO universe_leaves (
41-
asset_genesis_id, script_key_bytes, universe_root_id, leaf_node_key,
42-
leaf_node_namespace, minting_point
41+
asset_genesis_id, script_key_bytes, universe_root_id, leaf_node_key,
42+
leaf_node_namespace, minting_point, block_height
4343
) VALUES (
4444
@asset_genesis_id, @script_key_bytes, @universe_root_id, @leaf_node_key,
45-
@leaf_node_namespace, @minting_point
45+
@leaf_node_namespace, @minting_point, @block_height
4646
) ON CONFLICT (minting_point, script_key_bytes, leaf_node_namespace)
47-
-- This is a NOP, minting_point and script_key_bytes are the unique fields
48-
-- that caused the conflict.
47+
-- Update the block_height on conflict. The minting_point, script_key_bytes,
48+
-- and leaf_node_namespace remain unchanged as they form the unique constraint.
4949
DO UPDATE SET minting_point = EXCLUDED.minting_point,
5050
script_key_bytes = EXCLUDED.script_key_bytes,
51-
leaf_node_namespace = EXCLUDED.leaf_node_namespace;
51+
leaf_node_namespace = EXCLUDED.leaf_node_namespace,
52+
block_height = EXCLUDED.block_height;
5253

5354
-- name: DeleteUniverseLeaves :exec
5455
DELETE FROM universe_leaves

0 commit comments

Comments
 (0)