Root cause: peers were keyed only by (IP, listen port). An IPv6 host holds several addresses at once, so one node appeared as several peers and could not recognise its own addresses. Protocol: - Added a random per-run node identity (localNodeId), advertised as a length-guarded trailing field in HELLO and ACK_HELLO — backwards compatible with peers that omit it - Added peerNodeId to tcp_connection_t; peers are now identified by this rather than by an endpoint - ACK_HELLO is now sent before any decision to drop the connection, so a rejected dialer learns whose address it reached instead of retrying forever Self-connection: - Identity match → close the connection and record that endpoint permanently as our own - Self-endpoint set preemptively seeded from getifaddrs() at startup, so a node knows its own addresses before ever dialing one - Node_ConnectPeer refuses self endpoints, which also kills the echo-back chain that could exhaust connection slots Duplicate connections and churn: - Dedup by identity per direction — one inbound and one outbound per physical peer, regardless of how many addresses it has - Moved dial history out of the peer table, so striking a peer no longer resets its connect-retry cooldown (this was the loop engine: strike → re-learn via gossip → redial next tick) - Node_HasOtherInboundFrom / Node_HasLiveConnectionTo now ignore connections that are already tearing down, and match on identity as well as endpoint Gossip hygiene: - Never hand a peer its own other addresses in a PEERS reply (matched on identity, not just the socket address) - Reject unusable endpoints: link-local without scope id, unspecified, multicast, site-local (loopback stays allowed for local testing) - Normalise IPv4-mapped IPv6 so one host cannot occupy two entries Two bugs found along the way: - All nodes drew the same identity — random_eight_byte() comes from srand(time(NULL)), so processes started in the same second produced identical values. Added random_secure_eight_byte() (/dev/urandom) for the identity, and mixed the pid into the seed so connection IDs stop colliding too - Identity dedup initially left inbound-only nodes mute — broadcasts traverse outbound connections only, so suppressing a dial-back because an inbound existed would have silenced such a node. Corrected to per-direction Other: - peers output now shows each entry's node identity and the node's own endpoints - Added _DEFAULT_SOURCE to the build so getifaddrs() stays visible on glibc
30 lines
975 B
C
30 lines
975 B
C
#ifndef RUNTIME_STATE_H
|
|
#define RUNTIME_STATE_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <block/chain.h>
|
|
#include <pthread.h>
|
|
|
|
extern uint64_t currentBlockHeight;
|
|
extern blockchain_t* currentChain;
|
|
extern uint256_t currentSupply;
|
|
extern uint64_t currentReward;
|
|
extern uint32_t difficultyTarget;
|
|
extern const char* chainDataDir;
|
|
extern unsigned short listenPort;
|
|
extern bool echoPeersEnabled;
|
|
extern bool forceOrphanReorgEnabled;
|
|
// Random per-run identity of this node, advertised in HELLO/ACK_HELLO. A host can be reachable
|
|
// under many addresses (especially over IPv6), so an (ip, port) endpoint is not a peer identity:
|
|
// this nonce is what lets us recognise our own connections and a peer we already talk to.
|
|
extern uint64_t localNodeId;
|
|
|
|
// Global synchronization primitives for runtime state
|
|
extern pthread_rwlock_t chainLock; // protects chain structure and related mutations
|
|
extern pthread_mutex_t balanceSheetLock; // protects balance sheet map
|
|
|
|
#endif
|