Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only log a warning if there is no preimage when settling an incoming transaction #666

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions transactions/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ func TestMarkSettled_Sent(t *testing.T) {
assert.Equal(t, &dbTransaction, settledTransaction)
}

func TestMarkSettled_SentNoPreimage(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

dbTransaction := db.Transaction{
State: constants.TRANSACTION_STATE_PENDING,
Type: constants.TRANSACTION_TYPE_OUTGOING,
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
AmountMsat: 123000,
}
svc.DB.Create(&dbTransaction)

mockEventConsumer := tests.NewMockEventConsumer()
svc.EventPublisher.RegisterSubscriber(mockEventConsumer)
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
err = svc.DB.Transaction(func(tx *gorm.DB) error {
_, err = transactionsService.markTransactionSettled(tx, &dbTransaction, "", 0, false)
return err
})

assert.Error(t, err)
assert.ErrorContains(t, err, "no preimage in payment")
}

func TestMarkSettled_Received(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand Down Expand Up @@ -111,6 +136,36 @@ func TestMarkSettled_Received(t *testing.T) {
assert.Equal(t, &dbTransaction, settledTransaction)
}

func TestMarkSettled_ReceivedNoPreimage(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
assert.NoError(t, err)

dbTransaction := db.Transaction{
State: constants.TRANSACTION_STATE_PENDING,
Type: constants.TRANSACTION_TYPE_INCOMING,
PaymentHash: tests.MockLNClientTransaction.PaymentHash,
AmountMsat: 123000,
}
svc.DB.Create(&dbTransaction)

mockEventConsumer := tests.NewMockEventConsumer()
svc.EventPublisher.RegisterSubscriber(mockEventConsumer)
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
err = svc.DB.Transaction(func(tx *gorm.DB) error {
_, err = transactionsService.markTransactionSettled(tx, &dbTransaction, "", 0, false)
return err
})

// incoming payment does not necessarily need preimage
assert.Nil(t, err)
assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, dbTransaction.State)
assert.Equal(t, 1, len(mockEventConsumer.GetConsumeEvents()))
assert.Equal(t, "nwc_payment_received", mockEventConsumer.GetConsumeEvents()[0].Event)
settledTransaction := mockEventConsumer.GetConsumeEvents()[0].Properties.(*db.Transaction)
assert.Equal(t, &dbTransaction, settledTransaction)
}

func TestDoNotMarkSettledTwice(t *testing.T) {
defer tests.RemoveTestService()
svc, err := tests.CreateTestService()
Expand Down
8 changes: 7 additions & 1 deletion transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,13 @@ func (svc *transactionsService) markTransactionSettled(tx *gorm.DB, dbTransactio
}

if preimage == "" {
return nil, errors.New("no preimage in payment")
// outgoing payments MUST have a preimage
if dbTransaction.Type == constants.TRANSACTION_TYPE_OUTGOING {
return nil, errors.New("no preimage in payment")
}
// incoming payments SHOULD have a preimage
// (currently cashu backend does not)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does this apply? only cashu? why can transactions not have a preimage.

is cashu worth this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wanted to add that not only are the transactions not showing up in history, I believe the following issue is due to the same cause (not having a preimage).

When an invoice is created, in the Hub or Go, the GUI waits for payment indefinitely.

image

logger.Logger.WithField("payment_hash", dbTransaction.PaymentHash).Warn("no preimage in settled incoming transaction")
}

now := time.Now()
Expand Down
Loading