htlcswitch/hop: parse and validate AMP records

This commit is contained in:
Conner Fromknecht committed 2021-04-07 12:08:33 -07:00
1 parent 135a0a9f7f
commit c2729cbbbd
2 files changed
+100

No files matched your search

+82
View File
@@ -8,6 +8,7 @@ import (
"github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
"github.com/stretchr/testify/require"
)
const testUnknownRequiredType = 0x10
@@ -18,6 +19,7 @@ type decodePayloadTest struct {
expErr error
expCustomRecords map[uint64][]byte
shouldHaveMPP bool
shouldHaveAMP bool
}
var decodePayloadTests = []decodePayloadTest{
@@ -183,6 +185,37 @@ var decodePayloadTests = []decodePayloadTest{
FinalHop: false,
},
},
{
name: "intermediate hop with amp",
payload: []byte{
// amount
0x02, 0x00,
// cltv
0x04, 0x00,
// next hop id
0x06, 0x08,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// amp
0x0e, 0x41,
// amp.root_share
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
// amp.set_id
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
// amp.child_index
0x09,
},
expErr: hop.ErrInvalidPayload{
Type: record.AMPOnionType,
Violation: hop.IncludedViolation,
FinalHop: false,
},
},
{
name: "final hop with mpp",
payload: []byte{
@@ -201,6 +234,30 @@ var decodePayloadTests = []decodePayloadTest{
expErr: nil,
shouldHaveMPP: true,
},
{
name: "final hop with amp",
payload: []byte{
// amount
0x02, 0x00,
// cltv
0x04, 0x00,
// amp
0x0e, 0x41,
// amp.root_share
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
// amp.set_id
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
// amp.child_index
0x09,
},
shouldHaveAMP: true,
},
}
// TestDecodeHopPayloadRecordValidation asserts that parsing the payloads in the
@@ -223,6 +280,20 @@ func testDecodeHopPayloadValidation(t *testing.T, test decodePayloadTest) {
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
}
testRootShare = [32]byte{
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12, 0x12,
}
testSetID = [32]byte{
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
}
testChildIndex = uint32(9)
)
p, err := hop.NewPayloadFromReader(bytes.NewReader(test.payload))
@@ -249,6 +320,17 @@ func testDecodeHopPayloadValidation(t *testing.T, test decodePayloadTest) {
t.Fatalf("unexpected MPP payload")
}
if test.shouldHaveAMP {
if p.AMP == nil {
t.Fatalf("payload should have AMP record")
}
require.Equal(t, testRootShare, p.AMP.RootShare())
require.Equal(t, testSetID, p.AMP.SetID())
require.Equal(t, testChildIndex, p.AMP.ChildIndex())
} else if p.AMP != nil {
t.Fatalf("unexpected AMP payload")
}
// Convert expected nil map to empty map, because we always expect an
// initiated map from the payload.
expCustomRecords := make(record.CustomSet)