changes for possible delivery

This commit is contained in:
Spekulaas
2024-01-21 22:50:19 +01:00
parent a9a5ca877a
commit f49f55cb04
3 changed files with 37 additions and 49 deletions

View File

@@ -382,11 +382,11 @@ def socketBlock(block):
if block_status:
if block.id == last_block.id and block.blockHash == last_block.blockHash and last_block.metadata['validated'] == False:
block.previousBlock = last_block.previousBlock
# block.previousBlock = last_block.previousBlock
return updateBlockValidation(block)
if block.id == last_block.id + 1 and block.previousHash == last_block.blockHash:
block.previousBlock = last_block
# block.previousBlock = last_block
return addBlockToChain(block, last_block)
return False, False, block
@@ -443,21 +443,24 @@ def addBlockToChain(block, last_block):
def updateBlockValidation(block):
blocks = utilityHelper.loadFile("../data/ledger.dat")
last_block = getLastBlock()
if blocks == []:
return False, False, block
try:
del blocks[-1]
except:
return False, False, block
last_block = getLastBlock()
utilityHelper.resetFile("../data/ledger.dat")
block.previousBlock = last_block
if blocks == []:
last_block = None
# block.previousBlock = last_block
blocks.append(block)
if block.metadata['false_validations'] >= 3:
if not block.metadata['validated']:
# Create log for miner
# setsaveFil transactions in block back to the pool
for transaction in block.data:
@@ -480,21 +483,29 @@ def removeInvalid(block):
if blocks == []:
return False
# remove last transaction from pool
transactions = Tx()
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
if transactions != []:
if transactions[-1].type == 1 and transactions[-1].outputs[0][0] == block.metadata['miner']:
try:
del transactions[-1]
except:
return False
for transaction in block.data:
utilityHelper.addFile("../data/transaction_pool.dat", transaction)
try:
del blocks[-1]
except:
print(f"{utilityHelper.errorMessage('Something went wrong')}")
return False
if last_block.blockHash == block.blockHash:
# try to delete
try:
del blocks[-1]
except:
return False
# remove last transaction from pool
transactions = Tx()
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
if transactions[-1].type == 1 and transactions[-1].outputs[0][0] == block.metadata['miner']:
try:
del transactions[-1]
except:
return False
utilityHelper.resetFile("../data/ledger.dat")

View File

@@ -50,7 +50,7 @@ def sendObj(ip_addr, blk, port):
return True
except:
print("Could not connect to peer")
# utilityHelper.close()
utilityHelper.close()
return False
def connection(ip, port, db):
@@ -85,6 +85,10 @@ def connection(ip, port, db):
valid, exit, block = blockHelper.socketBlock(item)
if valid:
sendObj(os.getenv("PEER_IP"), ["VALIDATE BLOCK",block], int(os.getenv("PEER_PORT")))
continue
if not valid:
print(f"{utilityHelper.errorMessage('Received block is not valid')}")
sendObj(os.getenv("PEER_IP"), ["INVALID BLOCK",block], int(os.getenv("PEER_PORT")))
@@ -144,6 +148,9 @@ def connection(ip, port, db):
print(">>: ")
continue
case "VALIDATE BLOCK":
blockHelper.updateBlockValidation(item[1])
if type(item) == str:
match item:
case "EXIT":