diff --git a/blockshell.py b/blockshell.py index e423245..ba1a139 100644 --- a/blockshell.py +++ b/blockshell.py @@ -1,12 +1,30 @@ +# -*- coding: utf-8 -*- +# ================================================== +# ==================== META DATA =================== +# ================================================== +__author__ = "Daxeel Soni" +__url__ = "https://daxeel.github.io" +__email__ = "daxeelsoni44@gmail.com" +__license__ = "MIT" +__version__ = "0.1" +__maintainer__ = "Daxeel Soni" + +# ================================================== +# ================= IMPORT MODULES ================= +# ================================================== import hashlib import datetime import json from colorama import Fore, Back, Style import time -# index, timestamp, previousHash, blockHash, data - +# ================================================== +# ====================BLOCK CLASS ================== +# ================================================== class Block: + """ + Create new block with arguments 'data' + """ def __init__(self, data, index=0): self.index = index self.previousHash = "" @@ -16,10 +34,16 @@ class Block: self.hash = self.calculateHash() def calculateHash(self): + """ + Method to calculate hash for the block. + """ hashData = str(self.index) + str(self.data) + self.timestamp + str(self.nonce) return hashlib.sha256(hashData).hexdigest() def mineBlock(self, difficulty): + """ + Mine block with proof of work by initial zeros algorithm. + """ print Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ..." startTime = time.time() @@ -33,14 +57,23 @@ class Block: print Style.RESET_ALL class Blockchain: + """ + Inititate new blockchain with this class. + """ def __init__(self): self.chain = [self.createGenesisBlock()] self.difficulty = 3 def createGenesisBlock(self): + """ + Method to create genesis block after just creating an object. + """ return Block("Genesis Block") def addBlock(self, newBlock): + """ + Method to mine new block from data to blockchain. + """ newBlock.index = len(self.chain) newBlock.previousHash = self.chain[-1].hash newBlock.mineBlock(self.difficulty) @@ -48,6 +81,9 @@ class Blockchain: self.writeBlocks() def writeBlocks(self): + """ + This method will write newly created block to chain.txt file in json format. + """ dataFile = file("chain.txt", "w") chainData = [] for eachBlock in self.chain: