Skip to content

Commit

Permalink
itest: add interceptor wire records access test
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed May 2, 2024
1 parent 8ebeb3d commit 5203776
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions itest/list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ var allTestCases = []*lntest.TestCase{
Name: "forward interceptor modified htlc",
TestFunc: testForwardInterceptorModifiedHtlc,
},
{
Name: "forward interceptor wire records",
TestFunc: testForwardInterceptorWireRecords,
},
{
Name: "zero conf channel open",
TestFunc: testZeroConfChannelOpen,
Expand Down
72 changes: 72 additions & 0 deletions itest/lnd_forward_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,78 @@ func testForwardInterceptorModifiedHtlc(ht *lntest.HarnessTest) {
ht.CloseChannel(bob, cpBC)
}

func testForwardInterceptorWireRecords(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.AssertTopologyChannelOpen(alice, cpBC)

// Connect an interceptor to Bob's node.
bobInterceptor, cancelBobInterceptor := bob.RPC.HtlcInterceptor()

req := &lnrpc.Invoice{ValueMsat: 1000}
addResponse := carol.RPC.AddInvoice(req)
invoice := carol.RPC.LookupInvoice(addResponse.RHash)

sendReq := &routerrpc.SendPaymentRequest{
PaymentRequest: invoice.PaymentRequest,
TimeoutSeconds: int32(wait.PaymentTimeout.Seconds()),
FeeLimitMsat: noFeeLimitMsat,
FirstHopCustomRecords: map[uint64][]byte{
65537: []byte("test"),
},
}

_ = alice.RPC.SendPayment(sendReq)

// We start the htlc interceptor with a simple implementation that saves
// all intercepted packets. These packets are held to simulate a
// pending payment.
packet := ht.ReceiveHtlcInterceptor(bobInterceptor)

require.Len(ht, packet.WireCustomRecords, 1)

val, ok := packet.WireCustomRecords[65537]
require.True(ht, ok, "expected custom record")
require.Equal(ht, []byte("test"), val)

action := routerrpc.ResolveHoldForwardAction_RESUME_MODIFIED
newOutgoingAmountMsat := packet.OutgoingAmountMsat + 800

err := bobInterceptor.Send(&routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: packet.IncomingCircuitKey,
OutgoingAmountMsat: newOutgoingAmountMsat,
Action: action,
})
require.NoError(ht, err, "failed to send request")

// Cancel the context, which will disconnect Bob's interceptor.
cancelBobInterceptor()

// Assert that the payment was successful.
var preimage lntypes.Preimage
copy(preimage[:], invoice.RPreimage)
ht.AssertPaymentStatus(alice, preimage, lnrpc.Payment_SUCCEEDED)

// Finally, close channels.
ht.CloseChannel(alice, cpAB)
ht.CloseChannel(bob, cpBC)

}

// interceptorTestScenario is a helper struct to hold the test context and
// provide the needed functionality.
type interceptorTestScenario struct {
Expand Down

0 comments on commit 5203776

Please sign in to comment.