Skip to content

Instantly share code, notes, and snippets.

Last active Jan 28, 2021
What would you like to do?
Blockchain: Step 3
import hashlib
import json
from time import time
class Blockchain(object):
def __init__(self):
self.current_transactions = []
self.chain = []
# Create the genesis block
self.new_block(previous_hash=1, proof=100)
def new_block(self, proof, previous_hash=None):
Create a new Block in the Blockchain
:param proof: <int> The proof given by the Proof of Work algorithm
:param previous_hash: (Optional) <str> Hash of previous Block
:return: <dict> New Block
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.current_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
# Reset the current list of transactions
self.current_transactions = []
return block
def new_transaction(self, sender, recipient, amount):
Creates a new transaction to go into the next mined Block
:param sender: <str> Address of the Sender
:param recipient: <str> Address of the Recipient
:param amount: <int> Amount
:return: <int> The index of the Block that will hold this transaction
'sender': sender,
'recipient': recipient,
'amount': amount,
return self.last_block['index'] + 1
def last_block(self):
return self.chain[-1]
def hash(block):
Creates a SHA-256 hash of a Block
:param block: <dict> Block
:return: <str>
# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes
block_string = json.dumps(block, sort_keys=True).encode()
return hashlib.sha256(block_string).hexdigest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment