2019-09-12 03:23:51 -07:00
//! Definitions of constants.
2019-10-11 12:41:37 -07:00
use std ::time ::Duration ;
2019-09-25 13:31:41 -07:00
// XXX should these constants be split into protocol also?
2019-11-26 22:42:42 -08:00
use crate ::protocol ::external ::types ::* ;
2019-09-12 03:23:51 -07:00
2020-08-15 15:45:37 -07:00
use zebra_chain ::parameters ::NetworkUpgrade ;
2020-07-21 01:41:18 -07:00
2020-08-06 11:29:00 -07:00
/// The buffer size for the peer set.
2020-09-01 18:20:32 -07:00
///
2020-09-08 03:04:01 -07:00
/// This should be greater than 1 to avoid sender contention, but also reasonably
/// small, to avoid queueing too many in-flight block downloads. (A large queue
/// of in-flight block downloads can choke a constrained local network
/// connection, or a small peer set on testnet.)
///
2020-09-01 18:20:32 -07:00
/// We assume that Zebra nodes have at least 10 Mbps bandwidth. Therefore, a
2020-09-08 03:04:01 -07:00
/// maximum-sized block can take up to 2 seconds to download. So the peer set
/// buffer adds up to 6 seconds worth of blocks to the queue.
pub const PEERSET_BUFFER_SIZE : usize = 3 ;
2020-08-06 11:29:00 -07:00
2019-10-10 17:54:15 -07:00
/// The timeout for requests made to a remote peer.
2020-09-01 18:20:32 -07:00
pub const REQUEST_TIMEOUT : Duration = Duration ::from_secs ( 20 ) ;
2019-10-10 17:54:15 -07:00
2020-08-06 11:29:00 -07:00
/// The timeout for handshakes when connecting to new peers.
2020-09-08 03:04:01 -07:00
///
/// This timeout should remain small, because it helps stop slow peers getting
/// into the peer set. This is particularly important for network-constrained
/// nodes, and on testnet.
2020-08-06 11:29:00 -07:00
pub const HANDSHAKE_TIMEOUT : Duration = Duration ::from_secs ( 4 ) ;
2019-10-11 12:41:37 -07:00
/// We expect to receive a message from a live peer at least once in this time duration.
2019-10-21 12:16:28 -07:00
///
/// This is the sum of:
/// - the interval between connection heartbeats
/// - the timeout of a possible pending (already-sent) request
/// - the timeout for a possible queued request
/// - the timeout for the heartbeat request itself
///
/// This avoids explicit synchronization, but relies on the peer
/// connector actually setting up channels and these heartbeats in a
/// specific manner that matches up with this math.
2020-09-01 18:20:32 -07:00
pub const LIVE_PEER_DURATION : Duration = Duration ::from_secs ( 60 + 20 + 20 + 20 ) ;
2019-10-21 11:59:47 -07:00
/// Regular interval for sending keepalive `Ping` messages to each
/// connected peer.
pub const HEARTBEAT_INTERVAL : Duration = Duration ::from_secs ( 60 ) ;
2019-10-11 12:41:37 -07:00
2019-11-13 14:03:12 -08:00
/// Truncate timestamps in outbound address messages to this time interval.
///
2020-07-05 03:58:50 -07:00
/// This is intended to prevent a peer from learning exactly when we received
2019-11-13 14:03:12 -08:00
/// messages from each of our peers.
pub const TIMESTAMP_TRUNCATION_SECONDS : i64 = 30 * 60 ;
2019-09-19 16:16:06 -07:00
/// The User-Agent string provided by the node.
2020-07-29 08:53:57 -07:00
///
/// This must be a valid [BIP 14] user agent.
///
/// [BIP 14]: https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki
2020-07-29 18:28:33 -07:00
pub const USER_AGENT : & str = " /🦓Zebra🦓:3.0.0-alpha.0/ " ;
2019-09-19 16:16:06 -07:00
2020-08-03 19:53:09 -07:00
/// The Zcash network protocol version implemented by this crate, and advertised
/// during connection setup.
2020-07-21 01:41:18 -07:00
///
2020-08-03 19:53:09 -07:00
/// The current protocol version is checked by our peers. If it is too old,
/// newer peers will refuse to connect to us.
///
/// The current protocol version typically changes before Mainnet and Testnet
/// network upgrades.
2020-08-02 18:04:51 -07:00
pub const CURRENT_VERSION : Version = Version ( 170_012 ) ;
2019-09-13 05:28:38 -07:00
2020-07-21 01:41:18 -07:00
/// The most recent bilateral consensus upgrade implemented by this crate.
///
2020-08-03 19:53:09 -07:00
/// The minimum network upgrade is used to check the protocol versions of our
/// peers. If their versions are too old, we will disconnect from them.
2020-07-21 01:41:18 -07:00
//
2020-07-22 05:10:02 -07:00
// TODO: replace with NetworkUpgrade::current(network, height).
2020-07-21 01:41:18 -07:00
// See the detailed comment in handshake.rs, where this constant is used.
2020-08-15 15:45:37 -07:00
pub const MIN_NETWORK_UPGRADE : NetworkUpgrade = NetworkUpgrade ::Heartwood ;
2019-09-19 16:16:55 -07:00
2020-08-06 11:29:00 -07:00
/// The default RTT estimate for peer responses.
2020-09-01 18:20:32 -07:00
///
/// We choose a high value for the default RTT, so that new peers must prove they
/// are fast, before we prefer them to other peers. This is particularly
/// important on testnet, which has a small number of peers, which are often
/// slow.
///
/// Make the default RTT one second higher than the response timeout.
pub const EWMA_DEFAULT_RTT : Duration = Duration ::from_secs ( 20 + 1 ) ;
2020-08-06 11:29:00 -07:00
/// The decay time for the EWMA response time metric used for load balancing.
2020-09-01 18:20:32 -07:00
///
/// This should be much larger than the `SYNC_RESTART_TIMEOUT`, so we choose
/// better peers when we restart the sync.
pub const EWMA_DECAY_TIME : Duration = Duration ::from_secs ( 120 ) ;
2020-08-06 11:29:00 -07:00
2020-03-12 16:34:34 -07:00
/// Magic numbers used to identify different Zcash networks.
pub mod magics {
use super ::* ;
/// The production mainnet.
pub const MAINNET : Magic = Magic ( [ 0x24 , 0xe9 , 0x27 , 0x64 ] ) ;
/// The testnet.
pub const TESTNET : Magic = Magic ( [ 0xfa , 0x1a , 0xf9 , 0xbf ] ) ;
}
2019-10-21 11:59:47 -07:00
#[ cfg(test) ]
mod tests {
use super ::* ;
2019-10-21 12:16:28 -07:00
/// This assures that the `Duration` value we are computing for
/// LIVE_PEER_DURATION actually matches the other const values it
/// relies on.
2019-10-21 11:59:47 -07:00
#[ test ]
fn ensure_live_peer_duration_value_matches_others ( ) {
let constructed_live_peer_duration =
HEARTBEAT_INTERVAL + REQUEST_TIMEOUT + REQUEST_TIMEOUT + REQUEST_TIMEOUT ;
assert_eq! ( LIVE_PEER_DURATION , constructed_live_peer_duration ) ;
}
2020-09-01 18:20:32 -07:00
/// Make sure that the timeout values are consistent with each other.
#[ test ]
fn ensure_timeouts_consistent ( ) {
assert! ( HANDSHAKE_TIMEOUT < = REQUEST_TIMEOUT ,
" Handshakes are requests, so the handshake timeout can't be longer than the timeout for all requests. " ) ;
// This check is particularly important on testnet, which has a small
// number of peers, which are often slow.
assert! ( EWMA_DEFAULT_RTT > REQUEST_TIMEOUT ,
" The default EWMA RTT should be higher than the request timeout, so new peers are required to prove they are fast, before we prefer them to other peers. " ) ;
assert! ( EWMA_DECAY_TIME > REQUEST_TIMEOUT ,
" The EWMA decay time should be higher than the request timeout, so timed out peers are penalised by the EWMA. " ) ;
}
2019-10-21 11:59:47 -07:00
}