Skip to content

Commit 8cbf9aa

Browse files
committed
multi: remove GetTxFee(), improve fee logging
In this commit, we add extra logging of fee rates to the minter, and also remove a reimplementation of psbt.GetTxFee(). We also deduplicate some dummy PkScript generation.
1 parent 99c3e79 commit 8cbf9aa

File tree

3 files changed

+30
-48
lines changed

3 files changed

+30
-48
lines changed

tapfreighter/wallet.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ func (f *AssetWallet) AnchorVirtualTransactions(ctx context.Context,
14371437

14381438
// Before we finalize, we need to calculate the actual, final fees that
14391439
// we pay.
1440-
chainFees, err := tapgarden.GetTxFee(signedPsbt)
1440+
chainFees, err := signedPsbt.GetTxFee()
14411441
if err != nil {
14421442
return nil, fmt.Errorf("unable to get on-chain fees for psbt: "+
14431443
"%w", err)
@@ -1466,7 +1466,7 @@ func (f *AssetWallet) AnchorVirtualTransactions(ctx context.Context,
14661466
FundedPsbt: &anchorPkt,
14671467
FinalTx: finalTx,
14681468
TargetFeeRate: params.FeeRate,
1469-
ChainFees: chainFees,
1469+
ChainFees: int64(chainFees),
14701470
OutputCommitments: mergedCommitments,
14711471
}, nil
14721472
}
@@ -1717,10 +1717,15 @@ func addAnchorPsbtInputs(btcPkt *psbt.Packet, vPkt *tappsbt.VPacket,
17171717
// Earlier in adjustFundedPsbt we set wallet's change output to be the
17181718
// very last output in the transaction.
17191719
lastIdx := len(btcPkt.UnsignedTx.TxOut) - 1
1720-
currentFee := inputAmt - outputAmt
1721-
feeDelta := int64(requiredFee) - currentFee
1720+
currentFee, err := btcPkt.GetTxFee()
1721+
if err != nil {
1722+
return err
1723+
}
1724+
1725+
feeDelta := int64(requiredFee) - int64(currentFee)
17221726
changeValue := btcPkt.UnsignedTx.TxOut[lastIdx].Value
17231727

1728+
log.Infof("Current fee: %d, fee delta: %d", currentFee, feeDelta)
17241729
// The fee may exceed the total value of the change output, which means
17251730
// this spend is impossible with the given inputs and fee rate.
17261731
if changeValue-feeDelta < 0 {
@@ -1730,8 +1735,8 @@ func addAnchorPsbtInputs(btcPkt *psbt.Packet, vPkt *tappsbt.VPacket,
17301735

17311736
btcPkt.UnsignedTx.TxOut[lastIdx].Value -= feeDelta
17321737

1733-
log.Infof("Adjusting send pkt by delta of %v from %d sats to %d sats",
1734-
feeDelta, currentFee, requiredFee)
1738+
log.Infof("Adjusting send pkt fee by delta of %d from %d sats to %d "+
1739+
"sats", feeDelta, currentFee, requiredFee)
17351740

17361741
return nil
17371742
}

tapgarden/caretaker.go

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ import (
3030
)
3131

3232
var (
33-
34-
// DummyGenesisTxOut is the dummy TxOut we'll place in the PSBt funding
35-
// request to make sure we leave enough room for change and fees.
36-
DummyGenesisTxOut = wire.TxOut{
37-
PkScript: tapscript.GenesisDummyScript[:],
38-
Value: int64(GenesisAmtSats),
39-
}
40-
4133
// ErrGroupKeyUnknown is an error returned if an asset has a group key
4234
// attached that has not been previously verified.
4335
ErrGroupKeyUnknown = errors.New("group key not known")
@@ -402,7 +394,7 @@ func (b *BatchCaretaker) fundGenesisPsbt(ctx context.Context) (*FundedPsbt, erro
402394
b.batchKey[:])
403395

404396
txTemplate := wire.NewMsgTx(2)
405-
txTemplate.AddTxOut(&DummyGenesisTxOut)
397+
txTemplate.AddTxOut(tapscript.CreateDummyOutput())
406398
genesisPkt, err := psbt.NewFromUnsignedTx(txTemplate)
407399
if err != nil {
408400
return nil, fmt.Errorf("unable to make psbt packet: %w", err)
@@ -411,26 +403,28 @@ func (b *BatchCaretaker) fundGenesisPsbt(ctx context.Context) (*FundedPsbt, erro
411403
log.Infof("BatchCaretaker(%x): creating skeleton PSBT", b.batchKey[:])
412404
log.Tracef("PSBT: %v", spew.Sdump(genesisPkt))
413405

414-
// If a fee rate was manually assigned for this batch, use that instead
415-
// of a fee rate estimate.
416406
var feeRate chainfee.SatPerKWeight
417407
switch {
408+
// If a fee rate was manually assigned for this batch, use that instead
409+
// of a fee rate estimate.
418410
case b.cfg.BatchFeeRate != nil:
419411
feeRate = *b.cfg.BatchFeeRate
420-
log.Infof("BatchCaretaker(%x): using manual fee rate",
421-
b.batchKey[:])
412+
log.Infof("BatchCaretaker(%x): using manual fee rate: %s, %d "+
413+
"sat/vB", b.batchKey[:], feeRate.String(),
414+
feeRate.FeePerKVByte()/1000)
422415

423416
default:
424417
feeRate, err = b.cfg.ChainBridge.EstimateFee(
425418
ctx, GenesisConfTarget,
426419
)
427420
if err != nil {
428-
return nil, fmt.Errorf("unable to estimate fee: %w", err)
421+
return nil, fmt.Errorf("unable to estimate fee: %w",
422+
err)
429423
}
430-
}
431424

432-
log.Infof("BatchCaretaker(%x): using fee rate: %v",
433-
b.batchKey[:], feeRate.FeePerKVByte().String())
425+
log.Infof("BatchCaretaker(%x): estimated fee rate: %s",
426+
b.batchKey[:], feeRate.FeePerKVByte().String())
427+
}
434428

435429
fundedGenesisPkt, err := b.cfg.Wallet.FundPsbt(
436430
ctx, genesisPkt, 1, feeRate,
@@ -755,17 +749,15 @@ func (b *BatchCaretaker) stateStep(currentState BatchState) (BatchState, error)
755749
b.cfg.Batch.GenesisPacket.Pkt = signedPkt
756750

757751
// Populate how much this tx paid in on-chain fees.
758-
chainFees, err := GetTxFee(signedPkt)
752+
chainFees, err := signedPkt.GetTxFee()
759753
if err != nil {
760754
return 0, fmt.Errorf("unable to get on-chain fees "+
761755
"for psbt: %w", err)
762756
}
763-
b.cfg.Batch.GenesisPacket.ChainFees = chainFees
757+
b.cfg.Batch.GenesisPacket.ChainFees = int64(chainFees)
764758

765-
log.Infof("BatchCaretaker(%x): GenesisPacket absolute fee: "+
766-
"%d sats", b.batchKey[:], chainFees)
767-
log.Infof("BatchCaretaker(%x): GenesisPacket finalized",
768-
b.batchKey[:])
759+
log.Infof("BatchCaretaker(%x): GenesisPacket finalized "+
760+
"(absolute_fee_sats: %d)", b.batchKey[:], chainFees)
769761
log.Tracef("GenesisPacket: %v", spew.Sdump(signedPkt))
770762

771763
// At this point we have a fully signed PSBT packet which'll
@@ -1344,21 +1336,6 @@ func SortAssets(fullAssets []*asset.Asset,
13441336
return anchorAssets, nonAnchorAssets, nil
13451337
}
13461338

1347-
// GetTxFee returns the value of the on-chain fees paid by a finalized PSBT.
1348-
func GetTxFee(pkt *psbt.Packet) (int64, error) {
1349-
inputValue, err := psbt.SumUtxoInputValues(pkt)
1350-
if err != nil {
1351-
return 0, fmt.Errorf("unable to sum input values: %v", err)
1352-
}
1353-
1354-
outputValue := int64(0)
1355-
for _, out := range pkt.UnsignedTx.TxOut {
1356-
outputValue += out.Value
1357-
}
1358-
1359-
return inputValue - outputValue, nil
1360-
}
1361-
13621339
// GenHeaderVerifier generates a block header on-chain verification callback
13631340
// function given a chain bridge.
13641341
func GenHeaderVerifier(ctx context.Context,

tapscript/send.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ var (
115115
)
116116
)
117117

118-
// createDummyOutput creates a new Bitcoin transaction output that is later
118+
// CreateDummyOutput creates a new Bitcoin transaction output that is later
119119
// used to embed a Taproot Asset commitment.
120-
func createDummyOutput() *wire.TxOut {
120+
func CreateDummyOutput() *wire.TxOut {
121121
// The dummy PkScript is the same size as an encoded P2TR output and has
122122
// a valid P2TR prefix.
123123
newOutput := wire.TxOut{
124124
Value: int64(DummyAmtSats),
125-
PkScript: GenesisDummyScript,
125+
PkScript: bytes.Clone(GenesisDummyScript),
126126
}
127127
return &newOutput
128128
}
@@ -1029,7 +1029,7 @@ func CreateAnchorTx(outputs []*tappsbt.VOutput) (*psbt.Packet, error) {
10291029

10301030
txTemplate := wire.NewMsgTx(2)
10311031
for i := uint32(0); i < maxOutputIndex; i++ {
1032-
txTemplate.AddTxOut(createDummyOutput())
1032+
txTemplate.AddTxOut(CreateDummyOutput())
10331033
}
10341034

10351035
spendPkt, err := psbt.NewFromUnsignedTx(txTemplate)

0 commit comments

Comments
 (0)