Skip to content

Commit

Permalink
Merge pull request #7976 from w3irdrobot/serialize-deserialize-amp
Browse files Browse the repository at this point in the history
Serialize/deserialize AMP struct in hop
  • Loading branch information
yyforyongyu authored Jan 29, 2024
2 parents d44c72b + 0767a2c commit 59feec4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
22 changes: 21 additions & 1 deletion channeldb/payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions channeldb/payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
5 changes: 5 additions & 0 deletions docs/release-notes/release-notes-0.18.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
a `shutdown` message if there were currently HTLCs on the channel. After this
change, the shutdown procedure should be compliant with BOLT2 requirements.

* The AMP struct in payment hops will [now be populated](https://github.com/lightningnetwork/lnd/pull/7976) when the AMP TLV is set.

# New Features
## Functional Enhancements

Expand Down Expand Up @@ -281,14 +283,17 @@

* Amin Bashiri
* Andras Banki-Horvath
* BitcoinerCoderBob
* Carla Kirk-Cohen
* Elle Mouton
* ErikEk
* Keagan McClelland
* Marcos Fernandez Perez
* Matt Morehouse
* Slyghtning
* Tee8z
* Turtle
* Ononiwu Maureen Chiamaka
* w3irdrobot
* Yong Yu
* Ziggie
10 changes: 10 additions & 0 deletions itest/lnd_amp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ func testSendPaymentAMP(ht *lntest.HarnessTest) {
if htlc.Status == lnrpc.HTLCAttempt_SUCCEEDED {
succeeded++
}

// When an AMP record is expected, it will only be seen on the
// last hop of a route. So we make sure it isn't set on any of
// the hops except the last one
for _, hop := range htlc.Route.Hops[:len(htlc.Route.Hops)-1] {
require.Nil(ht, hop.AmpRecord)
}

lastHop := htlc.Route.Hops[len(htlc.Route.Hops)-1]
require.NotNil(ht, lastHop.AmpRecord)
}

const minExpectedShards = 3
Expand Down
13 changes: 13 additions & 0 deletions lnrpc/routerrpc/router_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,18 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
}
}

var amp *lnrpc.AMPRecord
if hop.AMP != nil {
rootShare := hop.AMP.RootShare()
setID := hop.AMP.SetID()

amp = &lnrpc.AMPRecord{
RootShare: rootShare[:],
SetId: setID[:],
ChildIndex: hop.AMP.ChildIndex(),
}
}

resp.Hops[i] = &lnrpc.Hop{
ChanId: hop.ChannelID,
ChanCapacity: int64(chanCapacity),
Expand All @@ -627,6 +639,7 @@ func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error)
CustomRecords: hop.CustomRecords,
TlvPayload: !hop.LegacyPayload,
MppRecord: mpp,
AmpRecord: amp,
Metadata: hop.Metadata,
EncryptedData: hop.EncryptedData,
TotalAmtMsat: uint64(hop.TotalAmtMsat),
Expand Down

0 comments on commit 59feec4

Please sign in to comment.