Skip to content

Commit 8b624e3

Browse files
committed
tapdb: refactor BindMintingBatchWithTx into insertMintAnchorTx
Refactor BindMintingBatchWithTx into insertMintAnchorTx, which will also handle inserting pre-commitment information into the database. insertMintAnchorTx will also ensure that the genesis point is in the database.
1 parent 21cc5ba commit 8b624e3

File tree

1 file changed

+100
-56
lines changed

1 file changed

+100
-56
lines changed

tapdb/asset_minting.go

Lines changed: 100 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ type (
127127
// NewAssetMeta wraps the params needed to insert a new asset meta on
128128
// disk.
129129
NewAssetMeta = sqlc.UpsertAssetMetaParams
130+
131+
// MintAnchorUniCommitParams wraps the params needed to insert a new
132+
// mint anchor uni commitment on disk.
133+
MintAnchorUniCommitParams = sqlc.UpsertMintAnchorUniCommitmentParams
130134
)
131135

132136
// PendingAssetStore is a sub-set of the main sqlc.Querier interface that
@@ -244,6 +248,11 @@ type PendingAssetStore interface {
244248
// for a given batch.
245249
FetchMintAnchorUniCommitment(ctx context.Context,
246250
batchID int32) (sqlc.MintAnchorUniCommitment, error)
251+
252+
// UpsertMintAnchorUniCommitment inserts a new or updates an existing
253+
// mint anchor uni commitment on disk.
254+
UpsertMintAnchorUniCommitment(ctx context.Context,
255+
arg MintAnchorUniCommitParams) (int64, error)
247256
}
248257

249258
var (
@@ -317,6 +326,77 @@ type OptionalSeedlingFields struct {
317326
GroupAnchorID sql.NullInt64
318327
}
319328

329+
// insertMintAnchorTx inserts a mint anchor transaction into the database.
330+
func insertMintAnchorTx(ctx context.Context, q PendingAssetStore,
331+
anchorPackage tapgarden.FundedMintAnchorPsbt,
332+
batchKey btcec.PublicKey, genesisOutpoint wire.OutPoint) error {
333+
334+
// Ensure that the genesis point is in the database.
335+
genesisPointDbID, err := upsertGenesisPoint(
336+
ctx, q, genesisOutpoint,
337+
)
338+
if err != nil {
339+
return fmt.Errorf("%w: %w", ErrUpsertGenesisPoint, err)
340+
}
341+
342+
var psbtBuf bytes.Buffer
343+
err = anchorPackage.Pkt.Serialize(&psbtBuf)
344+
if err != nil {
345+
return fmt.Errorf("%w: %w", ErrEncodePsbt, err)
346+
}
347+
348+
rawBatchKey := batchKey.SerializeCompressed()
349+
enableUniverseCommitments := anchorPackage.PreCommitmentOutput.IsSome()
350+
351+
batchID, err := q.BindMintingBatchWithTx(ctx, BatchChainUpdate{
352+
RawKey: rawBatchKey,
353+
MintingTxPsbt: psbtBuf.Bytes(),
354+
ChangeOutputIndex: sqlInt32(anchorPackage.ChangeOutputIndex),
355+
AssetsOutputIndex: sqlInt32(anchorPackage.AssetAnchorOutIdx),
356+
GenesisID: sqlInt64(genesisPointDbID),
357+
UniverseCommitments: enableUniverseCommitments,
358+
})
359+
if err != nil {
360+
return fmt.Errorf("%w: %w", ErrBindBatchTx, err)
361+
}
362+
363+
// If universe commitments are not enabled for this batch, we can
364+
// return early.
365+
if !enableUniverseCommitments {
366+
return nil
367+
}
368+
369+
// At this point, universe commitments are enabled for this batch, so
370+
// we'll insert the mint anchor uni commitment record.
371+
preCommitOut, err := anchorPackage.PreCommitmentOutput.UnwrapOrErr(
372+
fmt.Errorf("pre-commitment outpoint bundle not set"),
373+
)
374+
if err != nil {
375+
return err
376+
}
377+
378+
// Serialize internal key.
379+
internalKey := preCommitOut.InternalKey.SerializeCompressed()
380+
381+
// Serialize group key.
382+
groupPubKey := preCommitOut.GroupPubKey.SerializeCompressed()
383+
384+
_, err = q.UpsertMintAnchorUniCommitment(
385+
ctx, MintAnchorUniCommitParams{
386+
BatchID: int32(batchID),
387+
TxOutputIndex: int32(preCommitOut.OutIdx),
388+
TaprootInternalKey: internalKey,
389+
GroupKey: groupPubKey,
390+
},
391+
)
392+
if err != nil {
393+
return fmt.Errorf("unable to insert mint anchor uni "+
394+
"commitment: %w", err)
395+
}
396+
397+
return nil
398+
}
399+
320400
// CommitMintingBatch commits a new minting batch to disk along with any
321401
// seedlings specified as part of the batch. A new internal key is also
322402
// created, with the batch referencing that internal key. This internal key
@@ -371,31 +451,16 @@ func (a *AssetMintingStore) CommitMintingBatch(ctx context.Context,
371451
if newBatch.GenesisPacket != nil {
372452
genesisPacket := newBatch.GenesisPacket
373453
genesisTx := genesisPacket.Pkt.UnsignedTx
374-
changeIdx := genesisPacket.ChangeOutputIndex
375454
genesisOutpoint := genesisTx.TxIn[0].PreviousOutPoint
376455

377-
var psbtBuf bytes.Buffer
378-
err := genesisPacket.Pkt.Serialize(&psbtBuf)
379-
if err != nil {
380-
return fmt.Errorf("%w: %w", ErrEncodePsbt, err)
381-
}
382-
383-
genesisPointID, err := upsertGenesisPoint(
384-
ctx, q, genesisOutpoint,
456+
// Insert the batch transaction.
457+
err = insertMintAnchorTx(
458+
ctx, q, *genesisPacket,
459+
*newBatch.BatchKey.PubKey, genesisOutpoint,
385460
)
386461
if err != nil {
387-
return fmt.Errorf("%w: %w",
388-
ErrUpsertGenesisPoint, err)
389-
}
390-
391-
_, err = q.BindMintingBatchWithTx(ctx, BatchChainUpdate{
392-
RawKey: rawBatchKey,
393-
MintingTxPsbt: psbtBuf.Bytes(),
394-
ChangeOutputIndex: sqlInt32(changeIdx),
395-
GenesisID: sqlInt64(genesisPointID),
396-
})
397-
if err != nil {
398-
return fmt.Errorf("%w: %w", ErrBindBatchTx, err)
462+
return fmt.Errorf("unable to insert mint "+
463+
"anchor tx: %w", err)
399464
}
400465
}
401466

@@ -1340,31 +1405,19 @@ func (a *AssetMintingStore) CommitBatchTx(ctx context.Context,
13401405
genesisPacket tapgarden.FundedMintAnchorPsbt) error {
13411406

13421407
genesisOutpoint := genesisPacket.Pkt.UnsignedTx.TxIn[0].PreviousOutPoint
1343-
rawBatchKey := batchKey.SerializeCompressed()
1344-
1345-
var psbtBuf bytes.Buffer
1346-
if err := genesisPacket.Pkt.Serialize(&psbtBuf); err != nil {
1347-
return fmt.Errorf("%w: %w", ErrEncodePsbt, err)
1348-
}
13491408

13501409
var writeTxOpts AssetStoreTxOptions
13511410
return a.db.ExecTx(ctx, &writeTxOpts, func(q PendingAssetStore) error {
1352-
genesisPointID, err := upsertGenesisPoint(
1353-
ctx, q, genesisOutpoint,
1411+
// Insert the batch transaction.
1412+
err := insertMintAnchorTx(
1413+
ctx, q, genesisPacket, *batchKey, genesisOutpoint,
13541414
)
13551415
if err != nil {
1356-
return fmt.Errorf("%w: %w", ErrUpsertGenesisPoint, err)
1416+
return fmt.Errorf("unable to insert mint anchor "+
1417+
"tx: %w", err)
13571418
}
13581419

1359-
_, err = q.BindMintingBatchWithTx(ctx, BatchChainUpdate{
1360-
RawKey: rawBatchKey,
1361-
MintingTxPsbt: psbtBuf.Bytes(),
1362-
ChangeOutputIndex: sqlInt32(
1363-
genesisPacket.ChangeOutputIndex,
1364-
),
1365-
GenesisID: sqlInt64(genesisPointID),
1366-
})
1367-
return err
1420+
return nil
13681421
})
13691422
}
13701423

@@ -1501,31 +1554,22 @@ func (a *AssetMintingStore) AddSproutsToBatch(ctx context.Context,
15011554

15021555
var writeTxOpts AssetStoreTxOptions
15031556
return a.db.ExecTx(ctx, &writeTxOpts, func(q PendingAssetStore) error {
1504-
genesisPointID, _, err := upsertAssetsWithGenesis(
1557+
// Upsert the assets with genesis.
1558+
_, _, err := upsertAssetsWithGenesis(
15051559
ctx, q, genesisOutpoint, sortedAssets, nil,
15061560
)
15071561
if err != nil {
15081562
return fmt.Errorf("error inserting assets with "+
15091563
"genesis: %w", err)
15101564
}
15111565

1512-
// With all the assets inserted, we'll now update the
1513-
// corresponding batch that references all these assets with
1514-
// the genesis packet, and genesis point information.
1515-
var psbtBuf bytes.Buffer
1516-
if err := genesisPacket.Pkt.Serialize(&psbtBuf); err != nil {
1517-
return fmt.Errorf("%w: %w", ErrEncodePsbt, err)
1518-
}
1519-
_, err = q.BindMintingBatchWithTx(ctx, BatchChainUpdate{
1520-
RawKey: rawBatchKey,
1521-
MintingTxPsbt: psbtBuf.Bytes(),
1522-
ChangeOutputIndex: sqlInt32(
1523-
genesisPacket.ChangeOutputIndex,
1524-
),
1525-
GenesisID: sqlInt64(genesisPointID),
1526-
})
1566+
// Insert the batch transaction.
1567+
err = insertMintAnchorTx(
1568+
ctx, q, *genesisPacket, *batchKey, genesisOutpoint,
1569+
)
15271570
if err != nil {
1528-
return fmt.Errorf("%w: %w", ErrBindBatchTx, err)
1571+
return fmt.Errorf("unable to insert mint anchor "+
1572+
"tx: %w", err)
15291573
}
15301574

15311575
// Finally, update the batch state to BatchStateCommitted.

0 commit comments

Comments
 (0)