lntemp+itest: refactor testNonstdSweep

This commit is contained in:
yyforyongyu 2022-08-11 11:13:47 +08:00
parent c4ffd8b469
commit 2b11a619db
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
4 changed files with 38 additions and 39 deletions

View File

@ -11,6 +11,7 @@ import (
"testing"
"time"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
@ -253,6 +254,16 @@ func (h *HarnessMiner) GetRawTransaction(txid *chainhash.Hash) *btcutil.Tx {
return tx
}
// GetRawTransactionVerbose makes a RPC call to the miner's
// GetRawTransactionVerbose and asserts.
func (h *HarnessMiner) GetRawTransactionVerbose(
txid *chainhash.Hash) *btcjson.TxRawResult {
tx, err := h.Client.GetRawTransactionVerbose(txid)
require.NoErrorf(h, err, "failed to get raw tx verbose: %v", txid)
return tx
}
// AssertTxInMempool asserts a given transaction can be found in the mempool.
func (h *HarnessMiner) AssertTxInMempool(txid *chainhash.Hash) *wire.MsgTx {
var msgTx *wire.MsgTx

View File

@ -433,4 +433,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "scid alias upgrade",
TestFunc: testOptionScidUpgrade,
},
{
Name: "nonstd sweep",
TestFunc: testNonstdSweep,
},
}

View File

@ -1,40 +1,39 @@
package itest
import (
"context"
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest"
"github.com/lightningnetwork/lnd/lntemp"
"github.com/stretchr/testify/require"
)
func testNonstdSweep(net *lntest.NetworkHarness, t *harnessTest) {
func testNonstdSweep(ht *lntemp.HarnessTest) {
p2shAddr, err := btcutil.NewAddressScriptHash(
make([]byte, 1), harnessNetParams,
)
require.NoError(t.t, err)
require.NoError(ht, err)
p2pkhAddr, err := btcutil.NewAddressPubKeyHash(
make([]byte, 20), harnessNetParams,
)
require.NoError(t.t, err)
require.NoError(ht, err)
p2wshAddr, err := btcutil.NewAddressWitnessScriptHash(
make([]byte, 32), harnessNetParams,
)
require.NoError(t.t, err)
require.NoError(ht, err)
p2wkhAddr, err := btcutil.NewAddressWitnessPubKeyHash(
make([]byte, 20), harnessNetParams,
)
require.NoError(t.t, err)
require.NoError(ht, err)
p2trAddr, err := btcutil.NewAddressTaproot(
make([]byte, 32), harnessNetParams,
)
require.NoError(t.t, err)
require.NoError(ht, err)
tests := []struct {
name string
@ -64,10 +63,10 @@ func testNonstdSweep(net *lntest.NetworkHarness, t *harnessTest) {
for _, test := range tests {
test := test
success := t.t.Run(test.name, func(t *testing.T) {
h := newHarnessTest(t, net)
success := ht.Run(test.name, func(t *testing.T) {
st := ht.Subtest(t)
testNonStdSweepInner(net, h, test.address)
testNonStdSweepInner(st, test.address)
})
if !success {
break
@ -75,18 +74,14 @@ func testNonstdSweep(net *lntest.NetworkHarness, t *harnessTest) {
}
}
func testNonStdSweepInner(net *lntest.NetworkHarness, t *harnessTest,
address string) {
ctxb := context.Background()
carol := net.NewNode(t.t, "carol", nil)
func testNonStdSweepInner(ht *lntemp.HarnessTest, address string) {
carol := ht.NewNode("carol", nil)
// Give Carol a UTXO so SendCoins will behave as expected.
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, carol)
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
// Set the fee estimate to 1sat/vbyte.
net.SetFeeEstimate(250)
ht.SetFeeEstimate(250)
// Make Carol call SendCoins with the SendAll flag and the created
// address.
@ -96,20 +91,13 @@ func testNonStdSweepInner(net *lntest.NetworkHarness, t *harnessTest,
SendAll: true,
}
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
defer cancel()
// If a non-standard transaction was created, then this SendCoins call
// will fail.
_, err := carol.SendCoins(ctxt, sendReq)
require.NoError(t.t, err)
carol.RPC.SendCoins(sendReq)
// Fetch the txid so we can grab the raw transaction.
txid, err := waitForTxInMempool(net.Miner.Client, minerMempoolTimeout)
require.NoError(t.t, err)
tx, err := net.Miner.Client.GetRawTransaction(txid)
require.NoError(t.t, err)
txid := ht.Miner.AssertNumTxsInMempool(1)[0]
tx := ht.Miner.GetRawTransaction(txid)
msgTx := tx.MsgTx()
@ -123,9 +111,7 @@ func testNonStdSweepInner(net *lntest.NetworkHarness, t *harnessTest,
for _, inp := range msgTx.TxIn {
// Fetch the previous outpoint's value.
prevOut := inp.PreviousOutPoint
ptx, err := net.Miner.Client.GetRawTransaction(&prevOut.Hash)
require.NoError(t.t, err)
ptx := ht.Miner.GetRawTransaction(&prevOut.Hash)
pout := ptx.MsgTx().TxOut[prevOut.Index]
inputVal += int(pout.Value)
@ -139,9 +125,11 @@ func testNonStdSweepInner(net *lntest.NetworkHarness, t *harnessTest,
// Fetch the vsize of the transaction so we can determine if the
// transaction pays >= 1 sat/vbyte.
rawTx, err := net.Miner.Client.GetRawTransactionVerbose(txid)
require.NoError(t.t, err)
rawTx := ht.Miner.GetRawTransactionVerbose(txid)
// Require fee >= vbytes.
require.True(t.t, fee >= int(rawTx.Vsize))
require.True(ht, fee >= int(rawTx.Vsize))
// Mine a block to keep the mempool clean.
ht.MineBlocksAndAssertNumTxes(1, 1)
}

View File

@ -56,10 +56,6 @@ var allTestCases = []*testCase{
name: "taproot",
test: testTaproot,
},
{
name: "nonstd sweep",
test: testNonstdSweep,
},
{
name: "taproot coop close",
test: testTaprootCoopClose,