Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor to packNibbles and unpackNibbles and add additional tests. #2162

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions fluffy/network/state/state_content.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
58 changes: 58 additions & 0 deletions fluffy/tests/state_network_tests/test_state_content_keys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down