From 5d7119fe9c1f50bd51945e5cbc2565ed3bc7d96d Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 16 Jan 2018 19:37:41 -0800 Subject: [PATCH] channeldb: expose methods to serialize HTLC's on-disk MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These new methods will be used by the contract court package to properly serialize the state of HTLC’s that have yet to be fully resolved on-chain. --- channeldb/channel.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/channeldb/channel.go b/channeldb/channel.go index 9c938cd1..dab36170 100644 --- a/channeldb/channel.go +++ b/channeldb/channel.go @@ -746,7 +746,12 @@ type HTLC struct { LogIndex uint64 } -func serializeHtlcs(b io.Writer, htlcs []HTLC) error { +// SerializeHtlcs writes out the passed set of HTLC's into the passed writer +// using the current default on-disk serialization format. +// +// NOTE: This API is NOT stable, the on-disk format will likely change in the +// future. +func SerializeHtlcs(b io.Writer, htlcs ...HTLC) error { numHtlcs := uint16(len(htlcs)) if err := writeElement(b, numHtlcs); err != nil { return err @@ -765,7 +770,13 @@ func serializeHtlcs(b io.Writer, htlcs []HTLC) error { return nil } -func deserializeHtlcs(r io.Reader) ([]HTLC, error) { +// DeserializeHtlcs attempts to read out a slice of HTLC's from the passed +// io.Reader. The bytes within the passed reader MUST have been previously +// written to using the SerializeHtlcs function. +// +// NOTE: This API is NOT stable, the on-disk format will likely change in the +// future. +func DeserializeHtlcs(r io.Reader) ([]HTLC, error) { var numHtlcs uint16 if err := readElement(r, &numHtlcs); err != nil { return nil, err @@ -1520,7 +1531,7 @@ func serializeChanCommit(w io.Writer, c *ChannelCommitment) error { return err } - return serializeHtlcs(w, c.Htlcs) + return SerializeHtlcs(w, c.Htlcs...) } func putChanCommitment(chanBucket *bolt.Bucket, c *ChannelCommitment, @@ -1624,7 +1635,7 @@ func deserializeChanCommit(r io.Reader) (ChannelCommitment, error) { return c, err } - c.Htlcs, err = deserializeHtlcs(r) + c.Htlcs, err = DeserializeHtlcs(r) if err != nil { return c, err }