|
| 1 | +package itest |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + |
| 6 | + "github.com/lightningnetwork/lnd/invoices" |
| 7 | + "github.com/lightningnetwork/lnd/lnrpc" |
| 8 | + "github.com/lightningnetwork/lnd/lntest" |
| 9 | + "github.com/lightningnetwork/lnd/lntypes" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func testDeleteCanceledInvoice(ht *lntest.HarnessTest) { |
| 14 | + bob := ht.NewNode("bob", nil) |
| 15 | + |
| 16 | + // Create 2 invoices for Bob. |
| 17 | + const numInvoices = 2 |
| 18 | + invoiceResp := make([]*lnrpc.AddInvoiceResponse, numInvoices) |
| 19 | + |
| 20 | + for i := range numInvoices { |
| 21 | + // Create a unique 32-byte preimage for each invoice. |
| 22 | + preimage := bytes.Repeat([]byte{byte(i + 1)}, 32) |
| 23 | + |
| 24 | + invoice := &lnrpc.Invoice{ |
| 25 | + RPreimage: preimage, |
| 26 | + Value: paymentAmt, |
| 27 | + } |
| 28 | + |
| 29 | + invoiceResp[i] = bob.RPC.AddInvoice(invoice) |
| 30 | + } |
| 31 | + |
| 32 | + // Cancel all invoices except the last one. |
| 33 | + for i := range numInvoices - 1 { |
| 34 | + bob.RPC.CancelInvoice(invoiceResp[i].RHash) |
| 35 | + } |
| 36 | + |
| 37 | + // Let's assert an error while providing no invoice hash. |
| 38 | + bob.RPC.DeleteCanceledInvoiceAssertErr( |
| 39 | + &lnrpc.DelCanceledInvoiceReq{}, |
| 40 | + invoices.ErrNoInvoiceHash.Error(), |
| 41 | + ) |
| 42 | + |
| 43 | + // Let's assert an error while providing a hash with an invalid length. |
| 44 | + bob.RPC.DeleteCanceledInvoiceAssertErr( |
| 45 | + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: "bb02fbfa62983b6b62"}, |
| 46 | + "invalid hash string length", |
| 47 | + ) |
| 48 | + |
| 49 | + // Let's assert an error is returned for a hash with invalid encoding. |
| 50 | + bob.RPC.DeleteCanceledInvoiceAssertErr( |
| 51 | + &lnrpc.DelCanceledInvoiceReq{ |
| 52 | + InvoiceHash: "bb02fbfa62983b6b621376bf8230732dd3a6dce" + |
| 53 | + "a9f5df803c0935ae6ce7440dg", |
| 54 | + }, "encoding/hex: invalid byte", |
| 55 | + ) |
| 56 | + |
| 57 | + // Let's delete one canceled invoice. |
| 58 | + invoiceToDelete := lntypes.Hash(invoiceResp[0].RHash).String() |
| 59 | + resp := bob.RPC.DeleteCanceledInvoice( |
| 60 | + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: invoiceToDelete}, |
| 61 | + ) |
| 62 | + |
| 63 | + require.Contains( |
| 64 | + ht, resp.String(), "canceled invoice deleted successfully", |
| 65 | + ) |
| 66 | + |
| 67 | + respList := bob.RPC.ListInvoices(&lnrpc.ListInvoiceRequest{}) |
| 68 | + |
| 69 | + // Assert that the invoice was deleted. |
| 70 | + require.Len(ht, respList.Invoices, numInvoices-1) |
| 71 | + |
| 72 | + // Let's assert an error while deleting a non-existent invoice. |
| 73 | + bob.RPC.DeleteCanceledInvoiceAssertErr( |
| 74 | + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: invoiceToDelete}, |
| 75 | + invoices.ErrInvoiceNotFound.Error(), |
| 76 | + ) |
| 77 | + |
| 78 | + // Let's assert an error while deleting a non canceled invoice. |
| 79 | + notCanceledInvoice := lntypes.Hash(invoiceResp[numInvoices-1].RHash). |
| 80 | + String() |
| 81 | + |
| 82 | + bob.RPC.DeleteCanceledInvoiceAssertErr( |
| 83 | + &lnrpc.DelCanceledInvoiceReq{InvoiceHash: notCanceledInvoice}, |
| 84 | + invoices.ErrInvoiceNotCanceled.Error(), |
| 85 | + ) |
| 86 | + |
| 87 | + // Assert that the last invoice in the invoiceResp is still present and |
| 88 | + // OPEN. |
| 89 | + respList = bob.RPC.ListInvoices(&lnrpc.ListInvoiceRequest{}) |
| 90 | + require.Equal( |
| 91 | + ht, invoiceResp[numInvoices-1].RHash, |
| 92 | + respList.Invoices[0].RHash, |
| 93 | + ) |
| 94 | + |
| 95 | + require.Equal(ht, lnrpc.Invoice_OPEN, respList.Invoices[0].State) |
| 96 | + |
| 97 | + // Assert that just one invoice exists. |
| 98 | + require.Len(ht, respList.Invoices, 1) |
| 99 | +} |
0 commit comments