tlv: expose unknown values

This commit is contained in:
Joost Jager 2019-11-19 12:05:20 +01:00
parent 1ab3107df4
commit cbe213fd0c
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
4 changed files with 39 additions and 24 deletions

@ -178,7 +178,7 @@ func (h *Payload) ForwardingInfo() ForwardingInfo {
// ensure that the proper fields are either included or omitted. The finalHop // ensure that the proper fields are either included or omitted. The finalHop
// boolean should be true if the payload was parsed for an exit hop. The // boolean should be true if the payload was parsed for an exit hop. The
// requirements for this method are described in BOLT 04. // requirements for this method are described in BOLT 04.
func ValidateParsedPayloadTypes(parsedTypes tlv.TypeSet, func ValidateParsedPayloadTypes(parsedTypes tlv.TypeMap,
nextHop lnwire.ShortChannelID) error { nextHop lnwire.ShortChannelID) error {
isFinalHop := nextHop == Exit isFinalHop := nextHop == Exit
@ -237,19 +237,19 @@ func (h *Payload) MultiPath() *record.MPP {
// getMinRequiredViolation checks for unrecognized required (even) fields in the // getMinRequiredViolation checks for unrecognized required (even) fields in the
// standard range and returns the lowest required type. Always returning the // standard range and returns the lowest required type. Always returning the
// lowest required type allows a failure message to be deterministic. // lowest required type allows a failure message to be deterministic.
func getMinRequiredViolation(set tlv.TypeSet) *tlv.Type { func getMinRequiredViolation(set tlv.TypeMap) *tlv.Type {
var ( var (
requiredViolation bool requiredViolation bool
minRequiredViolationType tlv.Type minRequiredViolationType tlv.Type
) )
for t, known := range set { for t, parseResult := range set {
// If a type is even but not known to us, we cannot process the // If a type is even but not known to us, we cannot process the
// payload. We are required to understand a field that we don't // payload. We are required to understand a field that we don't
// support. // support.
// //
// We always accept custom fields, because a higher level // We always accept custom fields, because a higher level
// application may understand them. // application may understand them.
if known || t%2 != 0 || t >= CustomTypeStart { if parseResult == nil || t%2 != 0 || t >= CustomTypeStart {
continue continue
} }

@ -12,9 +12,10 @@ import (
// Type is an 64-bit identifier for a TLV Record. // Type is an 64-bit identifier for a TLV Record.
type Type uint64 type Type uint64
// TypeSet is an unordered set of Types. The map item boolean values indicate // TypeMap is a map of parsed Types. The map values are byte slices. If the byte
// whether the type that we parsed was known. // slice is nil, the type was successfully parsed. Otherwise the value is byte
type TypeSet map[Type]bool // slice containing the encoded data.
type TypeMap map[Type][]byte
// Encoder is a signature for methods that can encode TLV values. An error // Encoder is a signature for methods that can encode TLV values. An error
// should be returned if the Encoder cannot support the underlying type of val. // should be returned if the Encoder cannot support the underlying type of val.

@ -1,6 +1,7 @@
package tlv package tlv
import ( import (
"bytes"
"errors" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
@ -139,16 +140,16 @@ func (s *Stream) Decode(r io.Reader) error {
} }
// DecodeWithParsedTypes is identical to Decode, but if successful, returns a // DecodeWithParsedTypes is identical to Decode, but if successful, returns a
// TypeSet containing the types of all records that were decoded or ignored from // TypeMap containing the types of all records that were decoded or ignored from
// the stream. // the stream.
func (s *Stream) DecodeWithParsedTypes(r io.Reader) (TypeSet, error) { func (s *Stream) DecodeWithParsedTypes(r io.Reader) (TypeMap, error) {
return s.decode(r, make(TypeSet)) return s.decode(r, make(TypeMap))
} }
// decode is a helper function that performs the basis of stream decoding. If // decode is a helper function that performs the basis of stream decoding. If
// the caller needs the set of parsed types, it must provide an initialized // the caller needs the set of parsed types, it must provide an initialized
// parsedTypes, otherwise the returned TypeSet will be nil. // parsedTypes, otherwise the returned TypeMap will be nil.
func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) { func (s *Stream) decode(r io.Reader, parsedTypes TypeMap) (TypeMap, error) {
var ( var (
typ Type typ Type
min Type min Type
@ -230,10 +231,25 @@ func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) {
return nil, err return nil, err
} }
// Record the successfully decoded type if the caller
// provided an initialized TypeMap.
if parsedTypes != nil {
parsedTypes[typ] = nil
}
// Otherwise, the record type is unknown and is odd, discard the // Otherwise, the record type is unknown and is odd, discard the
// number of bytes specified by length. // number of bytes specified by length.
default: default:
_, err := io.CopyN(ioutil.Discard, r, int64(length)) // If the caller provided an initialized TypeMap, record
// the encoded bytes.
var b *bytes.Buffer
writer := ioutil.Discard
if parsedTypes != nil {
b = bytes.NewBuffer(make([]byte, 0, length))
writer = b
}
_, err := io.CopyN(writer, r, int64(length))
switch { switch {
// We'll convert any EOFs to ErrUnexpectedEOF, since this // We'll convert any EOFs to ErrUnexpectedEOF, since this
@ -245,12 +261,10 @@ func (s *Stream) decode(r io.Reader, parsedTypes TypeSet) (TypeSet, error) {
case err != nil: case err != nil:
return nil, err return nil, err
} }
}
// Record the successfully decoded or ignored type if the if parsedTypes != nil {
// caller provided an initialized TypeSet. parsedTypes[typ] = b.Bytes()
if parsedTypes != nil { }
parsedTypes[typ] = ok
} }
// Update our record index so that we can begin our next search // Update our record index so that we can begin our next search

@ -12,7 +12,7 @@ type parsedTypeTest struct {
name string name string
encode []tlv.Type encode []tlv.Type
decode []tlv.Type decode []tlv.Type
expParsedTypes tlv.TypeSet expParsedTypes tlv.TypeMap
} }
// TestParsedTypes asserts that a Stream will properly return the set of types // TestParsedTypes asserts that a Stream will properly return the set of types
@ -29,17 +29,17 @@ func TestParsedTypes(t *testing.T) {
name: "known and unknown", name: "known and unknown",
encode: []tlv.Type{knownType, unknownType}, encode: []tlv.Type{knownType, unknownType},
decode: []tlv.Type{knownType}, decode: []tlv.Type{knownType},
expParsedTypes: tlv.TypeSet{ expParsedTypes: tlv.TypeMap{
unknownType: false, unknownType: []byte{0, 0, 0, 0, 0, 0, 0, 0},
knownType: true, knownType: nil,
}, },
}, },
{ {
name: "known and missing known", name: "known and missing known",
encode: []tlv.Type{knownType}, encode: []tlv.Type{knownType},
decode: []tlv.Type{knownType, secondKnownType}, decode: []tlv.Type{knownType, secondKnownType},
expParsedTypes: tlv.TypeSet{ expParsedTypes: tlv.TypeMap{
knownType: true, knownType: nil,
}, },
}, },
} }