forked from Chia-Network/chia-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_merkle_set.py
326 lines (267 loc) · 9.67 KB
/
test_merkle_set.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from __future__ import annotations
import itertools
import random
from hashlib import sha256
from itertools import permutations
from typing import List
import pytest
from chia_rs import compute_merkle_set_root
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.merkle_set import MerkleSet, confirm_included_already_hashed
class TestMerkleSet:
@pytest.mark.asyncio
async def test_basics(self, bt):
num_blocks = 20
blocks = bt.get_consecutive_blocks(num_blocks)
merkle_set = MerkleSet()
merkle_set_reverse = MerkleSet()
coins = list(itertools.chain.from_iterable(map(lambda block: block.get_included_reward_coins(), blocks)))
# excluded coin (not present in 'coins' and Merkle sets)
excl_coin = coins.pop()
for coin in reversed(coins):
merkle_set_reverse.add_already_hashed(coin.name())
for coin in coins:
merkle_set.add_already_hashed(coin.name())
for coin in coins:
result, proof = merkle_set.is_included_already_hashed(coin.name())
assert result is True
result_excl, proof_excl = merkle_set.is_included_already_hashed(excl_coin.name())
assert result_excl is False
validate_proof = confirm_included_already_hashed(merkle_set.get_root(), coin.name(), proof)
validate_proof_excl = confirm_included_already_hashed(merkle_set.get_root(), excl_coin.name(), proof_excl)
assert validate_proof is True
assert validate_proof_excl is False
# Test if the order of adding items changes the outcome
assert merkle_set.get_root() == merkle_set_reverse.get_root()
def hashdown(buf: bytes) -> bytes32:
return bytes32(sha256(bytes([0] * 30) + buf).digest())
@pytest.mark.asyncio
async def test_merkle_set_invalid_hash_size():
merkle_set = MerkleSet()
# this is too large
with pytest.raises(AssertionError):
merkle_set.add_already_hashed(bytes([0x80] + [0] * 32))
with pytest.raises(ValueError, match="could not convert slice to array"):
compute_merkle_set_root([bytes([0x80] + [0] * 32)])
# this is too small
with pytest.raises(AssertionError):
merkle_set.add_already_hashed(bytes([0x80] + [0] * 30))
with pytest.raises(ValueError, match="could not convert slice to array"):
compute_merkle_set_root([bytes([0x80] + [0] * 30)])
# empty
with pytest.raises(AssertionError):
merkle_set.add_already_hashed(b"")
with pytest.raises(ValueError, match="could not convert slice to array"):
compute_merkle_set_root([b""])
@pytest.mark.asyncio
async def test_merkle_set_1():
a = bytes32([0x80] + [0] * 31)
merkle_set = MerkleSet()
merkle_set.add_already_hashed(a)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root([a]))
assert merkle_set.get_root() == sha256(b"\1" + a).digest()
@pytest.mark.asyncio
async def test_merkle_set_duplicate():
a = bytes32([0x80] + [0] * 31)
merkle_set = MerkleSet()
merkle_set.add_already_hashed(a)
merkle_set.add_already_hashed(a)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root([a, a]))
assert merkle_set.get_root() == sha256(b"\1" + a).digest()
@pytest.mark.asyncio
async def test_merkle_set_0():
merkle_set = MerkleSet()
assert merkle_set.get_root() == bytes32(compute_merkle_set_root([]))
assert merkle_set.get_root() == bytes32([0] * 32)
@pytest.mark.asyncio
async def test_merkle_set_2():
a = bytes32([0x80] + [0] * 31)
b = bytes32([0x70] + [0] * 31)
merkle_set = MerkleSet()
merkle_set.add_already_hashed(a)
merkle_set.add_already_hashed(b)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root([a, b]))
assert merkle_set.get_root() == hashdown(b"\1\1" + b + a)
@pytest.mark.asyncio
async def test_merkle_set_2_reverse():
a = bytes32([0x80] + [0] * 31)
b = bytes32([0x70] + [0] * 31)
merkle_set = MerkleSet()
merkle_set.add_already_hashed(b)
merkle_set.add_already_hashed(a)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root([b, a]))
assert merkle_set.get_root() == hashdown(b"\1\1" + b + a)
@pytest.mark.asyncio
async def test_merkle_set_3():
a = bytes32([0x80] + [0] * 31)
b = bytes32([0x70] + [0] * 31)
c = bytes32([0x71] + [0] * 31)
values = [a, b, c]
for vals in permutations(values):
merkle_set = MerkleSet()
for v in vals:
merkle_set.add_already_hashed(v)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root(list(vals)))
assert merkle_set.get_root() == hashdown(b"\2\1" + hashdown(b"\1\1" + b + c) + a)
# this tree looks like this:
#
# o
# / \
# o a
# / \
# b c
@pytest.mark.asyncio
async def test_merkle_set_4():
a = bytes32([0x80] + [0] * 31)
b = bytes32([0x70] + [0] * 31)
c = bytes32([0x71] + [0] * 31)
d = bytes32([0x81] + [0] * 31)
values = [a, b, c, d]
for vals in permutations(values):
merkle_set = MerkleSet()
for v in vals:
merkle_set.add_already_hashed(v)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root(list(vals)))
assert merkle_set.get_root() == hashdown(b"\2\2" + hashdown(b"\1\1" + b + c) + hashdown(b"\1\1" + a + d))
# this tree looks like this:
#
# o
# / \
# o o
# / \ / \
# b c a d
@pytest.mark.asyncio
async def test_merkle_set_5():
BLANK = bytes32([0] * 32)
a = bytes32([0x58] + [0] * 31)
b = bytes32([0x23] + [0] * 31)
c = bytes32([0x21] + [0] * 31)
d = bytes32([0xCA] + [0] * 31)
e = bytes32([0x20] + [0] * 31)
# build the expected tree bottom up, since that's simpler
expected = hashdown(b"\1\1" + e + c)
expected = hashdown(b"\2\1" + expected + b)
expected = hashdown(b"\2\0" + expected + BLANK)
expected = hashdown(b"\2\0" + expected + BLANK)
expected = hashdown(b"\2\0" + expected + BLANK)
expected = hashdown(b"\0\2" + BLANK + expected)
expected = hashdown(b"\2\1" + expected + a)
expected = hashdown(b"\2\1" + expected + d)
values = [a, b, c, d, e]
for vals in permutations(values):
merkle_set = MerkleSet()
for v in vals:
merkle_set.add_already_hashed(v)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root(list(vals)))
assert merkle_set.get_root() == expected
# this tree looks like this:
#
# o
# / \
# o d
# / \
# o a
# / \
# E o
# / \
# o E
# / \
# o E
# / \
# o E
# / \
# o b
# / \
# e c
@pytest.mark.asyncio
async def test_merkle_left_edge():
BLANK = bytes32([0] * 32)
a = bytes32([0x80] + [0] * 31)
b = bytes32([0] * 31 + [1])
c = bytes32([0] * 31 + [2])
d = bytes32([0] * 31 + [3])
values = [a, b, c, d]
expected = hashdown(b"\1\1" + c + d)
expected = hashdown(b"\1\2" + b + expected)
for _ in range(253):
expected = hashdown(b"\2\0" + expected + BLANK)
expected = hashdown(b"\2\1" + expected + a)
for vals in permutations(values):
merkle_set = MerkleSet()
for v in vals:
merkle_set.add_already_hashed(v)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root(list(vals)))
assert merkle_set.get_root() == expected
# this tree looks like this:
# o
# / \
# o a
# / \
# o E
# / \
# . E
# .
# .
# / \
# o E
# / \
# b o
# / \
# c d
@pytest.mark.asyncio
async def test_merkle_right_edge():
BLANK = bytes32([0] * 32)
a = bytes32([0x40] + [0] * 31)
b = bytes32([0xFF] * 31 + [0xFF])
c = bytes32([0xFF] * 31 + [0xFE])
d = bytes32([0xFF] * 31 + [0xFD])
values = [a, b, c, d]
expected = hashdown(b"\1\1" + c + b)
expected = hashdown(b"\1\2" + d + expected)
for _ in range(253):
expected = hashdown(b"\0\2" + BLANK + expected)
expected = hashdown(b"\1\2" + a + expected)
for vals in permutations(values):
merkle_set = MerkleSet()
for v in vals:
merkle_set.add_already_hashed(v)
assert merkle_set.get_root() == bytes32(compute_merkle_set_root(list(vals)))
assert merkle_set.get_root() == expected
# this tree looks like this:
# o
# / \
# a o
# / \
# E o
# / \
# E o
# .
# .
# .
# o
# / \
# d o
# / \
# c b
def rand_hash(rng: random.Random) -> bytes32:
ret = bytearray(32)
for i in range(32):
ret[i] = rng.getrandbits(8)
return bytes32(ret)
@pytest.mark.asyncio
@pytest.mark.skip("This test is expensive and has already convinced us there are no discrepancies")
async def test_merkle_set_random_regression():
rng = random.Random()
rng.seed(123456)
for i in range(100):
size = rng.randint(0, 4000)
values: List[bytes32] = [rand_hash(rng) for _ in range(size)]
print(f"iter: {i}/100 size: {size}")
for _ in range(10):
rng.shuffle(values)
merkle_set = MerkleSet()
for v in values:
merkle_set.add_already_hashed(v)
python_root = merkle_set.get_root()
rust_root = bytes32(compute_merkle_set_root(values))
assert rust_root == python_root