routing/route+tlv: add new TLV-EOB awareness to Hop+Route
In this commit, we extend the Hop struct to carry an arbitrary set of TLV values, and add a new field that allows us to distinguish between the modern and legacy TLV payload. We add a new `PackPayload` method that will be used to encode the combined required routing TLV fields along any set of TLV fields that were specified as part of path finding. Finally, the `ToSphinxPath` has been extended to be able to recognize if a hop needs the modern, or legacy payload.
This commit is contained in:
5 files changed
+331
-15
No files matched your search
@@ -1,8 +1,10 @@
|
||||
package tlv
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
@@ -166,3 +168,63 @@ func MakeDynamicRecord(typ Type, val interface{}, sizeFunc SizeFunc,
|
||||
decoder: decoder,
|
||||
}
|
||||
}
|
||||
|
||||
// RecordsToMap encodes a series of TLV records as raw key-value pairs in the
|
||||
// form of a map.
|
||||
func RecordsToMap(records []Record) (map[uint64][]byte, error) {
|
||||
tlvMap := make(map[uint64][]byte, len(records))
|
||||
|
||||
for _, record := range records {
|
||||
var b bytes.Buffer
|
||||
if err := record.Encode(&b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tlvMap[uint64(record.Type())] = b.Bytes()
|
||||
}
|
||||
|
||||
return tlvMap, nil
|
||||
}
|
||||
|
||||
// StubEncoder is a factory function that makes a stub tlv.Encoder out of a raw
|
||||
// value. We can use this to make a record that can be encoded when we don't
|
||||
// actually know it's true underlying value, and only it serialization.
|
||||
func StubEncoder(v []byte) Encoder {
|
||||
return func(w io.Writer, val interface{}, buf *[8]byte) error {
|
||||
_, err := w.Write(v)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// MapToRecords encodes the passed TLV map as a series of regular tlv.Record
|
||||
// instances. The resulting set of records will be returned in sorted order by
|
||||
// their type.
|
||||
func MapToRecords(tlvMap map[uint64][]byte) ([]Record, error) {
|
||||
records := make([]Record, 0, len(tlvMap))
|
||||
for k, v := range tlvMap {
|
||||
// We don't pass in a decoder here since we don't actually know
|
||||
// the type, and only expect this Record to be used for display
|
||||
// and encoding purposes.
|
||||
record := MakeStaticRecord(
|
||||
Type(k), nil, uint64(len(v)), StubEncoder(v), nil,
|
||||
)
|
||||
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
SortRecords(records)
|
||||
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// SortRecords is a helper function that will sort a slice of records in place
|
||||
// according to their type.
|
||||
func SortRecords(records []Record) {
|
||||
if len(records) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
sort.Slice(records, func(i, j int) bool {
|
||||
return records[i].Type() < records[j].Type()
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user