-
Notifications
You must be signed in to change notification settings - Fork 2
/
merkleblock.py
193 lines (166 loc) · 5.75 KB
/
merkleblock.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
from io import BytesIO
import math
from tkinter.messagebox import NO
from typing import List
from typing_extensions import Self
from helper import bytes_to_bit_field, little_endian_to_int, merkle_parent, read_varint
class MerkleTree:
def __init__(self, total: int) -> Self:
"""
Instantiates a merkle tree object
"""
self.total = total
self.max_depth: int = math.ceil(math.log(self.total, 2))
self.nodes: List[List] = []
for depth in range(self.max_depth + 1):
num_items = math.ceil(self.total / 2**(self.max_depth - depth))
level_hashes = [None] * num_items
self.nodes.append(level_hashes)
self.current_depth: int = 0
self.current_index: int = 0
def __repr__(self) -> str:
"""
String representation of a MerkleTree
"""
result = []
for depth, level in enumerate(self.nodes):
items = []
for index, h in enumerate(level):
if h is None:
short = 'None'
else:
short = f'{h.hex()[:8]}...'
if depth == self.current_depth and index == self.current_index:
items.append(f'*{short[:-2]}*')
else:
items.append(f'{short}')
result.append(', '.join(items))
return '\n'.join(result)
def populate_tree(self, flag_bits: List[int], hashes: List[bytes]):
"""
Populate the merkle tree and calculate merkle root
"""
while self.root() is None:
if self.is_leaf():
flag_bits.pop(0)
self.set_current_node(hashes.pop(0))
self.up()
else:
left_hash = self.get_left_node()
if left_hash is None:
if flag_bits.pop(0) == 0:
self.set_current_node(hashes.pop(0))
self.up()
else:
self.left()
elif self.right_exists():
right_hash = self.get_right_node()
if right_hash is None:
self.right()
else:
self.set_current_node(merkle_parent(left_hash, right_hash))
self.up()
else:
self.set_current_node(merkle_parent(left_hash, left_hash))
self.up()
if len(hashes) != 0:
raise RuntimeError(f"hashes not all consumes {len(hashes)}")
for flag_bit in flag_bits:
if flag_bit != 0:
raise RuntimeError('flag bits not all consumed')
def up(self):
"""
Traverse up the merkle tree
"""
self.current_depth -= 1
self.current_index //= 2
def left(self):
"""
Traverse to the the left of the merkle tree
"""
self.current_depth +=1
self.current_index *= 2
def right(self):
"""
Traverse to the right of the merkle tree
"""
self.current_depth += 1
self.current_index = self.current_index * 2 + 1
def root(self):
"""
Retrieve the root of the merkle tree
"""
return self.nodes[0][0]
def set_current_node(self, value):
"""
Given a value, set the current node to the value
"""
self.nodes[self.current_depth][self.current_index] = value
def get_current_node(self):
return self.nodes[self.current_depth][self.current_index]
def get_left_node(self):
return self.nodes[self.current_depth + 1][self.current_index * 2]
def get_right_node(self):
return self.nodes[self.current_depth + 1][self.current_index * 2 + 1]
def is_leaf(self):
return self.current_depth == self.max_depth
def right_exists(self):
return len(self.nodes[self.current_depth + 1]) > self.current_index * 2 + 1
class MerkleBlock:
def __init__(
self,
version,
prev_block,
merkle_root,
timestamp,
bits,
nonce,
total,
hashes,
flags
) -> None:
"""
Instantiates a MerkleBlock object
"""
self.version = version
self.prev_block = prev_block
self.merkle_root = merkle_root
self.timestamp = timestamp
self.bits = bits
self.nonce = nonce
self.total = total
self.hashes = hashes
self.flags = flags
def __repr__(self) -> str:
"""
String representation of a MerkleBlock
"""
result = f'{self.total}\n'
for h in self.hashes:
result += f'\t{h.hex()}\n'
result += f'{self.flags.hex()}'
@classmethod
def parse(cls, s: BytesIO):
"""
Parse a stream of merkle block into MerkleBlock object
"""
version = little_endian_to_int(s.read(4))
prev_block = s.read(32)[::-1]
merkle_root = s.read(32)[::-1]
timestamp = little_endian_to_int(s.read(4))
bits = s.read(4)
nonce = s.read(4)
total = little_endian_to_int(s.read(4))
num_hashes = read_varint(s)
hashes = []
for _ in range(num_hashes):
hashes.append(s.read(32)[::-1])
flags_length = read_varint(s)
flags = s.read(flags_length)
return cls(version, prev_block, merkle_root, timestamp, bits, nonce, total, hashes, flags)
def is_valid(self):
flag_bits = bytes_to_bit_field(self.flags)
hashes = [h[::-1] for h in self.hashes]
merkle_tree = MerkleTree(self.total)
merkle_tree.populate_tree(flag_bits=flag_bits, hashes=hashes)
return merkle_tree.root()[::-1] == self.merkle_root