Skip to content

Commit

Permalink
Merge packages in lib/trie/node
Browse files Browse the repository at this point in the history
- `lib/trie/leaf`
- `lib/trie/branch`
- `lib/trie/encode`
- `lib/trie/decode`
  • Loading branch information
qdm12 committed Dec 7, 2021
1 parent f0c51e9 commit 30e2318
Show file tree
Hide file tree
Showing 49 changed files with 786 additions and 1,142 deletions.
77 changes: 0 additions & 77 deletions lib/trie/branch/buffer_mock_test.go

This file was deleted.

11 changes: 0 additions & 11 deletions lib/trie/branch/key.go

This file was deleted.

49 changes: 0 additions & 49 deletions lib/trie/branch/writer_mock_test.go

This file was deleted.

59 changes: 20 additions & 39 deletions lib/trie/encode/key.go → lib/trie/codec/nibbles.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package encode

import (
"errors"
"fmt"
"io"
)

const maxPartialKeySize = ^uint16(0)

var ErrPartialKeyTooBig = errors.New("partial key length cannot be larger than or equal to 2^16")

// KeyLength encodes the public key length.
func KeyLength(keyLength int, writer io.Writer) (err error) {
keyLength -= 63

if keyLength >= int(maxPartialKeySize) {
return fmt.Errorf("%w: %d",
ErrPartialKeyTooBig, keyLength)
}

for i := uint16(0); i < maxPartialKeySize; i++ {
if keyLength < 255 {
_, err = writer.Write([]byte{byte(keyLength)})
if err != nil {
return err
}
break
}
_, err = writer.Write([]byte{255})
if err != nil {
return err
}

keyLength -= 255
}

return nil
}
package codec

// NibblesToKeyLE converts a slice of nibbles with length k into a
// Little Endian byte slice.
Expand All @@ -64,3 +26,22 @@ func NibblesToKeyLE(nibbles []byte) (keyLE []byte) {

return keyLE
}

// KeyLEToNibbles converts a Little Endian byte slice into nibbles.
// It assumes bytes are already in Little Endian and does not rearrange nibbles.
func KeyLEToNibbles(in []byte) (nibbles []byte) {
if len(in) == 0 {
return []byte{}
} else if len(in) == 1 && in[0] == 0 {
return []byte{0, 0}
}

l := len(in) * 2
nibbles = make([]byte, l)
for i, b := range in {
nibbles[2*i] = b / 16
nibbles[2*i+1] = b % 16
}

return nibbles
}
142 changes: 142 additions & 0 deletions lib/trie/codec/nibbles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package codec

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_NibblesToKeyLE(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
nibbles []byte
keyLE []byte
}{
"nil nibbles": {
keyLE: []byte{},
},
"empty nibbles": {
nibbles: []byte{},
keyLE: []byte{},
},
"0xF 0xF": {
nibbles: []byte{0xF, 0xF},
keyLE: []byte{0xFF},
},
"0x3 0xa 0x0 0x5": {
nibbles: []byte{0x3, 0xa, 0x0, 0x5},
keyLE: []byte{0x3a, 0x05},
},
"0xa 0xa 0xf 0xf 0x0 0x1": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1},
keyLE: []byte{0xaa, 0xff, 0x01},
},
"0xa 0xa 0xf 0xf 0x0 0x1 0xc 0x2": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc, 0x2},
keyLE: []byte{0xaa, 0xff, 0x01, 0xc2},
},
"0xa 0xa 0xf 0xf 0x0 0x1 0xc": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc},
keyLE: []byte{0xa, 0xaf, 0xf0, 0x1c},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

keyLE := NibblesToKeyLE(testCase.nibbles)

assert.Equal(t, testCase.keyLE, keyLE)
})
}
}

func Test_KeyLEToNibbles(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
in []byte
nibbles []byte
}{
"nil input": {
nibbles: []byte{},
},
"empty input": {
in: []byte{},
nibbles: []byte{},
},
"0x0": {
in: []byte{0x0},
nibbles: []byte{0, 0}},
"0xFF": {
in: []byte{0xFF},
nibbles: []byte{0xF, 0xF}},
"0x3a 0x05": {
in: []byte{0x3a, 0x05},
nibbles: []byte{0x3, 0xa, 0x0, 0x5}},
"0xAA 0xFF 0x01": {
in: []byte{0xAA, 0xFF, 0x01},
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1}},
"0xAA 0xFF 0x01 0xc2": {
in: []byte{0xAA, 0xFF, 0x01, 0xc2},
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc, 0x2}},
"0xAA 0xFF 0x01 0xc0": {
in: []byte{0xAA, 0xFF, 0x01, 0xc0},
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc, 0x0}},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

nibbles := KeyLEToNibbles(testCase.in)

assert.Equal(t, testCase.nibbles, nibbles)
})
}
}

func Test_NibblesKeyLE(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
nibblesToEncode []byte
nibblesDecoded []byte
}{
"empty input": {
nibblesToEncode: []byte{},
nibblesDecoded: []byte{},
},
"one byte": {
nibblesToEncode: []byte{1},
nibblesDecoded: []byte{0, 1},
},
"two bytes": {
nibblesToEncode: []byte{1, 2},
nibblesDecoded: []byte{1, 2},
},
"three bytes": {
nibblesToEncode: []byte{1, 2, 3},
nibblesDecoded: []byte{0, 1, 2, 3},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

keyLE := NibblesToKeyLE(testCase.nibblesToEncode)
nibblesDecoded := KeyLEToNibbles(keyLE)

assert.Equal(t, testCase.nibblesDecoded, nibblesDecoded)
})
}
}
Loading

0 comments on commit 30e2318

Please sign in to comment.