From 3b0ca900ea03565dc370bb123945b0c9279381d3 Mon Sep 17 00:00:00 2001 From: spekulaas Date: Sat, 11 Nov 2023 11:26:28 +0100 Subject: [PATCH] added login startup checks (done, validate mine) --- goodchain/src/helpers/BlockHelper.py | 79 +++++++++++++++++++++++++- goodchain/src/helpers/MenuHelper.py | 1 + goodchain/src/helpers/UtilityHelper.py | 11 +++- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/goodchain/src/helpers/BlockHelper.py b/goodchain/src/helpers/BlockHelper.py index 1dee031..6874e88 100644 --- a/goodchain/src/helpers/BlockHelper.py +++ b/goodchain/src/helpers/BlockHelper.py @@ -43,8 +43,14 @@ def exploreBlocks(self): utilityHelper.clearScreen() print(f"---------------------------------------{user_input +1}-------------------------------------------") print(f"Block created: {time.strftime('%Y-%m-%d', time.localtime(blocks[user_input].date))}") + if blocks[user_input].metadata['validated']: + print(f"Block validated: True") + else: + print(f"Block validated: False, {blocks[user_input].metadata['true_validations']} valid flags, {blocks[user_input].metadata['false_validations']} invalid flags.") + print(f"Block id: {blocks[user_input].id}") print(f"Block hash: {blocks[user_input].blockHash}") print(f"Block nonce: {blocks[user_input].nonce}") + print(f"Block miner: {blocks[user_input].metadata['miner']}") print(f"Block mined time: {blocks[user_input].time}") print(f"Block transactions: {len(blocks[user_input].data)}") print(f"Block previous hash: {blocks[user_input].previousHash}") @@ -100,7 +106,11 @@ def createBlock(self): lastBlock = lastBlock[-1] if lastBlock != None and time.time() - lastBlock.date < 180: - print("You can only mine a block every 3 minutes") + print(f"You can only mine a block every 3 minutes. Time left: {int(180 - (time.time() - lastBlock.date))}") + return False + + if lastBlock != None and lastBlock.metadata['validated'] == False: + print("You can only mine a block if the last block is validated") return False transactions = Tx() @@ -135,6 +145,7 @@ def createBlock(self): print("Enter the numbers of the transactions you want to mine, seperated by a comma. (ex: 1,2,3)") user_input = input(">>: ") if user_input == "": + utilityHelper.clearScreen() return False # seperate user input by comma, check if all the values are in available_transactions user_input = set(user_input.split(",")) @@ -199,13 +210,14 @@ def createBlock(self): return False # add block to blockchain - if lastBlock == []: + if lastBlock == None: block.id = 0 else: block.id = lastBlock.id + 1 block.metadata['miner'] = self.user.public_ser block.metadata['validated'] = False + block.metadata['validated_by'] = [] block.metadata['true_validations'] = 0 block.metadata['false_validations'] = 0 @@ -222,4 +234,65 @@ def createBlock(self): utilityHelper.saveFile("../data/transaction_pool.dat", transaction) transaction_count += 1 - return True \ No newline at end of file + return True + +def validateMinedBlock(self): + blocks = utilityHelper.loadFile("../data/ledger.dat") + + if blocks == []: + return False + + new_block = blocks[-1] + + if new_block.metadata['validated']: + return False + + if new_block.metadata['miner'] == self.user.public_ser: + return False + + if self.user.public_ser in new_block.metadata['validated_by']: + return False + + block_status = new_block.is_valid() + new_block.metadata['validated_by'].append(self.user.public_ser) + if block_status: + new_block.metadata['true_validations'] += 1 + + + if new_block.metadata['true_validations'] >= 3: + new_block.metadata['validated'] = True + new_reward = Tx() + new_reward.createRewardTransaction(self.user.public_ser, self.user.private_ser, "MINE") + + try: + del blocks[-1] + except: + return False + + utilityHelper.resetFile("../data/ledger.dat") + blocks.append(new_block) + for block in blocks: + utilityHelper.saveFile("../data/ledger.dat", block) + + return True + + else: + new_block.metadata['false_validations'] += 1 + if new_block.metadata['false_validations'] >= 3: + # set transactions in block back to the pool + for transaction in new_block.data: + utilityHelper.saveFile("../data/transaction_pool.dat", transaction) + try: + del blocks[-1] + except: + print("error biatch") + return False + + utilityHelper.resetFile("../data/ledger.dat") + for block in blocks: + utilityHelper.saveFile("../data/ledger.dat", block) + + return True + + + diff --git a/goodchain/src/helpers/MenuHelper.py b/goodchain/src/helpers/MenuHelper.py index c2ed949..8f11a4e 100644 --- a/goodchain/src/helpers/MenuHelper.py +++ b/goodchain/src/helpers/MenuHelper.py @@ -103,6 +103,7 @@ class MenuHelper: print("Wrong input, try again") def runUserMainMenu(self): + utilityHelper.loginStartup(self) while(self.user): choice = self.getMenuInput(self.user_main_menu) diff --git a/goodchain/src/helpers/UtilityHelper.py b/goodchain/src/helpers/UtilityHelper.py index 1954ddb..76c3370 100644 --- a/goodchain/src/helpers/UtilityHelper.py +++ b/goodchain/src/helpers/UtilityHelper.py @@ -1,6 +1,8 @@ import pickle import hashlib import os +from helpers import BlockHelper as blockHelper + def computeHash(data): hash = hashlib.sha256() @@ -38,4 +40,11 @@ def loadFile(fileloc): except EOFError: break - return objs \ No newline at end of file + return objs + +def loginStartup(self): + # TODO - check if there are unvalidated blocks + blockHelper.validateMinedBlock(self) + # TODO - check if there are any notifcations + + \ No newline at end of file