Skip to content

Commit

Permalink
add key to mint quotes tests in mint and wallet db
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Feb 12, 2025
1 parent 62e083c commit 7e7ee7b
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 45 deletions.
113 changes: 75 additions & 38 deletions mint/storage/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sqlite

import (
"bytes"
"encoding/hex"
"log"
"math/rand/v2"
Expand All @@ -11,6 +12,7 @@ import (
"sync"
"testing"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/elnosh/gonuts/cashu"
"github.com/elnosh/gonuts/cashu/nuts/nut04"
"github.com/elnosh/gonuts/cashu/nuts/nut05"
Expand Down Expand Up @@ -158,49 +160,50 @@ func TestPendingProofs(t *testing.T) {
}

func TestMintQuotes(t *testing.T) {
mintQuotes := generateRandomMintQuotes(150)
mintQuotes := generateRandomMintQuotes(150, false)

var wg sync.WaitGroup
errChan := make(chan error, 150)
done := make(chan interface{})
var mu sync.RWMutex
errs := make([]error, 0)
for _, quote := range mintQuotes {
wg.Add(1)
go func() {
go func(quote storage.MintQuote) {
if err := db.SaveMintQuote(quote); err != nil {
errChan <- err
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}
wg.Done()
}()
}(quote)
}
wg.Wait()
go func() {
done <- struct{}{}
}()

select {
case err := <-errChan:
t.Fatalf("error saving mint quote: %v", err)
case <-done:
if len(errs) > 0 {
t.Fatalf("error saving mint quote: %v", errs[0])
}

expectedQuote := mintQuotes[21]
quote, err := db.GetMintQuote(expectedQuote.Id)
if err != nil {
t.Fatalf("error getting mint quote by id: %v", err)
}

if !reflect.DeepEqual(expectedQuote, quote) {
t.Fatal("quote from db does not match generated one")
}
if quote.Pubkey != nil {
t.Fatalf("expected nil pubkey but got '%v'", quote.Pubkey)
}

quote, err = db.GetMintQuoteByPaymentHash(expectedQuote.PaymentHash)
if err != nil {
t.Fatalf("error getting mint quote by payment hash: %v", err)
}

if !reflect.DeepEqual(expectedQuote, quote) {
t.Fatal("quote from db does not match generated one")
}
if quote.Pubkey != nil {
t.Fatalf("expected nil pubkey but got '%v'", quote.Pubkey)
}

if err := db.UpdateMintQuoteState(quote.Id, nut04.Paid); err != nil {
t.Fatalf("error updating mint quote: %v", err)
Expand All @@ -227,32 +230,62 @@ func TestMintQuotes(t *testing.T) {
if !reflect.DeepEqual(expectedQuote, quote) {
t.Fatal("quote from db does not match generated one")
}

// test mint quotes with pubkey
mintQuotes = generateRandomMintQuotes(20, true)

errs = make([]error, 0)
for _, quote := range mintQuotes {
wg.Add(1)
go func(quote storage.MintQuote) {
if err := db.SaveMintQuote(quote); err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}
wg.Done()
}(quote)
}
wg.Wait()

expectedQuote = mintQuotes[10]
quote, err = db.GetMintQuote(expectedQuote.Id)
if err != nil {
t.Fatalf("error getting mint quote by id: %v", err)
}
if !reflect.DeepEqual(expectedQuote, quote) {
t.Fatal("quote from db does not match generated one")
}
if expectedQuote.Pubkey == nil {
t.Fatal("expected pubkey in mint quote but got nil")
}
expectedPubkey := expectedQuote.Pubkey.SerializeCompressed()
if bytes.Compare(expectedPubkey, quote.Pubkey.SerializeCompressed()) != 0 {
t.Fatalf("expected pubkey '%v' but got '%v'", expectedPubkey, quote.Pubkey.SerializeCompressed())
}
}

func TestMeltQuote(t *testing.T) {
meltQuotes := generateRandomMeltQuotes(150)

var wg sync.WaitGroup
errChan := make(chan error, 150)
done := make(chan interface{})
var mu sync.RWMutex
errs := make([]error, 0)
for _, quote := range meltQuotes {
wg.Add(1)
go func() {
go func(quote storage.MeltQuote) {
if err := db.SaveMeltQuote(quote); err != nil {
errChan <- err
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}
wg.Done()
}()
}(quote)
}
wg.Wait()
go func() {
done <- struct{}{}
}()

select {
case err := <-errChan:
t.Fatalf("error saving melt quote: %v", err)
case <-done:
if len(errs) > 0 {
t.Fatalf("error saving melt quote: %v", errs[0])
}

expectedQuote := meltQuotes[21]
Expand Down Expand Up @@ -308,26 +341,23 @@ func TestBlindSignatures(t *testing.T) {
blindSignatures := generateBlindSignatures(count)

var wg sync.WaitGroup
errChan := make(chan error, count)
done := make(chan interface{})
var mu sync.RWMutex
errs := make([]error, 0)
for i := 0; i < count; i++ {
wg.Add(1)
go func() {
if err := db.SaveBlindSignature(blindedMessages[i], blindSignatures[i]); err != nil {
errChan <- err
mu.Lock()
errs = append(errs, err)
mu.Unlock()
}
wg.Done()
}()
}
wg.Wait()
go func() {
done <- struct{}{}
}()

select {
case err := <-errChan:
t.Fatalf("error saving blind signature: %v", err)
case <-done:
if len(errs) > 0 {
t.Fatalf("error saving blind signature: %v", errs[0])
}

expectedBlindSig := blindSignatures[21]
Expand Down Expand Up @@ -394,7 +424,7 @@ func sortDBProofs(proofs []storage.DBProof) {
})
}

func generateRandomMintQuotes(num int) []storage.MintQuote {
func generateRandomMintQuotes(num int, pubkey bool) []storage.MintQuote {
quotes := make([]storage.MintQuote, num)
for i := 0; i < num; i++ {
quote := storage.MintQuote{
Expand All @@ -404,6 +434,13 @@ func generateRandomMintQuotes(num int) []storage.MintQuote {
PaymentHash: generateRandomString(50),
State: nut04.Unpaid,
}
if pubkey {
key, err := secp256k1.GeneratePrivateKey()
if err != nil {
panic(err)
}
quote.Pubkey = key.PubKey()
}
quotes[i] = quote
}
return quotes
Expand Down
49 changes: 42 additions & 7 deletions wallet/storage/bolt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"bytes"
"encoding/hex"
"log"
"math/rand/v2"
Expand Down Expand Up @@ -221,12 +222,12 @@ func TestKeysets(t *testing.T) {

func TestMintQuotes(t *testing.T) {
quoteId := "quoteId1"
mintQuote := generateMintQuote(quoteId)
mintQuote := generateMintQuote(quoteId, false)
if err := db.SaveMintQuote(mintQuote); err != nil {
t.Fatalf("error saving mint quote: %v", err)
}

mintQuotes := generateRandomMintQuotes(50)
mintQuotes := generateRandomMintQuotes(50, false)
for _, quote := range mintQuotes {
if err := db.SaveMintQuote(quote); err != nil {
t.Fatalf("error saving mint quote: %v", err)
Expand All @@ -238,16 +239,41 @@ func TestMintQuotes(t *testing.T) {
if quoteById == nil {
t.Fatal("expected valid quote but got nil")
}

if !reflect.DeepEqual(mintQuote, *quoteById) {
t.Fatal("mint quote from db does not match generated one")
}
if quoteById.PrivateKey != nil {
t.Fatalf("expected nil private key but got %v", quoteById.PrivateKey)
}

quotesFromDb := db.GetMintQuotes()
expectedNumQuotes := 51
if len(quotesFromDb) != expectedNumQuotes {
t.Fatalf("expected '%v' mint quotes but got '%v' ", expectedNumQuotes, len(quotesFromDb))
}

// test mint quote with private key
quoteId = "quote-with-privatekey"
mintQuote = generateMintQuote(quoteId, true)
if err := db.SaveMintQuote(mintQuote); err != nil {
t.Fatalf("error saving mint quote: %v", err)
}
quoteById = db.GetMintQuoteById(quoteId)
if quoteById == nil {
t.Fatal("expected valid quote but got nil")
}
if !reflect.DeepEqual(mintQuote, *quoteById) {
t.Fatal("mint quote from db does not match generated one")
}
if quoteById.PrivateKey == nil {
t.Fatal("expected private key but got nil")
}

expectedKey := mintQuote.PrivateKey.Serialize()
if bytes.Compare(expectedKey, quoteById.PrivateKey.Serialize()) != 0 {
t.Fatalf("expected key '%v' but got '%v'", expectedKey, quoteById.PrivateKey.Serialize())
}

}

func TestMeltQuotes(t *testing.T) {
Expand Down Expand Up @@ -351,21 +377,30 @@ func generateKeyset(mint string) crypto.WalletKeyset {
}
}

func generateMintQuote(id string) MintQuote {
return MintQuote{
func generateMintQuote(id string, privateKey bool) MintQuote {
mintQuote := MintQuote{
QuoteId: id,
Mint: "http://localhost:3338",
Method: "bolt11",
State: nut04.Unpaid,
Amount: 21,
}
if privateKey {
pk, err := secp256k1.GeneratePrivateKey()
if err != nil {
panic(err)
}
mintQuote.PrivateKey = pk
}

return mintQuote
}

func generateRandomMintQuotes(num int) []MintQuote {
func generateRandomMintQuotes(num int, privateKey bool) []MintQuote {
quotes := make([]MintQuote, num)
for i := 0; i < num; i++ {
id := generateRandomString(32)
quote := generateMintQuote(id)
quote := generateMintQuote(id, privateKey)
quotes[i] = quote
}
return quotes
Expand Down

0 comments on commit 7e7ee7b

Please sign in to comment.