Skip to content

Commit 215fc5f

Browse files
committed
tapdb: extract upsertSupplyPreCommit from maybeUpsertSupplyPreCommit
Refactor the code by separating the core upsertSupplyPreCommit logic from maybeUpsertSupplyPreCommit. This makes the upsert operation reusable.
1 parent d81d3a9 commit 215fc5f

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

tapdb/universe.go

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -611,52 +611,27 @@ func shouldInsertPreCommit(proofType universe.ProofType,
611611
return true
612612
}
613613

614-
// maybeUpsertSupplyPreCommit inserts a supply pre-commitment output if the
615-
// asset group supports supply commitments and this is an issuance proof.
616-
func maybeUpsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
617-
proofType universe.ProofType, issuanceProof proof.Proof,
618-
metaReveal *proof.MetaReveal) error {
614+
// upsertSupplyPreCommit upserts a supply pre-commitment into the database.
615+
func upsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
616+
preCommit supplycommit.PreCommitment) error {
619617

620-
if !shouldInsertPreCommit(proofType, issuanceProof, metaReveal) {
621-
return nil
622-
}
623-
624-
delegationKey, err := metaReveal.DelegationKey.UnwrapOrErr(
625-
errors.New("missing delegation key"),
626-
)
627-
if err != nil {
628-
return err
629-
}
630-
631-
preCommitOutput, err := supplycommit.NewPreCommitFromProof(
632-
issuanceProof, delegationKey,
633-
)
634-
if err != nil {
635-
return fmt.Errorf("unable to extract pre-commit "+
636-
"output: %w", err)
637-
}
618+
// Encode the group key, taproot internal key, and pre-commit outpoint.
619+
groupKeyBytes := schnorr.SerializePubKey(&preCommit.GroupPubKey)
620+
taprootInternalKeyBytes :=
621+
preCommit.InternalKey.PubKey.SerializeCompressed()
638622

639-
outPointBytes, err := encodeOutpoint(preCommitOutput.OutPoint())
623+
outPointBytes, err := encodeOutpoint(preCommit.OutPoint())
640624
if err != nil {
641625
return fmt.Errorf("unable to encode supply pre-commit "+
642626
"outpoint: %w", err)
643627
}
644628

645-
// Upsert the supply pre-commitment output.
646-
//
647-
// Encode the group key and taproot internal key.
648-
groupKeyBytes := schnorr.SerializePubKey(
649-
&issuanceProof.Asset.GroupKey.GroupPubKey,
650-
)
651-
taprootInternalKeyBytes :=
652-
preCommitOutput.InternalKey.PubKey.SerializeCompressed()
653-
654629
// Try to fetch an existing chain tx row from the database. We fetch
655630
// first instead of blindly upserting to avoid overwriting existing data
656631
// with null values.
657632
var chainTxDbID fn.Option[int64]
658633

659-
txIDBytes := fn.ByteSlice(issuanceProof.AnchorTx.TxHash())
634+
txIDBytes := fn.ByteSlice(preCommit.MintingTxn.TxHash())
660635
chainTxn, err := dbTx.FetchChainTx(ctx, txIDBytes)
661636
switch {
662637
case errors.Is(err, sql.ErrNoRows):
@@ -677,17 +652,15 @@ func maybeUpsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
677652
// If we didn't find an existing chain tx, then we'll insert a new
678653
// one now.
679654
if chainTxDbID.IsNone() {
680-
blockHash := issuanceProof.BlockHeader.BlockHash()
681-
txBytes, err := fn.Serialize(&issuanceProof.AnchorTx)
655+
txBytes, err := fn.Serialize(preCommit.MintingTxn)
682656
if err != nil {
683657
return fmt.Errorf("failed to serialize tx: %w", err)
684658
}
685659

686660
txDbID, err := dbTx.UpsertChainTx(ctx, ChainTxParams{
687661
Txid: txIDBytes,
688662
RawTx: txBytes,
689-
BlockHeight: sqlInt32(issuanceProof.BlockHeight),
690-
BlockHash: blockHash.CloneBytes(),
663+
BlockHeight: sqlInt32(preCommit.BlockHeight),
691664
})
692665
if err != nil {
693666
return fmt.Errorf("unable to upsert chain tx: %w", err)
@@ -718,6 +691,39 @@ func maybeUpsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
718691
return nil
719692
}
720693

694+
// maybeUpsertSupplyPreCommit inserts a supply pre-commitment output if the
695+
// asset group supports supply commitments and this is an issuance proof.
696+
func maybeUpsertSupplyPreCommit(ctx context.Context, dbTx UpsertAssetStore,
697+
proofType universe.ProofType, issuanceProof proof.Proof,
698+
metaReveal *proof.MetaReveal) error {
699+
700+
if !shouldInsertPreCommit(proofType, issuanceProof, metaReveal) {
701+
return nil
702+
}
703+
704+
delegationKey, err := metaReveal.DelegationKey.UnwrapOrErr(
705+
errors.New("missing delegation key"),
706+
)
707+
if err != nil {
708+
return err
709+
}
710+
711+
preCommit, err := supplycommit.NewPreCommitFromProof(
712+
issuanceProof, delegationKey,
713+
)
714+
if err != nil {
715+
return fmt.Errorf("unable to extract pre-commit "+
716+
"output: %w", err)
717+
}
718+
719+
err = upsertSupplyPreCommit(ctx, dbTx, preCommit)
720+
if err != nil {
721+
return fmt.Errorf("unable to upsert supply pre-commit: %w", err)
722+
}
723+
724+
return nil
725+
}
726+
721727
// UpsertProofLeaf inserts or updates a proof leaf within the universe tree,
722728
// stored at the base key. The metaReveal type is purely optional, and should be
723729
// specified if the genesis proof committed to a non-zero meta hash.

0 commit comments

Comments
 (0)