sweep: rename fee() to feeWithParent() for clarity

To make sure the caller has a better idea about what this method is
doing.
This commit is contained in:
yyforyongyu 2024-03-21 06:31:08 +08:00
parent 370e4ce98d
commit 59fbcb18d5
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
5 changed files with 24 additions and 19 deletions

View file

@ -222,7 +222,7 @@ func (t *txInputSet) enoughInput() bool {
// We did not have enough input for a change output. Check if we have // We did not have enough input for a change output. Check if we have
// enough input to pay the fees for a transaction with no change // enough input to pay the fees for a transaction with no change
// output. // output.
fee := t.weightEstimate(false).fee() fee := t.weightEstimate(false).feeWithParent()
if t.inputTotal < t.requiredOutput+fee { if t.inputTotal < t.requiredOutput+fee {
return false return false
} }
@ -289,7 +289,7 @@ func (t *txInputSet) addToState(inp input.Input,
newSet.inputTotal += value newSet.inputTotal += value
// Recalculate the tx fee. // Recalculate the tx fee.
fee := newSet.weightEstimate(true).fee() fee := newSet.weightEstimate(true).feeWithParent()
// Calculate the new output value. // Calculate the new output value.
if reqOut != nil { if reqOut != nil {

View file

@ -35,7 +35,7 @@ func TestTxInputSet(t *testing.T) {
t.Fatal("expected add of positively yielding input to succeed") t.Fatal("expected add of positively yielding input to succeed")
} }
fee := set.weightEstimate(true).fee() fee := set.weightEstimate(true).feeWithParent()
require.Equal(t, btcutil.Amount(487), fee) require.Equal(t, btcutil.Amount(487), fee)
// The tx output should now be 700-487 = 213 sats. The dust limit isn't // The tx output should now be 700-487 = 213 sats. The dust limit isn't
@ -164,13 +164,13 @@ func TestTxInputSetRequiredOutput(t *testing.T) {
require.True(t, set.add(inp, constraintsRegular), "failed adding input") require.True(t, set.add(inp, constraintsRegular), "failed adding input")
// The fee needed to pay for this input and output should be 439 sats. // The fee needed to pay for this input and output should be 439 sats.
fee := set.weightEstimate(false).fee() fee := set.weightEstimate(false).feeWithParent()
require.Equal(t, btcutil.Amount(439), fee) require.Equal(t, btcutil.Amount(439), fee)
// Since the tx set currently pays no fees, we expect the current // Since the tx set currently pays no fees, we expect the current
// change to actually be negative, since this is what it would cost us // change to actually be negative, since this is what it would cost us
// in fees to add a change output. // in fees to add a change output.
feeWithChange := set.weightEstimate(true).fee() feeWithChange := set.weightEstimate(true).feeWithParent()
if set.changeOutput != -feeWithChange { if set.changeOutput != -feeWithChange {
t.Fatalf("expected negative change of %v, had %v", t.Fatalf("expected negative change of %v, had %v",
-feeWithChange, set.changeOutput) -feeWithChange, set.changeOutput)
@ -188,9 +188,10 @@ func TestTxInputSetRequiredOutput(t *testing.T) {
// Now we add a an input that is large enough to pay the fee for the // Now we add a an input that is large enough to pay the fee for the
// transaction without a change output, but not large enough to afford // transaction without a change output, but not large enough to afford
// adding a change output. // adding a change output.
extraInput1 := weight.fee() + 100 extraInput1 := weight.feeWithParent() + 100
require.True(t, set.add(createP2WKHInput(extraInput1), constraintsRegular), require.True(t, set.add(
"expected add of positively yielding input to succeed") createP2WKHInput(extraInput1), constraintsRegular,
), "expected add of positively yielding input to succeed")
// The change should be negative, since we would have to add a change // The change should be negative, since we would have to add a change
// output, which we cannot yet afford. // output, which we cannot yet afford.
@ -208,10 +209,12 @@ func TestTxInputSetRequiredOutput(t *testing.T) {
require.NoError(t, weight.add(dummyInput)) require.NoError(t, weight.add(dummyInput))
// We add what is left to reach this value. // We add what is left to reach this value.
extraInput2 := weight.fee() - extraInput1 + 100 extraInput2 := weight.feeWithParent() - extraInput1 + 100
// Add this input, which should result in the change now being 100 sats. // Add this input, which should result in the change now being 100 sats.
require.True(t, set.add(createP2WKHInput(extraInput2), constraintsRegular)) require.True(t, set.add(
createP2WKHInput(extraInput2), constraintsRegular,
))
// The change should be 100, since this is what is left after paying // The change should be 100, since this is what is left after paying
// fees in case of a change output. // fees in case of a change output.
@ -232,7 +235,7 @@ func TestTxInputSetRequiredOutput(t *testing.T) {
// We expect the change to everything that is left after paying the tx // We expect the change to everything that is left after paying the tx
// fee. // fee.
extraInput3 := weight.fee() - extraInput1 - extraInput2 + 1000 extraInput3 := weight.feeWithParent() - extraInput1 - extraInput2 + 1000
require.True(t, set.add(createP2WKHInput(extraInput3), constraintsRegular)) require.True(t, set.add(createP2WKHInput(extraInput3), constraintsRegular))
change = set.changeOutput change = set.changeOutput

View file

@ -44,7 +44,7 @@ func createSweepTx(inputs []input.Input, outputs []*wire.TxOut,
return nil, 0, err return nil, 0, err
} }
txFee := estimator.fee() txFee := estimator.feeWithParent()
var ( var (
// Create the sweep transaction that we will be building. We // Create the sweep transaction that we will be building. We

View file

@ -106,9 +106,9 @@ func (w *weightEstimator) weight() int {
return w.estimator.Weight() return w.estimator.Weight()
} }
// fee returns the tx fee to use for the aggregated inputs and outputs, taking // feeWithParent returns the tx fee to use for the aggregated inputs and
// into account unconfirmed parent transactions (cpfp). // outputs, taking into account unconfirmed parent transactions (cpfp).
func (w *weightEstimator) fee() btcutil.Amount { func (w *weightEstimator) feeWithParent() btcutil.Amount {
// Calculate fee and weight for just this tx. // Calculate fee and weight for just this tx.
childWeight := int64(w.estimator.Weight()) childWeight := int64(w.estimator.Weight())

View file

@ -30,7 +30,8 @@ func TestWeightEstimator(t *testing.T) {
// The expectations is that this input is added. // The expectations is that this input is added.
const expectedWeight1 = 322 const expectedWeight1 = 322
require.Equal(t, expectedWeight1, w.weight()) require.Equal(t, expectedWeight1, w.weight())
require.Equal(t, testFeeRate.FeeForWeight(expectedWeight1), w.fee()) require.Equal(t, testFeeRate.FeeForWeight(expectedWeight1),
w.feeWithParent())
// Define a parent transaction that pays a fee of 30000 sat/kw. // Define a parent transaction that pays a fee of 30000 sat/kw.
parentTxHighFee := &input.TxInfo{ parentTxHighFee := &input.TxInfo{
@ -51,7 +52,8 @@ func TestWeightEstimator(t *testing.T) {
// rate than the child. We expect no additional fee on the child. // rate than the child. We expect no additional fee on the child.
const expectedWeight2 = expectedWeight1 + 280 const expectedWeight2 = expectedWeight1 + 280
require.Equal(t, expectedWeight2, w.weight()) require.Equal(t, expectedWeight2, w.weight())
require.Equal(t, testFeeRate.FeeForWeight(expectedWeight2), w.fee()) require.Equal(t, testFeeRate.FeeForWeight(expectedWeight2),
w.feeWithParent())
// Define a parent transaction that pays a fee of 10000 sat/kw. // Define a parent transaction that pays a fee of 10000 sat/kw.
parentTxLowFee := &input.TxInfo{ parentTxLowFee := &input.TxInfo{
@ -78,7 +80,7 @@ func TestWeightEstimator(t *testing.T) {
expectedWeight3+parentTxLowFee.Weight, expectedWeight3+parentTxLowFee.Weight,
) - parentTxLowFee.Fee ) - parentTxLowFee.Fee
require.Equal(t, expectedFee, w.fee()) require.Equal(t, expectedFee, w.feeWithParent())
} }
// TestWeightEstimatorMaxFee tests that the weight estimator correctly caps the // TestWeightEstimatorMaxFee tests that the weight estimator correctly caps the
@ -118,7 +120,7 @@ func TestWeightEstimatorMaxFee(t *testing.T) {
// //
// Thus we cap at the maxFee. // Thus we cap at the maxFee.
expectedFee := maxFeeRate.FeeForWeight(childWeight) expectedFee := maxFeeRate.FeeForWeight(childWeight)
require.Equal(t, expectedFee, w.fee()) require.Equal(t, expectedFee, w.feeWithParent())
} }
// TestWeightEstimatorAddOutput tests that adding the raw P2WKH output to the // TestWeightEstimatorAddOutput tests that adding the raw P2WKH output to the