From d9a073ad7e2a93cac981032a833c50ccac0b2178 Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Tue, 8 Oct 2024 13:32:12 +0200 Subject: [PATCH] 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. --- channeldb/migration32/route.go | 26 ++++++++++++++++++++++++++ routing/route/route.go | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/channeldb/migration32/route.go b/channeldb/migration32/route.go index a4d40a45c..a35338e50 100644 --- a/channeldb/migration32/route.go +++ b/channeldb/migration32/route.go @@ -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 diff --git a/routing/route/route.go b/routing/route/route.go index 9aa28759b..2d62af465 100644 --- a/routing/route/route.go +++ b/routing/route/route.go @@ -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