Skip to content

Commit

Permalink
itest: use ht.CreateSimpleNetwork whenever applicable
Browse files Browse the repository at this point in the history
So we won't forget to assert the topology after opening a chain of
channels.
  • Loading branch information
yyforyongyu committed Nov 7, 2024
1 parent 70f96df commit e73835b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 120 deletions.
153 changes: 53 additions & 100 deletions itest/lnd_forward_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ type interceptorTestCase struct {
// testForwardInterceptorDedupHtlc tests that upon reconnection, duplicate
// HTLCs aren't re-notified using the HTLC interceptor API.
func testForwardInterceptorDedupHtlc(ht *lntest.HarnessTest) {
// Initialize the test context with 3 connected nodes.
ts := newInterceptorTestScenario(ht)
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}

alice, bob, carol := ts.alice, ts.bob, ts.carol
// Initialize the test context with 3 connected nodes.
cfgs := [][]string{nil, nil, nil}

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpAB, cpBC := resp[0], resp[1]
chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol := nodes[0], nodes[1], nodes[2]
cpAB := chanPoints[0]

// Make sure Alice is aware of channel Bob=>Carol.
ht.AssertChannelInGraph(alice, cpBC)
// Init the scenario.
ts := &interceptorTestScenario{
ht: ht,
alice: alice,
bob: bob,
carol: carol,
}

// Connect the interceptor.
interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor()
Expand Down Expand Up @@ -190,22 +191,24 @@ func testForwardInterceptorDedupHtlc(ht *lntest.HarnessTest) {
// 4. When Interceptor disconnects it resumes all held htlcs, which result in
// valid payment (invoice is settled).
func testForwardInterceptorBasic(ht *lntest.HarnessTest) {
ts := newInterceptorTestScenario(ht)
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}

alice, bob, carol := ts.alice, ts.bob, ts.carol
// Initialize the test context with 3 connected nodes.
cfgs := [][]string{nil, nil, nil}

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpAB, cpBC := resp[0], resp[1]
chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol := nodes[0], nodes[1], nodes[2]
cpAB := chanPoints[0]

// Make sure Alice is aware of channel Bob=>Carol.
ht.AssertChannelInGraph(alice, cpBC)
// Init the scenario.
ts := &interceptorTestScenario{
ht: ht,
alice: alice,
bob: bob,
carol: carol,
}

// Connect the interceptor.
interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor()
Expand Down Expand Up @@ -346,23 +349,23 @@ func testForwardInterceptorBasic(ht *lntest.HarnessTest) {
// testForwardInterceptorModifiedHtlc tests that the interceptor can modify the
// amount and custom records of an intercepted HTLC and resume it.
func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) {
// Initialize the test context with 3 connected nodes.
ts := newInterceptorTestScenario(ht)
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}

alice, bob, carol := ts.alice, ts.bob, ts.carol
// Initialize the test context with 3 connected nodes.
cfgs := [][]string{nil, nil, nil}

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpBC := resp[1]
_, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol := nodes[0], nodes[1], nodes[2]

// Make sure Alice is aware of channel Bob=>Carol.
ht.AssertChannelInGraph(alice, cpBC)
// Init the scenario.
ts := &interceptorTestScenario{
ht: ht,
alice: alice,
bob: bob,
carol: carol,
}

// Connect an interceptor to Bob's node.
bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor()
Expand Down Expand Up @@ -449,24 +452,15 @@ func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) {
// wire custom records provided by the sender of a payment as part of the
// update_add_htlc message.
func testForwardInterceptorWireRecords(ht *lntest.HarnessTest) {
// Initialize the test context with 3 connected nodes.
ts := newInterceptorTestScenario(ht)

alice, bob, carol, dave := ts.alice, ts.bob, ts.carol, ts.dave

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
{Local: carol, Remote: dave, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpBC := resp[1]

// Make sure Alice is aware of channel Bob=>Carol.
ht.AssertChannelInGraph(alice, cpBC)
// Initialize the test context with 4 connected nodes.
cfgs := [][]string{nil, nil, nil, nil}

// Open and wait for channels.
_, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol, dave := nodes[0], nodes[1], nodes[2], nodes[3]

// Connect an interceptor to Bob's node.
bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor()
Expand Down Expand Up @@ -574,25 +568,15 @@ func testForwardInterceptorWireRecords(ht *lntest.HarnessTest) {
// update_add_htlc message and that those records are persisted correctly and
// re-sent on node restart.
func testForwardInterceptorRestart(ht *lntest.HarnessTest) {
// Initialize the test context with 3 connected nodes.
ts := newInterceptorTestScenario(ht)

alice, bob, carol, dave := ts.alice, ts.bob, ts.carol, ts.dave

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
{Local: carol, Remote: dave, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpBC, cpCD := resp[1], resp[2]

// Make sure Alice is aware of channels Bob=>Carol and Carol=>Dave.
ht.AssertChannelInGraph(alice, cpBC)
ht.AssertChannelInGraph(alice, cpCD)
// Initialize the test context with 4 connected nodes.
cfgs := [][]string{nil, nil, nil, nil}

// Open and wait for channels.
_, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol, dave := nodes[0], nodes[1], nodes[2], nodes[3]

// Connect an interceptor to Bob's node.
bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor()
Expand Down Expand Up @@ -731,37 +715,6 @@ type interceptorTestScenario struct {
alice, bob, carol, dave *node.HarnessNode
}

// newInterceptorTestScenario initializes a new test scenario with three nodes
// and connects them to have the following topology,
//
// Alice --> Bob --> Carol --> Dave
//
// Among them, Alice and Bob are standby nodes and Carol is a new node.
func newInterceptorTestScenario(
ht *lntest.HarnessTest) *interceptorTestScenario {

alice := ht.NewNodeWithCoins("Alice", nil)
bob := ht.NewNodeWithCoins("bob", nil)

carol := ht.NewNode("carol", nil)
dave := ht.NewNode("dave", nil)

ht.EnsureConnected(alice, bob)
ht.EnsureConnected(bob, carol)
ht.EnsureConnected(carol, dave)

// So that carol can open channels.
ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)

return &interceptorTestScenario{
ht: ht,
alice: alice,
bob: bob,
carol: carol,
dave: dave,
}
}

// prepareTestCases prepares 4 tests:
// 1. failed htlc.
// 2. resumed htlc.
Expand Down
32 changes: 12 additions & 20 deletions itest/lnd_payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,23 +1067,16 @@ func assertChannelState(ht *lntest.HarnessTest, hn *node.HarnessNode,
// 5.) Alice observes a failed OR succeeded payment with failure reason
// FAILURE_REASON_CANCELED which suppresses further payment attempts.
func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) {
// Initialize the test context with 3 connected nodes.
ts := newInterceptorTestScenario(ht)

alice, bob, carol := ts.alice, ts.bob, ts.carol

// Open and wait for channels.
const chanAmt = btcutil.Amount(300000)
p := lntest.OpenChannelParams{Amt: chanAmt}
reqs := []*lntest.OpenChannelRequest{
{Local: alice, Remote: bob, Param: p},
{Local: bob, Remote: carol, Param: p},
}
resp := ht.OpenMultiChannelsAsync(reqs)
cpAB, cpBC := resp[0], resp[1]

// Make sure Alice is aware of channel Bob=>Carol.
ht.AssertChannelInGraph(alice, cpBC)
// Initialize the test context with 3 connected nodes.
cfgs := [][]string{nil, nil, nil}

// Open and wait for channels.
chanPoints, nodes := ht.CreateSimpleNetwork(cfgs, p)
alice, bob, carol := nodes[0], nodes[1], nodes[2]
cpAB := chanPoints[0]

// Connect the interceptor.
interceptor, cancelInterceptor := bob.RPC.HtlcInterceptor()
Expand All @@ -1093,7 +1086,8 @@ func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) {
// htlc even though the payment context was canceled before invoice
// settlement.
sendPaymentInterceptAndCancel(
ht, ts, cpAB, routerrpc.ResolveHoldForwardAction_RESUME,
ht, alice, bob, carol, cpAB,
routerrpc.ResolveHoldForwardAction_RESUME,
lnrpc.Payment_SUCCEEDED, interceptor,
)

Expand All @@ -1103,20 +1097,18 @@ func testPaymentFailureReasonCanceled(ht *lntest.HarnessTest) {
// Note that we'd have to reset Alice's mission control if we tested the
// htlc fail case before the htlc resume case.
sendPaymentInterceptAndCancel(
ht, ts, cpAB, routerrpc.ResolveHoldForwardAction_FAIL,
ht, alice, bob, carol, cpAB,
routerrpc.ResolveHoldForwardAction_FAIL,
lnrpc.Payment_FAILED, interceptor,
)
}

func sendPaymentInterceptAndCancel(ht *lntest.HarnessTest,
ts *interceptorTestScenario, cpAB *lnrpc.ChannelPoint,
alice, bob, carol *node.HarnessNode, cpAB *lnrpc.ChannelPoint,
interceptorAction routerrpc.ResolveHoldForwardAction,
expectedPaymentStatus lnrpc.Payment_PaymentStatus,
interceptor rpc.InterceptorClient) {

// Prepare the test cases.
alice, bob, carol := ts.alice, ts.bob, ts.carol

// Prepare the test cases.
addResponse := carol.RPC.AddInvoice(&lnrpc.Invoice{
ValueMsat: 1000,
Expand Down

0 comments on commit e73835b

Please sign in to comment.