multi: make sure CPFP won't exceed max allowed fee rate

This commit updates the `fee()` method in `weightEstimator` to make sure
when doing CPFP we are not exceeding the max allowed fee rate. In order
to use the max fee rate, we need to modify several methods to pass the
configured value to the estimator.
This commit is contained in:
yyforyongyu
2023-08-22 11:06:51 +08:00
committed by Olaoluwa Osuntokun
parent 42ff52bbfb
commit 24fa35ec80
12 changed files with 132 additions and 48 deletions

View File

@@ -17,7 +17,7 @@ import (
func TestWeightEstimator(t *testing.T) {
testFeeRate := chainfee.SatPerKWeight(20000)
w := newWeightEstimator(testFeeRate)
w := newWeightEstimator(testFeeRate, 0)
// Add an input without unconfirmed parent tx.
input1 := input.MakeBaseInput(
@@ -81,6 +81,46 @@ func TestWeightEstimator(t *testing.T) {
require.Equal(t, expectedFee, w.fee())
}
// TestWeightEstimatorMaxFee tests that the weight estimator correctly caps the
// fee at the maximum allowed fee.
func TestWeightEstimatorMaxFee(t *testing.T) {
t.Parallel()
testFeeRate := chainfee.SatPerKWeight(9_000)
maxFeeRate := chainfee.SatPerKWeight(10_000)
w := newWeightEstimator(testFeeRate, maxFeeRate)
// Define a parent transaction that pays a fee of 1000 sat/kw.
parentTxLowFee := &input.TxInfo{
Weight: 100,
Fee: 100,
}
// Add an output of the low-fee parent tx above.
childInput := input.MakeBaseInput(
&wire.OutPoint{}, input.CommitmentAnchor,
&input.SignDescriptor{}, 0, parentTxLowFee,
)
require.NoError(t, w.add(&childInput))
// The child weight should be 322 weight uints.
const childWeight = 322
require.Equal(t, childWeight, w.weight())
// Check the fee is capped at the maximum allowed fee. The
// calculations,
//
// totalWeight = childWeight + parentWeight = 422
// fee = totalWeight * testFeeRate - parentsFee =
// 422 * 9_000 / 1000 - 100 = 3598
// maxFee = childWeight * maxFeeRate = 422 * 10_000 / 1000 = 3220
//
// Thus we cap at the maxFee.
expectedFee := maxFeeRate.FeeForWeight(childWeight)
require.Equal(t, expectedFee, w.fee())
}
// TestWeightEstimatorAddOutput tests that adding the raw P2WKH output to the
// estimator yield the same result as an estimated add.
func TestWeightEstimatorAddOutput(t *testing.T) {
@@ -100,10 +140,10 @@ func TestWeightEstimatorAddOutput(t *testing.T) {
Value: 10000,
}
w1 := newWeightEstimator(testFeeRate)
w1 := newWeightEstimator(testFeeRate, 0)
w1.addOutput(txOut)
w2 := newWeightEstimator(testFeeRate)
w2 := newWeightEstimator(testFeeRate, 0)
w2.addP2WKHOutput()
// Estimate hhould be the same.