diff --git a/mint/lightning/lnd.go b/mint/lightning/lnd.go index 717f96a..28b645b 100644 --- a/mint/lightning/lnd.go +++ b/mint/lightning/lnd.go @@ -132,13 +132,20 @@ func (lnd *LndClient) OutgoingPaymentStatus(ctx context.Context, hash string) (P trackPaymentStream, err := lnd.routerClient.TrackPaymentV2(ctx, &trackPaymentRequest) if err != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + return PaymentStatus{PaymentStatus: Pending}, nil + } return PaymentStatus{PaymentStatus: Failed}, err } // this should block until final payment update payment, err := trackPaymentStream.Recv() if err != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + return PaymentStatus{PaymentStatus: Pending}, nil + } return PaymentStatus{PaymentStatus: Failed}, fmt.Errorf("payment failed: %w", err) + } if payment.Status == lnrpc.Payment_UNKNOWN || payment.Status == lnrpc.Payment_FAILED { return PaymentStatus{PaymentStatus: Failed}, diff --git a/mint/mint_integration_test.go b/mint/mint_integration_test.go index 22df84b..fcdf8df 100644 --- a/mint/mint_integration_test.go +++ b/mint/mint_integration_test.go @@ -634,8 +634,10 @@ func TestMelt(t *testing.T) { if err != nil { t.Fatalf("got unexpected error in mint: %v", err) } +} - // tests for pending state +func TestPendingProofs(t *testing.T) { + // use hodl invoice to cause payment to stuck and put quote and proofs in state of pending preimage, _ := testutils.GenerateRandomBytes() hash := sha256.Sum256(preimage) hodlInvoice := invoicesrpc.AddHoldInvoiceRequest{Hash: hash[:], Value: 2100} @@ -644,22 +646,22 @@ func TestMelt(t *testing.T) { t.Fatalf("error creating hodl invoice: %v", err) } - meltQuote, err = testMint.RequestMeltQuote(testutils.BOLT11_METHOD, addHodlInvoiceRes.PaymentRequest, testutils.SAT_UNIT) + meltQuote, err := testMint.RequestMeltQuote(testutils.BOLT11_METHOD, addHodlInvoiceRes.PaymentRequest, testutils.SAT_UNIT) if err != nil { t.Fatalf("got unexpected error in melt request: %v", err) } - validProofs, err = testutils.GetValidProofsForAmount(2200, testMint, lnd2) + validProofs, err := testutils.GetValidProofsForAmount(2200, testMint, lnd2) if err != nil { t.Fatalf("error generating valid proofs: %v", err) } - // custom context just for this melt call to timeout afte 5s and return pending + // custom context just for this melt call to timeout after 5s and return pending // for the stuck hodl invoice meltContext, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() - melt, err = testMint.MeltTokens(meltContext, testutils.BOLT11_METHOD, meltQuote.Id, validProofs) + melt, err := testMint.MeltTokens(meltContext, testutils.BOLT11_METHOD, meltQuote.Id, validProofs) if err != nil { t.Fatalf("got unexpected error in melt: %v", err) } @@ -667,6 +669,15 @@ func TestMelt(t *testing.T) { t.Fatalf("expected melt quote with state of '%v' but got '%v' instead", nut05.Pending.String(), melt.State.String()) } + meltQuote, err = testMint.GetMeltQuoteState(meltContext, testutils.BOLT11_METHOD, meltQuote.Id) + if err != nil { + t.Fatalf("unexpected error getting melt quote state: %v", err) + } + + if meltQuote.State != nut05.Pending { + t.Fatalf("expected melt quote with state of '%s' but got '%s' instead", nut05.Pending, melt.State) + } + _, err = testMint.MeltTokens(ctx, testutils.BOLT11_METHOD, meltQuote.Id, validProofs) if !errors.Is(err, cashu.MeltQuotePending) { t.Fatalf("expected error '%v' but got '%v' instead", cashu.MeltQuotePending, err) @@ -674,7 +685,7 @@ func TestMelt(t *testing.T) { // try to use currently pending proofs in another op. // swap should return err saying proofs are pending - blindedMessages, _, _, _ = testutils.CreateBlindedMessages(validProofs.Amount(), testMint.GetActiveKeyset()) + blindedMessages, _, _, _ := testutils.CreateBlindedMessages(validProofs.Amount(), testMint.GetActiveKeyset()) _, err = testMint.Swap(validProofs, blindedMessages) if !errors.Is(err, cashu.ProofPendingErr) { t.Fatalf("expected error '%v' but got '%v' instead", cashu.ProofPendingErr, err) diff --git a/mint/server.go b/mint/server.go index 64c6261..26fe285 100644 --- a/mint/server.go +++ b/mint/server.go @@ -406,7 +406,7 @@ func (ms *MintServer) meltQuoteState(rw http.ResponseWriter, req *http.Request) method := vars["method"] quoteId := vars["quote_id"] - ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) defer cancel() meltQuote, err := ms.mint.GetMeltQuoteState(ctx, method, quoteId)