created socket block
This commit is contained in:
parent
119d328c98
commit
cbbccda0db
@ -5,10 +5,21 @@ from helpers import SocketHelper as socketHelper
|
||||
from helpers import TaskHelper as taskHelper
|
||||
|
||||
import time
|
||||
import pickle
|
||||
|
||||
MIN_MINING_TIME = 0
|
||||
MAX_MINING_TIME = 20
|
||||
|
||||
def getLastBlock():
|
||||
try:
|
||||
blocks = utilityHelper.loadFile("../data/ledger.dat")
|
||||
except:
|
||||
blocks = []
|
||||
|
||||
if blocks == []:
|
||||
return []
|
||||
return blocks[-1]
|
||||
|
||||
def exploreBlocks(self):
|
||||
blocks = utilityHelper.loadFile("../data/ledger.dat")
|
||||
x = 0
|
||||
@ -112,13 +123,7 @@ def createBlock(self):
|
||||
if lastBlock != []:
|
||||
lastBlock = lastBlock[-1]
|
||||
|
||||
if lastBlock != None and time.time() - lastBlock.date < 180:
|
||||
print(f"{utilityHelper.bcolors.WARNING}You can only mine a block every 3 minutes. Time left: {int(180 - (time.time() - lastBlock.date))}{utilityHelper.bcolors.ENDC}")
|
||||
return False
|
||||
|
||||
if lastBlock != None and lastBlock.metadata['validated'] == False:
|
||||
print(f"{utilityHelper.warningMessage('You can only mine a block if the last block is validated')}")
|
||||
return False
|
||||
canAddBlock(lastBlock)
|
||||
|
||||
transactions = Tx()
|
||||
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
|
||||
@ -154,6 +159,7 @@ def createBlock(self):
|
||||
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(","))
|
||||
for i in user_input:
|
||||
@ -345,5 +351,83 @@ def validateMinedBlock(self):
|
||||
|
||||
return True
|
||||
|
||||
def socketBlock(block):
|
||||
# check if item is indeed a cblock
|
||||
if type(block) != TxBlock:
|
||||
return False
|
||||
|
||||
# check if ID exists in ledger
|
||||
last_block = getLastBlock()
|
||||
|
||||
if last_block == []:
|
||||
if block.id != 0:
|
||||
return False, True
|
||||
else:
|
||||
return addBlockToChain(block, last_block)
|
||||
|
||||
if block.id < last_block.id:
|
||||
return False, True
|
||||
|
||||
if last_block.id == block.id and last_block.metadata['validated'] == False:
|
||||
# update lastblock
|
||||
return
|
||||
|
||||
if block.id == last_block.id + 1:
|
||||
return addBlockToChain(block, last_block)
|
||||
|
||||
def canAddBlock(last_block):
|
||||
if last_block == [] or last_block == None:
|
||||
return True
|
||||
|
||||
if time.time() - last_block.date < 180:
|
||||
print(f"{utilityHelper.bcolors.WARNING}You can only mine a block every 3 minutes. Time left: {int(180 - (time.time() - last_block.date))}{utilityHelper.bcolors.ENDC}")
|
||||
return False
|
||||
|
||||
if last_block.metadata['validated'] == False:
|
||||
print(f"{utilityHelper.warningMessage('You can only mine a block if the last block is validated')}")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def transactionsInPool(transactions, block):
|
||||
# check if block.data exists in transactions
|
||||
i = 0
|
||||
for transaction in block.data:
|
||||
if transaction not in transactions:
|
||||
print(type(transactions[0]))
|
||||
print(type(transaction))
|
||||
print(transactions[0] == transaction)
|
||||
print(i)
|
||||
return False
|
||||
i += 1
|
||||
return True
|
||||
|
||||
def addBlockToChain(block, last_block):
|
||||
|
||||
if not canAddBlock(last_block):
|
||||
return False, False
|
||||
|
||||
# get transaction pool
|
||||
transactions = Tx()
|
||||
transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
|
||||
|
||||
if not transactionsInPool(transactions, block):
|
||||
print("deze man")
|
||||
return False, True
|
||||
|
||||
if len(block.data) < 5 or len(block.data) > 10:
|
||||
return False, False
|
||||
|
||||
utilityHelper.addFile("../data/ledger.dat", block)
|
||||
utilityHelper.resetFile("../data/transaction_pool.dat")
|
||||
for transaction in transactions:
|
||||
if transaction not in block.data:
|
||||
utilityHelper.addFile("../data/transaction_pool.dat", transaction)
|
||||
|
||||
return True, False
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user