2021-08-22 10:20:59 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
|
2023-01-04 12:07:55 +00:00
|
|
|
* Copyright (c) 2021-2023 SChernykh <https://github.com/SChernykh>
|
2021-08-22 10:20:59 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "tcp_server.h"
|
2023-04-21 09:09:57 +00:00
|
|
|
#include "pool_block.h"
|
2022-12-14 13:24:09 +00:00
|
|
|
#include <list>
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
namespace p2pool {
|
|
|
|
|
|
|
|
class p2pool;
|
|
|
|
struct PoolBlock;
|
2021-08-24 09:42:41 +00:00
|
|
|
class BlockCache;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2023-04-21 09:09:57 +00:00
|
|
|
// Max block size plus BLOCK_RESPONSE header (5 bytes)
|
|
|
|
static constexpr uint64_t P2P_BUF_SIZE = MAX_BLOCK_SIZE + (1 + sizeof(uint32_t));
|
|
|
|
static_assert((P2P_BUF_SIZE & (P2P_BUF_SIZE - 1)) == 0, "P2P_BUF_SIZE is not a power of 2, fix MAX_BLOCK_SIZE");
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
static constexpr size_t PEER_LIST_RESPONSE_MAX_PEERS = 16;
|
2021-09-18 08:03:06 +00:00
|
|
|
static constexpr int DEFAULT_P2P_PORT = 37889;
|
2021-12-30 10:10:18 +00:00
|
|
|
static constexpr int DEFAULT_P2P_PORT_MINI = 37888;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-11-09 18:03:58 +00:00
|
|
|
static constexpr uint32_t PROTOCOL_VERSION_1_0 = 0x00010000UL;
|
|
|
|
static constexpr uint32_t PROTOCOL_VERSION_1_1 = 0x00010001UL;
|
2023-07-04 14:06:01 +00:00
|
|
|
static constexpr uint32_t PROTOCOL_VERSION_1_2 = 0x00010002UL;
|
2022-11-09 18:03:58 +00:00
|
|
|
|
2023-07-04 14:06:01 +00:00
|
|
|
static constexpr uint32_t SUPPORTED_PROTOCOL_VERSION = PROTOCOL_VERSION_1_2;
|
2022-11-09 18:03:58 +00:00
|
|
|
|
2023-04-19 09:36:12 +00:00
|
|
|
class P2PServer : public TCPServer
|
2021-08-22 10:20:59 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class MessageId {
|
2023-07-04 14:47:14 +00:00
|
|
|
HANDSHAKE_CHALLENGE,
|
|
|
|
HANDSHAKE_SOLUTION,
|
|
|
|
LISTEN_PORT,
|
|
|
|
BLOCK_REQUEST,
|
|
|
|
BLOCK_RESPONSE,
|
|
|
|
BLOCK_BROADCAST,
|
|
|
|
PEER_LIST_REQUEST,
|
|
|
|
PEER_LIST_RESPONSE,
|
|
|
|
BLOCK_BROADCAST_COMPACT,
|
|
|
|
BLOCK_NOTIFY,
|
|
|
|
LAST = BLOCK_NOTIFY,
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit P2PServer(p2pool *pool);
|
|
|
|
~P2PServer();
|
|
|
|
|
2021-08-24 09:42:41 +00:00
|
|
|
void add_cached_block(const PoolBlock& block);
|
2021-10-01 13:21:32 +00:00
|
|
|
void clear_cached_blocks();
|
2021-08-24 09:42:41 +00:00
|
|
|
void store_in_cache(const PoolBlock& block);
|
|
|
|
|
2022-08-31 14:37:33 +00:00
|
|
|
void connect_to_peers_async(const char* peer_list);
|
2021-08-22 10:20:59 +00:00
|
|
|
void connect_to_peers(const std::string& peer_list);
|
|
|
|
void on_connect_failed(bool is_v6, const raw_ip& ip, int port) override;
|
|
|
|
|
|
|
|
struct P2PClient : public Client
|
|
|
|
{
|
|
|
|
P2PClient();
|
|
|
|
~P2PClient();
|
|
|
|
|
|
|
|
static Client* allocate() { return new P2PClient(); }
|
2023-02-17 07:47:52 +00:00
|
|
|
virtual size_t size() const override { return sizeof(P2PClient); }
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
void reset() override;
|
|
|
|
bool on_connect() override;
|
|
|
|
bool on_read(char* data, uint32_t size) override;
|
2021-09-06 14:17:20 +00:00
|
|
|
void on_read_failed(int err) override;
|
2021-09-07 07:53:38 +00:00
|
|
|
void on_disconnected() override;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
// Both peers send handshake challenge immediately after a connection is established
|
|
|
|
// Both peers must have the same consensus ID for handshake to succeed
|
|
|
|
// Consensus ID is never sent over the network
|
|
|
|
//
|
|
|
|
// Handshake sequence:
|
|
|
|
//
|
|
|
|
// - Both peers send 8-byte random challenges (and 8 bytes of peer ID) to each other
|
|
|
|
// - Each peer receives 8-byte challenge, chooses 8-byte random SALT and calculates H = KECCAK(CHALLENGE|CONSENSUS_ID|SALT)
|
|
|
|
// - Both peers send their H and SALT, calculate H of the other peer and check if it matches with what other peer calculated
|
|
|
|
// - Peer that initiated the connection must also provide enough PoW in H (difficulty = 10000, 5-10 ms on modern CPU)
|
|
|
|
// - If H doesn't match or doesn't have enough PoW, connection is closed immediately
|
|
|
|
enum {
|
|
|
|
CHALLENGE_SIZE = 8,
|
|
|
|
CHALLENGE_DIFFICULTY = 10000,
|
|
|
|
};
|
|
|
|
|
|
|
|
bool send_handshake_challenge();
|
|
|
|
void send_handshake_solution(const uint8_t (&challenge)[CHALLENGE_SIZE]);
|
|
|
|
bool check_handshake_solution(const hash& solution, const uint8_t (&solution_salt)[CHALLENGE_SIZE]);
|
|
|
|
|
|
|
|
bool on_handshake_challenge(const uint8_t* buf);
|
|
|
|
bool on_handshake_solution(const uint8_t* buf);
|
|
|
|
void on_after_handshake(uint8_t* &p);
|
|
|
|
bool on_listen_port(const uint8_t* buf);
|
|
|
|
bool on_block_request(const uint8_t* buf);
|
2022-12-14 13:24:09 +00:00
|
|
|
bool on_block_response(const uint8_t* buf, uint32_t size, const hash& expected_id);
|
2022-11-09 18:03:58 +00:00
|
|
|
bool on_block_broadcast(const uint8_t* buf, uint32_t size, bool compact);
|
2021-08-22 10:20:59 +00:00
|
|
|
bool on_peer_list_request(const uint8_t* buf);
|
2023-03-27 09:47:48 +00:00
|
|
|
void on_peer_list_response(const uint8_t* buf);
|
2023-07-04 14:06:01 +00:00
|
|
|
void on_block_notify(const uint8_t* buf);
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2022-11-02 17:41:16 +00:00
|
|
|
bool handle_incoming_block_async(const PoolBlock* block, uint64_t max_time_delta = 0);
|
2023-04-27 08:28:32 +00:00
|
|
|
void handle_incoming_block(p2pool* pool, PoolBlock& block, const uint32_t reset_counter, bool is_v6, const raw_ip& addr, std::vector<hash>& missing_blocks);
|
2021-08-22 10:20:59 +00:00
|
|
|
void post_handle_incoming_block(const uint32_t reset_counter, std::vector<hash>& missing_blocks);
|
|
|
|
|
2022-07-10 08:24:03 +00:00
|
|
|
bool is_good() const { return m_handshakeComplete && !m_handshakeInvalid && (m_listenPort >= 0); }
|
|
|
|
|
2022-12-02 14:29:35 +00:00
|
|
|
const char* software_name() const;
|
|
|
|
|
2023-04-19 09:36:12 +00:00
|
|
|
alignas(8) char m_p2pReadBuf[P2P_BUF_SIZE];
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
uint64_t m_peerId;
|
2022-12-09 10:31:05 +00:00
|
|
|
uint64_t m_connectedTime;
|
|
|
|
uint64_t m_broadcastMaxHeight;
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
MessageId m_expectedMessage;
|
|
|
|
uint64_t m_handshakeChallenge;
|
|
|
|
bool m_handshakeSolutionSent;
|
|
|
|
bool m_handshakeComplete;
|
2021-08-29 15:26:30 +00:00
|
|
|
bool m_handshakeInvalid;
|
2021-08-22 10:20:59 +00:00
|
|
|
int m_listenPort;
|
2021-10-24 14:04:30 +00:00
|
|
|
|
2023-06-29 16:20:07 +00:00
|
|
|
uint64_t m_prevPeersSent;
|
2022-02-21 17:41:36 +00:00
|
|
|
uint64_t m_nextOutgoingPeerListRequest;
|
2022-02-21 17:46:53 +00:00
|
|
|
std::chrono::high_resolution_clock::time_point m_lastPeerListRequestTime;
|
2021-10-15 15:09:51 +00:00
|
|
|
int m_peerListPendingRequests;
|
2022-11-09 18:03:58 +00:00
|
|
|
|
|
|
|
uint32_t m_protocolVersion;
|
2022-12-02 14:29:35 +00:00
|
|
|
uint32_t m_SoftwareVersion;
|
|
|
|
uint32_t m_SoftwareID;
|
2022-11-09 18:03:58 +00:00
|
|
|
|
2021-10-24 14:04:30 +00:00
|
|
|
int64_t m_pingTime;
|
|
|
|
|
2022-12-14 13:24:09 +00:00
|
|
|
std::list<hash> m_blockPendingRequests;
|
2022-02-21 15:54:53 +00:00
|
|
|
|
2022-03-23 10:30:38 +00:00
|
|
|
uint64_t m_lastAlive;
|
|
|
|
uint64_t m_lastBroadcastTimestamp;
|
|
|
|
uint64_t m_lastBlockrequestTimestamp;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2021-10-17 08:48:09 +00:00
|
|
|
hash m_broadcastedHashes[8];
|
2023-07-04 14:06:01 +00:00
|
|
|
uint32_t m_broadcastedHashesIndex;
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
2022-11-09 10:17:35 +00:00
|
|
|
void broadcast(const PoolBlock& block, const PoolBlock* parent);
|
2021-08-22 10:20:59 +00:00
|
|
|
uint64_t get_random64();
|
|
|
|
uint64_t get_peerId() const { return m_peerId; }
|
|
|
|
|
|
|
|
void print_status() override;
|
2022-08-31 14:37:33 +00:00
|
|
|
void show_peers_async();
|
2022-05-28 18:47:49 +00:00
|
|
|
size_t peer_list_size() const { MutexLock lock(m_peerListLock); return m_peerList.size(); }
|
2021-08-22 10:20:59 +00:00
|
|
|
|
2023-03-19 21:54:18 +00:00
|
|
|
int external_listen_port() const override;
|
2022-10-28 12:06:26 +00:00
|
|
|
|
2021-12-26 14:28:33 +00:00
|
|
|
uint32_t max_outgoing_peers() const { return m_maxOutgoingPeers; }
|
|
|
|
uint32_t max_incoming_peers() const { return m_maxIncomingPeers; }
|
|
|
|
|
2022-09-20 08:39:55 +00:00
|
|
|
void set_max_outgoing_peers(uint32_t n) { m_maxOutgoingPeers = std::min(std::max(n, 10U), 450U); }
|
|
|
|
void set_max_incoming_peers(uint32_t n) { m_maxIncomingPeers = std::min(std::max(n, 10U), 450U); }
|
2021-12-26 14:28:33 +00:00
|
|
|
|
2023-03-21 09:58:32 +00:00
|
|
|
int deserialize_block(const uint8_t* buf, uint32_t size, bool compact, uint64_t received_timestamp);
|
2022-07-10 08:24:03 +00:00
|
|
|
const PoolBlock* get_block() const { return m_block; }
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
private:
|
2023-04-19 09:36:12 +00:00
|
|
|
const char* get_category() const override { return "P2PServer "; }
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
p2pool* m_pool;
|
2021-08-24 09:42:41 +00:00
|
|
|
BlockCache* m_cache;
|
|
|
|
bool m_cacheLoaded;
|
2021-09-07 10:56:22 +00:00
|
|
|
std::string m_initialPeerList;
|
2021-12-26 14:28:33 +00:00
|
|
|
uint32_t m_maxOutgoingPeers;
|
|
|
|
uint32_t m_maxIncomingPeers;
|
2021-08-24 09:42:41 +00:00
|
|
|
|
|
|
|
uv_rwlock_t m_cachedBlocksLock;
|
2022-03-24 17:30:23 +00:00
|
|
|
unordered_map<hash, PoolBlock*>* m_cachedBlocks;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void on_timer(uv_timer_t* timer) { reinterpret_cast<P2PServer*>(timer->data)->on_timer(); }
|
|
|
|
void on_timer();
|
|
|
|
|
2021-08-24 09:42:41 +00:00
|
|
|
void flush_cache();
|
2021-08-22 10:20:59 +00:00
|
|
|
void download_missing_blocks();
|
2023-06-16 13:51:33 +00:00
|
|
|
void check_host();
|
2022-11-04 06:47:07 +00:00
|
|
|
void check_block_template();
|
2021-08-22 10:20:59 +00:00
|
|
|
void update_peer_connections();
|
|
|
|
void update_peer_list();
|
2022-09-13 08:51:15 +00:00
|
|
|
void send_peer_list_request(P2PClient* client, uint64_t cur_time);
|
2021-08-22 10:20:59 +00:00
|
|
|
void save_peer_list_async();
|
|
|
|
void save_peer_list();
|
2021-09-07 19:30:52 +00:00
|
|
|
void load_peer_list();
|
2022-02-17 19:03:47 +00:00
|
|
|
void load_monerod_peer_list();
|
2021-08-22 10:20:59 +00:00
|
|
|
void update_peer_in_list(bool is_v6, const raw_ip& ip, int port);
|
2023-05-10 08:35:23 +00:00
|
|
|
void remove_peer_from_list(const P2PClient* client);
|
2021-08-31 11:14:35 +00:00
|
|
|
void remove_peer_from_list(const raw_ip& ip);
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
std::mt19937_64 m_rng;
|
|
|
|
|
|
|
|
uv_mutex_t m_blockLock;
|
|
|
|
PoolBlock* m_block;
|
2022-07-10 08:24:03 +00:00
|
|
|
std::vector<uint8_t> m_blockDeserializeBuf;
|
|
|
|
int m_blockDeserializeResult;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
uv_timer_t m_timer;
|
2022-02-21 17:41:36 +00:00
|
|
|
uint64_t m_timerCounter;
|
|
|
|
uint64_t m_timerInterval;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
uint64_t m_peerId;
|
|
|
|
|
2022-05-28 18:47:49 +00:00
|
|
|
mutable uv_mutex_t m_peerListLock;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
struct Peer
|
|
|
|
{
|
|
|
|
bool m_isV6;
|
|
|
|
raw_ip m_addr;
|
|
|
|
int m_port;
|
|
|
|
uint32_t m_numFailedConnections;
|
2022-03-23 10:30:38 +00:00
|
|
|
uint64_t m_lastSeen;
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Peer> m_peerList;
|
2022-02-17 19:03:47 +00:00
|
|
|
std::vector<Peer> m_peerListMonero;
|
2022-05-28 18:47:49 +00:00
|
|
|
std::atomic<uint64_t> m_peerListLastSaved;
|
2021-08-22 10:20:59 +00:00
|
|
|
|
|
|
|
struct Broadcast
|
|
|
|
{
|
2023-03-21 09:58:32 +00:00
|
|
|
hash id;
|
|
|
|
uint64_t received_timestamp;
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
std::vector<uint8_t> blob;
|
|
|
|
std::vector<uint8_t> pruned_blob;
|
2022-11-09 10:17:35 +00:00
|
|
|
std::vector<uint8_t> compact_blob;
|
2021-08-22 10:20:59 +00:00
|
|
|
std::vector<hash> ancestor_hashes;
|
|
|
|
};
|
|
|
|
|
|
|
|
uv_mutex_t m_broadcastLock;
|
|
|
|
uv_async_t m_broadcastAsync;
|
|
|
|
std::vector<Broadcast*> m_broadcastQueue;
|
|
|
|
|
2022-07-10 08:24:03 +00:00
|
|
|
bool m_lookForMissingBlocks;
|
2021-10-29 12:24:05 +00:00
|
|
|
unordered_set<std::pair<uint64_t, uint64_t>> m_missingBlockRequests;
|
2021-08-27 15:26:42 +00:00
|
|
|
|
2022-09-13 08:51:15 +00:00
|
|
|
P2PClient* m_fastestPeer;
|
|
|
|
|
2021-08-22 10:20:59 +00:00
|
|
|
static void on_broadcast(uv_async_t* handle) { reinterpret_cast<P2PServer*>(handle->data)->on_broadcast(); }
|
|
|
|
void on_broadcast();
|
2022-08-31 14:37:33 +00:00
|
|
|
|
|
|
|
uv_mutex_t m_connectToPeersLock;
|
|
|
|
uv_async_t m_connectToPeersAsync;
|
|
|
|
std::string m_connectToPeersData;
|
|
|
|
|
|
|
|
static void on_connect_to_peers(uv_async_t* handle);
|
|
|
|
|
|
|
|
uv_async_t m_showPeersAsync;
|
|
|
|
|
|
|
|
static void on_show_peers(uv_async_t* handle) { reinterpret_cast<P2PServer*>(handle->data)->show_peers(); }
|
2023-02-27 14:38:30 +00:00
|
|
|
void show_peers() const;
|
2022-10-07 14:02:08 +00:00
|
|
|
|
|
|
|
void on_shutdown() override;
|
2023-03-27 09:04:02 +00:00
|
|
|
|
|
|
|
void api_update_local_stats();
|
2021-08-22 10:20:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace p2pool
|