From 947cc50c136b3a0a98a66ec2987dcf0014d7ae8a Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 4 Sep 2019 14:11:31 -0700 Subject: [PATCH] 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. --- routing/route/route.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/routing/route/route.go b/routing/route/route.go index e9e159f6..ef902468 100644 --- a/routing/route/route.go +++ b/routing/route/route.go @@ -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 }