forked from llSourcell/The_Power_of_the_blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.py
41 lines (30 loc) · 1.13 KB
/
blockchain.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
import time
from . import block
from . import block_params
class BlockChain():
def __init__(self):
self.blockchain_store = self.fetch_blockchain()
def latest_block(self):
return self.blockchain_store[-1]
def generate_next_block(self, data):
index = len(self.blockchain_store)
previous_hash = self.latest_block().hash
timestamp = int(time.time())
params = block_params.BlockParams(index, previous_hash, timestamp, data)
new_block = block.Block(params)
self.blockchain_store.append(new_block)
# @TODO mock implement
def fetch_blockchain(self):
return [block.Block.genesis_block()]
def receive_new_block(self, new_block):
previous_block = self.latest_block()
if not new_block.has_valid_index(previous_block):
print('invalid index')
return
if not new_block.has_valid_previous_hash(previous_block):
print('invalid previous hash')
return
if not new_block.has_valid_hash():
print('invalid hash')
return
self.blockchain_store.append(new_block)