Skip to content

Instantly share code, notes, and snippets.

@hiruthicShaSS
Last active July 5, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiruthicShaSS/e50b42219893882574967c2bb310910a to your computer and use it in GitHub Desktop.
Save hiruthicShaSS/e50b42219893882574967c2bb310910a to your computer and use it in GitHub Desktop.
Simple blockchain implementation with python
# This code has been updated multiple times. You can access the new version here:
# https://github.com/hiruthicShaSS/simple-blockchain
from hashlib import sha1
from datetime import datetime
import time
class Block:
def __init__(self, data: dict):
self.previousBlockHash = "0"
self.timestamp = datetime.now().strftime("%d/%m/%Y")
self.data = data
self.nonce = 0
self.hash = self.generateHash()
print(f"Block generated: {self.hash}")
def __repr__(self):
return f"(timestamp: {self.timestamp} | data: {self.data})"
def generateHash(self):
blockString = self.timestamp + self.previousBlockHash + \
str(self.data) + str(self.nonce)
return sha1(blockString.encode()).hexdigest()
def mine(self, difficulty: int):
expectedInitial = "".join(map(str, [0]*difficulty))
# Recalculate the hash with varying nonce untill we get the desired
# number of zeros before each blocks hash
start = time.time()
while (self.hash[0:difficulty] != expectedInitial):
self.nonce += 1
self.hash = self.generateHash()
duration = time.time() - start
print(f"Block mined: {self.hash} ({int(duration)} sec's)")
class BlockChain:
def __init__(self):
self.chain = [self.generateGenesisBlock()]
self.difficulty = 5
print("Blockchain initiated\n\n")
def generateGenesisBlock(self):
# Genesis block with empty data and default hash
return Block("Genesis block")
def addBlock(self, block: Block):
# Append future blocks to the chain
block.previousBlockHash = self.getLastBlock().hash
# Recalculate the blocks hash with the previous block hash
block.mine(self.difficulty)
self.chain.append(block)
print(f"Block added\n")
def getLastBlock(self):
return self.chain[-1]
def verifyIntegrity(self):
# Iterate through the chain and calculate eash blocks hash
# and previous blocks hash and verify it mathces with consecuent blocks
for i in range(len(self.chain)):
currentBlock = self.chain[i]
previousBlock = self.chain[i - 1]
if (currentBlock.hash != currentBlock.generateHash() and previousBlock.hash == currentBlock.previousBlockHash):
print("\nBlockchain tampered.")
print(f"Tampared block: {currentBlock}")
print(
f"Block hash: {currentBlock.hash} | Original hash: {currentBlock.generateHash()}")
break
else:
print("Blockchain is clean")
def display(self):
for block in self.chain:
print(block.hash[0:5], end=" => ")
blockchain = BlockChain()
block1 = Block({"data": "Adding block 1"})
blockchain.addBlock(block1)
block2 = Block({"data": "Appending block 2"})
blockchain.addBlock(block2)
block3 = Block({"data": "Appending block 3"})
blockchain.addBlock(block3)
blockchain.verifyIntegrity()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment