From f4bc93a4cc2b53865fc7cf2f6a6ebd9982753e25 Mon Sep 17 00:00:00 2001 From: E7F3 Date: Sat, 9 Apr 2022 14:55:04 +0200 Subject: [PATCH 1/2] deps(python): Python 2 -> 3 by hand --- blockchain/chain.py | 12 ++++++------ bscli.py | 30 +++++++++++++++--------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/blockchain/chain.py b/blockchain/chain.py index 894696e..c9f7365 100644 --- a/blockchain/chain.py +++ b/blockchain/chain.py @@ -39,13 +39,13 @@ 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("utf8")).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: @@ -53,9 +53,9 @@ def mineBlock(self, difficulty): 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 ================ @@ -88,7 +88,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__) diff --git a/bscli.py b/bscli.py index ac512a5..485a5a1 100644 --- a/bscli.py +++ b/bscli.py @@ -45,7 +45,7 @@ def cli(): @click.option("--difficulty", default=3, help="Define difficulty level of blockchain.") def init(difficulty): """Initialize local blockchain""" - print """ + print(""" ____ _ _ _____ _ _ _ | _ \ | | | | / ____| | | | | | | | |_) | | | ___ ___ | | __ | (___ | |__ ___ | | | | @@ -57,14 +57,14 @@ 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 @@ -92,17 +92,17 @@ 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): """ @@ -111,21 +111,21 @@ 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 Create new transaction" - print " allblocks Fetch all mined blocks in blockchain" - print " getblock Fetch information about particular block" + print("Commands:") + print(" dotx Create new transaction") + print(" allblocks Fetch all mined blocks in blockchain") + print(" getblock Fetch information about particular block") def throwError(msg): """ Method to throw an error from Blockshell. """ - print "Error : " + msg + print("Error : " + msg) From 49632518f51245407e74e32a712b5db3394abc2a Mon Sep 17 00:00:00 2001 From: Et7f3 Date: Tue, 19 Apr 2022 16:05:18 +0200 Subject: [PATCH 2/2] fix(hash): Don't use specific encoding --- blockchain/chain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain/chain.py b/blockchain/chain.py index c9f7365..60fcf8a 100644 --- a/blockchain/chain.py +++ b/blockchain/chain.py @@ -39,7 +39,7 @@ 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.encode("utf8")).hexdigest() + return hashlib.sha256(bytes(ord(x) for x in hashData)).hexdigest() def mineBlock(self, difficulty): """