From a66fb691e42de787eed927bc91ddf4e0beb36756 Mon Sep 17 00:00:00 2001 From: Aurelien Rebourg Date: Thu, 27 Apr 2023 23:25:47 +0200 Subject: [PATCH] add modify block command --- .gitignore | 1 + add_blocks.sh | 18 ++++++++++++++++++ blockchain/chain.py | 27 +++++++++++++++++++++++++++ bscli.py | 20 ++++++++++++++++---- 4 files changed, 62 insertions(+), 4 deletions(-) create mode 100755 add_blocks.sh diff --git a/.gitignore b/.gitignore index eac7f72..0f0671a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ build/ dist/ blockshell/ blockshell.egg-info/ +*.csv \ No newline at end of file diff --git a/add_blocks.sh b/add_blocks.sh new file mode 100755 index 0000000..3e8edf0 --- /dev/null +++ b/add_blocks.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +CMD="" +PREVIFS=$IFS +IFS=',' + +[ $# -ne 1 ] && { echo "Usage: add_blocks "; exit 1; } +[ ! -f $1 ] && { echo "file $1 not found"; exit 1; } + +while read uid mail firstname lastname end +do + CMD="${CMD}dotx $uid $mail $firstname $lastname\n" +done < $1 + +# Add blocks to existing blockchain (if it exists) or to a new blockchain +printf $CMD | blockshell load chain.txt + +IFS=$PREVIFS diff --git a/blockchain/chain.py b/blockchain/chain.py index 1e8cf3f..82a3b9b 100644 --- a/blockchain/chain.py +++ b/blockchain/chain.py @@ -77,6 +77,7 @@ class Block: """ print(Back.RED + "\n[Status] Mining block (" + str(self.index) + ") with PoW ...") startTime = time.time() + self.hash = "" while self.hash[:difficulty] != "0"*difficulty: self.nonce += 1 @@ -152,3 +153,29 @@ class Blockchain: print(Back.RED + "[Error] Creating new blockchain.") print(Style.RESET_ALL) self.writeBlocks() + + def modifyBlock(self, hash, uid_epita, email_epita, firstname, lastname): + """ + Method to modify existing block + """ + i = 0 + for i in range(len(self.chain)): + eachBlock = self.chain[i] + if eachBlock.hash == hash: + eachBlock.uid_epita = uid_epita + eachBlock.email_epita = email_epita + eachBlock.firstname = firstname + eachBlock.lastname = lastname + eachBlock.createRandomImage() + eachBlock.mineBlock(self.difficulty) + break + if i == len(self.chain) - 1: + return False + for i in range(i+1, len(self.chain)): + eachBlock = self.chain[i] + eachBlock.index = i + eachBlock.previousHash = self.chain[i-1].hash + eachBlock.mineBlock(self.difficulty) + self.writeBlocks() + return True + diff --git a/bscli.py b/bscli.py index a9a4279..540aa8e 100644 --- a/bscli.py +++ b/bscli.py @@ -25,7 +25,8 @@ SUPPORTED_COMMANDS = [ 'dotx', 'allblocks', 'getblock', - 'help' + 'help', + 'modify', ] def printLogo(): @@ -91,6 +92,7 @@ def load(filename, difficulty): # Set difficulty of blockchain coin.difficulty = difficulty + printSuccess(f"Blockchain loaded successfully. {len(coin.chain)} blocks loaded.") # Start blockshell shell startShell() @@ -145,9 +147,19 @@ 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(" dotx Create new transaction") + print(" allblocks Fetch all mined blocks in blockchain") + print(" getblock Fetch information about particular block") + print(" modify Modify a particular block keeping blockchain validity") + +def modify(args): + """ + Method to modify any block in blockchain. + """ + if coin.modifyBlock(args[0], args[1], args[2], args[3], args[4]): + printSuccess("Block modified successfully!") + else: + throwError("Block modification failed!") def throwError(msg): """