Minor refactor to packNibbles and unpackNibbles and add additional tests. (#2162)
This commit is contained in:
parent
24b4d52dea
commit
7da36bf459
|
@ -214,18 +214,17 @@ func packNibbles*(nibbles: seq[byte]): Nibbles =
|
|||
if nibbles.len() == 0:
|
||||
return Nibbles(@[byte(0x00)])
|
||||
|
||||
let isOddLength = (nibbles.len() %% 2 == 1)
|
||||
let isEvenLength = nibbles.len() mod 2 == 0
|
||||
|
||||
var
|
||||
output = newSeq[byte]()
|
||||
highNibble = not isOddLength
|
||||
output = newSeqOfCap[byte](nibbles.len() div 2 + 1)
|
||||
highNibble = isEvenLength
|
||||
currentByte: byte = 0
|
||||
|
||||
if isOddLength:
|
||||
currentByte = 0x10
|
||||
|
||||
if not isOddLength:
|
||||
if isEvenLength:
|
||||
output.add(0x00)
|
||||
else:
|
||||
currentByte = 0x10
|
||||
|
||||
for i, nibble in nibbles:
|
||||
if highNibble:
|
||||
|
@ -240,7 +239,7 @@ func packNibbles*(nibbles: seq[byte]): Nibbles =
|
|||
func unpackNibbles*(nibbles: Nibbles): seq[byte] =
|
||||
doAssert(nibbles.len() <= MAX_PACKED_NIBBLES_LEN, "Packed nibbles length is too long")
|
||||
|
||||
var output = newSeq[byte]()
|
||||
var output = newSeqOfCap[byte](nibbles.len * 2)
|
||||
|
||||
for i, pair in nibbles:
|
||||
if i == 0 and pair == 0x00:
|
||||
|
|
|
@ -14,6 +14,45 @@ import
|
|||
const testVectorDir = "./vendor/portal-spec-tests/tests/mainnet/state/serialization/"
|
||||
|
||||
suite "State Content Keys":
|
||||
test "Encode/decode empty nibbles":
|
||||
const
|
||||
expected = "00"
|
||||
nibbles: seq[byte] = @[]
|
||||
packedNibbles = packNibbles(nibbles)
|
||||
unpackedNibbles = unpackNibbles(packedNibbles)
|
||||
|
||||
let encoded = SSZ.encode(packedNibbles)
|
||||
|
||||
check:
|
||||
encoded.toHex() == expected
|
||||
unpackedNibbles == nibbles
|
||||
|
||||
test "Encode/decode zero nibble":
|
||||
const
|
||||
expected = "10"
|
||||
nibbles: seq[byte] = @[0]
|
||||
packedNibbles = packNibbles(nibbles)
|
||||
unpackedNibbles = unpackNibbles(packedNibbles)
|
||||
|
||||
let encoded = SSZ.encode(packedNibbles)
|
||||
|
||||
check:
|
||||
encoded.toHex() == expected
|
||||
unpackedNibbles == nibbles
|
||||
|
||||
test "Encode/decode one nibble":
|
||||
const
|
||||
expected = "11"
|
||||
nibbles: seq[byte] = @[1]
|
||||
packedNibbles = packNibbles(nibbles)
|
||||
unpackedNibbles = unpackNibbles(packedNibbles)
|
||||
|
||||
let encoded = SSZ.encode(packedNibbles)
|
||||
|
||||
check:
|
||||
encoded.toHex() == expected
|
||||
unpackedNibbles == nibbles
|
||||
|
||||
test "Encode/decode even nibbles":
|
||||
const
|
||||
expected = "008679e8ed"
|
||||
|
@ -40,6 +79,25 @@ suite "State Content Keys":
|
|||
encoded.toHex() == expected
|
||||
unpackedNibbles == nibbles
|
||||
|
||||
test "Encode/decode max length nibbles":
|
||||
const
|
||||
expected = "008679e8eda65bd257638cf8cf09b8238888947cc3c0bea2aa2cc3f1c4ac7a3002"
|
||||
nibbles: seq[byte] =
|
||||
@[
|
||||
8, 6, 7, 9, 0xe, 8, 0xe, 0xd, 0xa, 6, 5, 0xb, 0xd, 2, 5, 7, 6, 3, 8, 0xc, 0xf,
|
||||
8, 0xc, 0xf, 0, 9, 0xb, 8, 2, 3, 8, 8, 8, 8, 9, 4, 7, 0xc, 0xc, 3, 0xc, 0,
|
||||
0xb, 0xe, 0xa, 2, 0xa, 0xa, 2, 0xc, 0xc, 3, 0xf, 1, 0xc, 4, 0xa, 0xc, 7, 0xa,
|
||||
3, 0, 0, 2,
|
||||
]
|
||||
packedNibbles = packNibbles(nibbles)
|
||||
unpackedNibbles = unpackNibbles(packedNibbles)
|
||||
|
||||
let encoded = SSZ.encode(packedNibbles)
|
||||
|
||||
check:
|
||||
encoded.toHex() == expected
|
||||
unpackedNibbles == nibbles
|
||||
|
||||
test "Encode/decode AccountTrieNodeKey":
|
||||
const file = testVectorDir & "account_trie_node_key.yaml"
|
||||
|
||||
|
|
Loading…
Reference in New Issue