Merge pull request #7976 from w3irdrobot/serialize-deserialize-amp

Serialize/deserialize AMP struct in hop
This commit is contained in:
Yong authored and GitHub committed 2024-01-29 09:48:05 +08:00
commit 59feec44ca
5 files changed
+63 -1

No files matched your search

+21 -1
View File
@@ -1157,6 +1157,10 @@ func serializeHop(w io.Writer, h *route.Hop) error {
))
}
if h.AMP != nil {
records = append(records, h.AMP.Record())
}
if h.Metadata != nil {
records = append(records, record.NewMetadataRecord(&h.Metadata))
}
@@ -1301,7 +1305,23 @@ func deserializeHop(r io.Reader) (*route.Hop, error) {
}
}
// If the metatdata type is present, remove it from the tlv map and
ampType := uint64(record.AMPOnionType)
if ampBytes, ok := tlvMap[ampType]; ok {
delete(tlvMap, ampType)
var (
amp = &record.AMP{}
ampRec = amp.Record()
r = bytes.NewReader(ampBytes)
)
err := ampRec.Decode(r, uint64(len(ampBytes)))
if err != nil {
return nil, err
}
h.AMP = amp
}
// If the metadata type is present, remove it from the tlv map and
// populate directly on the hop.
metadataType := uint64(record.MetadataOnionType)
if metadata, ok := tlvMap[metadataType]; ok {
+14
View File
@@ -44,11 +44,25 @@ var (
LegacyPayload: true,
}
testHop3 = &route.Hop{
PubKeyBytes: route.NewVertex(pub),
ChannelID: 12345,
OutgoingTimeLock: 111,
AmtToForward: 555,
CustomRecords: record.CustomSet{
65536: []byte{},
80001: []byte{},
},
AMP: record.NewAMP([32]byte{0x69}, [32]byte{0x42}, 1),
Metadata: []byte{1, 2, 3},
}
testRoute = route.Route{
TotalTimeLock: 123,
TotalAmount: 1234567,
SourcePubKey: vertex,
Hops: []*route.Hop{
testHop3,
testHop2,
testHop1,
},