bug fix blocks and added hash

This commit is contained in:
Ryan Bakkes 2023-11-09 12:05:47 +01:00
parent cca995df99
commit 0219a8a7e9

View File

@ -63,7 +63,10 @@ def createBlock(self):
x = 0
fees = {}
for transaction in transactions:
fees[x] = transaction.inputs[0][1] - transaction.outputs[0][1]
if transaction.type == 1:
fees[x] = 0
else:
fees[x] = transaction.inputs[0][1] - transaction.outputs[0][1]
x+=1
fees_list = list(fees.keys())
@ -81,18 +84,48 @@ def createBlock(self):
if user_input == "":
return False
# seperate user input by comma, check if all the values are in available_transactions
user_input = user_input.split(",")
user_input = set(user_input.split(","))
for i in user_input:
if int(i) not in available_transactions:
try:
if int(i) not in available_transactions:
print("Wrong input, try again")
return False
selected_transactions.append(int(i))
except:
utilityHelper.clearScreen()
print("Wrong input, try again")
return False
selected_transactions.append(int(i))
if len(selected_transactions) > 10 and len(selected_transactions) < 5:
if len(selected_transactions) > 10:
print("You can only select up to 10 transactions")
return False
if len(selected_transactions) < 5:
print("You need atleast 5 transactions to mine a block")
return False
only_personal = True
# check if the user selected more user id's in the selected list
for i in selected_transactions[2:]:
if transactions[i].type == 1 and transactions[i].outputs[0][0] != self.user.public_ser:
only_personal = False
elif transactions[i].type == 0 and transactions[i].inputs[0][0] != self.user.public_ser:
only_personal = False
if only_personal:
print("You need atleast 1 transaction from another user")
return False
# add last block if its available
block = TxBlock(None)
try:
lastBlock = utilityHelper.loadFileLine("../data/ledger.dat")
except:
lastBlock = None
if lastBlock != None:
lastBlock = lastBlock[0]
block = TxBlock(lastBlock)
# create block
for i in selected_transactions:
@ -103,6 +136,7 @@ def createBlock(self):
nonce = block.find_nonce()
elapsed = time.time() - start
utilityHelper.clearScreen()
if not block.good_nonce():
print("ERROR! Bad nonce")
return False
@ -122,6 +156,7 @@ def createBlock(self):
block.time = elapsed
block.nonce = nonce
block.date = time.time()
block.blockHash = block.computeHash()
utilityHelper.saveFile("../data/ledger.dat", block)
# TODO remove transactions from transaction pool
@ -136,6 +171,7 @@ def exploreBlocks(self):
x += 1
total_transactions += len(block.data)
print(f"---------------------------------------{x}-------------------------------------------")
print(block)
print(f"Block created: {time.strftime('%Y-%m-%d', time.localtime(block.date))}")
print("---------------------------------------END-------------------------------------------")
print(f"Total blocks: {x}")
@ -144,6 +180,7 @@ def exploreBlocks(self):
print("Select a number to view the block, keep empty to return.")
user_input = input(">>: ")
if user_input == "":
utilityHelper.clearScreen()
return
try:
@ -157,7 +194,7 @@ def exploreBlocks(self):
utilityHelper.clearScreen()
print("Wrong input, try again")
return
# print all info of the block
utilityHelper.clearScreen()
print(f"---------------------------------------{user_input +1}-------------------------------------------")
@ -186,9 +223,9 @@ def exploreBlocks(self):
for transaction in blocks[user_input].data:
x += 1
print(f"---------------------------------------{x}-------------------------------------------")
print(f"Transaction input: {transaction.inputs[0][1]}")
print(f"Transaction input: {transaction.type == 1 and 'reward' or transaction.inputs[0][1]}")
print(f"Transaction output: {transaction.outputs[0][1]}")
print(f"Transaction fees: {transaction.inputs[0][1] - transaction.outputs[0][1]}")
print(f"Transaction fees: {transaction.type == 1 and 0 or transaction.inputs[0][1] - transaction.outputs[0][1]}")
print(f"Transaction sender: \n{transaction.inputs[0][0]}")
print(f"Transaction recipient: \n{transaction.outputs[0][0]}")
print("-----------------------------------------------------------------------------------")