Skip to content

Instantly share code, notes, and snippets.

def consensus():
"""
Our simple consensus algorithm. If a longer valid chain is
found, our chain is replaced with it.
"""
global blockchain
longest_chain = None
current_len = len(blockchain.chain)
from flask import Flask, request
import requests
# Initialize flask application
app = Flask(__name__)
# Initialize a blockchain object.
blockchain = Blockchain()
# Flask's way of declaring end-points
class Block:
"""
previous code...
"""
def add_block(self, block, proof):
"""
A function that adds the block to the chain after verification.
Verification includes:
* Checking if the proof is valid.
* The previous_hash referred in the block and the hash of latest block
class Blockchain:
"""
previous code...
"""
def proof_of_work(self, block):
"""
Function that tries different values of nonce to get a hash
that satisfies our difficulty criteria.
"""
block.nonce = 0
class Blockchain:
# difficulty of our PoW algorithm
difficulty = 2
def __init__(self):
self.unconfirmed_transactions = []
self.chain = [] #store chain of Block
@property
def last_block(self):
@ngocbh
ngocbh / block.py
Last active March 10, 2020 14:41
a block
class Block:
def __init__(self, index, transactions, timestamp, previous_hash, nonce = 0):
self.index = index
self.transactions = transactions # data
self.timestamp = timestamp
self.previous_hash = previous_hash
self.nonce = nonce
def compute_hash(self):
block_string = json.dumps(self.__dict__, sort_keys=True)