Skip to content

Commit

Permalink
mint - tests for mint limits
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Aug 1, 2024
1 parent fcbb3bf commit 0bc3f33
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
109 changes: 105 additions & 4 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func testMain(m *testing.M) int {
}

testMintPath := filepath.Join(".", "testmint1")
testMint, err = testutils.CreateTestMint(lnd1, testMintPath, dbMigrationPath, 0)
testMint, err = testutils.CreateTestMint(lnd1, testMintPath, dbMigrationPath, 0, mint.MintLimits{})
if err != nil {
log.Println(err)
return 1
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestSwap(t *testing.T) {

// mint with fees
mintFeesPath := filepath.Join(".", "mintfees")
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100)
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100, mint.MintLimits{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -330,7 +330,7 @@ func TestSwap(t *testing.T) {
}
}

func TestMeltRequest(t *testing.T) {
func TestRequestMeltQuote(t *testing.T) {
invoice := lnrpc.Invoice{Value: 10000}
addInvoiceResponse, err := lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
Expand Down Expand Up @@ -499,7 +499,7 @@ func TestMelt(t *testing.T) {

// mint with fees
mintFeesPath := filepath.Join(".", "mintfeesmelt")
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100)
mintFees, err := testutils.CreateTestMint(lnd1, mintFeesPath, dbMigrationPath, 100, mint.MintLimits{})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -548,3 +548,104 @@ func TestMelt(t *testing.T) {
}

}

func TestMintLimits(t *testing.T) {
// setup mint with limits
limitsMintPath := filepath.Join(".", "limitsMint")
mintLimits := mint.MintLimits{
MaxBalance: 15000,
MintingSettings: mint.MintMethodSettings{MaxAmount: 10000},
MeltingSettings: mint.MeltMethodSettings{MaxAmount: 10000},
}

limitsMint, err := testutils.CreateTestMint(
lnd1,
limitsMintPath,
dbMigrationPath,
100,
mintLimits,
)

if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(limitsMintPath)
}()

var keyset crypto.MintKeyset
for _, k := range limitsMint.ActiveKeysets {
keyset = k
break
}

// test above mint max amount
var mintAmount uint64 = 20000
mintQuoteResponse, err := limitsMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT)
if !errors.Is(err, cashu.MintAmountExceededErr) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.MintAmountExceededErr, err)
}

// amount below max limit
mintAmount = 9500
mintQuoteResponse, err = limitsMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("error requesting mint quote: %v", err)
}

blindedMessages, secrets, rs, _ := testutils.CreateBlindedMessages(mintAmount, keyset)

//pay invoice
sendPaymentRequest := lnrpc.SendRequest{
PaymentRequest: mintQuoteResponse.PaymentRequest,
}
response, _ := lnd2.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
t.Fatalf("error paying invoice: %v", response.PaymentError)
}
blindedSignatures, err := limitsMint.MintTokens(testutils.BOLT11_METHOD, mintQuoteResponse.Id, blindedMessages)
if err != nil {
t.Fatalf("got unexpected error minting tokens: %v", err)
}

// test request mint that will make it go above max balance
mintQuoteResponse, err = limitsMint.RequestMintQuote(testutils.BOLT11_METHOD, 9000, testutils.SAT_UNIT)
if !errors.Is(err, cashu.MintingDisabled) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.MintingDisabled, err)
}

// test melt with invoice over max melt amount
invoice := lnrpc.Invoice{Value: 15000}
addInvoiceResponse, err := lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}

_, err = limitsMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if !errors.Is(err, cashu.MeltAmountExceededErr) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.MeltAmountExceededErr, err)
}

// test melt with invoice within limit
validProofs, err := testutils.ConstructProofs(blindedSignatures, secrets, rs, &keyset)
invoice = lnrpc.Invoice{Value: 8000}
addInvoiceResponse, err = lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}
meltQuote, err := limitsMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}
_, err = limitsMint.MeltTokens(testutils.BOLT11_METHOD, meltQuote.Id, validProofs)
if err != nil {
t.Fatalf("got unexpected error in melt: %v", err)
}

// this should be within max balance now
mintQuoteResponse, err = limitsMint.RequestMintQuote(testutils.BOLT11_METHOD, 9000, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("got unexpected error requesting mint quote: %v", err)
}

}
7 changes: 5 additions & 2 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func mintConfig(
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
limits mint.MintLimits,
) (*mint.Config, error) {
if err := os.MkdirAll(dbpath, 0750); err != nil {
return nil, err
Expand All @@ -192,6 +193,7 @@ func mintConfig(
DBPath: dbpath,
DBMigrationPath: dbMigrationPath,
InputFeePpk: inputFeePpk,
Limits: limits,
}
nodeDir := lnd.LndDir

Expand Down Expand Up @@ -219,8 +221,9 @@ func CreateTestMint(
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
limits mint.MintLimits,
) (*mint.Mint, error) {
config, err := mintConfig(lnd, "", dbpath, dbMigrationPath, inputFeePpk)
config, err := mintConfig(lnd, "", dbpath, dbMigrationPath, inputFeePpk, limits)
if err != nil {
return nil, err
}
Expand All @@ -239,7 +242,7 @@ func CreateTestMintServer(
dbMigrationPath string,
inputFeePpk uint,
) (*mint.MintServer, error) {
config, err := mintConfig(lnd, port, dbpath, dbMigrationPath, inputFeePpk)
config, err := mintConfig(lnd, port, dbpath, dbMigrationPath, inputFeePpk, mint.MintLimits{})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 0bc3f33

Please sign in to comment.