-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
78 lines (62 loc) · 2.86 KB
/
test.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
from unittest import TestCase
from merk import *
class TestMerkleTree(TestCase):
def test_construction_1(self):
should_be = [None, None, None, 1, 2, 3, 4]
m = MerkleTree([1, 2, 3, 4])
self.assertEqual(len(should_be), len(m.nodes))
def test_construction_2(self):
should_be = [None, None, 1, 2, 3]
m = MerkleTree([1, 2, 3])
self.assertEqual(len(should_be), len(m.nodes))
def test_construction_3(self):
should_be = [None, None, None, None, None, 1, 2, 3, 4, 5, 6]
m = MerkleTree([1, 2, 3, 4, 5, 6])
self.assertEqual(len(should_be), len(m.nodes))
def test_make_merkle_works(self):
# create a merkle tree that should be
leaves = [1, 2, 3, 4]
test_merkle = [None,
None,
None,
MerkleTree.hash(bytes(1)),
MerkleTree.hash(bytes(2)),
MerkleTree.hash(bytes(3)),
MerkleTree.hash(bytes(4))]
test_merkle[2] = MerkleTree.hash(test_merkle[-2] + test_merkle[-1])
test_merkle[1] = MerkleTree.hash(test_merkle[3] + test_merkle[4])
test_merkle[0] = MerkleTree.hash(test_merkle[1] + test_merkle[2])
m = MerkleTree(leaves)
self.assertEqual(test_merkle, m.nodes)
def test_find_root(self):
leaves = [1, 2, 3, 4]
test_merkle = [None,
None,
None,
MerkleTree.hash(bytes(1)),
MerkleTree.hash(bytes(2)),
MerkleTree.hash(bytes(3)),
MerkleTree.hash(bytes(4))]
test_merkle[2] = MerkleTree.hash(test_merkle[-2] + test_merkle[-1])
test_merkle[1] = MerkleTree.hash(test_merkle[3] + test_merkle[4])
test_merkle[0] = MerkleTree.hash(test_merkle[1] + test_merkle[2])
m = MerkleTree(leaves)
self.assertEqual(m.root(2), test_merkle[0])
self.assertEqual(m.root(4), test_merkle[1])
self.assertEqual(m.root(), test_merkle[0])
def test_find_children(self):
leaves = [1, 2, 3, 4]
test_merkle = [None,
None,
None,
MerkleTree.hash(bytes(1)),
MerkleTree.hash(bytes(2)),
MerkleTree.hash(bytes(3)),
MerkleTree.hash(bytes(4))]
test_merkle[2] = MerkleTree.hash(test_merkle[-2] + test_merkle[-1])
test_merkle[1] = MerkleTree.hash(test_merkle[3] + test_merkle[4])
test_merkle[0] = MerkleTree.hash(test_merkle[1] + test_merkle[2])
m = MerkleTree(leaves)
self.assertEqual(m.children(0), [test_merkle[1], test_merkle[2]])
self.assertEqual(m.children(1), [test_merkle[3], test_merkle[4]])
self.assertEqual(m.children(2), [test_merkle[5], test_merkle[6]])