Skip to content

Commit 66348ac

Browse files
committed
supplycommit+tapdb: populate block metadata in RootCommitment struct
Updated the RootCommitment struct to include metadata from the mined Bitcoin block.
1 parent a7366fb commit 66348ac

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

tapdb/supply_commit.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ 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"
16+
"github.com/lightninglabs/taproot-assets/fn"
1517
"github.com/lightninglabs/taproot-assets/mssmt"
1618
"github.com/lightninglabs/taproot-assets/proof"
1719
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
@@ -330,6 +332,41 @@ func (s *SupplyCommitMachine) SupplyCommit(ctx context.Context,
330332
err)
331333
}
332334

335+
// Parse block related data from row if present.
336+
var commitmentBlock fn.Option[supplycommit.CommitmentBlock]
337+
if len(row.BlockHash) > 0 {
338+
// Parse block height if present, otherwise return an
339+
// error as it must be set if block hash is set.
340+
if !row.BlockHeight.Valid {
341+
return fmt.Errorf("block height must be set " +
342+
"if block hash is set")
343+
}
344+
345+
blockHeight := uint32(row.BlockHeight.Int32)
346+
347+
// Parse the block hash, which should be valid at this
348+
// point.
349+
blockHash, err := chainhash.NewHash(row.BlockHash)
350+
if err != nil {
351+
return fmt.Errorf("parsing block hash: %w", err)
352+
}
353+
354+
// Parse transaction block index which should be set
355+
// if the block height is set.
356+
if !row.TxIndex.Valid {
357+
return fmt.Errorf("transaction index must be " +
358+
"set if block height is set")
359+
}
360+
txIndex := uint32(row.TxIndex.Int32)
361+
362+
commitmentBlock = fn.Some(supplycommit.CommitmentBlock{
363+
Hash: *blockHash,
364+
Height: blockHeight,
365+
TxIndex: txIndex,
366+
ChainFees: row.ChainFees,
367+
})
368+
}
369+
333370
// Construct the root node directly from the stored hash and
334371
// sum. Handle potential NULL values if the root wasn't set yet
335372
// (though FetchSupplyCommit filters for confirmed TX, so it
@@ -355,11 +392,12 @@ func (s *SupplyCommitMachine) SupplyCommit(ctx context.Context,
355392
}
356393

357394
rootCommitment := supplycommit.RootCommitment{
358-
Txn: &commitTx,
359-
TxOutIdx: uint32(row.OutputIndex.Int32),
360-
InternalKey: internalKey,
361-
OutputKey: outputKey,
362-
SupplyRoot: rootNode,
395+
Txn: &commitTx,
396+
TxOutIdx: uint32(row.OutputIndex.Int32),
397+
InternalKey: internalKey,
398+
OutputKey: outputKey,
399+
SupplyRoot: rootNode,
400+
CommitmentBlock: commitmentBlock,
363401
}
364402
rootCommitmentOpt = lfn.Some(rootCommitment)
365403

universe/supplycommit/env.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/btcsuite/btcd/btcutil"
1010
"github.com/btcsuite/btcd/btcutil/psbt"
1111
"github.com/btcsuite/btcd/chaincfg"
12+
"github.com/btcsuite/btcd/chaincfg/chainhash"
1213
"github.com/btcsuite/btcd/txscript"
1314
"github.com/btcsuite/btcd/wire"
1415
"github.com/lightninglabs/taproot-assets/asset"
16+
"github.com/lightninglabs/taproot-assets/fn"
1517
"github.com/lightninglabs/taproot-assets/mssmt"
1618
"github.com/lightninglabs/taproot-assets/proof"
1719
"github.com/lightninglabs/taproot-assets/tapgarden"
@@ -143,6 +145,26 @@ func (p *PreCommitment) TxIn() *wire.TxIn {
143145
// PreCommits is a slice of pre-commitments.
144146
type PreCommits = []PreCommitment
145147

148+
// CommitmentBlock captures the finalized on-chain state of a supply commitment
149+
// transaction after it has been mined. It records block-level metadata and the
150+
// actual fee paid to anchor the commitment.
151+
type CommitmentBlock struct {
152+
// Height is the block height of the block that contains the
153+
// commitment.
154+
Height uint32
155+
156+
// Hash is the hash of the block that contains the commitment.
157+
Hash chainhash.Hash
158+
159+
// TxIndex is the index of the supply commitment transaction within
160+
// the block.
161+
TxIndex uint32
162+
163+
// ChainFees is the amount in sats paid in on-chain fees for the
164+
// supply commitment transaction.
165+
ChainFees int64
166+
}
167+
146168
// RootCommitment is the root commitment that contains the commitment to the the
147169
// sub-supply trees for a given asset.
148170
type RootCommitment struct {
@@ -164,6 +186,11 @@ type RootCommitment struct {
164186
// sub-commitments. The sum value of this tree is the outstanding supply
165187
// value.
166188
SupplyRoot *mssmt.BranchNode
189+
190+
// CommitmentBlock is the block that contains the commitment to the
191+
// asset supply. This may be None if the commitment has not yet
192+
// been mined.
193+
CommitmentBlock fn.Option[CommitmentBlock]
167194
}
168195

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

0 commit comments

Comments
 (0)