Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ build/
dist/
blockshell/
blockshell.egg-info/
/.idea/blockshell.iml
/.idea/git_toolbox_prj.xml
/.idea/misc.xml
/.idea/modules.xml
/.idea/inspectionProfiles/profiles_settings.xml
/.idea/inspectionProfiles/Project_Default.xml
/.idea/vcs.xml
/.idea/workspace.xml
19 changes: 10 additions & 9 deletions blockchain/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
__url__ = "https://daxeel.github.io"
__email__ = "daxeelsoni44@gmail.com"
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Daxeel Soni"
__version__ = "0.11"
__maintainer__ = "Tymoteusz Ciesielski"

# ==================================================
# ================= IMPORT MODULES =================
Expand All @@ -17,7 +17,7 @@
import json
from colorama import Fore, Back, Style
import time
import sys


# ==================================================
# =================== BLOCK CLASS ==================
Expand All @@ -39,23 +39,24 @@ def calculateHash(self):
Method to calculate hash from metadata
"""
hashData = str(self.index) + str(self.data) + self.timestamp + self.previousHash + str(self.nonce)
return hashlib.sha256(hashData).hexdigest()
return hashlib.sha256(hashData.encode('utf-8')).hexdigest()

def mineBlock(self, difficulty):
"""
Method for Proof of Work
"""
print Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ..."
print(Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ...")
startTime = time.time()

while self.hash[:difficulty] != "0"*difficulty:
self.nonce += 1
self.hash = self.calculateHash()

endTime = time.time()
print Back.BLUE + "[ Info ] Time Elapsed : " + str(endTime - startTime) + " seconds."
print Back.BLUE + "[ Info ] Mined Hash : " + self.hash
print Style.RESET_ALL
print(Back.BLUE + "[ Info ] Time Elapsed : " + str(endTime - startTime) + " seconds.")
print(Back.BLUE + "[ Info ] Mined Hash : " + self.hash)
print(Style.RESET_ALL)


# ==================================================
# ================ BLOCKCHAIN CLASS ================
Expand Down Expand Up @@ -88,7 +89,7 @@ def writeBlocks(self):
"""
Method to write new mined block to blockchain
"""
dataFile = file("chain.txt", "w")
dataFile = open("chain.txt", "w")
chainData = []
for eachBlock in self.chain:
chainData.append(eachBlock.__dict__)
Expand Down
42 changes: 24 additions & 18 deletions bscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
__url__ = "https://daxeel.github.io"
__email__ = "daxeelsoni44@gmail.com"
__license__ = "MIT"
__version__ = "0.1"
__maintainer__ = "Daxeel Soni"
__version__ = "0.11"
__maintainer__ = "Tymoteusz Ciesielski"

# ==================================================
# ================= IMPORT MODULES =================
# ==================================================
import click
import urllib
import json
from blockchain.chain import Block, Blockchain

Expand All @@ -30,6 +29,7 @@
# Init blockchain
coin = Blockchain()


# Create group of commands
@click.group()
def cli():
Expand All @@ -38,14 +38,15 @@ def cli():
"""
pass


# ==================================================
# ============= BLOCKSHELL CLI COMMAND =============
# ==================================================
@cli.command()
@click.option("--difficulty", default=3, help="Define difficulty level of blockchain.")
def init(difficulty):
"""Initialize local blockchain"""
print """
print("""
____ _ _ _____ _ _ _
| _ \ | | | | / ____| | | | | | |
| |_) | | | ___ ___ | | __ | (___ | |__ ___ | | | |
Expand All @@ -57,16 +58,17 @@ def init(difficulty):
> Type 'help' to see supported commands.
> Project by Daxeel Soni - https://daxeel.github.io

"""
""")

# Set difficulty of blockchain
coin.difficulty = difficulty

# Start blockshell shell
while True:
cmd = raw_input("[BlockShell] $ ")
cmd = input("[BlockShell] $ ")
processInput(cmd)


# Process input from Blockshell shell
def processInput(cmd):
"""
Expand All @@ -92,17 +94,19 @@ def dotx(cmd):
txData = cmd.split("dotx ")[-1]
if "{" in txData:
txData = json.loads(txData)
print "Doing transaction..."
print("Doing transaction...")
coin.addBlock(Block(data=txData))


def allblocks(cmd):
"""
Method to list all mined blocks.
"""
print ""
print("")
for eachBlock in coin.chain:
print eachBlock.hash
print ""
print(eachBlock.hash)
print("")


def getblock(cmd):
"""
Expand All @@ -111,21 +115,23 @@ def getblock(cmd):
blockHash = cmd.split(" ")[-1]
for eachBlock in coin.chain:
if eachBlock.hash == blockHash:
print ""
print eachBlock.__dict__
print ""
print("")
print(eachBlock.__dict__)
print("")


def help(cmd):
"""
Method to display supported commands in Blockshell
"""
print "Commands:"
print " dotx <transaction data> Create new transaction"
print " allblocks Fetch all mined blocks in blockchain"
print " getblock <block hash> Fetch information about particular block"
print("Commands:")
print(" dotx <transaction data> Create new transaction")
print(" allblocks Fetch all mined blocks in blockchain")
print(" getblock <block hash> Fetch information about particular block")


def throwError(msg):
"""
Method to throw an error from Blockshell.
"""
print "Error : " + msg
print("Error : " + msg)