Skip to content

Commit

Permalink
refactor wallet tests to use mint with fake backend
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Nov 18, 2024
1 parent 8a36ddf commit a6a6385
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 348 deletions.
87 changes: 65 additions & 22 deletions mint/lightning/fakebackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,62 @@ import (
)

const (
FakePreimage = "0000000000000000"
FakePreimage = "0000000000000000"
FailPaymentDescription = "fail the payment"
)

type FakeBackendInvoice struct {
PaymentRequest string
PaymentHash string
Preimage string
Status State
Amount uint64
}

func (i *FakeBackendInvoice) ToInvoice() Invoice {
return Invoice{
PaymentRequest: i.PaymentRequest,
PaymentHash: i.PaymentHash,
Preimage: i.Preimage,
Settled: i.Status == Succeeded,
Amount: i.Amount,
}
}

type FakeBackend struct {
invoices []Invoice
Invoices []FakeBackendInvoice
PaymentDelay int64
}

func (fb *FakeBackend) ConnectionStatus() error { return nil }

func (fb *FakeBackend) CreateInvoice(amount uint64) (Invoice, error) {
req, preimage, paymentHash, err := createFakeInvoice(amount)
req, preimage, paymentHash, err := CreateFakeInvoice(amount, false)
if err != nil {
return Invoice{}, err
}

invoice := Invoice{
fakeInvoice := FakeBackendInvoice{
PaymentRequest: req,
PaymentHash: paymentHash,
Preimage: preimage,
Settled: true,
Status: Succeeded,
Amount: amount,
Expiry: uint64(time.Now().Unix()),
}
fb.invoices = append(fb.invoices, invoice)
fb.Invoices = append(fb.Invoices, fakeInvoice)

return invoice, nil
return fakeInvoice.ToInvoice(), nil
}

func (fb *FakeBackend) InvoiceStatus(hash string) (Invoice, error) {
invoiceIdx := slices.IndexFunc(fb.invoices, func(i Invoice) bool {
invoiceIdx := slices.IndexFunc(fb.Invoices, func(i FakeBackendInvoice) bool {
return i.PaymentHash == hash
})
if invoiceIdx == -1 {
return Invoice{}, errors.New("invoice does not exist")
}

return fb.invoices[invoiceIdx], nil
return fb.Invoices[invoiceIdx].ToInvoice(), nil
}

func (fb *FakeBackend) SendPayment(ctx context.Context, request string, amount uint64) (PaymentStatus, error) {
Expand All @@ -64,39 +83,58 @@ func (fb *FakeBackend) SendPayment(ctx context.Context, request string, amount u
return PaymentStatus{}, fmt.Errorf("error decoding invoice: %v", err)
}

outgoingPayment := Invoice{
PaymentRequest: request,
PaymentHash: invoice.PaymentHash,
Preimage: FakePreimage,
Settled: true,
status := Succeeded
if invoice.Description == FailPaymentDescription {
status = Failed
} else if fb.PaymentDelay > 0 {
if time.Now().Unix() < int64(invoice.CreatedAt)+fb.PaymentDelay {
status = Pending
}
}

outgoingPayment := FakeBackendInvoice{
PaymentHash: invoice.PaymentHash,
Preimage: FakePreimage,
Status: status,
Amount: uint64(invoice.MSatoshi) * 1000,
}
fb.invoices = append(fb.invoices, outgoingPayment)
fb.Invoices = append(fb.Invoices, outgoingPayment)

return PaymentStatus{
Preimage: FakePreimage,
PaymentStatus: Succeeded,
PaymentStatus: status,
}, nil
}

func (fb *FakeBackend) OutgoingPaymentStatus(ctx context.Context, hash string) (PaymentStatus, error) {
invoiceIdx := slices.IndexFunc(fb.invoices, func(i Invoice) bool {
invoiceIdx := slices.IndexFunc(fb.Invoices, func(i FakeBackendInvoice) bool {
return i.PaymentHash == hash
})
if invoiceIdx == -1 {
return PaymentStatus{}, errors.New("payment does not exist")
}

return PaymentStatus{
Preimage: fb.invoices[invoiceIdx].Preimage,
PaymentStatus: Succeeded,
Preimage: FakePreimage,
PaymentStatus: fb.Invoices[invoiceIdx].Status,
}, nil
}

func (fb *FakeBackend) FeeReserve(amount uint64) uint64 {
return 0
}

func createFakeInvoice(amount uint64) (string, string, string, error) {
func (fb *FakeBackend) SetInvoiceStatus(hash string, status State) {
invoiceIdx := slices.IndexFunc(fb.Invoices, func(i FakeBackendInvoice) bool {
return i.PaymentHash == hash
})
if invoiceIdx == -1 {
return
}
fb.Invoices[invoiceIdx].Status = status
}

func CreateFakeInvoice(amount uint64, failPayment bool) (string, string, string, error) {
var random [32]byte
_, err := rand.Read(random[:])
if err != nil {
Expand All @@ -106,12 +144,17 @@ func createFakeInvoice(amount uint64) (string, string, string, error) {
paymentHash := sha256.Sum256(random[:])
hash := hex.EncodeToString(paymentHash[:])

description := "test"
if failPayment {
description = FailPaymentDescription
}

invoice, err := zpay32.NewInvoice(
&chaincfg.SigNetParams,
paymentHash,
time.Now(),
zpay32.Amount(lnwire.MilliSatoshi(amount*1000)),
zpay32.Description("test"),
zpay32.Description(description),
)
if err != nil {
return "", "", "", err
Expand Down
43 changes: 19 additions & 24 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ func FundCashuWallet(ctx context.Context, wallet *wallet.Wallet, lnd *btcdocker.
return fmt.Errorf("error requesting mint: %v", err)
}

//pay invoice
sendPaymentRequest := lnrpc.SendRequest{
PaymentRequest: mintRes.Request,
}
response, _ := lnd.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
return fmt.Errorf("error paying invoice: %v", response.PaymentError)
if lnd != nil {
//pay invoice
sendPaymentRequest := lnrpc.SendRequest{
PaymentRequest: mintRes.Request,
}
response, _ := lnd.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
return fmt.Errorf("error paying invoice: %v", response.PaymentError)
}
}

_, err = wallet.MintTokens(mintRes.Quote)
Expand All @@ -188,7 +190,7 @@ func FundCashuWallet(ctx context.Context, wallet *wallet.Wallet, lnd *btcdocker.
}

func MintConfig(
lnd *btcdocker.Lnd,
backend lightning.Client,
port string,
derivationPathIdx uint32,
dbpath string,
Expand All @@ -200,17 +202,6 @@ func MintConfig(
return nil, err
}

var lightningClient lightning.Client
if lnd != nil {
var err error
lightningClient, err = LndClient(lnd, dbpath)
if err != nil {
return nil, err
}
} else {
lightningClient = &lightning.FakeBackend{}
}

timeout := time.Second * 2
mintConfig := &mint.Config{
DerivationPathIdx: derivationPathIdx,
Expand All @@ -219,7 +210,7 @@ func MintConfig(
DBMigrationPath: dbMigrationPath,
InputFeePpk: inputFeePpk,
Limits: limits,
LightningClient: lightningClient,
LightningClient: backend,
LogLevel: mint.Disable,
MeltTimeout: &timeout,
}
Expand Down Expand Up @@ -282,7 +273,11 @@ func CreateTestMint(
inputFeePpk uint,
limits mint.MintLimits,
) (*mint.Mint, error) {
config, err := MintConfig(lnd, "", 0, dbpath, dbMigrationPath, inputFeePpk, limits)
lndClient, err := LndClient(lnd, dbpath)
if err != nil {
return nil, err
}
config, err := MintConfig(lndClient, "", 0, dbpath, dbMigrationPath, inputFeePpk, limits)
if err != nil {
return nil, err
}
Expand All @@ -295,14 +290,14 @@ func CreateTestMint(
}

func CreateTestMintServer(
lnd *btcdocker.Lnd,
backend lightning.Client,
port string,
derivationPathIdx uint32,
dbpath string,
dbMigrationPath string,
inputFeePpk uint,
) (*mint.MintServer, error) {
config, err := MintConfig(lnd, port, derivationPathIdx, dbpath, dbMigrationPath, inputFeePpk, mint.MintLimits{})
config, err := MintConfig(backend, port, derivationPathIdx, dbpath, dbMigrationPath, inputFeePpk, mint.MintLimits{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -685,7 +680,7 @@ type NutshellMintContainer struct {

func CreateNutshellMintContainer(ctx context.Context, inputFeePpk int) (*NutshellMintContainer, error) {
req := testcontainers.ContainerRequest{
Image: "cashubtc/nutshell:0.16.0",
Image: "cashubtc/nutshell:latest",
ExposedPorts: []string{"3338"},
Cmd: []string{
"poetry",
Expand Down
Loading

0 comments on commit a6a6385

Please sign in to comment.