From d5d9de2cce85364f62393cc40c97b5ec81d1e695 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Fri, 29 Mar 2024 17:44:21 -0400 Subject: [PATCH] Move functions around so that encode and decode are next to each other --- x/merkledb/codec.go | 64 ++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/x/merkledb/codec.go b/x/merkledb/codec.go index 156a6630899c..a820c42df7cc 100644 --- a/x/merkledb/codec.go +++ b/x/merkledb/codec.go @@ -84,25 +84,6 @@ func encodedDBNodeSize(n *dbNode) int { return size } -// Assumes [n] is non-nil. -func encodeDBNode(n *dbNode) []byte { - buf := bytes.NewBuffer(make([]byte, 0, encodedDBNodeSize(n))) - encodeMaybeByteSlice(buf, n.value) - encodeUint(buf, uint64(len(n.children))) - // Note we insert children in order of increasing index - // for determinism. - keys := maps.Keys(n.children) - slices.Sort(keys) - for _, index := range keys { - entry := n.children[index] - encodeUint(buf, uint64(index)) - encodeKeyToBuffer(buf, entry.compressedKey) - _, _ = buf.Write(entry.id[:]) - encodeBool(buf, entry.hasValue) - } - return buf.Bytes() -} - // Returns the canonical hash of [n]. // // Assumes [n] is non-nil. @@ -153,6 +134,25 @@ func hashNode(n *node) ids.ID { return hash } +// Assumes [n] is non-nil. +func encodeDBNode(n *dbNode) []byte { + buf := bytes.NewBuffer(make([]byte, 0, encodedDBNodeSize(n))) + encodeMaybeByteSlice(buf, n.value) + encodeUint(buf, uint64(len(n.children))) + // Note we insert children in order of increasing index + // for determinism. + keys := maps.Keys(n.children) + slices.Sort(keys) + for _, index := range keys { + entry := n.children[index] + encodeUint(buf, uint64(index)) + encodeKeyToBuffer(buf, entry.compressedKey) + _, _ = buf.Write(entry.id[:]) + encodeBool(buf, entry.hasValue) + } + return buf.Bytes() +} + // Assumes [n] is non-nil. func decodeDBNode(b []byte, n *dbNode) error { if minDBNodeLen > len(b) { @@ -235,6 +235,12 @@ func decodeBool(src *bytes.Reader) (bool, error) { } } +func encodeUint(dst *bytes.Buffer, value uint64) { + var buf [binary.MaxVarintLen64]byte + size := binary.PutUvarint(buf[:], value) + _, _ = dst.Write(buf[:size]) +} + func decodeUint(src *bytes.Reader) (uint64, error) { // To ensure encoding/decoding is canonical, we need to check for leading // zeroes in the varint. @@ -268,12 +274,6 @@ func decodeUint(src *bytes.Reader) (uint64, error) { return val64, nil } -func encodeUint(dst *bytes.Buffer, value uint64) { - var buf [binary.MaxVarintLen64]byte - size := binary.PutUvarint(buf[:], value) - _, _ = dst.Write(buf[:size]) -} - func encodeMaybeByteSlice(dst *bytes.Buffer, maybeValue maybe.Maybe[[]byte]) { hasValue := maybeValue.HasValue() encodeBool(dst, hasValue) @@ -299,6 +299,13 @@ func decodeMaybeByteSlice(src *bytes.Reader) (maybe.Maybe[[]byte], error) { return maybe.Some(rawBytes), nil } +func encodeByteSlice(dst *bytes.Buffer, value []byte) { + encodeUint(dst, uint64(len(value))) + if value != nil { + _, _ = dst.Write(value) + } +} + func decodeByteSlice(src *bytes.Reader) ([]byte, error) { if minByteSliceLen > src.Len() { return nil, io.ErrUnexpectedEOF @@ -324,13 +331,6 @@ func decodeByteSlice(src *bytes.Reader) ([]byte, error) { return result, err } -func encodeByteSlice(dst *bytes.Buffer, value []byte) { - encodeUint(dst, uint64(len(value))) - if value != nil { - _, _ = dst.Write(value) - } -} - func decodeID(src *bytes.Reader) (ids.ID, error) { if ids.IDLen > src.Len() { return ids.ID{}, io.ErrUnexpectedEOF