multi: refactor testDisconnectingTargetPeer

This commit is contained in:
yyforyongyu committed 2022-11-17 20:29:50 +08:00
1 parent d39303f246
commit f5ebaa5f35
4 files changed
+81 -94

No files matched your search

+34
View File
@@ -1295,3 +1295,37 @@ func (h *HarnessTest) AssertActiveNodesSynced() {
h.WaitForBlockchainSync(node) h.WaitForBlockchainSync(node)
} }
} }
// AssertPeerNotConnected asserts that the given node b is not connected to a.
func (h *HarnessTest) AssertPeerNotConnected(a, b *node.HarnessNode) {
err := wait.NoError(func() error {
// We require the RPC call to be succeeded and won't wait for
// it as it's an unexpected behavior.
resp := a.RPC.ListPeers()
// If node B is seen in the ListPeers response from node A,
// then we return false as the connection has been fully
// established.
for _, peer := range resp.Peers {
if peer.PubKey == b.PubKeyStr {
return fmt.Errorf("peers %s and %s still "+
"connected", a.Name(), b.Name())
}
}
return nil
}, DefaultTimeout)
require.NoError(h, err, "timeout checking peers not connected")
}
// AssertNotConnected asserts that two peers are not connected.
func (h *HarnessTest) AssertNotConnected(a, b *node.HarnessNode) {
h.AssertPeerNotConnected(a, b)
h.AssertPeerNotConnected(b, a)
}
// AssertConnected asserts that two peers are connected.
func (h *HarnessTest) AssertConnected(a, b *node.HarnessNode) {
h.AssertPeerConnected(a, b)
h.AssertPeerConnected(b, a)
}
+4
View File
@@ -75,4 +75,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "sweep coins", Name: "sweep coins",
TestFunc: testSweepAllCoins, TestFunc: testSweepAllCoins,
}, },
{
Name: "disconnecting target peer",
TestFunc: testDisconnectingTargetPeer,
},
} }
+43 -90
View File
@@ -11,7 +11,6 @@ import (
"time" "time"
"github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/wallet" "github.com/btcsuite/btcwallet/wallet"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@@ -30,10 +29,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// testDisconnectingTargetPeer performs a test which disconnects Alice-peer from // testDisconnectingTargetPeer performs a test which disconnects Alice-peer
// Bob-peer and then re-connects them again. We expect Alice to be able to // from Bob-peer and then re-connects them again. We expect Alice to be able to
// disconnect at any point. // disconnect at any point.
func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) { //
// TODO(yy): move to lnd_network_test.
func testDisconnectingTargetPeer(ht *lntemp.HarnessTest) {
// We'll start both nodes with a high backoff so that they don't // We'll start both nodes with a high backoff so that they don't
// reconnect automatically during our test. // reconnect automatically during our test.
args := []string{ args := []string{
@@ -41,20 +42,12 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) {
"--maxbackoff=1m", "--maxbackoff=1m",
} }
alice := net.NewNode(t.t, "Alice", args) alice, bob := ht.Alice, ht.Bob
defer shutdownAndAssert(net, t, alice) ht.RestartNodeWithExtraArgs(alice, args)
ht.RestartNodeWithExtraArgs(bob, args)
bob := net.NewNode(t.t, "Bob", args)
defer shutdownAndAssert(net, t, bob)
// Start by connecting Alice and Bob with no channels. // Start by connecting Alice and Bob with no channels.
net.ConnectNodes(t.t, alice, bob) ht.EnsureConnected(alice, bob)
// Check existing connection.
assertConnected(t, alice, bob)
// Give Alice some coins so she can fund a channel.
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, alice)
chanAmt := funding.MaxBtcFundingAmount chanAmt := funding.MaxBtcFundingAmount
pushAmt := btcutil.Amount(0) pushAmt := btcutil.Amount(0)
@@ -62,109 +55,69 @@ func testDisconnectingTargetPeer(net *lntest.NetworkHarness, t *harnessTest) {
// Create a new channel that requires 1 confs before it's considered // Create a new channel that requires 1 confs before it's considered
// open, then broadcast the funding transaction // open, then broadcast the funding transaction
const numConfs = 1 const numConfs = 1
pendingUpdate, err := net.OpenPendingChannel( p := lntemp.OpenChannelParams{
alice, bob, chanAmt, pushAmt, Amt: chanAmt,
) PushAmt: pushAmt,
if err != nil {
t.Fatalf("unable to open channel: %v", err)
} }
stream := ht.OpenChannelAssertPending(alice, bob, p)
// At this point, the channel's funding transaction will have been // At this point, the channel's funding transaction will have been
// broadcast, but not confirmed. Alice and Bob's nodes should reflect // broadcast, but not confirmed. Alice and Bob's nodes should reflect
// this when queried via RPC. // this when queried via RPC.
assertNumOpenChannelsPending(t, alice, bob, 1) ht.AssertNumPendingOpenChannels(alice, 1)
ht.AssertNumPendingOpenChannels(bob, 1)
// Disconnect Alice-peer from Bob-peer and get error causes by one // Disconnect Alice-peer from Bob-peer should have no error.
// pending channel with detach node is existing. ht.DisconnectNodes(alice, bob)
if err := net.DisconnectNodes(alice, bob); err != nil {
t.Fatalf("Bob's peer was disconnected from Alice's"+
" while one pending channel is existing: err %v", err)
}
time.Sleep(time.Millisecond * 300)
// Assert that the connection was torn down. // Assert that the connection was torn down.
assertNotConnected(t, alice, bob) ht.AssertNotConnected(alice, bob)
fundingTxID, err := chainhash.NewHash(pendingUpdate.Txid)
if err != nil {
t.Fatalf("unable to convert funding txid into chainhash.Hash:"+
" %v", err)
}
// Mine a block, then wait for Alice's node to notify us that the // Mine a block, then wait for Alice's node to notify us that the
// channel has been opened. The funding transaction should be found // channel has been opened.
// within the newly mined block. ht.MineBlocksAndAssertNumTxes(numConfs, 1)
block := mineBlocks(t, net, numConfs, 1)[0]
assertTxInBlock(t, block, fundingTxID)
// At this point, the channel should be fully opened and there should be // At this point, the channel should be fully opened and there should
// no pending channels remaining for either node. // be no pending channels remaining for either node.
time.Sleep(time.Millisecond * 300) ht.AssertNumPendingOpenChannels(alice, 0)
ht.AssertNumPendingOpenChannels(bob, 0)
assertNumOpenChannelsPending(t, alice, bob, 0)
// Reconnect the nodes so that the channel can become active. // Reconnect the nodes so that the channel can become active.
net.ConnectNodes(t.t, alice, bob) ht.ConnectNodes(alice, bob)
// The channel should be listed in the peer information returned by both // The channel should be listed in the peer information returned by
// peers. // both peers.
outPoint := wire.OutPoint{ chanPoint := ht.WaitForChannelOpenEvent(stream)
Hash: *fundingTxID,
Index: pendingUpdate.OutputIndex,
}
// Check both nodes to ensure that the channel is ready for operation. // Check both nodes to ensure that the channel is ready for operation.
if err := net.AssertChannelExists(alice, &outPoint); err != nil { ht.AssertChannelExists(alice, chanPoint)
t.Fatalf("unable to assert channel existence: %v", err) ht.AssertChannelExists(bob, chanPoint)
}
if err := net.AssertChannelExists(bob, &outPoint); err != nil {
t.Fatalf("unable to assert channel existence: %v", err)
}
// Disconnect Alice-peer from Bob-peer and get error causes by one // Disconnect Alice-peer from Bob-peer should have no error.
// active channel with detach node is existing. ht.DisconnectNodes(alice, bob)
if err := net.DisconnectNodes(alice, bob); err != nil {
t.Fatalf("Bob's peer was disconnected from Alice's"+
" while one active channel is existing: err %v", err)
}
// Check existing connection. // Check existing connection.
assertNotConnected(t, alice, bob) ht.AssertNotConnected(alice, bob)
// Reconnect both nodes before force closing the channel. // Reconnect both nodes before force closing the channel.
net.ConnectNodes(t.t, alice, bob) ht.ConnectNodes(alice, bob)
// Finally, immediately close the channel. This function will also block // Finally, immediately close the channel. This function will also
// until the channel is closed and will additionally assert the relevant // block until the channel is closed and will additionally assert the
// channel closing post conditions. // relevant channel closing post conditions.
chanPoint := &lnrpc.ChannelPoint{ ht.ForceCloseChannel(alice, chanPoint)
FundingTxid: &lnrpc.ChannelPoint_FundingTxidBytes{
FundingTxidBytes: pendingUpdate.Txid,
},
OutputIndex: pendingUpdate.OutputIndex,
}
closeChannelAndAssert(t, net, alice, chanPoint, true) // Disconnect Alice-peer from Bob-peer should have no error.
ht.DisconnectNodes(alice, bob)
// Disconnect Alice-peer from Bob-peer without getting error about
// existing channels.
if err := net.DisconnectNodes(alice, bob); err != nil {
t.Fatalf("unable to disconnect Bob's peer from Alice's: err %v",
err)
}
// Check that the nodes not connected. // Check that the nodes not connected.
assertNotConnected(t, alice, bob) ht.AssertNotConnected(alice, bob)
// Finally, re-connect both nodes. // Finally, re-connect both nodes.
net.ConnectNodes(t.t, alice, bob) ht.ConnectNodes(alice, bob)
// Check existing connection. // Check existing connection.
assertConnected(t, alice, bob) ht.AssertConnected(alice, bob)
// Cleanup by mining the force close and sweep transaction.
cleanupForceClose(t, net, alice, chanPoint)
} }
// testSphinxReplayPersistence verifies that replayed onion packets are rejected // testSphinxReplayPersistence verifies that replayed onion packets are rejected
-4
View File
@@ -36,10 +36,6 @@ var allTestCases = []*testCase{
name: "open channel reorg test", name: "open channel reorg test",
test: testOpenChannelAfterReorg, test: testOpenChannelAfterReorg,
}, },
{
name: "disconnecting target peer",
test: testDisconnectingTargetPeer,
},
{ {
name: "reconnect after ip change", name: "reconnect after ip change",
test: testReconnectAfterIPChange, test: testReconnectAfterIPChange,