Skip to content

Commit dba7ba2

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 3cda1fe commit dba7ba2

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

@@ -1186,6 +1192,30 @@ func parseSupplyCommitmentRow(ctx context.Context, commit SupplyCommitment,
11861192
}
11871193
}
11881194

1195+
if commit.SpentCommitment.Valid {
1196+
spentRow, err := db.QuerySupplyCommitmentOutpoint(
1197+
ctx, commit.SpentCommitment.Int64,
1198+
)
1199+
if err != nil {
1200+
return nil, fmt.Errorf("failed to query spent "+
1201+
"commitment with ID %d for commit %d: %w",
1202+
commit.SpentCommitment.Int64, commit.CommitID,
1203+
err)
1204+
}
1205+
1206+
hash, err := chainhash.NewHash(spentRow.Txid)
1207+
if err != nil {
1208+
return nil, fmt.Errorf("failed to parse spent "+
1209+
"commitment txid %x for commit %d: %w",
1210+
spentRow.Txid, commit.CommitID, err)
1211+
}
1212+
1213+
rootCommitment.SpentCommitment = fn.Some(wire.OutPoint{
1214+
Hash: *hash,
1215+
Index: uint32(spentRow.OutputIndex.Int32),
1216+
})
1217+
}
1218+
11891219
return rootCommitment, nil
11901220
}
11911221

universe/supplycommit/env.go

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

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

0 commit comments

Comments
 (0)