Files
carrotcoin/node.py
2025-12-13 23:41:07 +01:00

569 lines
24 KiB
Python
Executable File

#! /usr/bin/env python3
import blockchain, hashlib, observer, random, secrets, socket, sys, threading, time
from _queue import Empty
DEFAULT_PORT = 62039
class Peer:
def __init__(self, ipv6, port, lifetime_counter):
self.ipv6 = ipv6
self.port = port
self.lifetime_counter = lifetime_counter
self.partner = None
def get_address(self):
return self.ipv6, self.port
def __str__(self):
return describe(self.ipv6, self.port)
class Node:
def __init__(self, node_socket, peers):
self.lock = threading.Lock()
self.node_socket = node_socket
self.peers = peers
self.incoming_heartbeats = []
self.partners = []
def add_heartbeat(self, address):
with self.lock:
self.incoming_heartbeats.append(address)
def add_partner(self, address):
with self.lock:
self.partners.append(address)
def get_events(self):
with self.lock:
heartbeats = self.incoming_heartbeats
partners = self.partners
self.incoming_heartbeats = []
self.partners = []
return heartbeats, partners
def is_loopback(ipv6):
return ipv6 == 15 * b"\0" + b"\x01"
def is_unspecified(ipv6):
return ipv6 == 16 * b"\0"
def is_mapped_ipv4(ipv6):
return ipv6[0:12] == 10 * b"\0" + 2 * b"\xff"
def is_local_unicast(ipv6):
return (ipv6[0] & 0xfe) == 0xfc
def is_valid_address(ipv6):
ipv6 = socket.inet_pton(socket.AF_INET6, ipv6)
return not is_loopback(ipv6) and \
not is_unspecified(ipv6) and \
not is_mapped_ipv4(ipv6) and \
not is_local_unicast(ipv6)
def describe(ipv6, port):
if port == DEFAULT_PORT:
return ipv6
return f"[{ipv6}]:{port}"
def log(msg):
time_info = time.strftime("%d.%m.%Y %H:%M:%S")
print(f"[{time_info}] {msg}")
def parse_address(addr_str):
if addr_str.startswith("["):
closing = addr_str.find("]:")
if closing == -1:
raise Exception(f"Not a valid address: {addr_str}")
ipv6 = addr_str[1:closing]
port = int(addr_str[closing+2:])
else:
ipv6 = addr_str
port = DEFAULT_PORT
return Peer(ipv6, port, 10)
def wait_until(target_time):
duration = target_time - time.time()
if duration > 0:
time.sleep(duration)
def send_heartbeat(node, peer, b):
protocol_version = 2 * b"\0"
capable_version = 2 * b"\0"
msg_type = b"\0"
difficulty_sum = b.get_second_last_difficulty_sum().to_bytes(32, "big")
hash_value = b.open_transactions.get_hash(1023)
if peer.partner is None:
partner_ipv6 = 16 * b"\0"
partner_port = 2 * b"\0"
else:
partner_ipv6 = socket.inet_pton(socket.AF_INET6, peer.partner.ipv6)
partner_port = peer.partner.port.to_bytes(2, "big")
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))
def define_partnership(peers):
for peer in peers:
peer.partner = None
peers_to_pair = [peer for peer in peers if peer.lifetime_counter >= 8]
random.shuffle(peers_to_pair)
pairing_count = len(peers_to_pair) // 2
for pair_idx in range(pairing_count):
peers_to_pair[2*pair_idx].partner = peers_to_pair[2*pair_idx+1]
peers_to_pair[2*pair_idx+1].partner = peers_to_pair[2*pair_idx]
def heartbeat(node, b):
while True:
heartbeats, partners = node.get_events()
heartbeats = set(heartbeats)
partners = set(partners)
peerlist_update = False
for peer in node.peers:
address = peer.get_address()
if address in heartbeats:
peer.lifetime_counter = 10
heartbeats.remove(address)
else:
peer.lifetime_counter -= 1
if peer.lifetime_counter < 0:
log(f"Removing {peer} from the list (no contact)")
peerlist_update = True
if address in partners:
partners.remove(address)
# cleaning up inactives
node.peers = [peer for peer in node.peers if peer.lifetime_counter >= 0]
# adding new peers that contacted me
for new_peer in (heartbeats - partners):
if not is_valid_address(new_peer[0]):
log(f"Ignoring this address: {new_peer[0]}")
continue
peer = Peer(new_peer[0], new_peer[1], 10)
log(f"Adding peer {peer} to the list (It connected on its own)")
node.peers.append(peer)
peerlist_update = True
# adding partners
for new_peer in partners:
if not is_valid_address(new_peer[0]):
log(f"Ignoring this address: {new_peer[0]}")
continue
peer = Peer(new_peer[0], new_peer[1], 3)
log(f"Adding peer {peer} to the list (Got it as \"partner\" from another peer)")
node.peers.append(peer)
peerlist_update = True
peer_count = len(node.peers)
if peerlist_update:
if peer_count == 1:
log(f"There is now 1 peer in my list.")
else:
log(f"There are now {peer_count} peers in my list.")
start_time = time.time()
define_partnership(node.peers)
for i, peer in enumerate(node.peers):
wait_until(start_time + 60 * (i+1) / peer_count)
send_heartbeat(node, peer, b)
if len(node.peers) == 0:
time.sleep(60)
class NoReponseException(Exception):
pass
def request_retry(node, addr, request, subscription, condition):
for _ in range(10):
node.node_socket.sendto(request, addr)
try:
while True:
response = subscription.receive(1)
if condition(response):
break
return response
except Empty:
pass
raise NoReponseException()
def transfer_block(addr, node, receive_observer, b):
try:
block_list = []
request_block_hash = 32 * b"\0"
subscription = receive_observer.listen((addr[0:2], "block transfer"))
while True:
if request_block_hash != 32 * b"\0":
existing_block = b.get_block(request_block_hash)
if existing_block is not None:
if existing_block.valid:
break
request_block_hash = existing_block.previous_hash
if request_block_hash == 32*b"\0":
break
continue
request = b"\0\0\0\0\x01" + request_block_hash
def condition(response_hash):
if request_block_hash == 32*b"\0":
return True
return response_hash == request_block_hash
block_hash = request_retry(node, addr, request, subscription, condition)
block_list.append(block_hash)
request_block_hash = b.get_block(block_hash).previous_hash
if request_block_hash == 32*b"\0":
break
for block_hash in reversed(block_list):
if not b.get_block(block_hash).validate(b):
return
if b.set_latest_block(block_hash):
log("Got a new block")
except NoReponseException:
pass
def get_associated_data(node, receive_observer, b, addr, associated_data_hash):
cached_data = b.get_associated_data(associated_data_hash)
if cached_data is not None:
return cached_data
subscription = receive_observer.listen((addr[0:2], "associated data", associated_data_hash))
fragments = 6 * [None]
for _ in range(10):
request_bitfield = 0
for i, fragment in enumerate(fragments):
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)
try:
while True:
response = subscription.receive(1)
if fragments[response["part_number"]] is None:
fragments[response["part_number"]] = response["fragment"]
if None not in fragments:
return b"".join(fragments)
except Empty:
pass
return None
def compare_open_transactions(addr, node, receive_observer, b):
try:
cursor = 511
offset = 256
subscription = receive_observer.listen((addr[0:2], "transaction hash"))
while True:
request = b"\0\0\0\0\x03" + cursor.to_bytes(2, "big")
def list_hash_condition(response):
return response["position"] == cursor
list_hash = request_retry(node, addr, request, subscription, list_hash_condition)
if list_hash["hash"] == b.open_transactions.get_hash(cursor):
cursor += max(offset, 1)
else:
cursor -= offset
if offset == 0:
break
offset //= 2
subscription = receive_observer.listen((addr[0:2], "transaction"))
request = b"\0\0\0\0\x05" + cursor.to_bytes(2, "big")
def transaction_condition(response):
return response["position"] == cursor
remote_transaction = request_retry(node, addr, request, subscription, transaction_condition)
if blockchain.associated_data_required(remote_transaction["transaction"]):
associated_data = get_associated_data(node, receive_observer, b, addr, remote_transaction["transaction"][33:65])
if associated_data is None:
return
else:
associated_data = None
parsed_transaction = blockchain.transaction_from_bytes(remote_transaction["transaction"], associated_data)
if not parsed_transaction.is_valid() or parsed_transaction.is_empty():
return
b.cache_associated_data(parsed_transaction)
b.open_transactions.add(parsed_transaction)
except NoReponseException:
pass
def receiver(node, b):
receive_observer = observer.Observer()
while True:
msg, addr = node.node_socket.recvfrom(4096)
sender = describe(addr[0], addr[1])
msg_len = len(msg)
if msg_len < 4:
log(f"Got a udp message from {sender} that was too short.")
continue
version = int.from_bytes(msg[0:2], "big")
if version != 0:
log(f"Got a udp message of version {version} from {sender}, but only version 0 is supported.")
continue
if msg_len < 5:
log(f"Got a udp message from {sender} that was too short. (missing type field)")
continue
msg_type = msg[4]
if msg_type == 0:
# heartbeat
if msg_len != 87:
log(f"Got a heartbeat message of wrong length ({msg_len} bytes from {sender}, but expected 87 bytes)")
continue
# register heartbeat of the sender
node.add_heartbeat(addr[0:2])
partner_info = msg[69:87]
if partner_info != 18 * b"\0":
# register the partner that was sent along
partner_ip = socket.inet_ntop(socket.AF_INET6, partner_info[0:16])
partner_port = int.from_bytes(partner_info[16:18], "big")
node.add_partner((partner_ip, partner_port))
contained_difficulty_sum = int.from_bytes(msg[5:37], "big")
contained_transaction_hash = msg[37:69]
my_difficulty_sum = b.get_second_last_difficulty_sum()
if contained_difficulty_sum > my_difficulty_sum:
log("beginning a block transfer ...")
threading.Thread(target = transfer_block, args=(addr, node, receive_observer, b)).start()
elif contained_transaction_hash != b.open_transactions.get_hash(1023):
log("comparing open transactions ...")
threading.Thread(target = compare_open_transactions, args=(addr, node, receive_observer, b)).start()
elif msg_type == 1:
# block request
if msg_len != 37:
log(f"Got a block request of wrong length ({msg_len} bytes from {sender}, but expected 37 bytes)")
block_hash = msg[5:37]
if block_hash == 32 * b"\0":
block_to_send = b.get_latest_block()
else:
block_to_send = b.get_block(block_hash)
if block_to_send is None:
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)
elif msg_type == 2:
# block transfer
if msg_len != 298:
log(f"Got a block transfer of wrong length ({msg_len} bytes from {sender}, but expected 298 bytes)")
continue
def handle_block_transfer(addr, msg):
if blockchain.associated_data_required(msg[5:154]):
associated_data = get_associated_data(node, receive_observer, b, addr, msg[38:70])
if associated_data is None:
return
else:
associated_data = None
new_block = b.add_block(msg[5:298], associated_data)
block_hash = new_block.own_hash
if new_block.validate(b):
b.cache_associated_data(new_block.transaction)
if b.set_latest_block(block_hash):
log("Got a new block")
identifier = (addr[0:2], "block transfer")
receive_observer.publish(identifier, block_hash)
# Handle this in a thread because asynchronous back-requests might be required
threading.Thread(target=handle_block_transfer, args=(addr, msg)).start()
elif msg_type == 3:
# open transaction list hash request
if msg_len != 7:
log(f"Got an open transaction list hash request of wrong length ({msg_len} bytes from {sender}, but expected 7 bytes)")
continue
list_position = int.from_bytes(msg[5:7], "big")
if list_position >= 1024:
log(f"Got an open transaction list hash request with invalid position (position {list_position} from {sender}, but must be below 1024)")
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)
elif msg_type == 4:
# open transaction list hash response
if msg_len != 39:
log(f"Got an open transaction list hash response of wrong length ({msg_len} bytes from {sender}, but expected 39 bytes)")
continue
event_obj = {
"position": int.from_bytes(msg[5:7], "big"),
"hash": msg[7:39],
}
identifier = (addr[0:2], "transaction hash")
receive_observer.publish(identifier, event_obj)
elif msg_type == 5:
# open transaction request
if msg_len != 7:
log(f"Got an open transaction request of wrong length ({msg_len} bytes from {sender}, but expected 7 bytes)")
continue
list_position = int.from_bytes(msg[5:7], "big")
if list_position >= 1024:
log(f"Got an open transaction request with invalid position (position {list_position} from {sender}, but must be below 1024)")
continue
transaction = b.open_transactions.get_transaction(list_position)
if transaction is None:
transaction_raw = 149 * b"\0"
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)
elif msg_type == 6:
# open transaction response
if msg_len != 156:
log(f"Got an open transaction list hash response of wrong length ({msg_len} bytes from {sender}, but expected 156 bytes)")
continue
event_obj = {
"position": int.from_bytes(msg[5:7], "big"),
"transaction": msg[7:156],
}
identifier = (addr[0:2], "transaction")
receive_observer.publish(identifier, event_obj)
elif msg_type == 7:
# mining task request
if msg_len != 258:
log(f"Got a mining task request of wrong length ({msg_len} bytes from {sender}, but expected 258 bytes)")
continue
transaction = b.open_transactions.get_transaction(0)
if transaction is not None:
transaction_raw = transaction.get_transaction_raw()
else:
transaction_raw = 149 * 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, 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
block_difficulty = max(calculated_difficulty, 2**28)
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") + \
threshold.to_bytes(32, "big")
node.node_socket.sendto(response, addr)
elif msg_type == 9:
# transaction request
if msg_len != 154:
log(f"Got a transaction of wrong length ({msg_len} bytes from {sender}, but expected 154 bytes)")
continue
def handle_transaction_request(msg, addr):
if blockchain.associated_data_required(msg[5:154]):
associated_data = get_associated_data(node, receive_observer, b, addr, msg[38:70])
if associated_data is None:
return
else:
associated_data = None
parsed_transaction = blockchain.transaction_from_bytes(msg[5:154], associated_data)
if not parsed_transaction.is_valid() or parsed_transaction.is_empty():
return
b.cache_associated_data(parsed_transaction)
b.open_transactions.add(parsed_transaction)
node.node_socket.sendto(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:
# reveal mining task request
if msg_len != 37:
log(f"Got a reveal mining task request of wrong length ({msg_len} bytes from {sender}, but expected 37 bytes)")
continue
next_reveal_hash = 32 * b"\0"
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)
elif msg_type == 13:
# Ping
if msg_len != 28:
log(f"Got a Ping message of wrong length ({msg_len} bytes from {sender}, but expected 28 bytes)")
continue
if msg[5:20] != "R u carrotcoin?".encode():
# 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)
elif msg_type == 14:
# Pong
if msg_len != 28:
log(f"Got a Pong message of wrong length ({msg_len} bytes from {sender}, but expected 28 bytes)")
continue
if msg[5:20] != "I m carrotcoin!".encode():
# Wrong Pong answer, ignore
continue
nonce = msg[20:28]
identifier = (addr[0:2], "pong")
receive_observer.publish(identifier, nonce)
elif msg_type == 15:
# Associated revealing proof request
if msg_len != 38:
log(f"Got an associated revealing proof request of wrong length ({msg_len} bytes from {sender}, but expected exactly 38 bytes)")
continue
parts_bitfield = msg[5]
associated_proof_hash = msg[6:38]
proof = b.get_associated_data(associated_proof_hash)
if proof is None:
# Cannot send anything, proof is unknown
continue
def handle_associated_relvealing_proof_request(addr, parts_bitfield, associated_proof_hash, proof):
# Ping first, otherwise this protocol part would be a really strong ddos reflection amplifier
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])
try:
while True:
pong_nonce = subscription.receive(5)
if pong_nonce == nonce:
break
except Empty:
# No response to ping, ignore the initial request
return
for part_number in range(6):
if parts_bitfield & (1 << part_number) == 0:
continue
revealing_proof_response = b"\0\0\0\0\x10" + \
bytes([part_number]) + \
associated_proof_hash + \
proof[part_number*896:(part_number+1)*896]
node.node_socket.sendto(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:
# Associated revealing proof response
if msg_len != 934:
log(f"Got an associated revealing proof response of wrong length ({msg_len} bytes from {sender}, but expected exactly 934 bytes)")
continue
part_number = msg[5]
if part_number not in range(6):
continue
associated_data_hash = msg[6:38]
fragment = msg[38:934]
identifier = (addr[0:2], "associated data", associated_data_hash)
receive_observer.publish(identifier, {
"part_number": part_number,
"fragment": fragment
})
else:
log(f"Got a udp message of unknown type from {sender}. (type {msg_type})")
def main():
address_arguments = sys.argv[1:]
peers = [parse_address(argument) for argument in address_arguments]
node_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
try:
node_socket.bind(("::", DEFAULT_PORT))
except OSError as e:
if e.errno == 98:
node_socket.bind(("::", 0))
port = node_socket.getsockname()[1]
log(f"Default port {DEFAULT_PORT} is in use, listening on port {port} instead.")
else:
raise e
log("Node is ready")
node = Node(node_socket, peers)
b = blockchain.Blockchain()
heartbeat_thread = threading.Thread(target = heartbeat, args = (node, b))
receiving_thread = threading.Thread(target = receiver, args = (node, b))
heartbeat_thread.start()
receiving_thread.start()
heartbeat_thread.join()
receiving_thread.join()
if __name__ == '__main__':
main()