use crate::util::RwLock;
use std::convert::From;
use std::fs::File;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::sync::mpsc;
use std::sync::Arc;
use chrono::prelude::*;
use crate::core::core::hash::Hash;
use crate::core::pow::Difficulty;
use crate::core::{core, ser};
use grin_store;
pub const MAX_BLOCK_HEADERS: u32 = 512;
#[allow(dead_code)]
pub const MAX_BLOCK_BODIES: u32 = 16;
pub const MAX_PEER_ADDRS: u32 = 256;
pub const MAX_LOCATORS: u32 = 20;
const BAN_WINDOW: i64 = 10800;
const PEER_MAX_COUNT: u32 = 25;
const PEER_MIN_PREFERRED_COUNT: u32 = 8;
#[derive(Debug)]
pub enum Error {
Serialization(ser::Error),
Connection(io::Error),
BadMessage,
MsgLen,
Banned,
ConnectionClose,
Timeout,
Store(grin_store::Error),
PeerWithSelf,
NoDandelionRelay,
ProtocolMismatch {
us: u32,
peer: u32,
},
GenesisMismatch {
us: Hash,
peer: Hash,
},
Send(String),
PeerException,
}
impl From<ser::Error> for Error {
fn from(e: ser::Error) -> Error {
Error::Serialization(e)
}
}
impl From<grin_store::Error> for Error {
fn from(e: grin_store::Error) -> Error {
Error::Store(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Connection(e)
}
}
impl<T> From<mpsc::TrySendError<T>> for Error {
fn from(e: mpsc::TrySendError<T>) -> Error {
Error::Send(e.to_string())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct P2PConfig {
pub host: IpAddr,
pub port: u16,
#[serde(default)]
pub seeding_type: Seeding,
pub seeds: Option<Vec<String>>,
pub capabilities: Capabilities,
pub peers_allow: Option<Vec<String>>,
pub peers_deny: Option<Vec<String>>,
pub peers_preferred: Option<Vec<String>>,
pub ban_window: Option<i64>,
pub peer_max_count: Option<u32>,
pub peer_min_preferred_count: Option<u32>,
pub dandelion_peer: Option<SocketAddr>,
}
impl Default for P2PConfig {
fn default() -> P2PConfig {
let ipaddr = "0.0.0.0".parse().unwrap();
P2PConfig {
host: ipaddr,
port: 3414,
capabilities: Capabilities::FULL_NODE,
seeding_type: Seeding::default(),
seeds: None,
peers_allow: None,
peers_deny: None,
peers_preferred: None,
ban_window: None,
peer_max_count: None,
peer_min_preferred_count: None,
dandelion_peer: None,
}
}
}
impl P2PConfig {
pub fn ban_window(&self) -> i64 {
match self.ban_window {
Some(n) => n,
None => BAN_WINDOW,
}
}
pub fn peer_max_count(&self) -> u32 {
match self.peer_max_count {
Some(n) => n,
None => PEER_MAX_COUNT,
}
}
pub fn peer_min_preferred_count(&self) -> u32 {
match self.peer_min_preferred_count {
Some(n) => n,
None => PEER_MIN_PREFERRED_COUNT,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Seeding {
None,
List,
DNSSeed,
Programmatic,
}
impl Default for Seeding {
fn default() -> Seeding {
Seeding::DNSSeed
}
}
bitflags! {
#[derive(Serialize, Deserialize)]
pub struct Capabilities: u32 {
const UNKNOWN = 0b00000000;
const HEADER_HIST = 0b00000001;
const TXHASHSET_HIST = 0b00000010;
const PEER_LIST = 0b00000100;
const TX_KERNEL_HASH = 0b00001000;
const FULL_NODE = Capabilities::HEADER_HIST.bits
| Capabilities::TXHASHSET_HIST.bits
| Capabilities::PEER_LIST.bits
| Capabilities::TX_KERNEL_HASH.bits;
}
}
enum_from_primitive! {
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Direction {
Inbound = 0,
Outbound = 1,
}
}
enum_from_primitive! {
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ReasonForBan {
None = 0,
BadBlock = 1,
BadCompactBlock = 2,
BadBlockHeader = 3,
BadTxHashSet = 4,
ManualBan = 5,
FraudHeight = 6,
}
}
#[derive(Clone, Debug)]
pub struct PeerLiveInfo {
pub total_difficulty: Difficulty,
pub height: u64,
pub last_seen: DateTime<Utc>,
pub stuck_detector: DateTime<Utc>,
}
#[derive(Clone, Debug)]
pub struct PeerInfo {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: u32,
pub addr: SocketAddr,
pub direction: Direction,
pub live_info: Arc<RwLock<PeerLiveInfo>>,
}
impl PeerInfo {
pub fn total_difficulty(&self) -> Difficulty {
self.live_info.read().total_difficulty
}
pub fn height(&self) -> u64 {
self.live_info.read().height
}
pub fn last_seen(&self) -> DateTime<Utc> {
self.live_info.read().last_seen
}
pub fn update(&self, height: u64, total_difficulty: Difficulty) {
let mut live_info = self.live_info.write();
if total_difficulty != live_info.total_difficulty {
live_info.stuck_detector = Utc::now();
}
live_info.height = height;
live_info.total_difficulty = total_difficulty;
live_info.last_seen = Utc::now()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PeerInfoDisplay {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: u32,
pub addr: SocketAddr,
pub direction: Direction,
pub total_difficulty: Difficulty,
pub height: u64,
}
impl From<PeerInfo> for PeerInfoDisplay {
fn from(info: PeerInfo) -> PeerInfoDisplay {
PeerInfoDisplay {
capabilities: info.capabilities.clone(),
user_agent: info.user_agent.clone(),
version: info.version.clone(),
addr: info.addr.clone(),
direction: info.direction.clone(),
total_difficulty: info.total_difficulty(),
height: info.height(),
}
}
}
pub struct TxHashSetRead {
pub output_index: u64,
pub kernel_index: u64,
pub reader: File,
}
pub trait ChainAdapter: Sync + Send {
fn total_difficulty(&self) -> Difficulty;
fn total_height(&self) -> u64;
fn transaction_received(&self, tx: core::Transaction, stem: bool);
fn get_transaction(&self, kernel_hash: Hash) -> Option<core::Transaction>;
fn tx_kernel_received(&self, kernel_hash: Hash, addr: SocketAddr);
fn block_received(&self, b: core::Block, addr: SocketAddr) -> bool;
fn compact_block_received(&self, cb: core::CompactBlock, addr: SocketAddr) -> bool;
fn header_received(&self, bh: core::BlockHeader, addr: SocketAddr) -> bool;
fn headers_received(&self, bh: &[core::BlockHeader], addr: SocketAddr) -> bool;
fn locate_headers(&self, locator: &[Hash]) -> Vec<core::BlockHeader>;
fn get_block(&self, h: Hash) -> Option<core::Block>;
fn txhashset_read(&self, h: Hash) -> Option<TxHashSetRead>;
fn txhashset_receive_ready(&self) -> bool;
fn txhashset_download_update(
&self,
start_time: DateTime<Utc>,
downloaded_size: u64,
total_size: u64,
) -> bool;
fn txhashset_write(&self, h: Hash, txhashset_data: File, peer_addr: SocketAddr) -> bool;
}
pub trait NetAdapter: ChainAdapter {
fn find_peer_addrs(&self, capab: Capabilities) -> Vec<SocketAddr>;
fn peer_addrs_received(&self, _: Vec<SocketAddr>);
fn peer_difficulty(&self, _: SocketAddr, _: Difficulty, _: u64);
fn is_banned(&self, addr: SocketAddr) -> bool;
}