itest: refactor testRejectHTLC

This commit is contained in:
yyforyongyu
2022-08-04 00:37:47 +08:00
parent 0c8bce0c1c
commit 1938e42f4e
3 changed files with 34 additions and 82 deletions

View File

@ -95,4 +95,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "garbage collect link nodes", Name: "garbage collect link nodes",
TestFunc: testGarbageCollectLinkNodes, TestFunc: testGarbageCollectLinkNodes,
}, },
{
Name: "reject onward htlc",
TestFunc: testRejectHTLC,
},
} }

View File

@ -2,7 +2,6 @@ package itest
import ( import (
"context" "context"
"crypto/rand"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -531,41 +530,35 @@ func testGarbageCollectLinkNodes(ht *lntemp.HarnessTest) {
// testRejectHTLC tests that a node can be created with the flag --rejecthtlc. // testRejectHTLC tests that a node can be created with the flag --rejecthtlc.
// This means that the node will reject all forwarded HTLCs but can still // This means that the node will reject all forwarded HTLCs but can still
// accept direct HTLCs as well as send HTLCs. // accept direct HTLCs as well as send HTLCs.
func testRejectHTLC(net *lntest.NetworkHarness, t *harnessTest) { func testRejectHTLC(ht *lntemp.HarnessTest) {
// RejectHTLC // RejectHTLC
// Alice ------> Carol ------> Bob // Alice ------> Carol ------> Bob
// //
const chanAmt = btcutil.Amount(1000000) const chanAmt = btcutil.Amount(1000000)
ctxb := context.Background() alice, bob := ht.Alice, ht.Bob
// Create Carol with reject htlc flag. // Create Carol with reject htlc flag.
carol := net.NewNode(t.t, "Carol", []string{"--rejecthtlc"}) carol := ht.NewNode("Carol", []string{"--rejecthtlc"})
defer shutdownAndAssert(net, t, carol)
// Connect Alice to Carol. // Connect Alice to Carol.
net.ConnectNodes(t.t, net.Alice, carol) ht.ConnectNodes(alice, carol)
// Connect Carol to Bob. // Connect Carol to Bob.
net.ConnectNodes(t.t, carol, net.Bob) ht.ConnectNodes(carol, bob)
// Send coins to Carol. // Send coins to Carol.
net.SendCoins(t.t, btcutil.SatoshiPerBitcoin, carol) ht.FundCoins(btcutil.SatoshiPerBitcoin, carol)
// Send coins to Alice.
net.SendCoins(t.t, btcutil.SatoshiPerBitcent, net.Alice)
// Open a channel between Alice and Carol. // Open a channel between Alice and Carol.
chanPointAlice := openChannelAndAssert( chanPointAlice := ht.OpenChannel(
t, net, net.Alice, carol, alice, carol, lntemp.OpenChannelParams{
lntest.OpenChannelParams{
Amt: chanAmt, Amt: chanAmt,
}, },
) )
// Open a channel between Carol and Bob. // Open a channel between Carol and Bob.
chanPointCarol := openChannelAndAssert( chanPointCarol := ht.OpenChannel(
t, net, carol, net.Bob, carol, bob, lntemp.OpenChannelParams{
lntest.OpenChannelParams{
Amt: chanAmt, Amt: chanAmt,
}, },
) )
@ -573,103 +566,62 @@ func testRejectHTLC(net *lntest.NetworkHarness, t *harnessTest) {
// Channel should be ready for payments. // Channel should be ready for payments.
const payAmt = 100 const payAmt = 100
// Helper closure to generate a random pre image.
genPreImage := func() []byte {
preimage := make([]byte, 32)
_, err := rand.Read(preimage)
if err != nil {
t.Fatalf("unable to generate preimage: %v", err)
}
return preimage
}
// Create an invoice from Carol of 100 satoshis. // Create an invoice from Carol of 100 satoshis.
// We expect Alice to be able to pay this invoice. // We expect Alice to be able to pay this invoice.
preimage := genPreImage()
carolInvoice := &lnrpc.Invoice{ carolInvoice := &lnrpc.Invoice{
Memo: "testing - alice should pay carol", Memo: "testing - alice should pay carol",
RPreimage: preimage, RPreimage: ht.Random32Bytes(),
Value: payAmt, Value: payAmt,
} }
// Carol adds the invoice to her database. // Carol adds the invoice to her database.
resp, err := carol.AddInvoice(ctxb, carolInvoice) resp := carol.RPC.AddInvoice(carolInvoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
// Alice pays Carols invoice. // Alice pays Carols invoice.
err = completePaymentRequests( ht.CompletePaymentRequests(alice, []string{resp.PaymentRequest})
net.Alice, net.Alice.RouterClient,
[]string{resp.PaymentRequest}, true,
)
if err != nil {
t.Fatalf("unable to send payments from alice to carol: %v", err)
}
// Create an invoice from Bob of 100 satoshis. // Create an invoice from Bob of 100 satoshis.
// We expect Carol to be able to pay this invoice. // We expect Carol to be able to pay this invoice.
preimage = genPreImage()
bobInvoice := &lnrpc.Invoice{ bobInvoice := &lnrpc.Invoice{
Memo: "testing - carol should pay bob", Memo: "testing - carol should pay bob",
RPreimage: preimage, RPreimage: ht.Random32Bytes(),
Value: payAmt, Value: payAmt,
} }
// Bob adds the invoice to his database. // Bob adds the invoice to his database.
resp, err = net.Bob.AddInvoice(ctxb, bobInvoice) resp = bob.RPC.AddInvoice(bobInvoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
// Carol pays Bobs invoice. // Carol pays Bobs invoice.
err = completePaymentRequests( ht.CompletePaymentRequests(carol, []string{resp.PaymentRequest})
carol, carol.RouterClient,
[]string{resp.PaymentRequest}, true,
)
if err != nil {
t.Fatalf("unable to send payments from carol to bob: %v", err)
}
// Create an invoice from Bob of 100 satoshis. // Create an invoice from Bob of 100 satoshis.
// Alice attempts to pay Bob but this should fail, since we are // Alice attempts to pay Bob but this should fail, since we are
// using Carol as a hop and her node will reject onward HTLCs. // using Carol as a hop and her node will reject onward HTLCs.
preimage = genPreImage()
bobInvoice = &lnrpc.Invoice{ bobInvoice = &lnrpc.Invoice{
Memo: "testing - alice tries to pay bob", Memo: "testing - alice tries to pay bob",
RPreimage: preimage, RPreimage: ht.Random32Bytes(),
Value: payAmt, Value: payAmt,
} }
// Bob adds the invoice to his database. // Bob adds the invoice to his database.
resp, err = net.Bob.AddInvoice(ctxb, bobInvoice) resp = bob.RPC.AddInvoice(bobInvoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
// Alice attempts to pay Bobs invoice. This payment should be rejected since // Alice attempts to pay Bobs invoice. This payment should be rejected
// we are using Carol as an intermediary hop, Carol is running lnd with // since we are using Carol as an intermediary hop, Carol is running
// --rejecthtlc. // lnd with --rejecthtlc.
err = completePaymentRequests( paymentReq := &routerrpc.SendPaymentRequest{
net.Alice, net.Alice.RouterClient, PaymentRequest: resp.PaymentRequest,
[]string{resp.PaymentRequest}, true, TimeoutSeconds: 60,
) FeeLimitMsat: noFeeLimitMsat,
if err == nil {
t.Fatalf(
"should have been rejected, carol will not accept forwarded htlcs",
)
} }
payStream := alice.RPC.SendPayment(paymentReq)
ht.AssertPaymentStatusFromStream(payStream, lnrpc.Payment_FAILED)
assertLastHTLCError(t, net.Alice, lnrpc.Failure_CHANNEL_DISABLED) ht.AssertLastHTLCError(alice, lnrpc.Failure_CHANNEL_DISABLED)
// Close all channels. // Close all channels.
closeChannelAndAssert(t, net, net.Alice, chanPointAlice, false) ht.CloseChannel(alice, chanPointAlice)
closeChannelAndAssert(t, net, carol, chanPointCarol, false) ht.CloseChannel(carol, chanPointCarol)
} }
func testNodeSignVerify(net *lntest.NetworkHarness, t *harnessTest) { func testNodeSignVerify(net *lntest.NetworkHarness, t *harnessTest) {

View File

@ -120,10 +120,6 @@ var allTestCases = []*testCase{
name: "multi-hop htlc error propagation", name: "multi-hop htlc error propagation",
test: testHtlcErrorPropagation, test: testHtlcErrorPropagation,
}, },
{
name: "reject onward htlc",
test: testRejectHTLC,
},
// TODO(roasbeef): multi-path integration test // TODO(roasbeef): multi-path integration test
{ {
name: "node announcement", name: "node announcement",