Created
December 11, 2021 03:30
-
-
Save SoftwareDevPro/3defafad147f1af3394554e53971f9f4 to your computer and use it in GitHub Desktop.
Simple Python Blockchain Implementation
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 hashlib import sha256 | |
import json | |
from time import time | |
class Block: | |
def __init__(self, timestamp=None, data=None): | |
self.timestamp = timestamp or time() | |
self.data = [] if data is None else data | |
self.prevHash = None | |
self.nonce = 0 | |
self.hash = self.getHash() | |
def getHash(self): | |
hash = sha256() | |
hash.update(str(self.prevHash).encode('utf-8')) | |
hash.update(str(self.timestamp).encode('utf-8')) | |
hash.update(str(self.data).encode('utf-8')) | |
hash.update(str(self.nonce).encode('utf-8')) | |
return hash.hexdigest() | |
def mine(self, difficulty): | |
while self.hash[:difficulty] != '0' * difficulty: | |
self.nonce += 1 | |
self.hash = self.getHash() | |
class Blockchain: | |
def __init__(self): | |
self.chain = [Block(str(int(time())))] | |
self.difficulty = 1 | |
self.blockTime = 30000 | |
def getLastBlock(self): | |
return self.chain[len(self.chain) - 1] | |
def addBlock(self, block): | |
block.prevHash = self.getLastBlock().hash | |
block.hash = block.getHash() | |
block.mine(self.difficulty) | |
self.chain.append(block) | |
self.difficulty += (-1, 1)[int(time()) - int(self.getLastBlock().timestamp) < self.blockTime] | |
def isValid(self): | |
for i in range(1, len(self.chain)): | |
currentBlock = self.chain[i] | |
prevBlock = self.chain[i - 1] | |
if (currentBlock.hash != currentBlock.getHash() or prevBlock.hash != currentBlock.prevHash): | |
return False | |
return True | |
def __repr__(self): | |
return json.dumps([{'data': item.data, 'timestamp': item.timestamp, 'nonce': item.nonce, 'hash': item.hash, 'prevHash': item.prevHash} for item in self.chain], indent=4) | |
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 blockchain import * | |
from time import time | |
if __name__ == '__main__': | |
JeChain = Blockchain() | |
# Add a new block | |
JeChain.addBlock(Block(str(int(time())), ({"from": "John", "to": "Bob", "amount": 100}))) | |
# Prints out the updated chain | |
print(JeChain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment