-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retrieval content validation to state network. (#2166)
* Add retrieval content validation to state network. * Complete state validation tests for retrieval content. * Improve naming functions and parameters.
- Loading branch information
1 parent
143f2e9
commit 5c4358a
Showing
4 changed files
with
189 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Fluffy | ||
# Copyright (c) 2024 Status Research & Development GmbH | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
import eth/common, ./state_content | ||
|
||
proc validateFetchedAccountTrieNode*( | ||
trustedAccountTrieNodeKey: AccountTrieNodeKey, | ||
accountTrieNode: AccountTrieNodeRetrieval, | ||
): bool = | ||
let expectedHash = trustedAccountTrieNodeKey.nodeHash | ||
let actualHash = keccakHash(accountTrieNode.node.asSeq()) | ||
|
||
expectedHash == actualHash | ||
|
||
proc validateFetchedContractTrieNode*( | ||
trustedContractTrieNodeKey: ContractTrieNodeKey, | ||
contractTrieNode: ContractTrieNodeRetrieval, | ||
): bool = | ||
let expectedHash = trustedContractTrieNodeKey.nodeHash | ||
let actualHash = keccakHash(contractTrieNode.node.asSeq()) | ||
|
||
expectedHash == actualHash | ||
|
||
proc validateFetchedContractCode*( | ||
trustedContractCodeKey: ContractCodeKey, contractCode: ContractCodeRetrieval | ||
): bool = | ||
let expectedHash = trustedContractCodeKey.codeHash | ||
let actualHash = keccakHash(contractCode.code.asSeq()) | ||
|
||
expectedHash == actualHash |
143 changes: 143 additions & 0 deletions
143
fluffy/tests/state_network_tests/test_state_validation.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Fluffy | ||
# Copyright (c) 2023-2024 Status Research & Development GmbH | ||
# Licensed and distributed under either of | ||
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT). | ||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0). | ||
# at your option. This file may not be copied, modified, or distributed except according to those terms. | ||
|
||
import | ||
std/os, | ||
unittest2, | ||
stew/byteutils, | ||
../../network/state/state_content, | ||
../../network/state/state_validation, | ||
../../eth_data/yaml_utils | ||
|
||
const testVectorDir = "./vendor/portal-spec-tests/tests/mainnet/state/validation/" | ||
|
||
type YamlTrieNodeRecursiveGossipKV = ref object | ||
content_key: string | ||
content_value_offer: string | ||
content_value_retrieval: string | ||
|
||
type YamlTrieNodeKV = object | ||
content_key: string | ||
content_value_offer: string | ||
content_value_retrieval: string | ||
recursive_gossip: YamlTrieNodeRecursiveGossipKV | ||
|
||
type YamlTrieNodeKVs = seq[YamlTrieNodeKV] | ||
|
||
type YamlContractBytecodeKV = object | ||
content_key: string | ||
content_value_offer: string | ||
content_value_retrieval: string | ||
|
||
type YamlContractBytecodeKVs = seq[YamlContractBytecodeKV] | ||
|
||
suite "State Validation": | ||
test "Validate valid AccountTrieNodeRetrieval nodes": | ||
const file = testVectorDir / "account_trie_node.yaml" | ||
|
||
let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
let contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), AccountTrieNodeRetrieval | ||
) | ||
|
||
check: | ||
validateFetchedAccountTrieNode( | ||
contentKey.accountTrieNodeKey, contentValueRetrieval | ||
) | ||
|
||
test "Validate invalid AccountTrieNodeRetrieval nodes": | ||
const file = testVectorDir / "account_trie_node.yaml" | ||
|
||
let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
var contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), AccountTrieNodeRetrieval | ||
) | ||
|
||
contentValueRetrieval.node[^1] += 1 # Modify node hash | ||
|
||
check: | ||
not validateFetchedAccountTrieNode( | ||
contentKey.accountTrieNodeKey, contentValueRetrieval | ||
) | ||
|
||
test "Validate valid ContractTrieNodeRetrieval nodes": | ||
const file = testVectorDir / "contract_storage_trie_node.yaml" | ||
|
||
let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
let contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), ContractTrieNodeRetrieval | ||
) | ||
|
||
check: | ||
validateFetchedContractTrieNode( | ||
contentKey.contractTrieNodeKey, contentValueRetrieval | ||
) | ||
|
||
test "Validate invalid ContractTrieNodeRetrieval nodes": | ||
const file = testVectorDir / "contract_storage_trie_node.yaml" | ||
|
||
let testCase = YamlTrieNodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
var contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), ContractTrieNodeRetrieval | ||
) | ||
|
||
contentValueRetrieval.node[^1] += 1 # Modify node hash | ||
|
||
check: | ||
not validateFetchedContractTrieNode( | ||
contentKey.contractTrieNodeKey, contentValueRetrieval | ||
) | ||
|
||
test "Validate valid ContractCodeRetrieval nodes": | ||
const file = testVectorDir / "contract_bytecode.yaml" | ||
|
||
let testCase = YamlContractBytecodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
let contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), ContractCodeRetrieval | ||
) | ||
|
||
check: | ||
validateFetchedContractCode(contentKey.contractCodeKey, contentValueRetrieval) | ||
|
||
test "Validate invalid ContractCodeRetrieval nodes": | ||
const file = testVectorDir / "contract_bytecode.yaml" | ||
|
||
let testCase = YamlContractBytecodeKVs.loadFromYaml(file).valueOr: | ||
raiseAssert "Cannot read test vector: " & error | ||
|
||
for testData in testCase: | ||
let contentKey = decode(testData.content_key.hexToSeqByte().ByteList).get() | ||
var contentValueRetrieval = SSZ.decode( | ||
testData.content_value_retrieval.hexToSeqByte(), ContractCodeRetrieval | ||
) | ||
|
||
contentValueRetrieval.code[^1] += 1 # Modify node hash | ||
|
||
check: | ||
not validateFetchedContractCode( | ||
contentKey.contractCodeKey, contentValueRetrieval | ||
) |