-
Notifications
You must be signed in to change notification settings - Fork 51
/
block.py
42 lines (38 loc) · 1.27 KB
/
block.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
from base import *
import transaction as T
class BlockTX(object):
def __init__(self, txs):
""" List of transactions (TX)"""
self.txs = txs
def hash(self):
sha(self.serialize())
def serialize(self):
return str([s.serialize() for s in self.txs])
def spends(self):
return [t.parent_tx for t in self.txs]
@staticmethod
def deserialize(s):
v = ast.literal_eval(s)
if isinstance(v, list):
b = BlockTX(map(T.TX.deserialize, v))
return b
else:
raise ValueError("Malformed tx block")
class BlockHeader(object):
def __init__(self, height, block_hash, prev, reward_address, nonce=None):
self.nonce = nonce
self.height = height
self.block_hash = block_hash
self.prev = prev
self.reward_address = reward_address
def serialize(self):
return str((self.nonce, self.height, self.block_hash, self.prev, self.reward_address))
@staticmethod
def deserialize(s):
v = ast.literal_eval(s)
if isinstance(v, tuple) and len(v) == 5:
return BlockHeader(v[1], v[2], v[3], v[4], v[0])
else:
raise ValueError("Bad BlockHeader")
def hash(self):
return sha(self.serialize())