-
Notifications
You must be signed in to change notification settings - Fork 29
/
block.py
37 lines (28 loc) · 987 Bytes
/
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
import hashlib
from . import block_params
class Block():
def __init__(self, params):
self.index = params.index
self.previous_hash = params.previous_hash
self.timestamp = params.timestamp
self.data = params.data
self.hash = self.calc_hash()
def params(self):
return block_params.BlockParams(
self.index,
self.previous_hash,
self.timestamp,
self.data
)
@classmethod
def genesis_block(cls):
params = block_params.BlockParams.genesis_params()
return cls(params)
def calc_hash(self):
return hashlib.sha256(str(self.params()).encode()).hexdigest()
def has_valid_index(self, previous_block):
return self.index == previous_block.index + 1
def has_valid_previous_hash(self, previous_block):
return self.previous_hash == previous_block.hash
def has_valid_hash(self):
return self.calc_hash() == self.hash