bug fix blocks and added hash
This commit is contained in:
parent
cca995df99
commit
0219a8a7e9
@ -63,6 +63,9 @@ def createBlock(self):
|
|||||||
x = 0
|
x = 0
|
||||||
fees = {}
|
fees = {}
|
||||||
for transaction in transactions:
|
for transaction in transactions:
|
||||||
|
if transaction.type == 1:
|
||||||
|
fees[x] = 0
|
||||||
|
else:
|
||||||
fees[x] = transaction.inputs[0][1] - transaction.outputs[0][1]
|
fees[x] = transaction.inputs[0][1] - transaction.outputs[0][1]
|
||||||
x+=1
|
x+=1
|
||||||
|
|
||||||
@ -81,18 +84,48 @@ def createBlock(self):
|
|||||||
if user_input == "":
|
if user_input == "":
|
||||||
return False
|
return False
|
||||||
# seperate user input by comma, check if all the values are in available_transactions
|
# seperate user input by comma, check if all the values are in available_transactions
|
||||||
user_input = user_input.split(",")
|
user_input = set(user_input.split(","))
|
||||||
for i in user_input:
|
for i in user_input:
|
||||||
|
try:
|
||||||
if int(i) not in available_transactions:
|
if int(i) not in available_transactions:
|
||||||
print("Wrong input, try again")
|
print("Wrong input, try again")
|
||||||
return False
|
return False
|
||||||
selected_transactions.append(int(i))
|
selected_transactions.append(int(i))
|
||||||
|
except:
|
||||||
|
utilityHelper.clearScreen()
|
||||||
|
print("Wrong input, try again")
|
||||||
|
return False
|
||||||
|
|
||||||
if len(selected_transactions) > 10 and len(selected_transactions) < 5:
|
if len(selected_transactions) > 10:
|
||||||
print("You can only select up to 10 transactions")
|
print("You can only select up to 10 transactions")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if len(selected_transactions) < 5:
|
||||||
|
print("You need atleast 5 transactions to mine a block")
|
||||||
|
return False
|
||||||
|
|
||||||
|
only_personal = True
|
||||||
|
# check if the user selected more user id's in the selected list
|
||||||
|
for i in selected_transactions[2:]:
|
||||||
|
if transactions[i].type == 1 and transactions[i].outputs[0][0] != self.user.public_ser:
|
||||||
|
only_personal = False
|
||||||
|
|
||||||
|
elif transactions[i].type == 0 and transactions[i].inputs[0][0] != self.user.public_ser:
|
||||||
|
only_personal = False
|
||||||
|
|
||||||
|
if only_personal:
|
||||||
|
print("You need atleast 1 transaction from another user")
|
||||||
|
return False
|
||||||
|
|
||||||
# add last block if its available
|
# add last block if its available
|
||||||
block = TxBlock(None)
|
try:
|
||||||
|
lastBlock = utilityHelper.loadFileLine("../data/ledger.dat")
|
||||||
|
except:
|
||||||
|
lastBlock = None
|
||||||
|
if lastBlock != None:
|
||||||
|
lastBlock = lastBlock[0]
|
||||||
|
|
||||||
|
block = TxBlock(lastBlock)
|
||||||
|
|
||||||
# create block
|
# create block
|
||||||
for i in selected_transactions:
|
for i in selected_transactions:
|
||||||
@ -103,6 +136,7 @@ def createBlock(self):
|
|||||||
nonce = block.find_nonce()
|
nonce = block.find_nonce()
|
||||||
elapsed = time.time() - start
|
elapsed = time.time() - start
|
||||||
|
|
||||||
|
utilityHelper.clearScreen()
|
||||||
if not block.good_nonce():
|
if not block.good_nonce():
|
||||||
print("ERROR! Bad nonce")
|
print("ERROR! Bad nonce")
|
||||||
return False
|
return False
|
||||||
@ -122,6 +156,7 @@ def createBlock(self):
|
|||||||
block.time = elapsed
|
block.time = elapsed
|
||||||
block.nonce = nonce
|
block.nonce = nonce
|
||||||
block.date = time.time()
|
block.date = time.time()
|
||||||
|
block.blockHash = block.computeHash()
|
||||||
utilityHelper.saveFile("../data/ledger.dat", block)
|
utilityHelper.saveFile("../data/ledger.dat", block)
|
||||||
# TODO remove transactions from transaction pool
|
# TODO remove transactions from transaction pool
|
||||||
|
|
||||||
@ -136,6 +171,7 @@ def exploreBlocks(self):
|
|||||||
x += 1
|
x += 1
|
||||||
total_transactions += len(block.data)
|
total_transactions += len(block.data)
|
||||||
print(f"---------------------------------------{x}-------------------------------------------")
|
print(f"---------------------------------------{x}-------------------------------------------")
|
||||||
|
print(block)
|
||||||
print(f"Block created: {time.strftime('%Y-%m-%d', time.localtime(block.date))}")
|
print(f"Block created: {time.strftime('%Y-%m-%d', time.localtime(block.date))}")
|
||||||
print("---------------------------------------END-------------------------------------------")
|
print("---------------------------------------END-------------------------------------------")
|
||||||
print(f"Total blocks: {x}")
|
print(f"Total blocks: {x}")
|
||||||
@ -144,6 +180,7 @@ def exploreBlocks(self):
|
|||||||
print("Select a number to view the block, keep empty to return.")
|
print("Select a number to view the block, keep empty to return.")
|
||||||
user_input = input(">>: ")
|
user_input = input(">>: ")
|
||||||
if user_input == "":
|
if user_input == "":
|
||||||
|
utilityHelper.clearScreen()
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -186,9 +223,9 @@ def exploreBlocks(self):
|
|||||||
for transaction in blocks[user_input].data:
|
for transaction in blocks[user_input].data:
|
||||||
x += 1
|
x += 1
|
||||||
print(f"---------------------------------------{x}-------------------------------------------")
|
print(f"---------------------------------------{x}-------------------------------------------")
|
||||||
print(f"Transaction input: {transaction.inputs[0][1]}")
|
print(f"Transaction input: {transaction.type == 1 and 'reward' or transaction.inputs[0][1]}")
|
||||||
print(f"Transaction output: {transaction.outputs[0][1]}")
|
print(f"Transaction output: {transaction.outputs[0][1]}")
|
||||||
print(f"Transaction fees: {transaction.inputs[0][1] - transaction.outputs[0][1]}")
|
print(f"Transaction fees: {transaction.type == 1 and 0 or transaction.inputs[0][1] - transaction.outputs[0][1]}")
|
||||||
print(f"Transaction sender: \n{transaction.inputs[0][0]}")
|
print(f"Transaction sender: \n{transaction.inputs[0][0]}")
|
||||||
print(f"Transaction recipient: \n{transaction.outputs[0][0]}")
|
print(f"Transaction recipient: \n{transaction.outputs[0][0]}")
|
||||||
print("-----------------------------------------------------------------------------------")
|
print("-----------------------------------------------------------------------------------")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user