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

@ -7,36 +7,6 @@ This is the final assesment of the blockchain minor of period 1. [EXPLANATION FI
pip install -r requirements.txt pip install -r requirements.txt
``` ```
# SUMMARY # SETUP
in dit project moet er een p2p connectie gestart worden door 2 sockets. Deze sockets moeten beide aanstaan om het project functioneel te hebben Create 2 instances of this project (copy the folder to any place, other pc, or vm)
deze 2 sockets sturen naar elkaar als er een nieuwe gebruiker aangemaakt wordt, transacties, nieuwe blocks. make sure to change the .env file with the right IP and ports for both instances
Deze sockets versturen geen gehele files. Dit moet per node gedaan worden. Dus alleen transactie data moet genoeg zijn bijvoorbeeld
De connectie wordt gemaakt bij het moment van starten,
User start Applicatie -> User komt in connectie scherm -> user 2 start applicatie -> beide apps starten.
Wanneer een connectie verbroken wordt tussen de 2 komt er een mooie melding waarop tezien wordt dat de chain gesloten wordt
Werking:
Node start python programma,
Server start
Client start
Client zoekt naar een server waarmee hij kan connecten
client kan niks vinden en blijft proberen
Node 2 start python
server van node 2 start
Node 1 connect met server van node 2
Client van node 2 start
client van node 2 connect met server van node 1
wanneer beide welkoms meldingen gestuurd zijn start de applicatie
# TODO
- voeg een validator aan de inkomende transactie
- voeg de block toe / kijk hem na bij inkomend
- verwijder transacties van inkomende block

View File

@ -382,11 +382,11 @@ def socketBlock(block):
if block_status: if block_status:
if block.id == last_block.id and block.blockHash == last_block.blockHash and last_block.metadata['validated'] == False: 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) return updateBlockValidation(block)
if block.id == last_block.id + 1 and block.previousHash == last_block.blockHash: 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 addBlockToChain(block, last_block)
return False, False, block return False, False, block
@ -443,21 +443,24 @@ def addBlockToChain(block, last_block):
def updateBlockValidation(block): def updateBlockValidation(block):
blocks = utilityHelper.loadFile("../data/ledger.dat") blocks = utilityHelper.loadFile("../data/ledger.dat")
last_block = getLastBlock()
if blocks == []: if blocks == []:
return False, False, block return False, False, block
try: try:
del blocks[-1] del blocks[-1]
except: except:
return False, False, block return False, False, block
last_block = getLastBlock()
utilityHelper.resetFile("../data/ledger.dat") utilityHelper.resetFile("../data/ledger.dat")
block.previousBlock = last_block if blocks == []:
last_block = None
# block.previousBlock = last_block
blocks.append(block) blocks.append(block)
if block.metadata['false_validations'] >= 3: if not block.metadata['validated']:
# Create log for miner # Create log for miner
# setsaveFil transactions in block back to the pool # setsaveFil transactions in block back to the pool
for transaction in block.data: for transaction in block.data:
@ -480,21 +483,29 @@ def removeInvalid(block):
if blocks == []: if blocks == []:
return False 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: if last_block.blockHash == block.blockHash:
# try to delete # try to delete
try: try:
del blocks[-1] del blocks[-1]
except: except:
return False 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") utilityHelper.resetFile("../data/ledger.dat")

View File

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