routing/route/route: prevent modification of hop.TLVRecords

Currently the underlying array backing the hop's TLVRecords is modified
when combining custom records with the primitive forwarding info. This
commit uses a fresh slice to prevent modifications from mutating the
hop itself.
This commit is contained in:
Conner Fromknecht 2019-09-04 14:11:31 -07:00
parent 278e10a2fd
commit 947cc50c13
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -104,8 +104,11 @@ func (h *Hop) PackHopPayload(w io.Writer, nextChanID uint64) error {
// Otherwise, we'll need to make a new stream that includes our
// required routing fields, as well as these optional values.
var records []tlv.Record
// Every hop must have an amount to forward and CLTV expiry.
amt := uint64(h.AmtToForward)
combinedRecords := append(h.TLVRecords,
records = append(records,
record.NewAmtToFwdRecord(&amt),
record.NewLockTimeRecord(&h.OutgoingTimeLock),
)
@ -115,16 +118,19 @@ func (h *Hop) PackHopPayload(w io.Writer, nextChanID uint64) error {
//
// TODO(conner): test using hop.Exit once available
if nextChanID != 0 {
combinedRecords = append(combinedRecords,
records = append(records,
record.NewNextHopIDRecord(&nextChanID),
)
}
// Append any custom types destined for this hop.
records = append(records, h.TLVRecords...)
// To ensure we produce a canonical stream, we'll sort the records
// before encoding them as a stream in the hop payload.
tlv.SortRecords(combinedRecords)
tlv.SortRecords(records)
tlvStream, err := tlv.NewStream(combinedRecords...)
tlvStream, err := tlv.NewStream(records...)
if err != nil {
return err
}