Retry a failed fractional redeem from its split note

The split burns the pasted note before the melt. A failed invoice or melt
used to leave the value in a bot-held split note while a second press
answered 'already claimed'. The retry now melts the split note.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-14 18:24:01 +00:00
co-authored by Claude Sonnet 5.5
parent 7f49e422f4
commit 82b291a69b
2 changed files with 110 additions and 0 deletions
+33
View File
@@ -246,6 +246,15 @@ func (s *Service) Redeem(ctx context.Context, redeemID, user string) (*Redeem, e
if r.UserName != user {
return r, ErrNotOwner
}
// A fractional note is burned by splitting it before the melt. If the
// invoice or the melt failed after that, the bot holds the value in the
// split note, so a retry melts that note instead of looking for the
// original, which no longer exists.
if r.SplitNoteID != nil && (r.State == RedeemInfo || r.State == RedeemFailed) {
if split, err := s.store.Note(*r.SplitNoteID); err == nil && split.State == NoteLive && split.ReportedMsat%1000 == 0 && split.ReportedMsat > 0 {
return s.meltRedeemSplit(ctx, r, split)
}
}
if r.State != RedeemInfo {
return r, ErrInFlight
}
@@ -280,6 +289,29 @@ func (s *Service) Redeem(ctx context.Context, redeemID, user string) (*Redeem, e
sid := split.ID
r.SplitNoteID = &sid
}
return s.meltRedeem(ctx, r, RedeemInfo, meltK1, meltCallback, valueMsat)
}
// meltRedeemSplit retries the melt of a redeem whose split note is still live.
func (s *Service) meltRedeemSplit(ctx context.Context, r *Redeem, split *Note) (*Redeem, error) {
from := r.State
if err := s.store.TransitionRedeem(r.ID, from, RedeemConfirmed, map[string]interface{}{"last_error": ""}); err != nil {
return r, err
}
r.State = RedeemConfirmed
k1, err := s.secret(split)
if err != nil {
_ = s.store.TransitionRedeem(r.ID, RedeemConfirmed, from, map[string]interface{}{"last_error": err.Error()})
r.State = from
return r, err
}
return s.meltRedeem(ctx, r, RedeemInfo, k1, split.MintURLCallback(), split.ReportedMsat)
}
// meltRedeem creates the LNbits invoice for a confirmed redeem and melts the
// note into it. A failed invoice creation returns the redeem to resetTo.
func (s *Service) meltRedeem(ctx context.Context, r *Redeem, resetTo, meltK1, meltCallback string, valueMsat int64) (*Redeem, error) {
user := r.UserName
pr, hash, err := s.ledger.CreateInvoice(ctx, user, valueMsat/1000, "LNURLcash redeem "+ShortID(r.ID), RefRedeem+r.ID)
if err != nil {
_ = s.store.TransitionRedeem(r.ID, RedeemConfirmed, RedeemInfo, map[string]interface{}{"last_error": err.Error()})
@@ -368,6 +400,7 @@ func (s *Service) splitWholeSat(ctx context.Context, r *Redeem, noteURL string,
if _, perr := s.client.FetchNoteInfoByHash(ctx, link, target.Hash); perr == nil {
_ = s.store.TransitionNote(target.ID, "", NoteLive, map[string]interface{}{"value_msat": amount, "reported_msat": amount})
target.State, target.ValueMsat, target.ReportedMsat = NoteLive, amount, amount
_ = s.store.TransitionRedeem(r.ID, "", RedeemConfirmed, map[string]interface{}{"split_note_id": target.ID})
return target, nil
}
if lnurlcash.IsAmbiguous(err) {
+77
View File
@@ -0,0 +1,77 @@
package lnurlcash
import (
"context"
"errors"
"testing"
lnurlcashlib "github.com/lnurlcash/lnurlcash-go"
)
// failingInvoices makes invoice creation fail while failCreate is set.
type failingInvoices struct {
*fakeLedger
failCreate bool
}
func (l *failingInvoices) CreateInvoice(ctx context.Context, user string, sat int64, memo, ref string) (string, string, error) {
if l.failCreate {
return "", "", errors.New("lnbits unavailable")
}
return l.fakeLedger.CreateInvoice(ctx, user, sat, memo, ref)
}
func externalNote(h *harness, value int64) string {
k1, _ := lnurlcashlib.GenerateNoteSecret()
hash, _ := lnurlcashlib.HashK1(k1)
h.mint.mu.Lock()
h.mint.notes[hash] = value
h.mint.mu.Unlock()
return lnurlcashlib.BuildNoteURL(h.mint.url()+"/w", k1, value)
}
// A fractional note is burned by the split before the melt. After a failed
// invoice or melt the redeem must melt the split note the bot now holds.
func TestRedeemRetryUsesSplitNoteAfterFailure(t *testing.T) {
for _, mode := range []string{"invoice", "melt"} {
t.Run(mode, func(t *testing.T) {
h := newHarness(t)
ledger := &failingInvoices{fakeLedger: h.ledger}
cipher, _ := NewCipher(testKey)
svc := NewService(h.cfg, h.store, h.reg, ledger, cipher)
ctx := context.Background()
prev, err := svc.RedeemInfo(ctx, "7", "en", externalNote(h, 1000_900))
if err != nil {
t.Fatal(err)
}
switch mode {
case "invoice":
ledger.failCreate = true
case "melt":
h.mint.mu.Lock()
h.mint.meltReason = "payment failed: no route"
h.mint.mu.Unlock()
}
if _, err := svc.Redeem(ctx, prev.Redeem.ID, "7"); err == nil {
t.Fatal("the first attempt should fail")
}
ledger.failCreate = false
h.mint.mu.Lock()
h.mint.meltReason = ""
h.mint.mu.Unlock()
r, err := svc.Redeem(ctx, prev.Redeem.ID, "7")
if err != nil || r.State != RedeemMelting {
t.Fatalf("retry: state=%s err=%v", r.State, err)
}
if got := ledger.invoices[r.InvoiceHash].sat; got != 999 {
t.Errorf("invoice for %d sat, want 999 (the split note)", got)
}
ledger.settleIncoming(r.InvoiceHash)
st, err := svc.SettleInvoice(ctx, r.InvoiceHash)
if err != nil || st == nil || st.Redeem == nil || st.Redeem.State != RedeemSettled {
t.Fatalf("settle: %+v %v", st, err)
}
})
}
}