-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_all.py
388 lines (284 loc) · 10.2 KB
/
test_all.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import random
from cbc_lmd.main import (
Block,
CompressedTree,
)
from cbc_lmd.message import (
LayerStore,
ValidatorSet
)
def test_inserting_on_genesis():
genesis = Block(None)
tree = CompressedTree(genesis)
block = Block(genesis)
node = tree.add_new_latest_block(block, 0)
assert tree.size == 2
assert tree.root.block == genesis
assert tree.root.children.pop() == node
def test_inserting_on_leaf():
genesis = Block(None)
tree = CompressedTree(genesis)
block_1 = Block(genesis)
_ = tree.add_new_latest_block(block_1, 0)
block_2 = Block(block_1)
node_2 = tree.add_new_latest_block(block_2, 0)
assert tree.size == 2
assert tree.root.block == genesis
assert tree.root.children.pop() == node_2
def test_inserting_on_intermediate():
genesis = Block(None)
tree = CompressedTree(genesis)
block_1 = Block(genesis)
_ = tree.add_new_latest_block(block_1, 0)
block_2 = Block(block_1)
_ = tree.add_new_latest_block(block_2, 0)
on_inter_block = Block(block_1)
on_inter_node = tree.add_new_latest_block(on_inter_block, 1)
assert tree.size == 4
assert tree.root == on_inter_node.parent.parent
def test_vals_add_on_other_blocks():
genesis = Block(None)
tree = CompressedTree(genesis)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
val_0_block = tree.latest_block_nodes[0].block
for i in range(3):
block = Block(val_0_block)
_ = tree.add_new_latest_block(block, i)
assert tree.size == 5
def test_height():
genesis = Block(None)
assert genesis.height == 0
prev_block = genesis
for _ in range(1024):
block = Block(prev_block)
assert block.height == prev_block.height + 1
prev_block = block
def test_skip_list():
blocks = []
genesis = Block(None)
blocks.append(genesis)
prev_block = genesis
for _ in range(1024):
block = Block(prev_block)
blocks.append(block)
prev_block = block
for block in blocks:
height = block.height
assert block == blocks[height]
for idx, skip_block in enumerate(block.skip_list):
if skip_block is None:
assert height - 2**idx < 0
else:
assert skip_block.height == height - 2**idx
def test_prev_at_height():
blocks = []
genesis = Block(None)
blocks.append(genesis)
prev_block = genesis
for i in range(256):
block = Block(prev_block)
blocks.append(block)
prev_block = block
for block in blocks:
for i in range(block.height):
at_height = block.prev_at_height(i)
assert at_height == blocks[i]
def test_new_finalised_node_pruning():
# Setup
genesis = Block(None)
tree = CompressedTree(genesis)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
val_0_block = tree.latest_block_nodes[0].block
for i in range(3):
block = Block(val_0_block)
_ = tree.add_new_latest_block(block, i)
assert tree.size == 5
# Test Pruning
new_root = tree.node_with_block[val_0_block]
tree.prune(new_root)
assert tree.size == 4
def test_ghost():
# Setup
genesis = Block(None)
tree = CompressedTree(genesis)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
val_0_block = tree.latest_block_nodes[0].block
for i in range(3):
block = Block(val_0_block)
_ = tree.add_new_latest_block(block, i)
val_0_block = tree.latest_block_nodes[0].block
# Giving this block more weight, gives GHOST determanism
b = Block(val_0_block)
head_node = tree.add_new_latest_block(b, 0)
weight = {b: 2}
assert head_node == tree.find_head(weight)
def test_path_block_to_child_node():
genesis = Block(None)
tree = CompressedTree(genesis)
block_1 = Block(genesis)
node_1 = tree.add_new_latest_block(block_1, 0)
assert tree.path_block_to_child_node[block_1] == node_1
assert len(tree.path_block_to_child_node) == 1
block_2 = Block(block_1)
node_2 = tree.add_new_latest_block(block_2, 0)
assert tree.path_block_to_child_node[block_1] == node_2
assert len(tree.path_block_to_child_node) == 1
on_inter_block = Block(block_1)
on_inter_node = tree.add_new_latest_block(on_inter_block, 1)
assert tree.path_block_to_child_node[block_1].block == block_1 # node_1 was deleted, so it's a dif node
assert tree.path_block_to_child_node[block_2] == node_2
assert tree.path_block_to_child_node[on_inter_block] == on_inter_node
assert len(tree.path_block_to_child_node) == 3
def test_delete_with_child():
genesis = Block(None)
tree = CompressedTree(genesis)
block_1_val_0 = Block(genesis)
_ = tree.add_new_latest_block(block_1_val_0, 0)
block_2_val_0 = Block(block_1_val_0)
_ = tree.add_new_latest_block(block_2_val_0, 0)
block_1_val_1 = Block(block_1_val_0)
_ = tree.add_new_latest_block(block_1_val_1, 1)
block_1_val_2 = Block(genesis)
_ = tree.add_new_latest_block(block_1_val_2, 2)
block_3_val_0 = Block(block_1_val_2)
_ = tree.add_new_latest_block(block_3_val_0, 0)
assert tree.size == 4
assert len(tree.root.children) == 2
assert tree.latest_block_nodes[1] in tree.root.children
assert tree.latest_block_nodes[2] in tree.root.children
assert tree.latest_block_nodes[0] not in tree.root.children
def test_add_not_on_root():
genesis = Block(None)
tree = CompressedTree(genesis)
block = Block(None)
node = tree.add_new_latest_block(block, 0)
assert node is None
block = Block(genesis)
node = tree.add_new_latest_block(block, 0)
assert tree.root.children.pop() == node
assert node.block == block
def test_find_prev_in_tree():
genesis = Block(None)
tree = CompressedTree(genesis)
block = Block(None)
assert None is tree.find_prev_node_in_tree(block)
block = Block(genesis)
assert tree.root is tree.find_prev_node_in_tree(block)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
block_1 = Block(tree.latest_block_nodes[2].block)
assert block_1.parent_block == tree.find_prev_node_in_tree(block_1).block
tree.add_new_latest_block(block_1, 2)
assert block_1 == tree.latest_block_nodes[2].block
block_2 = Block(tree.latest_block_nodes[2].block)
assert block_2.parent_block == tree.find_prev_node_in_tree(block_2).block
def test_random():
genesis = Block(None)
tree = CompressedTree(genesis)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
new_block = Block(tree.latest_block_nodes[1].block)
tree.add_new_latest_block(new_block, 1)
new_block = Block(tree.latest_block_nodes[1].block)
tree.add_new_latest_block(new_block, 1)
def test_massive_tree():
genesis = Block(None)
tree = CompressedTree(genesis)
for i in range(3):
block = Block(genesis)
_ = tree.add_new_latest_block(block, i)
for i in range(1000):
print(i)
prev_val = random.randint(0, 2)
new_block = Block(tree.latest_block_nodes[prev_val].block)
new_val = random.randint(0, 2)
tree.add_new_latest_block(new_block, new_val)
assert tree.size <= 6
block = tree.latest_block_nodes[2].block
assert tree.find_head({block: 1}).block.prev_at_height(block.height) == block
def test_make_val_set():
val_set = ValidatorSet(3)
assert len(val_set.validators) == 3
for val in val_set:
assert val.tree.root.block == val_set.genesis
def test_make_new_message():
val_set = ValidatorSet(3)
message = val_set.make_new_message(0)
assert message.block.parent_block == val_set.genesis
assert val_set.validators[0].latest_messages[0] == message
def test_make_new_messages():
val_set = ValidatorSet(3)
last_message = None
for i in range(100):
last_message = val_set.make_new_message(0)
for val in val_set:
val.see_message(last_message)
assert val.latest_messages[0] == last_message
assert len(val.justification) == 100
def test_make_new_messages_on_eachothers():
val_set = ValidatorSet(3)
for i in range(100):
latest = set()
for val in val_set:
latest.add(val.make_new_message())
for val in val_set:
for m in latest:
val.see_message(m)
for val in val_set:
for i in range(3):
assert val.latest_messages[i] in latest
def test_build_graph_basic():
val_set = ValidatorSet(3)
latest = set()
for val in val_set:
latest.add(val.make_new_message())
for m in latest:
for val in val_set:
val.see_message(m)
layer_store = LayerStore(val_set, val_set.genesis, 1)
for val in layer_store.layers[0]:
assert layer_store.layers[0][val] in latest
def test_build_graph_two_layers():
val_set = ValidatorSet(3)
zero = set()
for val in val_set:
zero.add(val.make_new_message())
for val in val_set:
for m in zero:
val.see_message(m)
one = set()
for val in val_set:
one.add(val.make_new_message())
layer_store = LayerStore(val_set, val_set.genesis, 1)
for val in layer_store.layers[1]:
assert layer_store.layers[1][val] in one
for val in layer_store.layers[0]:
assert layer_store.layers[0][val] in zero
def test_build_graph_two_layers_incremental():
val_set = ValidatorSet(3)
layer_store = LayerStore(val_set, val_set.genesis, 1)
zero = set()
for val in val_set:
new_message = val.make_new_message()
layer_store.add_message(new_message)
zero.add(new_message)
for val in val_set:
for m in zero:
val.see_message(m)
one = set()
for val in val_set:
new_message = val.make_new_message()
layer_store.add_message(new_message)
one.add(new_message)
for val in layer_store.layers[1]:
assert layer_store.layers[1][val] in one
for val in layer_store.layers[0]:
assert layer_store.layers[0][val] in zero