Skip to main content

cuprate_p2p/
constants.rs

1use std::time::Duration;
2
3/// The timeout we set on handshakes.
4pub(crate) const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(20);
5
6/// The timeout we set on receiving ping requests
7pub(crate) const PING_REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
8
9/// The amount of concurrency (maximum number of simultaneous tasks) we allow for handling ping requests
10pub(crate) const PING_REQUEST_CONCURRENCY: usize = 2;
11
12/// The maximum amount of connections to make to seed nodes for when we need peers.
13pub(crate) const MAX_SEED_CONNECTIONS: usize = 3;
14
15/// The timeout for when we fail to find a peer to connect to.
16pub(crate) const OUTBOUND_CONNECTION_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(5);
17
18/// The durations of a short ban.
19pub const SHORT_BAN: Duration = Duration::from_secs(60 * 10);
20
21/// The durations of a medium ban.
22pub const MEDIUM_BAN: Duration = Duration::from_secs(60 * 60 * 24);
23
24/// The durations of a long ban.
25pub const LONG_BAN: Duration = Duration::from_secs(60 * 60 * 24 * 7);
26
27/// The default amount of time between inbound diffusion flushes.
28pub(crate) const DIFFUSION_FLUSH_AVERAGE_SECONDS_INBOUND: Duration = Duration::from_secs(5);
29
30/// The default amount of time between outbound diffusion flushes.
31pub(crate) const DIFFUSION_FLUSH_AVERAGE_SECONDS_OUTBOUND: Duration = Duration::from_millis(2500);
32
33/// This size limit on [`NewTransactions`](monero_wire::protocol::NewTransactions) messages that we create.
34pub(crate) const SOFT_TX_MESSAGE_SIZE_SIZE_LIMIT: usize = 10 * 1024 * 1024;
35
36/// The amount of transactions in the broadcast queue. When this value is hit, old transactions will be dropped from
37/// the queue.
38///
39/// Because of internal implementation details this value is _always_ hit, i.e. a transaction will not be dropped until
40/// 50 more transactions after it are added to the queue.
41pub(crate) const MAX_TXS_IN_BROADCAST_CHANNEL: usize = 50;
42
43/// The time to sleep after an inbound connection comes in.
44///
45/// This is a safety measure to prevent Cuprate from getting spammed with a load of inbound connections.
46/// TODO: it might be a good idea to make this configurable.
47pub(crate) const INBOUND_CONNECTION_COOL_DOWN: Duration = Duration::from_millis(500);
48
49/// The initial amount of chain requests to send to find the best chain to sync from.
50pub(crate) const INITIAL_CHAIN_REQUESTS_TO_SEND: usize = 3;
51
52/// The timeout for individual peer requests during initial chain search.
53pub(crate) const INITIAL_CHAIN_SEARCH_TIMEOUT: Duration = Duration::from_secs(5);
54
55/// The enforced maximum amount of blocks to request in a batch.
56///
57/// Requesting more than this will cause the peer to disconnect and potentially lead to bans.
58pub const MAX_BLOCK_BATCH_LEN: usize = 100;
59
60/// The timeout that the block downloader will use for requests.
61pub(crate) const BLOCK_DOWNLOADER_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
62
63/// The maximum size of a transaction, a sanity limit that all transactions across all hard-forks must
64/// be less than.
65///
66/// ref: <https://monero-book.cuprate.org/consensus_rules/transactions.html#transaction-size>
67pub const MAX_TRANSACTION_BLOB_SIZE: usize = 1_000_000;
68
69/// The maximum amount of block IDs allowed in a chain entry response.
70///
71/// ref: <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/cryptonote_config.h#L97>
72// TODO: link to the protocol book when this section is added.
73pub const MAX_BLOCKS_IDS_IN_CHAIN_ENTRY: usize = 25_000;
74
75/// The amount of failures downloading a specific batch before we stop attempting to download it.
76pub(crate) const MAX_DOWNLOAD_FAILURES: usize = 5;
77
78/// The amount of empty chain entries to receive before we assume we have found the top of the chain.
79pub(crate) const EMPTY_CHAIN_ENTRIES_BEFORE_TOP_ASSUMED: usize = 5;
80
81/// The amount of most recent block batches we use to calculate batch size.
82pub(crate) const MOST_RECENT_BATCH_WEIGHTS_FOR_BATCH_SIZE: usize = 100;
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    /// Outbound diffusion flushes should be shorter than
89    /// inbound ones as we control these connections.
90    #[test]
91    fn outbound_diffusion_flush_shorter_than_inbound() {
92        assert!(DIFFUSION_FLUSH_AVERAGE_SECONDS_OUTBOUND < DIFFUSION_FLUSH_AVERAGE_SECONDS_INBOUND);
93    }
94
95    /// Checks that the ban time increases from short to long.
96    #[test]
97    fn ban_times_sanity_check() {
98        assert!(SHORT_BAN < MEDIUM_BAN && MEDIUM_BAN < LONG_BAN);
99    }
100}