use std::convert::From;
use std::fmt;
use std::fs::File;
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use chrono::prelude::*;
use serde::de::{SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
use grin_store;
use crate::chain;
use crate::core::core;
use crate::core::core::hash::Hash;
use crate::core::global;
use crate::core::pow::Difficulty;
use crate::core::ser::{self, ProtocolVersion, Readable, Reader, Writeable, Writer};
use crate::msg::PeerAddrs;
use crate::util::RwLock;
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_INBOUND_COUNT: u32 = 128;
const PEER_MAX_OUTBOUND_COUNT: u32 = 8;
const PEER_MIN_PREFERRED_OUTBOUND_COUNT: u32 = 8;
const PEER_LISTENER_BUFFER_COUNT: u32 = 8;
#[derive(Debug)]
pub enum Error {
Serialization(ser::Error),
Connection(io::Error),
BadMessage,
MsgLen,
Banned,
ConnectionClose,
Timeout,
Store(grin_store::Error),
Chain(chain::Error),
PeerWithSelf,
NoDandelionRelay,
GenesisMismatch {
us: Hash,
peer: Hash,
},
Send(String),
PeerNotFound,
PeerNotBanned,
PeerException,
Internal,
}
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<chain::Error> for Error {
fn from(e: chain::Error) -> Error {
Error::Chain(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Connection(e)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct PeerAddr(pub SocketAddr);
impl Writeable for PeerAddr {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
match self.0 {
SocketAddr::V4(sav4) => {
ser_multiwrite!(
writer,
[write_u8, 0],
[write_fixed_bytes, &sav4.ip().octets().to_vec()],
[write_u16, sav4.port()]
);
}
SocketAddr::V6(sav6) => {
writer.write_u8(1)?;
for seg in &sav6.ip().segments() {
writer.write_u16(*seg)?;
}
writer.write_u16(sav6.port())?;
}
}
Ok(())
}
}
impl Readable for PeerAddr {
fn read<R: Reader>(reader: &mut R) -> Result<PeerAddr, ser::Error> {
let v4_or_v6 = reader.read_u8()?;
if v4_or_v6 == 0 {
let ip = reader.read_fixed_bytes(4)?;
let port = reader.read_u16()?;
Ok(PeerAddr(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
port,
))))
} else {
let ip = try_iter_map_vec!(0..8, |_| reader.read_u16());
let ipv6 = Ipv6Addr::new(ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7]);
let port = reader.read_u16()?;
if let Some(ipv4) = ipv6.to_ipv4() {
Ok(PeerAddr(SocketAddr::V4(SocketAddrV4::new(ipv4, port))))
} else {
Ok(PeerAddr(SocketAddr::V6(SocketAddrV6::new(
ipv6, port, 0, 0,
))))
}
}
}
}
impl<'de> Visitor<'de> for PeerAddrs {
type Value = PeerAddrs;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("an array of dns names or IP addresses")
}
fn visit_seq<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: SeqAccess<'de>,
{
let mut peers = Vec::with_capacity(access.size_hint().unwrap_or(0));
while let Some(entry) = access.next_element::<&str>()? {
match SocketAddr::from_str(entry) {
Ok(ip) => peers.push(PeerAddr(ip)),
Err(_) => {
let socket_addrs = entry
.to_socket_addrs()
.unwrap_or_else(|_| panic!("Unable to resolve DNS: {}", entry));
peers.append(&mut socket_addrs.map(PeerAddr).collect());
}
}
}
Ok(PeerAddrs { peers })
}
}
impl<'de> Deserialize<'de> for PeerAddrs {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(PeerAddrs { peers: vec![] })
}
}
impl std::hash::Hash for PeerAddr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
if self.0.ip().is_loopback() {
self.0.hash(state);
} else {
self.0.ip().hash(state);
}
}
}
impl PartialEq for PeerAddr {
fn eq(&self, other: &PeerAddr) -> bool {
if self.0.ip().is_loopback() {
self.0 == other.0
} else {
self.0.ip() == other.0.ip()
}
}
}
impl Eq for PeerAddr {}
impl std::fmt::Display for PeerAddr {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl PeerAddr {
pub fn from_ip(addr: IpAddr) -> PeerAddr {
let port = if global::is_floonet() { 13414 } else { 3414 };
PeerAddr(SocketAddr::new(addr, port))
}
pub fn as_key(&self) -> String {
if self.0.ip().is_loopback() {
format!("{}:{}", self.0.ip(), self.0.port())
} else {
format!("{}", self.0.ip())
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct P2PConfig {
pub host: IpAddr,
pub port: u16,
#[serde(default)]
pub seeding_type: Seeding,
pub seeds: Option<PeerAddrs>,
pub capabilities: Capabilities,
pub peers_allow: Option<PeerAddrs>,
pub peers_deny: Option<PeerAddrs>,
pub peers_preferred: Option<PeerAddrs>,
pub ban_window: Option<i64>,
pub peer_max_inbound_count: Option<u32>,
pub peer_max_outbound_count: Option<u32>,
pub peer_min_preferred_outbound_count: Option<u32>,
pub peer_listener_buffer_count: Option<u32>,
pub dandelion_peer: Option<PeerAddr>,
}
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_inbound_count: None,
peer_max_outbound_count: None,
peer_min_preferred_outbound_count: None,
peer_listener_buffer_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_inbound_count(&self) -> u32 {
match self.peer_max_inbound_count {
Some(n) => n,
None => PEER_MAX_INBOUND_COUNT,
}
}
pub fn peer_max_outbound_count(&self) -> u32 {
match self.peer_max_outbound_count {
Some(n) => n,
None => PEER_MAX_OUTBOUND_COUNT,
}
}
pub fn peer_min_preferred_outbound_count(&self) -> u32 {
match self.peer_min_preferred_outbound_count {
Some(n) => n,
None => PEER_MIN_PREFERRED_OUTBOUND_COUNT,
}
}
pub fn peer_listener_buffer_count(&self) -> u32 {
match self.peer_listener_buffer_count {
Some(n) => n,
None => PEER_LISTENER_BUFFER_COUNT,
}
}
}
#[derive(Debug, Clone, Copy, 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 = 0b0000_0000;
const HEADER_HIST = 0b0000_0001;
const TXHASHSET_HIST = 0b0000_0010;
const PEER_LIST = 0b0000_0100;
const TX_KERNEL_HASH = 0b0000_1000;
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,
BadHandshake = 7,
}
}
#[derive(Clone, Debug)]
pub struct PeerLiveInfo {
pub total_difficulty: Difficulty,
pub height: u64,
pub last_seen: DateTime<Utc>,
pub stuck_detector: DateTime<Utc>,
pub first_seen: DateTime<Utc>,
}
#[derive(Clone, Debug)]
pub struct PeerInfo {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: ProtocolVersion,
pub addr: PeerAddr,
pub direction: Direction,
pub live_info: Arc<RwLock<PeerLiveInfo>>,
}
impl PeerLiveInfo {
pub fn new(difficulty: Difficulty) -> PeerLiveInfo {
PeerLiveInfo {
total_difficulty: difficulty,
height: 0,
first_seen: Utc::now(),
last_seen: Utc::now(),
stuck_detector: Utc::now(),
}
}
}
impl PeerInfo {
pub fn total_difficulty(&self) -> Difficulty {
self.live_info.read().total_difficulty
}
pub fn is_outbound(&self) -> bool {
self.direction == Direction::Outbound
}
pub fn is_inbound(&self) -> bool {
self.direction == Direction::Inbound
}
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 first_seen(&self) -> DateTime<Utc> {
self.live_info.read().first_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: ProtocolVersion,
pub addr: PeerAddr,
pub direction: Direction,
pub total_difficulty: Difficulty,
pub height: u64,
}
impl From<PeerInfo> for PeerInfoDisplay {
fn from(info: PeerInfo) -> PeerInfoDisplay {
PeerInfoDisplay {
capabilities: info.capabilities,
user_agent: info.user_agent.clone(),
version: info.version,
addr: info.addr,
direction: info.direction,
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) -> Result<Difficulty, chain::Error>;
fn total_height(&self) -> Result<u64, chain::Error>;
fn transaction_received(&self, tx: core::Transaction, stem: bool)
-> Result<bool, chain::Error>;
fn get_transaction(&self, kernel_hash: Hash) -> Option<core::Transaction>;
fn tx_kernel_received(
&self,
kernel_hash: Hash,
peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;
fn block_received(
&self,
b: core::Block,
peer_info: &PeerInfo,
opts: chain::Options,
) -> Result<bool, chain::Error>;
fn compact_block_received(
&self,
cb: core::CompactBlock,
peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;
fn header_received(
&self,
bh: core::BlockHeader,
peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;
fn headers_received(
&self,
bh: &[core::BlockHeader],
peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;
fn locate_headers(&self, locator: &[Hash]) -> Result<Vec<core::BlockHeader>, chain::Error>;
fn get_block(&self, h: Hash) -> Option<core::Block>;
fn txhashset_read(&self, h: Hash) -> Option<TxHashSetRead>;
fn txhashset_archive_header(&self) -> Result<core::BlockHeader, chain::Error>;
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_peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;
fn get_tmp_dir(&self) -> PathBuf;
fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf;
}
pub trait NetAdapter: ChainAdapter {
fn find_peer_addrs(&self, capab: Capabilities) -> Vec<PeerAddr>;
fn peer_addrs_received(&self, _: Vec<PeerAddr>);
fn peer_difficulty(&self, _: PeerAddr, _: Difficulty, _: u64);
fn is_banned(&self, addr: PeerAddr) -> bool;
}