Skip to content

Commit

Permalink
mint - check if melt quote already exists for invoice in request
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Oct 4, 2024
1 parent 47d55a2 commit a0bd83c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ var (
MeltQuotePending = Error{Detail: "quote is pending", Code: MeltQuotePendingErrCode}
MeltQuoteAlreadyPaid = Error{Detail: "quote already paid", Code: MeltQuoteAlreadyPaidErrCode}
MeltAmountExceededErr = Error{Detail: "max amount for melting exceeded", Code: AmountLimitExceeded}
MeltQuoteForRequestExists = Error{Detail: "melt quote for payment request already exists", Code: MeltQuoteErrCode}
InsufficientProofsAmount = Error{
Detail: "amount of input proofs is below amount needed for transaction",
Code: InsufficientProofAmountErrCode,
Expand Down
6 changes: 6 additions & 0 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,12 @@ func (m *Mint) RequestMeltQuote(method, request, unit string) (storage.MeltQuote
}
}

// check if a melt quote for the invoice already exists
quote, _ := m.db.GetMeltQuoteByPaymentRequest(request)
if quote != nil {
return storage.MeltQuote{}, cashu.MeltQuoteForRequestExists
}

quoteId, err := cashu.GenerateRandomQuoteId()
if err != nil {
m.logErrorf("error generating random quote id: %v", err)
Expand Down
12 changes: 12 additions & 0 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ func TestRequestMeltQuote(t *testing.T) {
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}

// trying to create another melt quote with same invoice should throw error
_, err = testMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if !errors.Is(err, cashu.MeltQuoteForRequestExists) {
//if !errors.Is(err, cashu.PaymentMethodNotSupportedErr) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.MeltQuoteForRequestExists, err)
}
}

func TestMeltQuoteState(t *testing.T) {
Expand Down Expand Up @@ -518,6 +525,11 @@ func TestMelt(t *testing.T) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.MeltQuoteAlreadyPaid, err)
}

invoice = lnrpc.Invoice{Value: 6000}
addInvoiceResponse, err = lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}
// test already used proofs
newQuote, err := testMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

DROP INDEX IF EXISTS idx_melt_quotes_request;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

CREATE INDEX IF NOT EXISTS idx_melt_quotes_request ON melt_quotes(request);
24 changes: 24 additions & 0 deletions mint/storage/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,30 @@ func (sqlite *SQLiteDB) GetMeltQuote(quoteId string) (storage.MeltQuote, error)
return meltQuote, nil
}

func (sqlite *SQLiteDB) GetMeltQuoteByPaymentRequest(invoice string) (*storage.MeltQuote, error) {
row := sqlite.db.QueryRow("SELECT * FROM melt_quotes WHERE request = ?", invoice)

var meltQuote storage.MeltQuote
var state string

err := row.Scan(
&meltQuote.Id,
&meltQuote.InvoiceRequest,
&meltQuote.PaymentHash,
&meltQuote.Amount,
&meltQuote.FeeReserve,
&state,
&meltQuote.Expiry,
&meltQuote.Preimage,
)
if err != nil {
return nil, err
}
meltQuote.State = nut05.StringToState(state)

return &meltQuote, nil
}

func (sqlite *SQLiteDB) UpdateMeltQuote(quoteId, preimage string, state nut05.State) error {
updatedState := state.String()
result, err := sqlite.db.Exec(
Expand Down
2 changes: 2 additions & 0 deletions mint/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type MintDB interface {

SaveMeltQuote(MeltQuote) error
GetMeltQuote(string) (MeltQuote, error)
// used to check if a melt quote already exists for the passed invoice
GetMeltQuoteByPaymentRequest(string) (*MeltQuote, error)
UpdateMeltQuote(quoteId string, preimage string, state nut05.State) error

SaveBlindSignature(B_ string, blindSignature cashu.BlindedSignature) error
Expand Down

0 comments on commit a0bd83c

Please sign in to comment.