github.com/decred/dcrlnd@v0.7.6/sweep/weight_estimator_test.go (about)

     1  package sweep
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrd/chaincfg/v3"
     7  	"github.com/decred/dcrd/txscript/v4/stdaddr"
     8  	"github.com/decred/dcrd/wire"
     9  	"github.com/decred/dcrlnd/input"
    10  	"github.com/decred/dcrlnd/lnwallet/chainfee"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  // TestWeightEstimator tests weight estimation for inputs with and without
    15  // unconfirmed parents.
    16  func TestWeightEstimator(t *testing.T) {
    17  	testFeeRate := chainfee.AtomPerKByte(20000)
    18  
    19  	w := newSizeEstimator(testFeeRate)
    20  
    21  	// Add an input without unconfirmed parent tx.
    22  	input1 := input.MakeBaseInput(
    23  		&wire.OutPoint{}, input.CommitmentAnchor,
    24  		&input.SignDescriptor{}, 0, nil,
    25  	)
    26  
    27  	require.NoError(t, w.add(&input1))
    28  
    29  	// The expectations is that this input is added.
    30  	const expectedWeight1 = 12 + 57 + 1 + 115 + 3
    31  	require.Equal(t, expectedWeight1, w.size())
    32  	require.Equal(t, testFeeRate.FeeForSize(expectedWeight1), w.fee())
    33  
    34  	// Define a parent transaction that pays a fee of 30000 sat/kw.
    35  	parentTxHighFee := &input.TxInfo{
    36  		Size: 100,
    37  		Fee:  3000,
    38  	}
    39  
    40  	// Add an output of the parent tx above.
    41  	input2 := input.MakeBaseInput(
    42  		&wire.OutPoint{}, input.CommitmentAnchor,
    43  		&input.SignDescriptor{}, 0,
    44  		parentTxHighFee,
    45  	)
    46  
    47  	require.NoError(t, w.add(&input2))
    48  
    49  	// Pay for parent isn't possible because the parent pays a higher fee
    50  	// rate than the child. We expect no additional fee on the child.
    51  	const expectedWeight2 = expectedWeight1 + 57 + 1 + 115
    52  	require.Equal(t, expectedWeight2, w.size())
    53  	require.Equal(t, testFeeRate.FeeForSize(expectedWeight2), w.fee())
    54  
    55  	// Define a parent transaction that pays a fee of 10000 sat/kw.
    56  	parentTxLowFee := &input.TxInfo{
    57  		Size: 100,
    58  		Fee:  1000,
    59  	}
    60  
    61  	// Add an output of the low-fee parent tx above.
    62  	input3 := input.MakeBaseInput(
    63  		&wire.OutPoint{}, input.CommitmentAnchor,
    64  		&input.SignDescriptor{}, 0,
    65  		parentTxLowFee,
    66  	)
    67  	require.NoError(t, w.add(&input3))
    68  
    69  	// Expect the weight to increase because of the third input.
    70  	const expectedWeight3 = expectedWeight2 + 57 + 1 + 115
    71  	require.Equal(t, expectedWeight3, w.size())
    72  
    73  	// Expect the fee to cover the child and the parent transaction at 20
    74  	// sat/kw after subtraction of the fee that was already paid by the
    75  	// parent.
    76  	expectedFee := testFeeRate.FeeForSize(
    77  		expectedWeight3+parentTxLowFee.Size,
    78  	) - parentTxLowFee.Fee
    79  
    80  	require.Equal(t, expectedFee, w.fee())
    81  }
    82  
    83  // TestWeightEstimatorAddOutput tests that adding the raw P2WKH output to the
    84  // estimator yield the same result as an estimated add.
    85  func TestWeightEstimatorAddOutput(t *testing.T) {
    86  	testFeeRate := chainfee.AtomPerKByte(20000)
    87  
    88  	p2pkhAddr, err := stdaddr.NewAddressPubKeyHashEcdsaSecp256k1(
    89  		0, make([]byte, 20), chaincfg.MainNetParams(),
    90  	)
    91  	require.NoError(t, err)
    92  
    93  	version, p2pkhScript := p2pkhAddr.PaymentScript()
    94  
    95  	// Create two estimators, add the raw P2WKH out to one.
    96  	txOut := &wire.TxOut{
    97  		Version:  version,
    98  		PkScript: p2pkhScript,
    99  		Value:    10000,
   100  	}
   101  
   102  	w1 := newSizeEstimator(testFeeRate)
   103  	w1.addOutput(txOut)
   104  
   105  	w2 := newSizeEstimator(testFeeRate)
   106  	w2.addP2PKHOutput()
   107  
   108  	// Estimate hhould be the same.
   109  	require.Equal(t, w1.size(), w2.size())
   110  }