Module 1 - Blockchain
Module 1 - Blockchain
# To be installed:
import datetime
import hashlib
import json
class Blockchain:
def __init__(self):
self.chain = []
'timestamp': str(datetime.datetime.now()),
'proof': proof,
'previous_hash': previous_hash}
self.chain.append(block)
return block
def get_previous_block(self):
return self.chain[-1]
check_proof = False
"""
check_proof = True
else:
new_proof += 1
return new_proof
return hashlib.sha256(encoded_block).hexdigest()
def is_chain_valid(self, chain):
while block_index < len(chain): # will stop if the conditon blockindex is less than length of chain , we
will not stop till we reach last block
block = chain[block_index]
if block['previous_hash'] != self.hash(previous_block):
return False
previous_proof = previous_block['proof']
proof = block['proof']
if hash_operation[:4] != '0000':
return False
previous_block = block
block_index += 1
return True
app = Flask(__name__)
# Creating a Blockchain
blockchain = Blockchain()
def mine_block():
previous_block = blockchain.get_previous_block()
previous_proof = previous_block['proof']
proof = blockchain.proof_of_work(previous_proof)
previous_hash = blockchain.hash(previous_block)
'index': block['index'],
'timestamp': block['timestamp'],
'proof': block['proof'],
'previous_hash': block['previous_hash']}
def get_chain():
'length': len(blockchain.chain)}
def is_valid():
is_valid = blockchain.is_chain_valid(blockchain.chain)
if is_valid:
else: