Skip to content

Commit

Permalink
invoices: add test to persist and retrieve invoice with blinded path
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Aug 16, 2024
1 parent 7498e5c commit d7ea024
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions invoices/invoices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
Expand All @@ -35,6 +36,15 @@ var (
lnwire.Features,
)

blindedPathFeatures = lnwire.NewFeatureVector(
lnwire.NewRawFeatureVector(
lnwire.TLVOnionPayloadOptional,
lnwire.RouteBlindingOptional,
lnwire.Bolt11BlindedPathsRequired,
),
lnwire.Features,
)

testNow = time.Unix(1, 0)
)

Expand Down Expand Up @@ -189,6 +199,10 @@ func TestInvoices(t *testing.T) {
name: "AddInvoiceWithHTLCs",
test: testAddInvoiceWithHTLCs,
},
{
name: "AddInvoiceWithBlindedPathInfo",
test: testAddInvoiceWithBlindedPathInfo,
},
{
name: "SetIDIndex",
test: testSetIDIndex,
Expand Down Expand Up @@ -2088,6 +2102,83 @@ func TestHTLCSet(t *testing.T) {
}
}

func testAddInvoiceWithBlindedPathInfo(t *testing.T,
makeDB func(t *testing.T) invpkg.InvoiceDB) {

t.Parallel()
db := makeDB(t)

testInvoice, err := randInvoice(1000)
require.Nil(t, err)

testInvoice.Terms.Features = blindedPathFeatures

path := models.BlindedPathsInfo{
genPubKey(t): {
Route: &models.MCRoute{
SourcePubKey: genPubKey(t),
TotalAmount: 10000,
Hops: []*models.MCHop{
{
PubKeyBytes: genPubKey(t),
AmtToFwd: 60,
ChannelID: 40,
HasBlindingPoint: true,
},
{
PubKeyBytes: genPubKey(t),
AmtToFwd: 50,
ChannelID: 10,
HasBlindingPoint: true,
},
},
},
SessionKey: genPrivKey(t),
},
genPubKey(t): {
Route: &models.MCRoute{
SourcePubKey: genPubKey(t),
TotalAmount: 30,
Hops: []*models.MCHop{
{
PubKeyBytes: genPubKey(t),
AmtToFwd: 50,
ChannelID: 10,
HasBlindingPoint: true,
},
},
},
SessionKey: genPrivKey(t),
},
}
testInvoice.BlindedPaths = path

ctx := context.Background()

payHash := testInvoice.Terms.PaymentPreimage.Hash()
_, err = db.AddInvoice(ctx, testInvoice, payHash)
require.NoError(t, err)

inv, err := db.LookupInvoice(ctx, invpkg.InvoiceRefByHash(payHash))
require.NoError(t, err)

require.EqualValues(t, path, inv.BlindedPaths)
}

func genPubKey(t *testing.T) route.Vertex {
pk, err := btcec.NewPrivateKey()
require.NoError(t, err)

return route.NewVertex(pk.PubKey())
}

func genPrivKey(t *testing.T) *btcec.PrivateKey {
pk, err := btcec.NewPrivateKey()
require.NoError(t, err)

return pk
}

// testAddInvoiceWithHTLCs asserts that you can't insert an invoice that already
// has HTLCs.
func testAddInvoiceWithHTLCs(t *testing.T,
Expand Down

0 comments on commit d7ea024

Please sign in to comment.