Implement the mining software

This commit is contained in:
2024-03-24 13:09:29 +01:00
parent 2f3216a0c4
commit 825b07bc11
7 changed files with 608 additions and 7 deletions

13
node.py
View File

@@ -362,8 +362,8 @@ def receiver(node, b):
receive_observer.publish(identifier, event_obj)
elif msg_type == 7:
# mining task request
if msg_len != 225:
log(f"Got a mining task request of wrong length ({msg_len} bytes from {sender}, but expected 255 bytes)")
if msg_len != 257:
log(f"Got a mining task request of wrong length ({msg_len} bytes from {sender}, but expected 257 bytes)")
continue
transaction = b.open_transactions.get_transaction(0)
if transaction is not None:
@@ -374,8 +374,8 @@ def receiver(node, b):
timestamp_raw = t.to_bytes(8, "big")
latest_block = b.get_latest_block()
if latest_block is not None:
B_1_difficulty_sum, _ = latest_block.get_difficulty_info(0, blockchain)
B_10_difficulty_sum, B_10_timestamp = latest_block.get_difficulty_info(9, blockchain)
B_1_difficulty_sum, _ = latest_block.get_difficulty_info(0, b)
B_10_difficulty_sum, B_10_timestamp = latest_block.get_difficulty_info(9, b)
D = B_1_difficulty_sum - B_10_difficulty_sum
T = t - B_10_timestamp
calculated_difficulty = D * 3000 // 9 // T
@@ -383,13 +383,16 @@ def receiver(node, b):
difficulty_sum = B_1_difficulty_sum + block_difficulty
previous_hash = latest_block.own_hash
else:
block_difficulty = 2**28
difficulty_sum = 2**29
previous_hash = 32 * b"\0"
threshold = (2**256 - 1) // block_difficulty
response = b"\0\0\0\0\x08" + \
transaction_raw + \
previous_hash + \
timestamp_raw + \
difficulty_sum.to_bytes(32, "big")
difficulty_sum.to_bytes(32, "big") + \
threshold.to_bytes(32, "big")
node.node_socket.sendto(response, addr)
elif msg_type == 9:
# payment request