|
| 1 | +package itest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/btcsuite/btcd/btcutil" |
| 7 | + "github.com/btcsuite/btcd/wire" |
| 8 | + "github.com/lightninglabs/taproot-assets/taprpc" |
| 9 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 10 | + "github.com/lightningnetwork/lnd/lnwallet/chainfee" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// testFeeEstimation tests that we're able to spend outputs of various script |
| 15 | +// types, and that the fee estimator and TX size estimator used during asset |
| 16 | +// transfers are accurate. |
| 17 | +func testFeeEstimation(t *harnessTest) { |
| 18 | + var ( |
| 19 | + // Make a ladder of UTXO values so use order is deterministic. |
| 20 | + anchorAmounts = []int64{10000, 9990, 9980, 9970} |
| 21 | + |
| 22 | + // The default feerate in the itests is 12.5 sat/vB, but we |
| 23 | + // define it here explicitly to use for assertions. |
| 24 | + defaultFeeRate = chainfee.SatPerKWeight(3125) |
| 25 | + higherFeeRate = defaultFeeRate * 2 |
| 26 | + excessiveFeeRate = defaultFeeRate * 8 |
| 27 | + lowFeeRate = chainfee.SatPerKWeight(500) |
| 28 | + |
| 29 | + // We will mint assets using the largest NP2WKH output, and then |
| 30 | + // use all three output types for transfers. |
| 31 | + initialUTXOs = []*UTXORequest{ |
| 32 | + { |
| 33 | + Type: lnrpc.AddressType_NESTED_PUBKEY_HASH, |
| 34 | + Amount: anchorAmounts[0], |
| 35 | + }, |
| 36 | + { |
| 37 | + Type: lnrpc.AddressType_NESTED_PUBKEY_HASH, |
| 38 | + Amount: anchorAmounts[1], |
| 39 | + }, |
| 40 | + { |
| 41 | + Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH, |
| 42 | + Amount: anchorAmounts[2], |
| 43 | + }, |
| 44 | + { |
| 45 | + Type: lnrpc.AddressType_TAPROOT_PUBKEY, |
| 46 | + Amount: anchorAmounts[3], |
| 47 | + }, |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + ctxb := context.Background() |
| 52 | + ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout) |
| 53 | + defer cancel() |
| 54 | + |
| 55 | + // Set the initial state of the wallet of the first node. The wallet |
| 56 | + // state will reset at the end of this test. |
| 57 | + SetNodeUTXOs(t, t.lndHarness.Alice, btcutil.Amount(1), initialUTXOs) |
| 58 | + defer ResetNodeWallet(t, t.lndHarness.Alice) |
| 59 | + |
| 60 | + // Mint some assets with a NP2WPKH input, which will give us an anchor |
| 61 | + // output to spend for a transfer. |
| 62 | + rpcAssets := MintAssetsConfirmBatch( |
| 63 | + t.t, t.lndHarness.Miner.Client, t.tapd, simpleAssets, |
| 64 | + ) |
| 65 | + |
| 66 | + // Check the final fee rate of the mint TX. |
| 67 | + rpcMintOutpoint := rpcAssets[0].ChainAnchor.AnchorOutpoint |
| 68 | + mintOutpoint, err := wire.NewOutPointFromString(rpcMintOutpoint) |
| 69 | + require.NoError(t.t, err) |
| 70 | + |
| 71 | + // We check the minting TX with a rounded fee rate as the minter does |
| 72 | + // not adjust the fee rate of the TX after it was funded by our backing |
| 73 | + // wallet. |
| 74 | + AssertFeeRate( |
| 75 | + t.t, t.lndHarness.Miner.Client, anchorAmounts[0], |
| 76 | + &mintOutpoint.Hash, defaultFeeRate, true, |
| 77 | + ) |
| 78 | + |
| 79 | + // Split the normal asset to create a transfer with two anchor outputs. |
| 80 | + normalAssetId := rpcAssets[0].AssetGenesis.AssetId |
| 81 | + splitAmount := rpcAssets[0].Amount / 2 |
| 82 | + addr, err := t.tapd.NewAddr( |
| 83 | + ctxt, &taprpc.NewAddrRequest{ |
| 84 | + AssetId: normalAssetId, |
| 85 | + Amt: splitAmount, |
| 86 | + }, |
| 87 | + ) |
| 88 | + require.NoError(t.t, err) |
| 89 | + |
| 90 | + AssertAddrCreated(t.t, t.tapd, rpcAssets[0], addr) |
| 91 | + sendResp := sendAssetsToAddr(t, t.tapd, addr) |
| 92 | + |
| 93 | + transferIdx := 0 |
| 94 | + ConfirmAndAssertOutboundTransfer( |
| 95 | + t.t, t.lndHarness.Miner.Client, t.tapd, sendResp, normalAssetId, |
| 96 | + []uint64{splitAmount, splitAmount}, transferIdx, transferIdx+1, |
| 97 | + ) |
| 98 | + transferIdx += 1 |
| 99 | + AssertNonInteractiveRecvComplete(t.t, t.tapd, transferIdx) |
| 100 | + |
| 101 | + sendInputAmt := anchorAmounts[1] + 1000 |
| 102 | + AssertTransferFeeRate( |
| 103 | + t.t, t.lndHarness.Miner.Client, sendResp, sendInputAmt, |
| 104 | + defaultFeeRate, false, |
| 105 | + ) |
| 106 | + |
| 107 | + // Double the fee rate to 25 sat/vB before performing another transfer. |
| 108 | + t.lndHarness.SetFeeEstimateWithConf(higherFeeRate, 6) |
| 109 | + |
| 110 | + secondSplitAmount := splitAmount / 2 |
| 111 | + addr2, err := t.tapd.NewAddr( |
| 112 | + ctxt, &taprpc.NewAddrRequest{ |
| 113 | + AssetId: normalAssetId, |
| 114 | + Amt: secondSplitAmount, |
| 115 | + }, |
| 116 | + ) |
| 117 | + require.NoError(t.t, err) |
| 118 | + |
| 119 | + AssertAddrCreated(t.t, t.tapd, rpcAssets[0], addr2) |
| 120 | + sendResp = sendAssetsToAddr(t, t.tapd, addr2) |
| 121 | + |
| 122 | + ConfirmAndAssertOutboundTransfer( |
| 123 | + t.t, t.lndHarness.Miner.Client, t.tapd, sendResp, normalAssetId, |
| 124 | + []uint64{secondSplitAmount, secondSplitAmount}, |
| 125 | + transferIdx, transferIdx+1, |
| 126 | + ) |
| 127 | + transferIdx += 1 |
| 128 | + AssertNonInteractiveRecvComplete(t.t, t.tapd, transferIdx) |
| 129 | + |
| 130 | + sendInputAmt = anchorAmounts[2] + 1000 |
| 131 | + AssertTransferFeeRate( |
| 132 | + t.t, t.lndHarness.Miner.Client, sendResp, sendInputAmt, |
| 133 | + higherFeeRate, false, |
| 134 | + ) |
| 135 | + |
| 136 | + // If we quadruple the fee rate, the freighter should fail during input |
| 137 | + // selection. |
| 138 | + t.lndHarness.SetFeeEstimateWithConf(excessiveFeeRate, 6) |
| 139 | + |
| 140 | + thirdSplitAmount := splitAmount / 4 |
| 141 | + addr3, err := t.tapd.NewAddr( |
| 142 | + ctxt, &taprpc.NewAddrRequest{ |
| 143 | + AssetId: normalAssetId, |
| 144 | + Amt: thirdSplitAmount, |
| 145 | + }, |
| 146 | + ) |
| 147 | + require.NoError(t.t, err) |
| 148 | + |
| 149 | + AssertAddrCreated(t.t, t.tapd, rpcAssets[0], addr3) |
| 150 | + _, err = t.tapd.SendAsset(ctxt, &taprpc.SendAssetRequest{ |
| 151 | + TapAddrs: []string{addr3.Encoded}, |
| 152 | + }) |
| 153 | + require.ErrorContains(t.t, err, "insufficient funds available") |
| 154 | + |
| 155 | + // The transfer should also be rejected if the manually-specified |
| 156 | + // feerate fails the sanity check against the fee estimator's fee floor |
| 157 | + // of 253 sat/kw, or 1.012 sat/vB. |
| 158 | + _, err = t.tapd.SendAsset(ctxt, &taprpc.SendAssetRequest{ |
| 159 | + TapAddrs: []string{addr3.Encoded}, |
| 160 | + FeeRate: uint32(chainfee.FeePerKwFloor) - 1, |
| 161 | + }) |
| 162 | + require.ErrorContains(t.t, err, "manual fee rate below floor") |
| 163 | + // After failure at the high feerate, we should still be able to make a |
| 164 | + // transfer at a very low feerate. |
| 165 | + t.lndHarness.SetFeeEstimateWithConf(lowFeeRate, 6) |
| 166 | + sendResp = sendAssetsToAddr(t, t.tapd, addr3) |
| 167 | + |
| 168 | + ConfirmAndAssertOutboundTransfer( |
| 169 | + t.t, t.lndHarness.Miner.Client, t.tapd, sendResp, normalAssetId, |
| 170 | + []uint64{thirdSplitAmount, thirdSplitAmount}, |
| 171 | + transferIdx, transferIdx+1, |
| 172 | + ) |
| 173 | + transferIdx += 1 |
| 174 | + AssertNonInteractiveRecvComplete(t.t, t.tapd, transferIdx) |
| 175 | + |
| 176 | + sendInputAmt = anchorAmounts[3] + 1000 |
| 177 | + AssertTransferFeeRate( |
| 178 | + t.t, t.lndHarness.Miner.Client, sendResp, sendInputAmt, |
| 179 | + lowFeeRate, false, |
| 180 | + ) |
| 181 | +} |
0 commit comments