Skip to content

Commit

Permalink
lntest+itest: fix flakes found using neutrino backend
Browse files Browse the repository at this point in the history
This commit makes sure the sweep requests are received before mining
blocks to trigger the actual sweeping.

In addition, `testFundingExpiryBlocksOnPending` is updated to deal with
the old `channel link not found` issue.
  • Loading branch information
yyforyongyu committed Jul 23, 2024
1 parent 2608c08 commit 4dcce9d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
18 changes: 10 additions & 8 deletions itest/lnd_funding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/funding"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/labels"
Expand Down Expand Up @@ -1341,22 +1340,22 @@ func testChannelFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
// that by dave force-closing the channel. Which let's carol sweep its
// to_remote output which is not encumbered by any relative locktime.
ht.CloseChannelAssertPending(dave, chanPoint2, true)

// Mine the force close commitment transaction.
ht.MineBlocksAndAssertNumTxes(1, 1)

// Make sure Carol sees her to_remote output from the force close tx.
ht.AssertNumPendingSweeps(carol, 1)

// Mine one block to trigger the sweep transaction.
ht.MineEmptyBlocks(1)

// We need to wait for carol initiating the sweep of the to_remote
// output of chanPoint2.
utxos := ht.AssertNumUTXOsUnconfirmed(carol, 1)
utxo := ht.AssertNumUTXOsUnconfirmed(carol, 1)[0]

// We filter for the unconfirmed utxo and try to open a channel with
// that utxo.
utxoOpt := fn.Find(func(u *lnrpc.Utxo) bool {
return u.Confirmations == 0
}, utxos)
fundingUtxo := utxoOpt.UnwrapOrFail(ht.T)
// We now try to open channel using the unconfirmed utxo.
fundingUtxo := utxo

// Now try to open the channel with this utxo and expect an error.
expectedErr := fmt.Errorf("outpoint already spent or "+
Expand Down Expand Up @@ -1405,6 +1404,9 @@ func testChannelFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
ht.CloseChannelAssertPending(dave, chanPoint3, true)
ht.MineBlocksAndAssertNumTxes(1, 1)

// Make sure Carol sees her to_remote output from the force close tx.
ht.AssertNumPendingSweeps(carol, 1)

// Mine one block to trigger the sweep transaction.
ht.MineEmptyBlocks(1)

Expand Down
13 changes: 13 additions & 0 deletions itest/lnd_open_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
Expand Down Expand Up @@ -767,6 +768,18 @@ func testFundingExpiryBlocksOnPending(ht *lntest.HarnessTest) {
// channel.
ht.MineBlocksAndAssertNumTxes(1, 1)
chanPoint := lntest.ChanPointFromPendingUpdate(update)

// TODO(yy): remove the sleep once the following bug is fixed.
//
// We may get the error `unable to gracefully close channel
// while peer is offline (try force closing it instead):
// channel link not found`. This happens because the channel
// link hasn't been added yet but we now proceed to closing the
// channel. We may need to revisit how the channel open event
// is created and make sure the event is only sent after all
// relevant states have been updated.
time.Sleep(2 * time.Second)

ht.CloseChannel(alice, chanPoint)
}

Expand Down
14 changes: 6 additions & 8 deletions itest/lnd_psbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,9 @@ func testPsbtChanFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
ht.CloseChannelAssertPending(dave, channelPoint, true)
ht.MineBlocksAndAssertNumTxes(1, 1)

// Make sure Carol sees her to_remote output from the force close tx.
ht.AssertNumPendingSweeps(carol, 1)

// Mine one block to trigger the sweep transaction.
ht.MineEmptyBlocks(1)

Expand Down Expand Up @@ -1805,6 +1808,9 @@ func testPsbtChanFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
ht.CloseChannelAssertPending(dave, channelPoint2, true)
ht.MineBlocksAndAssertNumTxes(1, 1)

// Make sure Carol sees her to_remote output from the force close tx.
ht.AssertNumPendingSweeps(carol, 1)

// Mine one block to trigger the sweep transaction.
ht.MineEmptyBlocks(1)

Expand Down Expand Up @@ -1929,19 +1935,11 @@ func testPsbtChanFundingWithUnstableUtxos(ht *lntest.HarnessTest) {
updateResp = ht.ReceiveOpenChannelUpdate(chanUpdates)
upd, ok = updateResp.Update.(*lnrpc.OpenStatusUpdate_ChanPending)
require.True(ht, ok)
channelPoint3 := &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: upd.ChanPending.Txid,
},
OutputIndex: upd.ChanPending.OutputIndex,
}

err = finalTx.Deserialize(bytes.NewReader(finalizeRes.RawFinalTx))
require.NoError(ht, err)

txHash = finalTx.TxHash()
block = ht.MineBlocksAndAssertNumTxes(1, 1)[0]
ht.AssertTxInBlock(block, &txHash)

ht.CloseChannel(carol, channelPoint3)
}
7 changes: 6 additions & 1 deletion lntest/harness_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,13 @@ func (h *HarnessTest) AssertNumUTXOsWithConf(hn *node.HarnessNode,
return nil
}

desc := "has UTXOs:\n"
for _, utxo := range resp.Utxos {
desc += fmt.Sprintf("%v\n", utxo)
}

return errNumNotMatched(hn.Name(), "num of UTXOs",
expectedUtxos, total-old, total, old)
expectedUtxos, total-old, total, old, desc)
}, DefaultTimeout)
require.NoError(h, err, "timeout waiting for UTXOs")

Expand Down
10 changes: 8 additions & 2 deletions lntest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ func CopyFile(dest, src string) error {

// errNumNotMatched is a helper method to return a nicely formatted error.
func errNumNotMatched(name string, subject string,
want, got, total, old int) error {
want, got, total, old int, desc ...any) error {

return fmt.Errorf("%s: assert %s failed: want %d, got: %d, total: "+
err := fmt.Errorf("%s: assert %s failed: want %d, got: %d, total: "+
"%d, previously had: %d", name, subject, want, got, total, old)

if len(desc) > 0 {
err = fmt.Errorf("%w, desc: %v", err, desc)
}

return err
}

// parseDerivationPath parses a path in the form of m/x'/y'/z'/a/b into a slice
Expand Down

0 comments on commit 4dcce9d

Please sign in to comment.