Skip to content

Commit 16b3add

Browse files
committed
tapdb+universe: add SpentCommitment to RootCommitment
To find out what commitment is being spent by a next one, we add the optional SpentCommitment field to the RootCommitment struct. The field should only be None for the very first commitment of an asset group.
1 parent f0acb18 commit 16b3add

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

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_commit.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ FROM supply_commitments AS sc
165165
ON sc.chain_txn_id = ct.txn_id
166166
WHERE sc.spent_commitment = (SELECT commit_id FROM spent_commitment);
167167

168+
-- name: QuerySupplyCommitmentOutpoint :one
169+
SELECT ct.txid, sc.output_index
170+
FROM supply_commitments AS sc
171+
JOIN chain_txns AS ct
172+
ON sc.chain_txn_id = ct.txn_id
173+
WHERE sc.commit_id = @commit_id;
174+
168175
-- name: UpdateSupplyCommitTransitionCommitment :exec
169176
UPDATE supply_commit_transitions
170177
SET new_commitment_id = @new_commitment_id,

tapdb/sqlc/supply_commit.sql.go

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

tapdb/supply_commit.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/btcsuite/btcd/btcec/v2"
13+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1314
"github.com/btcsuite/btcd/wire"
1415
"github.com/lightninglabs/taproot-assets/asset"
1516
"github.com/lightninglabs/taproot-assets/fn"
@@ -189,6 +190,11 @@ type SupplyCommitStore interface {
189190
arg sqlc.QuerySupplyCommitmentBySpentOutpointParams) (
190191
sqlc.QuerySupplyCommitmentBySpentOutpointRow, error)
191192

193+
// QuerySupplyCommitmentOutpoint fetches the outpoint of a supply
194+
// commitment by its ID.
195+
QuerySupplyCommitmentOutpoint(ctx context.Context,
196+
commitID int64) (sqlc.QuerySupplyCommitmentOutpointRow, error)
197+
192198
// FetchChainTx fetches a chain transaction by its TXID.
193199
FetchChainTx(ctx context.Context, txid []byte) (ChainTxn, error)
194200

@@ -1194,6 +1200,30 @@ func parseSupplyCommitmentRow(ctx context.Context, commit SupplyCommitment,
11941200
}
11951201
}
11961202

1203+
if commit.SpentCommitment.Valid {
1204+
spentRow, err := db.QuerySupplyCommitmentOutpoint(
1205+
ctx, commit.SpentCommitment.Int64,
1206+
)
1207+
if err != nil {
1208+
return nil, fmt.Errorf("failed to query spent "+
1209+
"commitment with ID %d for commit %d: %w",
1210+
commit.SpentCommitment.Int64, commit.CommitID,
1211+
err)
1212+
}
1213+
1214+
hash, err := chainhash.NewHash(spentRow.Txid)
1215+
if err != nil {
1216+
return nil, fmt.Errorf("failed to parse spent "+
1217+
"commitment txid %x for commit %d: %w",
1218+
spentRow.Txid, commit.CommitID, err)
1219+
}
1220+
1221+
rootCommitment.SpentCommitment = fn.Some(wire.OutPoint{
1222+
Hash: *hash,
1223+
Index: uint32(spentRow.OutputIndex.Int32),
1224+
})
1225+
}
1226+
11971227
return rootCommitment, nil
11981228
}
11991229

universe/supplycommit/env.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ type RootCommitment struct {
308308
// asset supply. This may be None if the commitment has not yet
309309
// been mined.
310310
CommitmentBlock fn.Option[CommitmentBlock]
311+
312+
// SpentCommitment is the outpoint of the previous root commitment that
313+
// this root commitment is spending. This will be None if this is the
314+
// first root commitment for the asset.
315+
SpentCommitment fn.Option[wire.OutPoint]
311316
}
312317

313318
// TxIn returns the transaction input that corresponds to the root commitment.

0 commit comments

Comments
 (0)