Skip to content

Commit

Permalink
Bump portal spec tests and update state validation tests to use state…
Browse files Browse the repository at this point in the history
… roots in test vectors. (#2193)

* Bump portal spec tests to version a549242

* Update state validation tests to use state roots in portal spec test vectors.
  • Loading branch information
web3-developer committed May 17, 2024
1 parent 33ac532 commit 047f8ec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 80 deletions.
11 changes: 10 additions & 1 deletion fluffy/common/common_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@

{.push raises: [].}

import std/[os, strutils], chronicles, stew/io2, eth/p2p/discoveryv5/enr
import
std/[os, strutils],
chronicles,
eth/common,
stew/[io2, arrayops],
eth/p2p/discoveryv5/enr

func init*(T: type KeccakHash, hash: openArray[byte]): T =
doAssert(hash.len() == 32)
KeccakHash(data: array[32, byte].initCopyFrom(hash))

iterator strippedLines(filename: string): string {.raises: [ref IOError].} =
for line in lines(filename):
Expand Down
7 changes: 5 additions & 2 deletions fluffy/network/state/state_validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import
results, stew/arrayops, eth/[common, trie], ../../common/common_types, ./state_content
results,
eth/[common, trie],
../../common/[common_types, common_utils],
./state_content

export results

Expand All @@ -28,7 +31,7 @@ proc isValidNextNode(thisNodeRlp: Rlp, rlpIdx: int, nextNode: TrieNode): bool =
let hash = hashOrShortRlp.toBytes()
if hash.len() != 32:
return false
KeccakHash(data: array[32, byte].initCopyFrom(hash))
KeccakHash.init(hash)

nextNode.hashEquals(nextHash)

Expand Down
17 changes: 10 additions & 7 deletions fluffy/tests/state_network_tests/test_state_network_gossip.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ procSuite "State Network Gossip":
asyncTest "Test Gossip of Account Trie Node Offer":
const file = testVectorDir / "recursive_gossip.yaml"

type
YamlOffer = object
content_key: string
content_value: string
type YamlRecursiveGossipKV = object
content_key: string
content_value: string

YamlRecursiveGossip = seq[seq[YamlOffer]]
type YamlRecursiveGossipData = object
state_root: string
recursive_gossip: seq[YamlRecursiveGossipKV]

type YamlRecursiveGossipKVs = seq[YamlRecursiveGossipData]

let
testCase = YamlRecursiveGossip.loadFromYaml(file).valueOr:
testCase = YamlRecursiveGossipKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
recursiveGossipSteps = testCase[0]
recursiveGossipSteps = testCase[0].recursive_gossip
numOfClients = recursiveGossipSteps.len() - 1

var clients: seq[StateNetwork]
Expand Down
102 changes: 33 additions & 69 deletions fluffy/tests/state_network_tests/test_state_validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import
unittest2,
stew/byteutils,
eth/common,
../../common/common_utils,
../../network/state/state_content,
../../network/state/state_validation,
../../eth_data/yaml_utils
Expand All @@ -23,6 +24,7 @@ type YamlTrieNodeRecursiveGossipKV = ref object
content_value_retrieval: string

type YamlTrieNodeKV = object
state_root: string
content_key: string
content_value_offer: string
content_value_retrieval: string
Expand All @@ -31,6 +33,7 @@ type YamlTrieNodeKV = object
type YamlTrieNodeKVs = seq[YamlTrieNodeKV]

type YamlContractBytecodeKV = object
state_root: string
content_key: string
content_value_offer: string
content_value_retrieval: string
Expand All @@ -41,7 +44,11 @@ type YamlRecursiveGossipKV = object
content_key: string
content_value: string

type YamlRecursiveGossipKVs = seq[seq[YamlRecursiveGossipKV]]
type YamlRecursiveGossipData = object
state_root: string
recursive_gossip: seq[YamlRecursiveGossipKV]

type YamlRecursiveGossipKVs = seq[YamlRecursiveGossipData]

suite "State Validation":
# Retrieval validation tests
Expand Down Expand Up @@ -166,18 +173,12 @@ suite "State Validation":

test "Validate valid AccountTrieNodeOffer nodes":
const file = testVectorDir / "account_trie_node.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".hexToSeqByte(),
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

block:
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
Expand Down Expand Up @@ -209,17 +210,16 @@ suite "State Validation":
test "Validate invalid AccountTrieNodeOffer nodes - bad state roots":
const file = testVectorDir / "account_trie_node.yaml"
const stateRoots = [
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xBAD8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".hexToSeqByte(),
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
"0xBAD8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544",
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(stateRoots[i].hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
Expand All @@ -234,18 +234,12 @@ suite "State Validation":

test "Validate invalid AccountTrieNodeOffer nodes - bad nodes":
const file = testVectorDir / "account_trie_node.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".hexToSeqByte(),
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
var contentValueOffer =
Expand All @@ -263,8 +257,7 @@ suite "State Validation":
for i, testData in testCase:
if i == 1:
continue # second test case only has root node
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
var contentValueOffer =
Expand All @@ -280,8 +273,7 @@ suite "State Validation":
"hash of next node doesn't match the expected" in res.error()

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
var contentValueOffer =
Expand All @@ -299,17 +291,12 @@ suite "State Validation":

test "Validate valid ContractTrieNodeOffer nodes":
const file = testVectorDir / "contract_storage_trie_node.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

block:
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
Expand Down Expand Up @@ -341,16 +328,15 @@ suite "State Validation":
test "Validate invalid ContractTrieNodeOffer nodes - bad state roots":
const file = testVectorDir / "contract_storage_trie_node.yaml"
const stateRoots = [
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(stateRoots[i].hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
Expand All @@ -365,17 +351,12 @@ suite "State Validation":

test "Validate invalid ContractTrieNodeOffer nodes - bad nodes":
const file = testVectorDir / "contract_storage_trie_node.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
]

let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

block:
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
Expand Down Expand Up @@ -448,16 +429,12 @@ suite "State Validation":

test "Validate valid ContractCodeOffer nodes":
const file = testVectorDir / "contract_bytecode.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte()
]

let testCase = YamlContractBytecodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
Expand All @@ -471,16 +448,14 @@ suite "State Validation":

test "Validate invalid ContractCodeOffer nodes - bad state root":
const file = testVectorDir / "contract_bytecode.yaml"
const stateRoots = [
"0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte()
]
const stateRoots =
["0xBAD7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61"]

let testCase = YamlContractBytecodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(stateRoots[i].hexToSeqByte())

let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
Expand All @@ -495,16 +470,12 @@ suite "State Validation":

test "Validate invalid ContractCodeOffer nodes - bad nodes and bytecode":
const file = testVectorDir / "contract_bytecode.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte()
]

let testCase = YamlContractBytecodeKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error

for i, testData in testCase:
var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

block:
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get()
Expand Down Expand Up @@ -566,9 +537,9 @@ suite "State Validation":
test "Validate valid AccountTrieNodeOffer recursive gossip nodes":
const file = testVectorDir / "recursive_gossip.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61",
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544",
]

let testCase = YamlRecursiveGossipKVs.loadFromYaml(file).valueOr:
Expand All @@ -578,10 +549,9 @@ suite "State Validation":
if i == 1:
continue

var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(stateRoots[i].hexToSeqByte())

for kv in testData:
for kv in testData.recursive_gossip:
let contentKey = decode(kv.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
SSZ.decode(kv.content_value.hexToSeqByte(), AccountTrieNodeOffer)
Expand All @@ -594,11 +564,6 @@ suite "State Validation":

test "Validate valid ContractTrieNodeOffer recursive gossip nodes":
const file = testVectorDir / "recursive_gossip.yaml"
const stateRoots = [
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0x1ad7b80af0c28bc1489513346d2706885be90abb07f23ca28e50482adb392d61".hexToSeqByte(),
"0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544".hexToSeqByte(),
]

let testCase = YamlRecursiveGossipKVs.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
Expand All @@ -607,10 +572,9 @@ suite "State Validation":
if i != 1:
continue

var stateRoot: KeccakHash
copyMem(addr stateRoot, unsafeAddr stateRoots[i][0], 32)
var stateRoot = KeccakHash.init(testData.state_root.hexToSeqByte())

for kv in testData:
for kv in testData.recursive_gossip:
let contentKey = decode(kv.content_key.hexToSeqByte().ByteList).get()
let contentValueOffer =
SSZ.decode(kv.content_value.hexToSeqByte(), ContractTrieNodeOffer)
Expand Down

0 comments on commit 047f8ec

Please sign in to comment.