zpay32: modify an invoice's r field to allow multiple routes

Before this commit, if an invoice encoded multiple `r` fields, we would
decode them as one single route. We fix this by allowing an invoice to
store multiple routes.
This commit is contained in:
Wilmer Paulino
2018-03-27 00:42:35 -04:00
parent beb08b3887
commit ea9e609ef0
3 changed files with 141 additions and 121 deletions

View File

@@ -8,6 +8,7 @@ import (
"time"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg"
"github.com/roasbeef/btcutil"
@@ -706,18 +707,18 @@ func TestParseFallbackAddr(t *testing.T) {
}
}
// TestParseRoutingInfo checks that the routing info is properly parsed.
func TestParseRoutingInfo(t *testing.T) {
// TestParseRouteHint checks that the routing info is properly parsed.
func TestParseRouteHint(t *testing.T) {
t.Parallel()
var testSingleHopData []byte
for _, r := range testSingleHop {
base256 := make([]byte, 51)
copy(base256[:33], r.PubKey.SerializeCompressed())
binary.BigEndian.PutUint64(base256[33:41], r.ShortChanID)
binary.BigEndian.PutUint32(base256[41:45], r.FeeBaseMsat)
copy(base256[:33], r.NodeID.SerializeCompressed())
binary.BigEndian.PutUint64(base256[33:41], r.ChannelID)
binary.BigEndian.PutUint32(base256[41:45], r.FeeBaseMSat)
binary.BigEndian.PutUint32(base256[45:49], r.FeeProportionalMillionths)
binary.BigEndian.PutUint16(base256[49:51], r.CltvExpDelta)
binary.BigEndian.PutUint16(base256[49:51], r.CLTVExpiryDelta)
testSingleHopData = append(testSingleHopData, base256...)
}
testSingleHopData, _ = bech32.ConvertBits(testSingleHopData, 8, 5, true)
@@ -725,11 +726,11 @@ func TestParseRoutingInfo(t *testing.T) {
var testDoubleHopData []byte
for _, r := range testDoubleHop {
base256 := make([]byte, 51)
copy(base256[:33], r.PubKey.SerializeCompressed())
binary.BigEndian.PutUint64(base256[33:41], r.ShortChanID)
binary.BigEndian.PutUint32(base256[41:45], r.FeeBaseMsat)
copy(base256[:33], r.NodeID.SerializeCompressed())
binary.BigEndian.PutUint64(base256[33:41], r.ChannelID)
binary.BigEndian.PutUint32(base256[41:45], r.FeeBaseMSat)
binary.BigEndian.PutUint32(base256[45:49], r.FeeProportionalMillionths)
binary.BigEndian.PutUint16(base256[49:51], r.CltvExpDelta)
binary.BigEndian.PutUint16(base256[49:51], r.CLTVExpiryDelta)
testDoubleHopData = append(testDoubleHopData, base256...)
}
testDoubleHopData, _ = bech32.ConvertBits(testDoubleHopData, 8, 5, true)
@@ -737,7 +738,7 @@ func TestParseRoutingInfo(t *testing.T) {
tests := []struct {
data []byte
valid bool
result []ExtraRoutingInfo
result []routing.HopHint
}{
{
data: []byte{0x0, 0x0, 0x0, 0x0},
@@ -746,7 +747,7 @@ func TestParseRoutingInfo(t *testing.T) {
{
data: []byte{},
valid: true,
result: []ExtraRoutingInfo{},
result: []routing.HopHint{},
},
{
data: testSingleHopData,
@@ -765,13 +766,13 @@ func TestParseRoutingInfo(t *testing.T) {
}
for i, test := range tests {
routingInfo, err := parseRoutingInfo(test.data)
routeHint, err := parseRouteHint(test.data)
if (err == nil) != test.valid {
t.Errorf("routing info decoding test %d failed: %v", i, err)
return
}
if test.valid {
if err := compareRoutingInfos(test.result, routingInfo); err != nil {
if err := compareRouteHints(test.result, routeHint); err != nil {
t.Fatalf("test %d failed decoding routing info: %v", i, err)
}
}