lnd_test: define createPayReqs helper method

This commit is contained in:
Johan T. Halseth 2018-11-20 15:09:44 +01:00
parent e1d8b07735
commit eb2f832bba
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

@ -546,6 +546,40 @@ func makeFakePayHash(t *harnessTest) []byte {
return randBuf return randBuf
} }
// createPayReqs is a helper method that will create a slice of payment
// requests for the given node.
func createPayReqs(ctx context.Context, node *lntest.HarnessNode,
paymentAmt btcutil.Amount, numInvoices int) ([]string, [][]byte,
[]*lnrpc.Invoice, error) {
payReqs := make([]string, numInvoices)
rHashes := make([][]byte, numInvoices)
invoices := make([]*lnrpc.Invoice, numInvoices)
for i := 0; i < numInvoices; i++ {
preimage := make([]byte, 32)
_, err := rand.Read(preimage)
if err != nil {
return nil, nil, nil, fmt.Errorf("unable to generate "+
"preimage: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: int64(paymentAmt),
}
resp, err := node.AddInvoice(ctx, invoice)
if err != nil {
return nil, nil, nil, fmt.Errorf("unable to add "+
"invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
rHashes[i] = resp.RHash
invoices[i] = invoice
}
return payReqs, rHashes, invoices, nil
}
const ( const (
AddrTypeWitnessPubkeyHash = lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH AddrTypeWitnessPubkeyHash = lnrpc.NewAddressRequest_WITNESS_PUBKEY_HASH
AddrTypeNestedPubkeyHash = lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH AddrTypeNestedPubkeyHash = lnrpc.NewAddressRequest_NESTED_PUBKEY_HASH
@ -3599,18 +3633,12 @@ func testMultiHopPayments(net *lntest.NetworkHarness, t *harnessTest) {
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
const paymentAmt = 1000 const paymentAmt = 1000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, net.Bob, paymentAmt, numPayments,
Memo: "testing", )
Value: paymentAmt, if err != nil {
} t.Fatalf("unable to create pay reqs: %v", err)
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -3828,17 +3856,12 @@ func testSingleHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
// Create 5 invoices for Bob, which expect a payment from Alice for 1k // Create 5 invoices for Bob, which expect a payment from Alice for 1k
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
rHashes := make([][]byte, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { _, rHashes, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, net.Bob, paymentAmt, numPayments,
Value: paymentAmt, )
} if err != nil {
resp, err := net.Bob.AddInvoice(ctxb, invoice) t.Fatalf("unable to create pay reqs: %v", err)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
rHashes[i] = resp.RHash
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -4016,17 +4039,12 @@ func testMultiHopSendToRoute(net *lntest.NetworkHarness, t *harnessTest) {
// Create 5 invoices for Carol, which expect a payment from Alice for 1k // Create 5 invoices for Carol, which expect a payment from Alice for 1k
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
rHashes := make([][]byte, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { _, rHashes, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, carol, paymentAmt, numPayments,
Value: paymentAmt, )
} if err != nil {
resp, err := carol.AddInvoice(ctxb, invoice) t.Fatalf("unable to create pay reqs: %v", err)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
rHashes[i] = resp.RHash
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -4532,25 +4550,12 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
// by only using one of the channels. // by only using one of the channels.
const numPayments = 2 const numPayments = 2
const paymentAmt = 70000 const paymentAmt = 70000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
preimage := make([]byte, 32) ctxt, net.Bob, paymentAmt, numPayments,
_, err := rand.Read(preimage) )
if err != nil { if err != nil {
t.Fatalf("unable to generate preimage: %v", err) t.Fatalf("unable to create pay reqs: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: paymentAmt,
}
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
time.Sleep(time.Millisecond * 50) time.Sleep(time.Millisecond * 50)
@ -4602,25 +4607,12 @@ func testPrivateChannels(net *lntest.NetworkHarness, t *harnessTest) {
// Alice should also be able to route payments using this channel, // Alice should also be able to route payments using this channel,
// so send two payments of 60k back to Carol. // so send two payments of 60k back to Carol.
const paymentAmt60k = 60000 const paymentAmt60k = 60000
payReqs = make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err = createPayReqs(
preimage := make([]byte, 32) ctxt, carol, paymentAmt60k, numPayments,
_, err := rand.Read(preimage) )
if err != nil { if err != nil {
t.Fatalf("unable to generate preimage: %v", err) t.Fatalf("unable to create pay reqs: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: paymentAmt60k,
}
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
time.Sleep(time.Millisecond * 50) time.Sleep(time.Millisecond * 50)
@ -5267,22 +5259,12 @@ func testInvoiceSubscriptions(net *lntest.NetworkHarness, t *harnessTest) {
// We'll now add 3 more invoices to Bob's invoice registry. // We'll now add 3 more invoices to Bob's invoice registry.
const numInvoices = 3 const numInvoices = 3
newInvoices := make([]*lnrpc.Invoice, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
payReqs := make([]string, numInvoices) payReqs, _, newInvoices, err := createPayReqs(
for i := 0; i < numInvoices; i++ { ctxt, net.Bob, paymentAmt, numInvoices,
preimage := bytes.Repeat([]byte{byte(90 + 1 + i)}, 32) )
invoice := &lnrpc.Invoice{ if err != nil {
Memo: "testing", t.Fatalf("unable to create pay reqs: %v", err)
RPreimage: preimage,
Value: paymentAmt,
}
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
newInvoices[i] = invoice
payReqs[i] = resp.PaymentRequest
} }
// Now that the set of invoices has been added, we'll re-register for // Now that the set of invoices has been added, we'll re-register for
@ -6104,20 +6086,12 @@ func testRevokedCloseRetribution(net *lntest.NetworkHarness, t *harnessTest) {
// With the channel open, we'll create a few invoices for Bob that // With the channel open, we'll create a few invoices for Bob that
// Carol will pay to in order to advance the state of the channel. // Carol will pay to in order to advance the state of the channel.
bobPayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { bobPayReqs, _, _, err := createPayReqs(
preimage := bytes.Repeat([]byte{byte(255 - i)}, 32) ctxt, net.Bob, paymentAmt, numInvoices,
invoice := &lnrpc.Invoice{ )
Memo: "testing", if err != nil {
RPreimage: preimage, t.Fatalf("unable to create pay reqs: %v", err)
Value: paymentAmt,
}
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
bobPayReqs[i] = resp.PaymentRequest
} }
// As we'll be querying the state of bob's channels frequently we'll // As we'll be querying the state of bob's channels frequently we'll
@ -6386,20 +6360,12 @@ func testRevokedCloseRetributionZeroValueRemoteOutput(net *lntest.NetworkHarness
// With the channel open, we'll create a few invoices for Carol that // With the channel open, we'll create a few invoices for Carol that
// Dave will pay to in order to advance the state of the channel. // Dave will pay to in order to advance the state of the channel.
carolPayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { carolPayReqs, _, _, err := createPayReqs(
preimage := bytes.Repeat([]byte{byte(192 - i)}, 32) ctxt, carol, paymentAmt, numInvoices,
invoice := &lnrpc.Invoice{ )
Memo: "testing", if err != nil {
RPreimage: preimage, t.Fatalf("unable to create pay reqs: %v", err)
Value: paymentAmt,
}
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
carolPayReqs[i] = resp.PaymentRequest
} }
// As we'll be querying the state of Carols's channels frequently we'll // As we'll be querying the state of Carols's channels frequently we'll
@ -6655,20 +6621,12 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// With the channel open, we'll create a few invoices for Carol that // With the channel open, we'll create a few invoices for Carol that
// Dave will pay to in order to advance the state of the channel. // Dave will pay to in order to advance the state of the channel.
carolPayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { carolPayReqs, _, _, err := createPayReqs(
preimage := bytes.Repeat([]byte{byte(192 - i)}, 32) ctxt, carol, paymentAmt, numInvoices,
invoice := &lnrpc.Invoice{ )
Memo: "testing", if err != nil {
RPreimage: preimage, t.Fatalf("unable to create pay reqs: %v", err)
Value: paymentAmt,
}
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
carolPayReqs[i] = resp.PaymentRequest
} }
// As we'll be querying the state of Carol's channels frequently we'll // As we'll be querying the state of Carol's channels frequently we'll
@ -6739,20 +6697,12 @@ func testRevokedCloseRetributionRemoteHodl(net *lntest.NetworkHarness,
// At this point, we'll also send over a set of HTLC's from Carol to // At this point, we'll also send over a set of HTLC's from Carol to
// Dave. This ensures that the final revoked transaction has HTLC's in // Dave. This ensures that the final revoked transaction has HTLC's in
// both directions. // both directions.
davePayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { davePayReqs, _, _, err := createPayReqs(
preimage := bytes.Repeat([]byte{byte(199 - i)}, 32) ctxt, dave, paymentAmt, numInvoices,
invoice := &lnrpc.Invoice{ )
Memo: "testing", if err != nil {
RPreimage: preimage, t.Fatalf("unable to create pay reqs: %v", err)
Value: paymentAmt,
}
resp, err := dave.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
davePayReqs[i] = resp.PaymentRequest
} }
// Send payments from Carol to Dave using 3 of Dave's payment hashes // Send payments from Carol to Dave using 3 of Dave's payment hashes
@ -8168,7 +8118,7 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
} }
const ( const (
timeout = time.Duration(time.Second * 5) timeout = time.Duration(time.Second * 15)
paymentAmt = 100 paymentAmt = 100
) )
@ -8203,25 +8153,12 @@ func testAsyncPayments(net *lntest.NetworkHarness, t *harnessTest) {
// With the channel open, we'll create invoices for Bob that Alice // With the channel open, we'll create invoices for Bob that Alice
// will pay to in order to advance the state of the channel. // will pay to in order to advance the state of the channel.
bobPayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { bobPayReqs, _, _, err := createPayReqs(
preimage := make([]byte, 32) ctxt, net.Bob, paymentAmt, numInvoices,
_, err := rand.Read(preimage) )
if err != nil { if err != nil {
t.Fatalf("unable to generate preimage: %v", err) t.Fatalf("unable to create pay reqs: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: paymentAmt,
}
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
bobPayReqs[i] = resp.PaymentRequest
} }
// Wait for Alice to receive the channel edge from the funding manager. // Wait for Alice to receive the channel edge from the funding manager.
@ -8383,48 +8320,22 @@ func testBidirectionalAsyncPayments(net *lntest.NetworkHarness, t *harnessTest)
// With the channel open, we'll create invoices for Bob that Alice // With the channel open, we'll create invoices for Bob that Alice
// will pay to in order to advance the state of the channel. // will pay to in order to advance the state of the channel.
bobPayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { bobPayReqs, _, _, err := createPayReqs(
preimage := make([]byte, 32) ctxt, net.Bob, paymentAmt, numInvoices,
_, err := rand.Read(preimage) )
if err != nil { if err != nil {
t.Fatalf("unable to generate preimage: %v", err) t.Fatalf("unable to create pay reqs: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: paymentAmt,
}
resp, err := net.Bob.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
bobPayReqs[i] = resp.PaymentRequest
} }
// With the channel open, we'll create invoices for Alice that Bob // With the channel open, we'll create invoices for Alice that Bob
// will pay to in order to advance the state of the channel. // will pay to in order to advance the state of the channel.
alicePayReqs := make([]string, numInvoices) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numInvoices; i++ { alicePayReqs, _, _, err := createPayReqs(
preimage := make([]byte, 32) ctxt, net.Alice, paymentAmt, numInvoices,
_, err := rand.Read(preimage) )
if err != nil { if err != nil {
t.Fatalf("unable to generate preimage: %v", err) t.Fatalf("unable to create pay reqs: %v", err)
}
invoice := &lnrpc.Invoice{
Memo: "testing",
RPreimage: preimage,
Value: paymentAmt,
}
resp, err := net.Alice.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
alicePayReqs[i] = resp.PaymentRequest
} }
// Wait for Alice to receive the channel edge from the funding manager. // Wait for Alice to receive the channel edge from the funding manager.
@ -10433,18 +10344,12 @@ func testSwitchCircuitPersistence(net *lntest.NetworkHarness, t *harnessTest) {
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
const paymentAmt = 1000 const paymentAmt = 1000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, carol, paymentAmt, numPayments,
Memo: "testing", )
Value: paymentAmt, if err != nil {
} t.Fatalf("unable to create pay reqs: %v", err)
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -10775,18 +10680,12 @@ func testSwitchOfflineDelivery(net *lntest.NetworkHarness, t *harnessTest) {
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
const paymentAmt = 1000 const paymentAmt = 1000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, carol, paymentAmt, numPayments,
Memo: "testing", )
Value: paymentAmt, if err != nil {
} t.Fatalf("unable to create pay reqs: %v", err)
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -11125,18 +11024,12 @@ func testSwitchOfflineDeliveryPersistence(net *lntest.NetworkHarness, t *harness
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
const paymentAmt = 1000 const paymentAmt = 1000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, carol, paymentAmt, numPayments,
Memo: "testing", )
Value: paymentAmt, if err != nil {
} t.Fatalf("unable to create pay reqs: %v", err)
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the
@ -11479,18 +11372,12 @@ func testSwitchOfflineDeliveryOutgoingOffline(
// satoshis with a different preimage each time. // satoshis with a different preimage each time.
const numPayments = 5 const numPayments = 5
const paymentAmt = 1000 const paymentAmt = 1000
payReqs := make([]string, numPayments) ctxt, _ = context.WithTimeout(ctxb, timeout)
for i := 0; i < numPayments; i++ { payReqs, _, _, err := createPayReqs(
invoice := &lnrpc.Invoice{ ctxt, carol, paymentAmt, numPayments,
Memo: "testing", )
Value: paymentAmt, if err != nil {
} t.Fatalf("unable to create pay reqs: %v", err)
resp, err := carol.AddInvoice(ctxb, invoice)
if err != nil {
t.Fatalf("unable to add invoice: %v", err)
}
payReqs[i] = resp.PaymentRequest
} }
// We'll wait for all parties to recognize the new channels within the // We'll wait for all parties to recognize the new channels within the