Skip to content

Commit

Permalink
Remove unneded NibblesToKey
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 2, 2021
1 parent a3c8152 commit 0a7aafa
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 74 deletions.
26 changes: 0 additions & 26 deletions lib/trie/encode/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,6 @@ func KeyLength(keyLength int) (encoding []byte, err error) {
return encoding, nil
}

// NibblesToKey converts a slice of nibbles with length k into a
// Big Endian byte slice.
// It assumes nibbles are already in Little Endian and does not rearrange nibbles.
// If the length of the input is odd, the result is
// [ in[1] in[0] | ... | 0000 in[k-1] ]
// Otherwise, the result is
// [ in[1] in[0] | ... | in[k-1] in[k-2] ]
func NibblesToKey(nibbles []byte) (key []byte) {
if len(nibbles)%2 == 0 {
key = make([]byte, len(nibbles)/2)
for i := 0; i < len(nibbles); i += 2 {
key[i/2] = (nibbles[i] & 0xf) | (nibbles[i+1] << 4 & 0xf0)
}
} else {
key = make([]byte, len(nibbles)/2+1)
for i := 0; i < len(nibbles); i += 2 {
key[i/2] = nibbles[i] & 0xf
if i < len(nibbles)-1 {
key[i/2] |= (nibbles[i+1] << 4 & 0xf0)
}
}
}

return key
}

// NibblesToKeyLE converts a slice of nibbles with length k into a
// Little Endian byte slice.
// It assumes nibbles are already in Little Endian and does not rearrange nibbles.
Expand Down
48 changes: 0 additions & 48 deletions lib/trie/encode/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,54 +73,6 @@ func Test_KeyLength(t *testing.T) {
}
}

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

testCases := map[string]struct {
nibbles []byte
key []byte
}{
"nil nibbles": {
key: []byte{},
},
"empty nibbles": {
nibbles: []byte{},
key: []byte{},
},
"0xF 0xF": {
nibbles: []byte{0xF, 0xF},
key: []byte{0xFF},
},
"0x3 0xa 0x0 0x5": {
nibbles: []byte{0x3, 0xa, 0x0, 0x5},
key: []byte{0xa3, 0x50},
},
"0xa 0xa 0xf 0xf 0x0 0x1": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1},
key: []byte{0xaa, 0xff, 0x10},
},
"0xa 0xa 0xf 0xf 0x0 0x1 0xc 0x2": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc, 0x2},
key: []byte{0xaa, 0xff, 0x10, 0x2c},
},
"0xa 0xa 0xf 0xf 0x0 0x1 0xc": {
nibbles: []byte{0xa, 0xa, 0xf, 0xf, 0x0, 0x1, 0xc},
key: []byte{0xaa, 0xff, 0x10, 0x0c},
},
}

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

key := NibblesToKey(testCase.nibbles)

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

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

Expand Down

0 comments on commit 0a7aafa

Please sign in to comment.