Do not fail a funding payment that LNbits may still settle

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-14 18:25:01 +00:00
co-authored by Claude Sonnet 5.5
parent 82b291a69b
commit b6178e335d
2 changed files with 56 additions and 2 deletions
+29 -1
View File
@@ -17,6 +17,7 @@ import (
"github.com/LightningTipBot/LightningTipBot/internal/runtime"
"github.com/LightningTipBot/LightningTipBot/internal/str"
"github.com/LightningTipBot/LightningTipBot/internal/telegram/intercept"
decodepay "github.com/fiatjaf/ln-decodepay"
log "github.com/sirupsen/logrus"
tb "gopkg.in/lightningtipbot/telebot.v3"
)
@@ -100,7 +101,16 @@ func (l *lnbitsLedger) PayInvoice(ctx context.Context, userName, pr string) (str
if err != nil {
var lnErr lnbits.Error
if errors.As(err, &lnErr) {
return "", err // LNbits answered: definitive
// LNbits answered with an error, but the payment can still be in
// flight or have settled, so ask LNbits what became of it.
var paid, pending bool
var lookupErr error
if b, derr := decodepay.Decodepay(pr); derr == nil {
paid, pending, lookupErr = l.PaymentSucceeded(ctx, userName, b.PaymentHash)
} else {
lookupErr = derr
}
return "", classifyPayError(err, paid, pending, lookupErr)
}
return "", &lnurlcash.AmbiguousLedgerError{Err: err}
}
@@ -390,3 +400,21 @@ func formatSat(msat int64) string {
}
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.3f", float64(msat)/1000), "0"), ".")
}
// classifyPayError turns an LNbits error from a payment into the ledger error.
// The payment is only a definitive failure when LNbits itself says it neither
// settled nor is pending. If that cannot be established the outcome is
// ambiguous and the batch is left for recovery instead of being failed.
func classifyPayError(payErr error, paid, pending bool, lookupErr error) error {
if paid || pending {
return &lnurlcash.AmbiguousLedgerError{Err: payErr}
}
if lookupErr != nil {
var lnErr lnbits.Error
if errors.As(lookupErr, &lnErr) {
return payErr // LNbits does not know the payment: it never started
}
return &lnurlcash.AmbiguousLedgerError{Err: payErr}
}
return payErr
}
+27 -1
View File
@@ -1,6 +1,12 @@
package telegram
import "testing"
import (
"errors"
"testing"
"github.com/LightningTipBot/LightningTipBot/internal/lnbits"
"github.com/LightningTipBot/LightningTipBot/internal/lnurlcash"
)
func TestParseBatchCommand(t *testing.T) {
amount, count, memo, ok := parseBatchCommand("/notes 100 x 10 party time")
@@ -60,3 +66,23 @@ func TestPreserveNoteCase(t *testing.T) {
t.Fatalf("got %q", got)
}
}
func TestClassifyPayError(t *testing.T) {
payErr := lnbits.Error{Detail: "boom"}
ambiguous := func(err error) bool { return lnurlcash.IsAmbiguousLedger(err) }
if !ambiguous(classifyPayError(payErr, true, false, nil)) {
t.Error("a settled payment must be ambiguous, not failed")
}
if !ambiguous(classifyPayError(payErr, false, true, nil)) {
t.Error("a pending payment must be ambiguous")
}
if !ambiguous(classifyPayError(payErr, false, false, errors.New("timeout"))) {
t.Error("an unreachable lookup must be ambiguous")
}
if ambiguous(classifyPayError(payErr, false, false, lnbits.Error{Detail: "not found"})) {
t.Error("a payment LNbits does not know is a definitive failure")
}
if ambiguous(classifyPayError(payErr, false, false, nil)) {
t.Error("a payment LNbits reports as failed is definitive")
}
}