-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_state_content_values_vectors.nim
171 lines (133 loc) · 5.78 KB
/
test_state_content_values_vectors.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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, sugar, sequtils],
unittest2,
stew/byteutils,
../../network/state/state_content,
../../eth_data/yaml_utils
const testVectorDir = "./vendor/portal-spec-tests/tests/mainnet/state/serialization/"
suite "State Content Values":
test "Encode/decode AccountTrieNodeOffer":
const file = testVectorDir / "account_trie_node_with_proof.yaml"
type YamlAccountTrieNodeWithProof = object
proof: seq[string]
block_hash: string
content_value: string
let
testCase = YamlAccountTrieNodeWithProof.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
blockHash = BlockHash.fromHex(testCase.block_hash)
proof =
TrieProof.init(testCase.proof.map((hex) => TrieNode.init(hex.hexToSeqByte())))
accountTrieNodeOffer = AccountTrieNodeOffer.init(proof, blockHash)
encoded = accountTrieNodeOffer.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = AccountTrieNodeOffer.decode(encoded).get()
check:
encoded == expected
decoded == accountTrieNodeOffer
test "Encode/decode AccountTrieNodeRetrieval":
const file = testVectorDir / "trie_node.yaml"
type YamlTrieNode = object
trie_node: string
content_value: string
let
testCase = YamlTrieNode.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
node = TrieNode.init(testCase.trie_node.hexToSeqByte())
accountTrieNodeRetrieval = AccountTrieNodeRetrieval(node: node)
encoded = accountTrieNodeRetrieval.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = AccountTrieNodeRetrieval.decode(encoded).get()
check:
encoded == expected
decoded == accountTrieNodeRetrieval
test "Encode/decode ContractTrieNodeOffer":
const file = testVectorDir / "contract_storage_trie_node_with_proof.yaml"
type YamlContractStorageTrieNodeWithProof = object
storage_proof: seq[string]
account_proof: seq[string]
block_hash: string
content_value: string
let
testCase = YamlContractStorageTrieNodeWithProof.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
blockHash = BlockHash.fromHex(testCase.block_hash)
storageProof = TrieProof.init(
testCase.storage_proof.map((hex) => TrieNode.init(hex.hexToSeqByte()))
)
accountProof = TrieProof.init(
testCase.account_proof.map((hex) => TrieNode.init(hex.hexToSeqByte()))
)
contractTrieNodeOffer = ContractTrieNodeOffer(
blockHash: blockHash, storage_proof: storageProof, account_proof: accountProof
)
encoded = contractTrieNodeOffer.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = ContractTrieNodeOffer.decode(encoded).get()
check:
encoded == expected
decoded == contractTrieNodeOffer
test "Encode/decode ContractTrieNodeRetrieval":
# TODO: This is practically the same as AccountTrieNodeRetrieval test,
# but we use different objects for it. Might want to adjust this to just
# 1 basic TrieNode type.
const file = testVectorDir / "trie_node.yaml"
type YamlTrieNode = object
trie_node: string
content_value: string
let
testCase = YamlTrieNode.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
node = TrieNode.init(testCase.trie_node.hexToSeqByte())
contractTrieNodeRetrieval = ContractTrieNodeRetrieval(node: node)
encoded = contractTrieNodeRetrieval.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = ContractTrieNodeRetrieval.decode(encoded).get()
check:
encoded == expected
decoded == contractTrieNodeRetrieval
test "Encode/decode ContractCodeOffer":
const file = testVectorDir / "contract_bytecode_with_proof.yaml"
type YamlContractBytecodeWithProof = object
bytecode: string
account_proof: seq[string]
block_hash: string
content_value: string
let
testCase = YamlContractBytecodeWithProof.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
code = Bytecode.init(testCase.bytecode.hexToSeqByte())
blockHash = BlockHash.fromHex(testCase.block_hash)
accountProof = TrieProof.init(
testCase.account_proof.map((hex) => TrieNode.init(hex.hexToSeqByte()))
)
contractCodeOffer =
ContractCodeOffer(code: code, blockHash: blockHash, accountProof: accountProof)
encoded = contractCodeOffer.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = ContractCodeOffer.decode(encoded).get()
check:
encoded == expected
decoded == contractCodeOffer
test "Encode/decode ContractCodeRetrieval":
const file = testVectorDir / "contract_bytecode.yaml"
type YamlContractBytecode = object
bytecode: string
content_value: string
let
testCase = YamlContractBytecode.loadFromYaml(file).valueOr:
raiseAssert "Cannot read test vector: " & error
code = Bytecode.init(testCase.bytecode.hexToSeqByte())
contractCodeRetrieval = ContractCodeRetrieval(code: code)
encoded = contractCodeRetrieval.encode()
expected = testCase.content_value.hexToSeqByte()
decoded = ContractCodeRetrieval.decode(encoded).get()
check:
encoded == expected
decoded == contractCodeRetrieval