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 enforced maximum amount of blocks to request in a batch.
53///
54/// Requesting more than this will cause the peer to disconnect and potentially lead to bans.
55pub const MAX_BLOCK_BATCH_LEN: usize = 100;
56
57/// The timeout that the block downloader will use for requests.
58pub(crate) const BLOCK_DOWNLOADER_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
59
60/// The maximum size of a transaction, a sanity limit that all transactions across all hard-forks must
61/// be less than.
62///
63/// ref: <https://monero-book.cuprate.org/consensus_rules/transactions.html#transaction-size>
64pub const MAX_TRANSACTION_BLOB_SIZE: usize = 1_000_000;
65
66/// The maximum amount of block IDs allowed in a chain entry response.
67///
68/// ref: <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/cryptonote_config.h#L97>
69// TODO: link to the protocol book when this section is added.
70pub const MAX_BLOCKS_IDS_IN_CHAIN_ENTRY: usize = 25_000;
71
72/// The amount of failures downloading a specific batch before we stop attempting to download it.
73pub(crate) const MAX_DOWNLOAD_FAILURES: usize = 5;
74
75/// The amount of empty chain entries to receive before we assume we have found the top of the chain.
76pub(crate) const EMPTY_CHAIN_ENTRIES_BEFORE_TOP_ASSUMED: usize = 5;
77
78/// The amount of most recent block batches we use to calculate batch size.
79pub(crate) const MOST_RECENT_BATCH_WEIGHTS_FOR_BATCH_SIZE: usize = 100;
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    /// Outbound diffusion flushes should be shorter than
86    /// inbound ones as we control these connections.
87    #[test]
88    fn outbound_diffusion_flush_shorter_than_inbound() {
89        assert!(DIFFUSION_FLUSH_AVERAGE_SECONDS_OUTBOUND < DIFFUSION_FLUSH_AVERAGE_SECONDS_INBOUND);
90    }
91
92    /// Checks that the ban time increases from short to long.
93    #[test]
94    fn ban_times_sanity_check() {
95        assert!(SHORT_BAN < MEDIUM_BAN && MEDIUM_BAN < LONG_BAN);
96    }
97}