Implement mining task request/response

This commit is contained in:
2024-03-23 18:57:57 +01:00
parent 56c0598d6b
commit 2f3216a0c4
3 changed files with 33 additions and 1 deletions

31
node.py
View File

@@ -360,6 +360,37 @@ def receiver(node, b):
}
identifier = (addr[0:2], "transaction")
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)")
continue
transaction = b.open_transactions.get_transaction(0)
if transaction is not None:
transaction_raw = transaction.get_transaction_raw()
else:
transaction_raw = 148 * b"\0"
t = int(time.time())
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)
D = B_1_difficulty_sum - B_10_difficulty_sum
T = t - B_10_timestamp
calculated_difficulty = D * 3000 // 9 // T
block_difficulty = max(calculated_difficulty, 2**28)
difficulty_sum = B_1_difficulty_sum + block_difficulty
previous_hash = latest_block.own_hash
else:
difficulty_sum = 2**29
previous_hash = 32 * b"\0"
response = b"\0\0\0\0\x08" + \
transaction_raw + \
previous_hash + \
timestamp_raw + \
difficulty_sum.to_bytes(32, "big")
node.node_socket.sendto(response, addr)
elif msg_type == 9:
# payment request
if msg_len != 153: