parity-zcash/p2p/src/protocol/ping.rs

98 lines
3.4 KiB
Rust
Raw Normal View History

use bytes::Bytes;
2016-11-15 03:48:01 -08:00
use net::PeerContext;
2019-06-28 14:27:46 -07:00
use protocol::Protocol;
use std::sync::Arc;
use time;
use util::nonce::{NonceGenerator, RandomNonce};
Prefix workspace crates with zebra- (#70) * Update license and author metadata in workspace crates. - ensure that the license field is set to GPL-3 for all GPL-3 licensed crates; - ensure that the author field is set to "Zcash Foundation", responsible for maintenance; - preserve the original authorship info in AUTHORS.md for human-readable history. Updating the author field ensures that all of the machine systems that read crate metadata list the ZF organization, not any single individual, as the maintainer of the crate. * Prefix all internal crate names with zebra-. This does not move the directories containing these crates to also have zebra- prefixes (for instance, zebra-chain instead of chain). I think that this would be preferable, but because it's a `git mv`, it will be simple to do later and leaving it out of this change makes it easier to see the renaming of all of the internal modules. * Remove git dependency from eth-secp256k1 * Avoid an error seemingly related to Deref coercions. This code caused an overflow while evaluating type constraints. As best as I can determine, the cause of the problem was something like so: the Rust implementation of the Bitcoin-specific hash function used in the Bloom filter doesn't operate on byte slices, but only on a `&mut R where R: Read`, so to hash a byte slice, you need to create a mutable copy of the input slice which can be consumed as a `Read` implementation by the hash function; the previous version of this code created a slice copy using a `Deref` coercion instead of `.clone()`, and when a tokio update added new trait impls, the type inference for the `Deref` coercion exploded (somehow -- I'm not sure about the last part?). This commit avoids the problem by manually cloning the input slice.
2019-07-02 12:07:06 -07:00
use zebra_message::common::Command;
use zebra_message::types::{Ping, Pong};
use zebra_message::{deserialize_payload, Error, Payload};
2017-01-11 00:48:40 -08:00
/// Time that must pass since last message from this peer, before we send ping request
const PING_INTERVAL_S: f64 = 60f64;
/// If peer has not responded to our ping request with pong during this interval => close connection
const MAX_PING_RESPONSE_TIME_S: f64 = 60f64;
/// Ping state
#[derive(Debug, Copy, Clone, PartialEq)]
enum State {
2019-06-28 14:27:46 -07:00
/// Peer is sending us messages && we wait for `PING_INTERVAL_S` to pass before sending ping request
WaitingTimeout(f64),
/// Ping message is sent to the peer && we are waiting for pong response for `MAX_PING_RESPONSE_TIME_S`
WaitingPong(f64),
2017-01-11 00:48:40 -08:00
}
2016-11-15 03:48:01 -08:00
pub struct PingProtocol<T = RandomNonce, C = PeerContext> {
2019-06-28 14:27:46 -07:00
/// Context
context: Arc<C>,
/// Nonce generator.
nonce_generator: T,
/// Ping state
state: State,
/// Last nonce sent in the ping message.
last_ping_nonce: Option<u64>,
}
impl PingProtocol {
2019-06-28 14:27:46 -07:00
pub fn new(context: Arc<PeerContext>) -> Self {
PingProtocol {
context: context,
nonce_generator: RandomNonce::default(),
state: State::WaitingTimeout(time::precise_time_s()),
last_ping_nonce: None,
}
}
}
2016-11-15 03:48:01 -08:00
impl Protocol for PingProtocol {
2019-06-28 14:27:46 -07:00
fn initialize(&mut self) {
// bitcoind always sends ping, let's do the same
self.maintain();
}
fn maintain(&mut self) {
let now = time::precise_time_s();
match self.state {
State::WaitingTimeout(time) => {
// send ping request if enough time has passed since last message
if now - time > PING_INTERVAL_S {
let nonce = self.nonce_generator.get();
self.state = State::WaitingPong(now);
self.last_ping_nonce = Some(nonce);
let ping = Ping::new(nonce);
self.context.send_request(&ping);
}
}
State::WaitingPong(time) => {
// if no new messages from peer for last MAX_PING_RESPONSE_TIME_S => disconnect
if now - time > MAX_PING_RESPONSE_TIME_S {
trace!(
"closing connection to peer {}: no messages for last {} seconds",
self.context.info().id,
now - time
);
self.context.close();
}
}
}
}
2017-01-11 00:48:40 -08:00
2019-06-28 14:27:46 -07:00
fn on_message(&mut self, command: &Command, payload: &Bytes) -> Result<(), Error> {
// we have received new message => do not close connection because of timeout
self.state = State::WaitingTimeout(time::precise_time_s());
2019-06-28 14:27:46 -07:00
if command == &Ping::command() {
let ping: Ping = try!(deserialize_payload(payload, self.context.info().version));
let pong = Pong::new(ping.nonce);
self.context.send_response_inline(&pong);
} else if command == &Pong::command() {
let pong: Pong = try!(deserialize_payload(payload, self.context.info().version));
if Some(pong.nonce) != self.last_ping_nonce.take() {
return Err(Error::InvalidCommand);
}
}
2019-06-28 14:27:46 -07:00
Ok(())
}
}