test: modify single hop invoice test to also test zpay32 payment requests

This commit adds an additional test case to the `testSingleHopInvoice`
test in order to exercise the proper parsing and dispatching of encoded
payment requests using the zpay32 encoding scheme.

With this test we ensure that the daemon properly encodes payreq’s upon
the creation of invoices, and also that the SendPayment RPC is able to
parse the payment request and properly complete a payment based off of
one.
This commit is contained in:
Olaoluwa Osuntokun 2017-01-07 12:46:10 -08:00
parent 9bb5a45f89
commit 573eb1dfc4
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2

@ -442,6 +442,51 @@ func testSingleHopInvoice(net *networkHarness, t *harnessTest) {
chanAmt := btcutil.Amount(100000) chanAmt := btcutil.Amount(100000)
chanPoint := openChannelAndAssert(t, net, ctxt, net.Alice, net.Bob, chanAmt) chanPoint := openChannelAndAssert(t, net, ctxt, net.Alice, net.Bob, chanAmt)
assertPaymentBalance := func(amt btcutil.Amount) {
balReq := &lnrpc.ChannelBalanceRequest{}
// The balances of Alice and Bob should be updated accordingly.
aliceBalance, err := net.Alice.ChannelBalance(ctxb, balReq)
if err != nil {
t.Fatalf("unable to query for alice's balance: %v", err)
}
bobBalance, err := net.Bob.ChannelBalance(ctxb, balReq)
if err != nil {
t.Fatalf("unable to query for bob's balance: %v", err)
}
if aliceBalance.Balance != int64(chanAmt-amt) {
t.Fatalf("Alice's balance is incorrect got %v, expected %v",
aliceBalance, int64(chanAmt-amt))
}
if bobBalance.Balance != int64(amt) {
t.Fatalf("Bob's balance is incorrect got %v, expected %v",
bobBalance, amt)
}
// Both channels should also have properly accunted from the amount
// that has been sent/received over the channel.
listReq := &lnrpc.ListChannelsRequest{}
aliceListChannels, err := net.Alice.ListChannels(ctxb, listReq)
if err != nil {
t.Fatalf("unable to query for alice's channel list: %v", err)
}
aliceSatoshisSent := aliceListChannels.Channels[0].TotalSatoshisSent
if aliceSatoshisSent != int64(amt) {
t.Fatalf("Alice's satoshis sent is incorrect got %v, expected %v",
aliceSatoshisSent, amt)
}
bobListChannels, err := net.Bob.ListChannels(ctxb, listReq)
if err != nil {
t.Fatalf("unable to query for bob's channel list: %v", err)
}
bobSatoshisReceived := bobListChannels.Channels[0].TotalSatoshisReceived
if bobSatoshisReceived != int64(amt) {
t.Fatalf("Bob's satoshis received is incorrect got %v, expected %v",
bobSatoshisReceived, amt)
}
}
// Now that the channel is open, create an invoice for Bob which // Now that the channel is open, create an invoice for Bob which
// expects a payment of 1000 satoshis from Alice paid via a particular // expects a payment of 1000 satoshis from Alice paid via a particular
// pre-image. // pre-image.
@ -490,47 +535,36 @@ func testSingleHopInvoice(net *networkHarness, t *harnessTest) {
spew.Sdump(dbInvoice)) spew.Sdump(dbInvoice))
} }
// The balances of Alice and Bob should be updated accordingly. // With the payment completed all balance related stats should be
aliceBalance, err := net.Alice.ChannelBalance(ctxb, &lnrpc.ChannelBalanceRequest{}) // properly updated.
if err != nil { assertPaymentBalance(paymentAmt)
t.Fatalf("unable to query for alice's balance: %v", err)
// Create another invoice for Bob, this time leaving off the preimage
// to one will be randomly generated. We'll test the proper
// encoding/decoding of the zpay32 payment requests.
invoice = &lnrpc.Invoice{
Memo: "test3",
Value: paymentAmt,
} }
bobBalance, err := net.Bob.ChannelBalance(ctxb, &lnrpc.ChannelBalanceRequest{}) invoiceResp, err = net.Bob.AddInvoice(ctxb, invoice)
if err != nil { if err != nil {
t.Fatalf("unable to query for bob's balance: %v", err) t.Fatalf("unable to add invoice: %v", err)
} }
if aliceBalance.Balance != int64(chanAmt-paymentAmt) { // Next send another payment, but this time using a zpay32 encoded
t.Fatalf("Alice's balance is incorrect got %v, expected %v", // invoice rather than manually specifying the payment details.
aliceBalance, int64(chanAmt-paymentAmt)) if err := sendStream.Send(&lnrpc.SendRequest{
PaymentRequest: invoiceResp.PaymentRequest,
}); err != nil {
t.Fatalf("unable to send payment: %v", err)
} }
if bobBalance.Balance != paymentAmt { if _, err := sendStream.Recv(); err != nil {
t.Fatalf("Bob's balance is incorrect got %v, expected %v", t.Fatalf("error when attempting recv: %v", err)
bobBalance, paymentAmt)
} }
// Both channels should also have properly accunted from the amount // The second payment should also have succeeded, with the balances
// that has been sent/received over the channel. // being update accordingly.
listReq := &lnrpc.ListChannelsRequest{} assertPaymentBalance(paymentAmt * 2)
aliceListChannels, err := net.Alice.ListChannels(ctxb, listReq)
if err != nil {
t.Fatalf("unable to query for alice's channel list: %v", err)
}
aliceSatoshisSent := aliceListChannels.Channels[0].TotalSatoshisSent
if aliceSatoshisSent != paymentAmt {
t.Fatalf("Alice's satoshis sent is incorrect got %v, expected %v",
aliceSatoshisSent, paymentAmt)
}
bobListChannels, err := net.Bob.ListChannels(ctxb, listReq)
if err != nil {
t.Fatalf("unable to query for bob's channel list: %v", err)
}
bobSatoshisReceived := bobListChannels.Channels[0].TotalSatoshisReceived
if bobSatoshisReceived != paymentAmt {
t.Fatalf("Bob's satoshis received is incorrect got %v, expected %v",
bobSatoshisReceived, paymentAmt)
}
ctxt, _ = context.WithTimeout(ctxb, timeout) ctxt, _ = context.WithTimeout(ctxb, timeout)
closeChannelAndAssert(t, net, ctxt, net.Alice, chanPoint, false) closeChannelAndAssert(t, net, ctxt, net.Alice, chanPoint, false)