routing+channeldb: let Vertex implement tlv.RecordProducer

So that we can use it in TLV encoding.
Also add this to the codec for channeldb migration 32 since we will be
using it there in an upcoming adjustment commit.
This commit is contained in:
Elle Mouton 2024-10-08 13:32:12 +02:00
parent a5f3fa17e7
commit d9a073ad7e
No known key found for this signature in database
GPG Key ID: D7D916376026F177
2 changed files with 52 additions and 0 deletions

View File

@ -29,6 +29,32 @@ const VertexSize = 33
// public key.
type Vertex [VertexSize]byte
// Record returns a TLV record that can be used to encode/decode a Vertex
// to/from a TLV stream.
func (v *Vertex) Record() tlv.Record {
return tlv.MakeStaticRecord(
0, v, VertexSize, encodeVertex, decodeVertex,
)
}
func encodeVertex(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*Vertex); ok {
_, err := w.Write(b[:])
return err
}
return tlv.NewTypeForEncodingErr(val, "Vertex")
}
func decodeVertex(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*Vertex); ok {
_, err := io.ReadFull(r, b[:])
return err
}
return tlv.NewTypeForDecodingErr(val, "Vertex", l, VertexSize)
}
// Route represents a path through the channel graph which runs over one or
// more channels in succession. This struct carries all the information
// required to craft the Sphinx onion packet, and send the payment along the

View File

@ -94,6 +94,32 @@ func (v Vertex) String() string {
return fmt.Sprintf("%x", v[:])
}
// Record returns a TLV record that can be used to encode/decode a Vertex
// to/from a TLV stream.
func (v *Vertex) Record() tlv.Record {
return tlv.MakeStaticRecord(
0, v, VertexSize, encodeVertex, decodeVertex,
)
}
func encodeVertex(w io.Writer, val interface{}, _ *[8]byte) error {
if b, ok := val.(*Vertex); ok {
_, err := w.Write(b[:])
return err
}
return tlv.NewTypeForEncodingErr(val, "Vertex")
}
func decodeVertex(r io.Reader, val interface{}, _ *[8]byte, l uint64) error {
if b, ok := val.(*Vertex); ok {
_, err := io.ReadFull(r, b[:])
return err
}
return tlv.NewTypeForDecodingErr(val, "Vertex", l, VertexSize)
}
// Hop represents an intermediate or final node of the route. This naming
// is in line with the definition given in BOLT #4: Onion Routing Protocol.
// The struct houses the channel along which this hop can be reached and