Skip to content

Commit

Permalink
Move functions so that encode and decode are adjacent (#2892)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Mar 29, 2024
1 parent ce8253c commit 4163dce
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions x/merkledb/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4163dce

Please # to comment.