finished basics of mining, added explore blocks

This commit is contained in:
Ryan Bakkes 2023-11-08 13:40:02 +01:00
parent c0155f94fe
commit 358de4ea6a

View File

@ -1,6 +1,11 @@
from classes.Transaction import Tx
from classes.TxBlock import TxBlock
from helpers import SignatureHelper as signatureHelper
from helpers import UtilityHelper as utilityHelper
import time
MIN_MINING_TIME = 0
MAX_MINING_TIME = 20
def transaction(self):
receiver = input("Enter the username of the receiver:")
@ -73,8 +78,110 @@ def createBlock(self):
if int(i) not in available_transactions:
print("Wrong input, try again")
return False
fees_list.append(int(i))
selected_transactions.append(int(i))
if len(selected_transactions) > 10 and len(selected_transactions) < 5:
print("You can only select up to 10 transactions")
return False
# add last block if its available
block = TxBlock(None)
# create block
for i in selected_transactions:
block.addTx(transactions[i])
start = time.time()
nonce = block.find_nonce()
elapsed = time.time() - start
if not block.good_nonce():
print("ERROR! Bad nonce")
return False
print("Success! Nonce is good!")
print(f'Accepted Nonce = {str(nonce)}')
print("elapsed time: " + str(elapsed) + " s.")
if elapsed < MIN_MINING_TIME:
print("Mining declined, too fast")
return False
elif elapsed > MAX_MINING_TIME:
print("Mining declined, too Slow")
return False
# add block to blockchain
block.time = elapsed
block.nonce = nonce
# TODO ADD DATE
block.date = None
utilityHelper.saveFile("../data/ledger.dat", block)
# TODO remove transactions from transaction pool
return True
def exploreBlocks(self):
blocks = utilityHelper.loadFile("../data/ledger.dat")
x = 0
for block in blocks:
print(f"---------------------------------------{x}-------------------------------------------")
print(f"Block created: {block.date}")
x += 1
print("---------------------------------------END-------------------------------------------")
print("Select a number to view the block, keep empty to return.")
user_input = input(">>: ")
if user_input == "":
return
try:
user_input = int(user_input)
except:
utilityHelper.clearScreen()
print("Wrong input, try again")
return
if user_input > len(blocks):
utilityHelper.clearScreen()
print("Wrong input, try again")
return
# print all info of the block
utilityHelper.clearScreen()
print(f"---------------------------------------{user_input}-------------------------------------------")
print(f"Block created: {blocks[user_input].date}")
print(f"Block hash: {blocks[user_input].blockHash}")
print(f"Block nonce: {blocks[user_input].nonce}")
print(f"Block mined time: {blocks[user_input].time}")
# print(f"Block transactions: {blocks[user_input].transactions}")
print(f"Block previous hash: {blocks[user_input].previousHash}")
print("---------------------------------------------------------------------------------------")
print("0 -- go back")
print("1 -- View transactions of current block")
print("2 -- View another block")
choice = input(">>: ")
match choice:
case "0":
utilityHelper.clearScreen()
return
case "1":
utilityHelper.clearScreen()
x = 0
for transaction in blocks[user_input].data:
print(f"---------------------------------------{x}-------------------------------------------")
print(f"Transaction input: {transaction.inputs[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 sender: \n{transaction.inputs[0][0]}")
print(f"Transaction recipient: \n{transaction.outputs[0][0]}")
x += 1
print("-----------------------------------------------------------------------------------")
case "2":
utilityHelper.clearScreen()
exploreBlocks(self)
case _:
utilityHelper.clearScreen()
print("Wrong input, try again")
return