Skip to content

Commit

Permalink
itest: test min relay fee bump
Browse files Browse the repository at this point in the history
The `testMinRelayFeeBump` itest checks that the minting transaction and
a basic send obtain a fee bump when the min relay fee is increased to a
value that is higher than the fee estimation.

fix
  • Loading branch information
gijswijs committed Nov 14, 2024
1 parent a843f4f commit 454ea22
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
121 changes: 121 additions & 0 deletions itest/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/tapfreighter"
Expand All @@ -17,7 +19,9 @@ import (
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -129,6 +133,123 @@ func testBasicSendUnidirectional(t *harnessTest) {
wg.Wait()
}

// testMinRelayFeeBump tests that if the fee estimation is below the min relay
// fee the feerate is bumped to the min relay fee for both the minting
// transaction and a basic asset send.
func testMinRelayFeeBump(t *harnessTest) {
var (
ctxb = context.Background()
wg sync.WaitGroup
)

const numUnits = 10

// Subscribe to receive assent send events from primary tapd node.
events := SubscribeSendEvents(t.t, t.tapd)

// We will mint assets using the first output and then use the second
// output for the transfer. This ensures a valid fee calculation.
initialUTXOs := []*UTXORequest{
{
Type: lnrpc.AddressType_NESTED_PUBKEY_HASH,
Amount: 1_000_000,
},
{
Type: lnrpc.AddressType_NESTED_PUBKEY_HASH,
Amount: 999_990,
},
}

// Set the initial state of the wallet of the first node. The wallet
// state will reset at the end of this test.
SetNodeUTXOs(t, t.lndHarness.Alice, btcutil.Amount(1), initialUTXOs)
defer ResetNodeWallet(t, t.lndHarness.Alice)

// Set the min relay fee to a higher value than the fee rate that will
// be returned by the fee estimation.
lowFeeRate := chainfee.SatPerVByte(1).FeePerKWeight()
highMinRelayFeeRate := chainfee.SatPerVByte(2).FeePerKVByte()
defaultMinRelayFeeRate := chainfee.SatPerVByte(1).FeePerKVByte()
defaultFeeRate := chainfee.SatPerKWeight(3125)
t.lndHarness.SetFeeEstimateWithConf(lowFeeRate, 6)
t.lndHarness.SetMinRelayFeerate(highMinRelayFeeRate)

// Reset all fee rates to their default value at the end of this test.
defer t.lndHarness.SetMinRelayFeerate(defaultMinRelayFeeRate)
defer t.lndHarness.SetFeeEstimateWithConf(defaultFeeRate, 6)

// First, we'll make a normal assets with enough units to allow us to
// send it around a few times.
rpcAssets := MintAssetsConfirmBatch(
t.t, t.lndHarness.Miner().Client, t.tapd,
[]*mintrpc.MintAssetRequest{issuableAssets[0]},
)

genInfo := rpcAssets[0].AssetGenesis

// Check the final fee rate of the mint TX.
rpcMintOutpoint := rpcAssets[0].ChainAnchor.AnchorOutpoint
mintOutpoint, err := wire.NewOutPointFromString(rpcMintOutpoint)
require.NoError(t.t, err)

// We check whether the minting TX is bumped to the min relay fee.
AssertFeeRate(
t.t, t.lndHarness.Miner().Client, initialUTXOs[0].Amount,
&mintOutpoint.Hash, highMinRelayFeeRate.FeePerKWeight(),
)

// Now that we have the asset created, we'll make a new node that'll
// serve as the node which'll receive the assets. The existing tapd
// node will be used to synchronize universe state.
secondTapd := setupTapdHarness(
t.t, t, t.lndHarness.Bob, t.universeServer,
)
defer func() {
require.NoError(t.t, secondTapd.stop(!*noDelete))
}()

// Next, we'll attempt to complete two transfers with distinct
// addresses from our main node to Bob.
currentUnits := issuableAssets[0].Asset.Amount

// Issue a single address which will be reused for each send.
bobAddr, err := secondTapd.NewAddr(ctxb, &taprpc.NewAddrRequest{
AssetId: genInfo.AssetId,
Amt: numUnits,
AssetVersion: rpcAssets[0].Version,
})
require.NoError(t.t, err)

// Deduct what we sent from the expected current number of
// units.
currentUnits -= numUnits

AssertAddrCreated(t.t, secondTapd, rpcAssets[0], bobAddr)

sendResp, sendEvents := sendAssetsToAddr(t, t.tapd, bobAddr)

ConfirmAndAssertOutboundTransfer(
t.t, t.lndHarness.Miner().Client, t.tapd, sendResp,
genInfo.AssetId,
[]uint64{currentUnits, numUnits}, 0, 1,
)

sendInputAmt := initialUTXOs[1].Amount + 1000
AssertTransferFeeRate(
t.t, t.lndHarness.Miner().Client, sendResp, sendInputAmt,
highMinRelayFeeRate.FeePerKWeight(),
)

AssertNonInteractiveRecvComplete(t.t, secondTapd, 1)
AssertSendEventsComplete(t.t, bobAddr.ScriptKey, sendEvents)

// Close event stream.
err = events.CloseSend()
require.NoError(t.t, err)

wg.Wait()
}

// testRestartReceiver tests that the receiver node's asset balance after a
// single asset transfer does not change if the receiver node restarts.
// Before the addition of this test, after restarting the receiver node
Expand Down
4 changes: 4 additions & 0 deletions itest/test_list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ var testCases = []*testCase{
name: "basic send unidirectional",
test: testBasicSendUnidirectional,
},
{
name: "min relay fee bump",
test: testMinRelayFeeBump,
},
{
name: "restart receiver check balance",
test: testRestartReceiverCheckBalance,
Expand Down

0 comments on commit 454ea22

Please sign in to comment.