Files
lspd/itest/intercept_zero_conf_test.go
2022-12-16 11:36:51 +01:00

146 lines
4.9 KiB
Go

package itest
import (
"log"
"time"
"github.com/breez/lntest"
lspd "github.com/breez/lspd/rpc"
"github.com/stretchr/testify/assert"
)
var htlcInterceptorDelay = time.Second * 7
func testOpenZeroConfChannelOnReceive(p *testParams) {
alice := lntest.NewClnNode(p.h, p.m, "Alice")
alice.Fund(10000000)
p.lsp.LightningNode().Fund(10000000)
log.Print("Opening channel between Alice and the lsp")
channel := alice.OpenChannel(p.lsp.LightningNode(), &lntest.OpenChannelOptions{
AmountSat: publicChanAmount,
})
alice.WaitForChannelReady(channel)
log.Printf("Adding bob's invoices")
outerAmountMsat := uint64(2100000)
innerAmountMsat := calculateInnerAmountMsat(p.lsp, outerAmountMsat)
description := "Please pay me"
innerInvoice, outerInvoice := p.BreezClient().GenerateInvoices(generateInvoicesRequest{
innerAmountMsat: innerAmountMsat,
outerAmountMsat: outerAmountMsat,
description: description,
lsp: p.lsp,
})
log.Print("Connecting bob to lspd")
p.BreezClient().lightningNode.ConnectPeer(p.lsp.LightningNode())
// NOTE: We pretend to be paying fees to the lsp, but actually we won't.
log.Printf("Registering payment with lsp")
pretendAmount := outerAmountMsat - 2000000
RegisterPayment(p.lsp, &lspd.PaymentInformation{
PaymentHash: innerInvoice.paymentHash,
PaymentSecret: innerInvoice.paymentSecret,
Destination: p.BreezClient().lightningNode.NodeId(),
IncomingAmountMsat: int64(outerAmountMsat),
OutgoingAmountMsat: int64(pretendAmount),
})
// TODO: Fix race waiting for htlc interceptor.
log.Printf("Waiting %v to allow htlc interceptor to activate.", htlcInterceptorDelay)
<-time.After(htlcInterceptorDelay)
log.Printf("Alice paying")
payResp := alice.Pay(outerInvoice.bolt11)
bobInvoice := p.BreezClient().lightningNode.GetInvoice(payResp.PaymentHash)
assert.Equal(p.t, payResp.PaymentPreimage, bobInvoice.PaymentPreimage)
assert.Equal(p.t, innerAmountMsat, bobInvoice.AmountReceivedMsat)
// Make sure capacity is correct
chans := p.BreezClient().lightningNode.GetChannels()
assert.Len(p.t, chans, 1)
c := chans[0]
AssertChannelCapacity(p.t, outerAmountMsat, c.CapacityMsat)
}
func testOpenZeroConfSingleHtlc(p *testParams) {
alice := lntest.NewClnNode(p.h, p.m, "Alice")
alice.Fund(10000000)
p.lsp.LightningNode().Fund(10000000)
log.Print("Opening channel between Alice and the lsp")
channel := alice.OpenChannel(p.lsp.LightningNode(), &lntest.OpenChannelOptions{
AmountSat: publicChanAmount,
})
channelId := alice.WaitForChannelReady(channel)
log.Printf("Adding bob's invoices")
outerAmountMsat := uint64(2100000)
innerAmountMsat := calculateInnerAmountMsat(p.lsp, outerAmountMsat)
description := "Please pay me"
innerInvoice, outerInvoice := p.BreezClient().GenerateInvoices(generateInvoicesRequest{
innerAmountMsat: innerAmountMsat,
outerAmountMsat: outerAmountMsat,
description: description,
lsp: p.lsp,
})
log.Print("Connecting bob to lspd")
p.BreezClient().lightningNode.ConnectPeer(p.lsp.LightningNode())
// NOTE: We pretend to be paying fees to the lsp, but actually we won't.
log.Printf("Registering payment with lsp")
pretendAmount := outerAmountMsat - 2000000
RegisterPayment(p.lsp, &lspd.PaymentInformation{
PaymentHash: innerInvoice.paymentHash,
PaymentSecret: innerInvoice.paymentSecret,
Destination: p.BreezClient().lightningNode.NodeId(),
IncomingAmountMsat: int64(outerAmountMsat),
OutgoingAmountMsat: int64(pretendAmount),
})
// TODO: Fix race waiting for htlc interceptor.
log.Printf("Waiting %v to allow htlc interceptor to activate.", htlcInterceptorDelay)
<-time.After(htlcInterceptorDelay)
log.Printf("Alice paying")
route := constructRoute(p.lsp.LightningNode(), p.BreezClient().lightningNode, channelId, lntest.NewShortChanIDFromString("1x0x0"), outerAmountMsat)
payResp := alice.PayViaRoute(outerAmountMsat, outerInvoice.paymentHash, outerInvoice.paymentSecret, route)
bobInvoice := p.BreezClient().lightningNode.GetInvoice(payResp.PaymentHash)
assert.Equal(p.t, payResp.PaymentPreimage, bobInvoice.PaymentPreimage)
assert.Equal(p.t, innerAmountMsat, bobInvoice.AmountReceivedMsat)
// Make sure capacity is correct
chans := p.BreezClient().lightningNode.GetChannels()
assert.Len(p.t, chans, 1)
c := chans[0]
AssertChannelCapacity(p.t, outerAmountMsat, c.CapacityMsat)
}
func constructRoute(
lsp lntest.LightningNode,
bob lntest.LightningNode,
aliceLspChannel lntest.ShortChannelID,
lspBobChannel lntest.ShortChannelID,
amountMsat uint64,
) *lntest.Route {
return &lntest.Route{
Hops: []*lntest.Hop{
{
Id: lsp.NodeId(),
Channel: aliceLspChannel,
AmountMsat: amountMsat + uint64(lspBaseFeeMsat) + (amountMsat * uint64(lspFeeRatePpm) / 1000000),
Delay: 144 + lspCltvDelta,
},
{
Id: bob.NodeId(),
Channel: lspBobChannel,
AmountMsat: amountMsat,
Delay: 144,
},
},
}
}