This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |