delete invalid transaction

This commit is contained in:
Ryan Bakkes 2023-11-12 09:52:49 +01:00
parent d3973ca202
commit 5cbf06aaaf

View File

@ -65,7 +65,7 @@ def exploreBlocks(self):
utilityHelper.clearScreen()
return
case "1":
print("Validating block... TODO")
blocks[user_input].is_valid()
case "2":
utilityHelper.clearScreen()
x = 0
@ -185,10 +185,16 @@ def createBlock(self):
# create block
for i in selected_transactions:
# if not transaction[i].is_valid():
# # Return transaction as 'invalid' transaction
# # stop mining with the error "Invalid transaction found. transaction removed from pool. Please restart mining a block..."
# pass
if not transactions[i].is_valid():
self.db.createLog(transactions[i].inputs[0][0], time.time(), f"Transaction of {transactions[i].outputs[0][1]} with {transactions[i].inputs[0][1] - transactions[i].outputs[0][1]} coins fee is declined")
try:
del transactions[i]
except:
return False
for transaction in transactions:
utilityHelper.addFile("../data/transaction_pool.dat", transaction)
print(f"{utilityHelper.errorMessage('ERROR! Invalid transaction found. transaction removed from pool. Please restart mining a block...')}")
return False
block.addTx(transactions[i])
@ -229,12 +235,12 @@ def createBlock(self):
block.date = time.time()
block.blockHash = block.computeHash()
utilityHelper.saveFile("../data/ledger.dat", block)
utilityHelper.addFile("../data/ledger.dat", block)
utilityHelper.resetFile("../data/transaction_pool.dat")
transaction_count = 0
for transaction in transactions:
if transaction_count not in selected_transactions:
utilityHelper.saveFile("../data/transaction_pool.dat", transaction)
utilityHelper.addFile("../data/transaction_pool.dat", transaction)
transaction_count += 1
return True
@ -275,7 +281,7 @@ def validateMinedBlock(self):
new_reward = Tx()
new_reward.createRewardTransaction(self.user.public_ser, self.user.private_ser, "MINE", fees)
self.db.createLog(new_block.metadata['miner'], time.time(), f"Block id {new_block.id}, is validated!")
utilityHelper.saveFile("../data/transaction_pool.dat", new_reward)
utilityHelper.addFile("../data/transaction_pool.dat", new_reward)
try:
@ -286,7 +292,7 @@ def validateMinedBlock(self):
utilityHelper.resetFile("../data/ledger.dat")
blocks.append(new_block)
for block in blocks:
utilityHelper.saveFile("../data/ledger.dat", block)
utilityHelper.addFile("../data/ledger.dat", block)
return True
@ -295,9 +301,9 @@ def validateMinedBlock(self):
if new_block.metadata['false_validations'] >= 3:
# Create log for miner
self.db.createLog(new_block.metadata['miner'], time.time(), f"Block id {new_block.id}, was flagged invalid. Mine unsuccesful...")
# set transactions in block back to the pool
# setsaveFil transactions in block back to the pool
for transaction in new_block.data:
utilityHelper.saveFile("../data/transaction_pool.dat", transaction)
utilityHelper.addFile("../data/transaction_pool.dat", transaction)
try:
del blocks[-1]
except:
@ -306,7 +312,7 @@ def validateMinedBlock(self):
utilityHelper.resetFile("../data/ledger.dat")
for block in blocks:
utilityHelper.saveFile("../data/ledger.dat", block)
utilityHelper.addFile("../data/ledger.dat", block)
return True