add modify block command

This commit is contained in:
Aurelien Rebourg 2023-04-27 23:25:47 +02:00
parent baa981e4cc
commit a66fb691e4
Signed by: Aurelien
GPG Key ID: F02826677ABB98C1
4 changed files with 62 additions and 4 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ build/
dist/ dist/
blockshell/ blockshell/
blockshell.egg-info/ blockshell.egg-info/
*.csv

18
add_blocks.sh Executable file
View File

@ -0,0 +1,18 @@
#!/bin/sh
CMD=""
PREVIFS=$IFS
IFS=','
[ $# -ne 1 ] && { echo "Usage: add_blocks <file.csv>"; 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

View File

@ -77,6 +77,7 @@ class Block:
""" """
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() startTime = time.time()
self.hash = ""
while self.hash[:difficulty] != "0"*difficulty: while self.hash[:difficulty] != "0"*difficulty:
self.nonce += 1 self.nonce += 1
@ -152,3 +153,29 @@ class Blockchain:
print(Back.RED + "[Error] Creating new blockchain.") print(Back.RED + "[Error] Creating new blockchain.")
print(Style.RESET_ALL) print(Style.RESET_ALL)
self.writeBlocks() 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

View File

@ -25,7 +25,8 @@ SUPPORTED_COMMANDS = [
'dotx', 'dotx',
'allblocks', 'allblocks',
'getblock', 'getblock',
'help' 'help',
'modify',
] ]
def printLogo(): def printLogo():
@ -91,6 +92,7 @@ def load(filename, difficulty):
# Set difficulty of blockchain # Set difficulty of blockchain
coin.difficulty = difficulty coin.difficulty = difficulty
printSuccess(f"Blockchain loaded successfully. {len(coin.chain)} blocks loaded.")
# Start blockshell shell # Start blockshell shell
startShell() startShell()
@ -148,6 +150,16 @@ def help(cmd):
print(" dotx <uid> <mail> <firstname> <lastname> Create new transaction") print(" dotx <uid> <mail> <firstname> <lastname> Create new transaction")
print(" allblocks Fetch all mined blocks in blockchain") print(" allblocks Fetch all mined blocks in blockchain")
print(" getblock <block hash> Fetch information about particular block") print(" getblock <block hash> Fetch information about particular block")
print(" modify <block hash> <uid> <mail> <firstname> <lastname> 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): def throwError(msg):
""" """