Implement mining task request/response
This commit is contained in:
@@ -117,7 +117,7 @@ class Block:
|
|||||||
h = hashlib.sha256(self.miner_pubkey).hexdigest()
|
h = hashlib.sha256(self.miner_pubkey).hexdigest()
|
||||||
if h != "88023d392db35f2d3936abd0532003ae0a38b4d35e4d123a0fa28c568c7e3e2f":
|
if h != "88023d392db35f2d3936abd0532003ae0a38b4d35e4d123a0fa28c568c7e3e2f":
|
||||||
return False
|
return False
|
||||||
B_1_difficulty_sum, B_1_timestamp = self.get_difficulty_info(1, blockchain)
|
B_1_difficulty_sum, _ = self.get_difficulty_info(1, blockchain)
|
||||||
B_10_difficulty_sum, B_10_timestamp = self.get_difficulty_info(10, blockchain)
|
B_10_difficulty_sum, B_10_timestamp = self.get_difficulty_info(10, blockchain)
|
||||||
D = B_1_difficulty_sum - B_10_difficulty_sum
|
D = B_1_difficulty_sum - B_10_difficulty_sum
|
||||||
T = self.timestamp - B_10_timestamp
|
T = self.timestamp - B_10_timestamp
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ A "block transfer" message is sent back in response to a "block request" message
|
|||||||
| protocol version = 0 (BE) | 2 |
|
| protocol version = 0 (BE) | 2 |
|
||||||
| capable version = 0 (BE) | 2 |
|
| capable version = 0 (BE) | 2 |
|
||||||
| type = 7 (BE) | 1 |
|
| type = 7 (BE) | 1 |
|
||||||
|
| padding (nullbytes) | 220 |
|
||||||
|
|
||||||
The node should answer to a "Mining task request" with a "Mining task response"
|
The node should answer to a "Mining task request" with a "Mining task response"
|
||||||
|
|
||||||
|
|||||||
31
node.py
31
node.py
@@ -360,6 +360,37 @@ def receiver(node, b):
|
|||||||
}
|
}
|
||||||
identifier = (addr[0:2], "transaction")
|
identifier = (addr[0:2], "transaction")
|
||||||
receive_observer.publish(identifier, event_obj)
|
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:
|
elif msg_type == 9:
|
||||||
# payment request
|
# payment request
|
||||||
if msg_len != 153:
|
if msg_len != 153:
|
||||||
|
|||||||
Reference in New Issue
Block a user