Skip to content

Commit 3fcd5c7

Browse files
0xFFFC0000Boog900
andcommitted
Tx Relay v2: New Relay Logic with threshold-based peer dropping.
Co-authored-by: Boog900 <boog900@tutanota.com>
1 parent 125622d commit 3fcd5c7

17 files changed

+1224
-27
lines changed

contrib/epee/include/net/http_protocol_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "net_utils_base.h"
3737
#include "http_auth.h"
3838
#include "http_base.h"
39+
#include "syncobj.h"
3940

4041
#undef MONERO_DEFAULT_LOG_CATEGORY
4142
#define MONERO_DEFAULT_LOG_CATEGORY "net.http"

contrib/epee/include/syncobj.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#ifndef __WINH_OBJ_H__
3131
#define __WINH_OBJ_H__
3232

33+
#include <mutex>
34+
#include <shared_mutex>
3335
#include <boost/chrono/duration.hpp>
3436
#include <boost/thread/condition_variable.hpp>
3537
#include <boost/thread/locks.hpp>
@@ -84,6 +86,10 @@ namespace epee
8486

8587
#define CRITICAL_REGION_END() }
8688

89+
using rw_mutex = std::shared_timed_mutex;
90+
using read_lock = std::shared_lock<rw_mutex>;
91+
using write_lock = std::unique_lock<rw_mutex>;
92+
8793
}
8894

8995
#endif

src/cryptonote_basic/connection_context.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ namespace cryptonote
6565
return 1024 * 1024; // 1 MB
6666
case cryptonote::NOTIFY_GET_TXPOOL_COMPLEMENT::ID:
6767
return 1024 * 1024 * 4; // 4 MB
68+
case cryptonote::NOTIFY_TX_POOL_INV::ID:
69+
return 1024 * 1024 * 2; // 2 MB
70+
case cryptonote::NOTIFY_REQUEST_TX_POOL_TXS::ID:
71+
return 1024 * 1024 * 2; // 2 MB
6872
default:
6973
break;
7074
};

src/cryptonote_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
#define P2P_DEFAULT_PEERS_IN_HANDSHAKE 250
143143
#define P2P_MAX_PEERS_IN_HANDSHAKE 250
144144
#define P2P_DEFAULT_CONNECTION_TIMEOUT 5000 //5 seconds
145+
#define P2P_DEFAULT_REQUEST_TIMEOUT (P2P_DEFAULT_CONNECTION_TIMEOUT/1000) // 5 seconds
145146
#define P2P_DEFAULT_SOCKS_CONNECT_TIMEOUT 45 // seconds
146147
#define P2P_DEFAULT_PING_CONNECTION_TIMEOUT 2000 //2 seconds
147148
#define P2P_DEFAULT_INVOKE_TIMEOUT 60*2*1000 //2 minutes
@@ -151,14 +152,16 @@
151152
#define P2P_DEFAULT_SYNC_SEARCH_CONNECTIONS_COUNT 2
152153
#define P2P_DEFAULT_LIMIT_RATE_UP 8192 // kB/s
153154
#define P2P_DEFAULT_LIMIT_RATE_DOWN 32768 // kB/s
155+
#define P2P_REQUEST_FAILURE_THRESHOLD_PERCENTAGE 70 // if more than 70% of requests fail, the peer is dropped
154156

155157
#define P2P_FAILED_ADDR_FORGET_SECONDS (60*60) //1 hour
156158
#define P2P_IP_BLOCKTIME (60*60*24) //24 hour
157159
#define P2P_IP_FAILS_BEFORE_BLOCK 10
158160
#define P2P_IDLE_CONNECTION_KILL_INTERVAL (5*60) //5 minutes
159161

160162
#define P2P_SUPPORT_FLAG_FLUFFY_BLOCKS 0x01
161-
#define P2P_SUPPORT_FLAGS P2P_SUPPORT_FLAG_FLUFFY_BLOCKS
163+
#define P2P_SUPPORT_FLAG_TX_RELAY_V2 0x02
164+
#define P2P_SUPPORT_FLAGS (P2P_SUPPORT_FLAG_FLUFFY_BLOCKS | P2P_SUPPORT_FLAG_TX_RELAY_V2)
162165

163166
#define RPC_IP_FAILS_BEFORE_BLOCK 3
164167

src/cryptonote_core/cryptonote_core.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ namespace cryptonote
128128
"sync-pruned-blocks"
129129
, "Allow syncing from nodes with only pruned blocks"
130130
};
131+
const command_line::arg_descriptor<size_t> arg_request_deadline = {
132+
"request-deadline"
133+
, "Set the deadline for waiting requested transactions (in seconds)"
134+
, P2P_DEFAULT_REQUEST_TIMEOUT
135+
};
131136

132137
static const command_line::arg_descriptor<bool> arg_test_drop_download = {
133138
"test-drop-download"
@@ -329,6 +334,7 @@ namespace cryptonote
329334
command_line::add_arg(desc, arg_disable_dns_checkpoints);
330335
command_line::add_arg(desc, arg_block_download_max_size);
331336
command_line::add_arg(desc, arg_sync_pruned_blocks);
337+
command_line::add_arg(desc, arg_request_deadline);
332338
command_line::add_arg(desc, arg_max_txpool_weight);
333339
command_line::add_arg(desc, arg_block_notify);
334340
command_line::add_arg(desc, arg_prune_blockchain);

src/cryptonote_core/cryptonote_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ namespace cryptonote
7070
extern const command_line::arg_descriptor<bool> arg_offline;
7171
extern const command_line::arg_descriptor<size_t> arg_block_download_max_size;
7272
extern const command_line::arg_descriptor<bool> arg_sync_pruned_blocks;
73+
extern const command_line::arg_descriptor<size_t> arg_request_deadline;
7374

7475
/************************************************************************/
7576
/* */

src/cryptonote_protocol/cryptonote_protocol_defs.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,51 @@ namespace cryptonote
375375
};
376376
typedef epee::misc_utils::struct_init<request_t> request;
377377
};
378+
379+
/************************************************************************
380+
* Announces new transaction hashes that *
381+
* the sender believes the receiver may not have. *
382+
* The receiver can pull which hashes are missing locally *
383+
* and optionally request the actual serialized transactions for them. *
384+
*************************************************************************/
385+
struct NOTIFY_TX_POOL_INV
386+
{
387+
const static int ID = BC_COMMANDS_POOL_BASE + 11;
388+
389+
struct request_t
390+
{
391+
std::vector<crypto::hash> t;
392+
std::string _; // padding
393+
394+
BEGIN_KV_SERIALIZE_MAP()
395+
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(t)
396+
KV_SERIALIZE(_)
397+
END_KV_SERIALIZE_MAP()
398+
};
399+
400+
typedef epee::misc_utils::struct_init<request_t> request;
401+
};
402+
403+
/************************************************************************
404+
* Requests the actual transaction data corresponding *
405+
* to a set of transaction hashes. *
406+
* The receiver should look up each transaction in its own pool *
407+
* or memory and respond with the serialized version, if known. *
408+
*************************************************************************/
409+
struct NOTIFY_REQUEST_TX_POOL_TXS
410+
{
411+
const static int ID = BC_COMMANDS_POOL_BASE + 12;
412+
413+
struct request_t
414+
{
415+
std::vector<crypto::hash> t;
416+
417+
BEGIN_KV_SERIALIZE_MAP()
418+
KV_SERIALIZE_CONTAINER_POD_AS_BLOB(t)
419+
END_KV_SERIALIZE_MAP()
420+
};
421+
422+
typedef epee::misc_utils::struct_init<request_t> request;
423+
};
378424

379425
}

src/cryptonote_protocol/cryptonote_protocol_handler.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include <boost/program_options/variables_map.hpp>
3838
#include <string>
39+
#include <thread>
3940

4041
#include "byte_slice.h"
4142
#include "math_helper.h"
@@ -49,7 +50,12 @@
4950
#include "cryptonote_basic/connection_context.h"
5051
#include "net/levin_base.h"
5152
#include "p2p/net_node_common.h"
53+
#include "peerinfo_manager.h"
54+
#include "request_manager.h"
55+
#include "txrequesthandler.h"
5256
#include <boost/circular_buffer.hpp>
57+
#include <boost/uuid/uuid.hpp>
58+
#include <boost/uuid/nil_generator.hpp>
5359

5460
PUSH_WARNINGS
5561
DISABLE_VS_WARNINGS(4355)
@@ -94,8 +100,10 @@ namespace cryptonote
94100
HANDLE_NOTIFY_T2(NOTIFY_REQUEST_CHAIN, &cryptonote_protocol_handler::handle_request_chain)
95101
HANDLE_NOTIFY_T2(NOTIFY_RESPONSE_CHAIN_ENTRY, &cryptonote_protocol_handler::handle_response_chain_entry)
96102
HANDLE_NOTIFY_T2(NOTIFY_NEW_FLUFFY_BLOCK, &cryptonote_protocol_handler::handle_notify_new_fluffy_block)
97-
HANDLE_NOTIFY_T2(NOTIFY_REQUEST_FLUFFY_MISSING_TX, &cryptonote_protocol_handler::handle_request_fluffy_missing_tx)
103+
HANDLE_NOTIFY_T2(NOTIFY_REQUEST_FLUFFY_MISSING_TX, &cryptonote_protocol_handler::handle_request_fluffy_missing_tx)
98104
HANDLE_NOTIFY_T2(NOTIFY_GET_TXPOOL_COMPLEMENT, &cryptonote_protocol_handler::handle_notify_get_txpool_complement)
105+
HANDLE_NOTIFY_T2(NOTIFY_TX_POOL_INV, &cryptonote_protocol_handler::handle_notify_tx_pool_inv)
106+
HANDLE_NOTIFY_T2(NOTIFY_REQUEST_TX_POOL_TXS, &cryptonote_protocol_handler::handle_request_tx_pool_txs)
99107
END_INVOKE_MAP2()
100108

101109
bool on_idle();
@@ -144,6 +152,8 @@ namespace cryptonote
144152
int handle_notify_new_fluffy_block(int command, NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& context);
145153
int handle_request_fluffy_missing_tx(int command, NOTIFY_REQUEST_FLUFFY_MISSING_TX::request& arg, cryptonote_connection_context& context);
146154
int handle_notify_get_txpool_complement(int command, NOTIFY_GET_TXPOOL_COMPLEMENT::request& arg, cryptonote_connection_context& context);
155+
int handle_notify_tx_pool_inv(int command, NOTIFY_TX_POOL_INV::request& arg, cryptonote_connection_context& context);
156+
int handle_request_tx_pool_txs(int command, NOTIFY_REQUEST_TX_POOL_TXS::request& arg, cryptonote_connection_context& context);
147157

148158
//----------------- i_bc_protocol_layout ---------------------------------------
149159
virtual bool relay_block(NOTIFY_NEW_FLUFFY_BLOCK::request& arg, cryptonote_connection_context& exclude_context);
@@ -193,6 +203,11 @@ namespace cryptonote
193203
size_t m_block_download_max_size;
194204
bool m_sync_pruned_blocks;
195205

206+
peer_info_manager m_peer_info_manager;
207+
request_manager m_request_manager;
208+
tx_request_handler m_tx_request_handler;
209+
tx_request_handler::tx_request_handler_runner m_tx_requests_runner;
210+
196211
// Values for sync time estimates
197212
boost::posix_time::ptime m_sync_start_time;
198213
boost::posix_time::ptime m_period_start_time;

0 commit comments

Comments
 (0)