-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterlink.py
31 lines (25 loc) · 980 Bytes
/
interlink.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
from merkle import mtr
from helpers import compact_list_repr
def normalized_block_id(maybe_block_id):
if isinstance(maybe_block_id, str):
return bytearray.fromhex(maybe_block_id)[::-1]
return maybe_block_id
class Interlink:
def __init__(self, genesis, blocks=None):
self.genesis = normalized_block_id(genesis)
self.blocks = [] if blocks is None else blocks
def update(self, block_id, level):
block_id = normalized_block_id(block_id)
blocks = self.blocks.copy()
for i in range(0, level+1):
if i < len(blocks):
blocks[i] = block_id
else:
blocks.append(block_id)
return Interlink(genesis=self.genesis, blocks=blocks)
def as_array(self):
return self.blocks + [self.genesis]
def hash(self):
return mtr(self.as_array())
def __str__(self):
return compact_list_repr(bytearr[::-1].hex() for bytearr in self.as_array())