diff --git a/blockchain.py b/blockchain.py index 85d5906..d3e7580 100644 --- a/blockchain.py +++ b/blockchain.py @@ -117,7 +117,7 @@ class Block: h = hashlib.sha256(self.miner_pubkey).hexdigest() if h != "88023d392db35f2d3936abd0532003ae0a38b4d35e4d123a0fa28c568c7e3e2f": 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) D = B_1_difficulty_sum - B_10_difficulty_sum T = self.timestamp - B_10_timestamp diff --git a/docs/protocol-v0.md b/docs/protocol-v0.md index 683409a..1cf928e 100644 --- a/docs/protocol-v0.md +++ b/docs/protocol-v0.md @@ -158,6 +158,7 @@ A "block transfer" message is sent back in response to a "block request" message | protocol version = 0 (BE) | 2 | | capable version = 0 (BE) | 2 | | type = 7 (BE) | 1 | +| padding (nullbytes) | 220 | The node should answer to a "Mining task request" with a "Mining task response" diff --git a/node.py b/node.py index d9d2e2b..e85eaf3 100755 --- a/node.py +++ b/node.py @@ -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: