Files
Alfis/src/p2p/peer.rs
T

210 lines
4.9 KiB
Rust
Raw Normal View History

use std::fmt::Display;
2021-06-09 20:36:36 +02:00
use std::net::SocketAddr;
2022-09-05 15:35:28 +02:00
use std::time::Instant;
2021-06-09 20:36:36 +02:00
use mio::net::TcpStream;
2021-06-09 20:36:36 +02:00
use crate::crypto::Chacha;
use crate::p2p::State;
#[derive(Debug)]
pub struct Peer {
addr: SocketAddr,
stream: TcpStream,
state: State,
id: String,
2021-02-22 12:49:36 +01:00
height: u64,
inbound: bool,
public: bool,
active: bool,
2022-09-05 15:35:28 +02:00
last_active: Instant,
2021-03-21 01:31:33 +01:00
reconnects: u32,
received_block: u64,
sent_height: u64,
2022-09-05 15:35:28 +02:00
cipher: Option<Chacha>
}
impl Peer {
pub fn new(addr: SocketAddr, stream: TcpStream, state: State, inbound: bool) -> Self {
2021-03-21 01:31:33 +01:00
Peer {
addr,
stream,
state,
id: String::new(),
2021-03-21 01:31:33 +01:00
height: 0,
inbound,
public: false,
active: false,
2022-09-05 15:35:28 +02:00
last_active: Instant::now(),
2021-03-21 01:31:33 +01:00
reconnects: 0,
received_block: 0,
sent_height: 0,
2022-09-05 15:35:28 +02:00
cipher: None
2021-03-21 01:31:33 +01:00
}
}
2021-05-30 00:33:13 +02:00
pub fn set_cipher(&mut self, cipher: Chacha) {
self.cipher = Some(cipher);
}
pub fn get_cipher(&self) -> &Option<Chacha> {
&self.cipher
}
2022-02-16 21:28:30 +01:00
pub fn get_nonce(&self) -> &[u8] {
2021-05-30 00:33:13 +02:00
match &self.cipher {
2021-06-09 20:36:36 +02:00
None => &crate::crypto::ZERO_NONCE,
Some(chacha) => chacha.get_nonce()
2021-05-30 00:33:13 +02:00
}
}
pub fn get_addr(&self) -> SocketAddr {
2021-12-25 18:40:36 +01:00
self.addr
}
pub fn get_stream(&mut self) -> &mut TcpStream {
&mut self.stream
}
2021-02-20 21:36:58 +01:00
pub fn set_stream(&mut self, stream: TcpStream) {
self.stream = stream;
}
pub fn get_state(&self) -> &State {
&self.state
}
pub fn set_state(&mut self, state: State) {
self.state = state;
}
pub fn get_id(&self) -> &str {
&self.id
2021-03-21 00:19:09 +01:00
}
2021-02-22 12:49:36 +01:00
pub fn set_height(&mut self, height: u64) {
self.height = height;
}
pub fn get_height(&self) -> u64 {
self.height
}
pub fn set_sent_height(&mut self, height: u64) {
self.sent_height = height;
}
pub fn get_sent_height(&self) -> u64 {
self.sent_height
}
2021-02-22 12:49:36 +01:00
pub fn is_higher(&self, height: u64) -> bool {
self.height > height
}
pub fn is_lower(&self, height: u64) -> bool {
self.height < height
}
pub fn set_received_block(&mut self, index: u64) {
self.received_block = index;
}
pub fn has_more_blocks(&self, height: u64) -> bool {
if self.height <= height {
return false;
}
if self.received_block > height {
return false;
}
if !self.get_state().is_idle() {
return false;
}
self.height > height
}
pub fn is_public(&self) -> bool {
self.public
}
pub fn set_public(&mut self, public: bool) {
self.public = public;
}
pub fn set_active(&mut self, active: bool) {
self.active = active;
2022-09-05 15:35:28 +02:00
if active {
self.last_active = Instant::now();
}
}
pub fn active(&self) -> bool {
match &self.state {
State::Connected { from } => {
return from.elapsed().as_secs() < 5;
}
State::Idle { from } => {
return from.elapsed().as_secs() < 120;
}
State::ServerHandshake { from } => {
return from.elapsed().as_secs() < 10;
}
_ => {}
}
2022-09-05 15:35:28 +02:00
self.active && self.last_active.elapsed().as_secs() < 120
}
pub fn active_recently(&self) -> bool {
self.active && self.last_active.elapsed().as_secs() < 10
}
2021-03-21 01:31:33 +01:00
pub fn reconnects(&self) -> u32 {
self.reconnects
}
pub fn inc_reconnects(&mut self) {
self.reconnects += 1;
}
pub fn reset_reconnects(&mut self) {
self.reconnects = 0;
}
pub fn disabled(&self) -> bool {
self.state.disabled() || self.reconnects > 2
}
2021-02-20 21:36:58 +01:00
pub fn is_inbound(&self) -> bool {
self.inbound
}
/// If loopback address then we care about ip and port.
/// If regular address then we only care about the ip and ignore the port.
pub fn equals(&self, addr: &SocketAddr) -> bool {
if self.addr.ip().is_loopback() {
self.addr == *addr
} else {
self.addr.ip() == addr.ip()
}
}
}
impl Display for Peer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = match &self.state {
State::Connecting => "Connecting",
State::Connected { .. } => "Connected",
State::ServerHandshake { .. } => "ServerHandshake",
State::HandshakeFinished => "HandshakeFinished",
State::Idle { .. } => "Idle",
State::Message { .. } => "Message",
State::Error => "Error",
State::Banned => "Banned",
State::SendLoop => "SendLoop",
State::Loop => "Loop",
State::Twin => "Twin",
State::Offline { .. } => "Offline"
};
write!(f, "{:?} {}", &self.addr, state)
}
}