added login startup checks (done, validate mine)
This commit is contained in:
@@ -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
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
||||
return objs
|
||||
|
||||
def loginStartup(self):
|
||||
# TODO - check if there are unvalidated blocks
|
||||
blockHelper.validateMinedBlock(self)
|
||||
# TODO - check if there are any notifcations
|
||||
|
||||
|
Reference in New Issue
Block a user