Log errors that occur when sending a udp packet

This commit is contained in:
2026-03-22 16:12:39 +01:00
parent de62f95f45
commit 1d3cf3e242

29
node.py
View File

@@ -23,6 +23,11 @@ class Node:
self.peers = peers
self.incoming_heartbeats = []
self.partners = []
def send(self, msg, address):
try:
self.node_socket.sendto(msg, address)
except Exception as e:
log(f"Failed to send a message to {address[0]} - {e}")
def add_heartbeat(self, address):
with self.lock:
self.incoming_heartbeats.append(address)
@@ -98,7 +103,7 @@ def send_heartbeat(node, peer, b):
heartbeat_msg = protocol_version + capable_version + msg_type + \
difficulty_sum + hash_value + partner_ipv6 + partner_port
node.node_socket.sendto(heartbeat_msg, (peer.ipv6, peer.port))
node.send(heartbeat_msg, (peer.ipv6, peer.port))
def define_partnership(peers):
for peer in peers:
@@ -171,7 +176,7 @@ class NoReponseException(Exception):
def request_retry(node, addr, request, subscription, condition):
for _ in range(10):
node.node_socket.sendto(request, addr)
node.send(request, addr)
try:
while True:
response = subscription.receive(1)
@@ -227,7 +232,7 @@ def get_associated_data(node, receive_observer, b, addr, associated_data_hash):
if fragment is None:
request_bitfield |= (1 << i)
request = b"\0\0\0\0\x0f" + bytes([request_bitfield]) + associated_data_hash
node.node_socket.sendto(request, addr)
node.send(request, addr)
try:
while True:
response = subscription.receive(1)
@@ -327,7 +332,7 @@ def receiver(node, b):
continue
block_raw = block_to_send.get_block_raw()
response_msg = b"\0\0\0\0\x02" + block_raw
node.node_socket.sendto(response_msg, addr)
node.send(response_msg, addr)
elif msg_type == 2:
# block transfer
if msg_len != 298:
@@ -361,7 +366,7 @@ def receiver(node, b):
continue
list_hash = b.open_transactions.get_hash(list_position)
response = b"\0\0\0\0\x04" + list_position.to_bytes(2, "big") + list_hash
node.node_socket.sendto(response, addr)
node.send(response, addr)
elif msg_type == 4:
# open transaction list hash response
if msg_len != 39:
@@ -388,7 +393,7 @@ def receiver(node, b):
else:
transaction_raw = transaction.get_transaction_raw()
response = b"\0\0\0\0\x06" + list_position.to_bytes(2, "big") + transaction_raw
node.node_socket.sendto(response, addr)
node.send(response, addr)
elif msg_type == 6:
# open transaction response
if msg_len != 156:
@@ -433,7 +438,7 @@ def receiver(node, b):
timestamp_raw + \
difficulty_sum.to_bytes(32, "big") + \
threshold.to_bytes(32, "big")
node.node_socket.sendto(response, addr)
node.send(response, addr)
elif msg_type == 9:
# transaction request
if msg_len != 154:
@@ -451,7 +456,7 @@ def receiver(node, b):
return
b.cache_associated_data(parsed_transaction)
b.open_transactions.add(parsed_transaction)
node.node_socket.sendto(b"\0\0\0\0\x0a", addr)
node.send(b"\0\0\0\0\x0a", addr)
# Handle this in a thread because asynchronous back-requests might be required
threading.Thread(target=handle_transaction_request, args=(msg, addr)).start()
elif msg_type == 11:
@@ -463,7 +468,7 @@ def receiver(node, b):
latest_block = b.get_latest_block()
if latest_block is not None and len(latest_block.pending_commitment_blocks) > 0:
next_reveal_hash = latest_block.pending_commitment_blocks[0][0]
node.node_socket.sendto(b"\0\0\0\0\x0c" + next_reveal_hash, addr)
node.send(b"\0\0\0\0\x0c" + next_reveal_hash, addr)
elif msg_type == 13:
# Ping
if msg_len != 28:
@@ -473,7 +478,7 @@ def receiver(node, b):
# Wrong Ping question, ignore
continue
nonce = msg[20:28]
node.node_socket.sendto(b"\0\0\0\0\x0e" + "I m carrotcoin!".encode() + nonce, addr)
node.send(b"\0\0\0\0\x0e" + "I m carrotcoin!".encode() + nonce, addr)
elif msg_type == 14:
# Pong
if msg_len != 28:
@@ -501,7 +506,7 @@ def receiver(node, b):
subscription = receive_observer.listen((addr[0:2], "pong"))
nonce = secrets.randbits(64).to_bytes(8, "big")
ping = b"\0\0\0\0\x0dR u carrotcoin?" + nonce
node.node_socket.sendto(ping, addr[0:2])
node.send(ping, addr[0:2])
try:
while True:
pong_nonce = subscription.receive(5)
@@ -517,7 +522,7 @@ def receiver(node, b):
bytes([part_number]) + \
associated_proof_hash + \
proof[part_number*896:(part_number+1)*896]
node.node_socket.sendto(revealing_proof_response, addr[0:2])
node.send(revealing_proof_response, addr[0:2])
# Start a thread because of the asynchronous ping
threading.Thread(target=handle_associated_relvealing_proof_request, args=(addr, parts_bitfield, associated_proof_hash, proof)).start()
elif msg_type == 16: