use crate::types::PeerAddr::Ip;
use crate::types::PeerAddr::Onion;
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 crate::chain;
use crate::chain::txhashset::BitmapChunk;
use crate::msg::PeerAddrs;
use crate::mwc_core::core;
use crate::mwc_core::core::hash::Hash;
use crate::mwc_core::core::{OutputIdentifier, Segment, SegmentIdentifier, TxKernel};
use crate::mwc_core::global;
use crate::mwc_core::pow::Difficulty;
use crate::mwc_core::ser::{self, ProtocolVersion, Readable, Reader, Writeable, Writer};
use crate::util::secp::pedersen::RangeProof;
use crate::util::RwLock;
use mwc_chain::txhashset::Segmenter;
use mwc_chain::types::HEADERS_PER_BATCH;
pub const MAX_BLOCK_HEADERS: u32 = HEADERS_PER_BATCH;
#[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 = 10;
const PEER_MIN_PREFERRED_OUTBOUND_COUNT: u32 = 8;
const PEER_BOOST_OUTBOUND_COUNT: u32 = 20;
const PEER_LISTENER_BUFFER_COUNT: u32 = 8;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("p2p Serialization error, {0}")]
Serialization(ser::Error),
#[error("p2p Connection error, {0}")]
Connection(io::Error),
#[error("p2p bad message")]
BadMessage,
#[error("p2p unexpected message {0}")]
UnexpectedMessage(String),
#[error("p2p message Length error")]
MsgLen,
#[error("p2p banned")]
Banned,
#[error("p2p closed connection, {0}")]
ConnectionClose(String),
#[error("p2p timeout")]
Timeout,
#[error("p2p store error, {0}")]
Store(mwc_store::Error),
#[error("p2p chain error, {0}")]
Chain(chain::Error),
#[error("peer with self")]
PeerWithSelf,
#[error("p2p no dandelion relay")]
NoDandelionRelay,
#[error("p2p genesis mismatch: {us} vs peer {peer}")]
GenesisMismatch { us: Hash, peer: Hash },
#[error("p2p send error, {0}")]
Send(String),
#[error("peer not found")]
PeerNotFound,
#[error("peer not banned")]
PeerNotBanned,
#[error("peer exception, {0}")]
PeerException(String),
#[error("p2p internal error: {0}")]
Internal(String),
#[error("libp2p error: {0}")]
Libp2pError(String),
}
impl From<ser::Error> for Error {
fn from(e: ser::Error) -> Error {
Error::Serialization(e)
}
}
impl From<mwc_store::Error> for Error {
fn from(e: mwc_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, Serialize, Deserialize)]
pub enum PeerAddr {
Ip(SocketAddr),
Onion(String),
}
impl Writeable for PeerAddr {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
match self {
Ip(ip) => match ip {
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())?;
}
},
Onion(onion) => {
if onion.len() > 100 {
return Err(ser::Error::TooLargeWriteErr(format!(
"Unreasonable long onion address. UA length is {}",
onion.len()
)));
}
writer.write_u8(2)?;
writer.write_bytes(onion)?;
}
}
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::Ip(SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
port,
))))
} else if v4_or_v6 == 1 {
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::Ip(SocketAddr::V4(SocketAddrV4::new(ipv4, port))))
} else {
Ok(PeerAddr::Ip(SocketAddr::V6(SocketAddrV6::new(
ipv6, port, 0, 0,
))))
}
} else {
let oa = reader.read_bytes_len_prefix()?;
let onion_address = String::from_utf8(oa).unwrap_or("".to_string());
Ok(PeerAddr::Onion(onion_address))
}
}
}
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>()? {
peers.push(PeerAddr::from_str(entry));
}
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) {
match self {
Ip(ip) => {
if ip.ip().is_loopback() {
ip.hash(state);
} else {
ip.ip().hash(state);
}
}
Onion(onion) => {
onion.hash(state);
}
}
}
}
impl PartialEq for PeerAddr {
fn eq(&self, other: &PeerAddr) -> bool {
match self {
Ip(ip) => match other {
Ip(other_ip) => {
if ip.ip().is_loopback() {
ip == other_ip
} else {
ip.ip() == other_ip.ip()
}
}
_ => false,
},
Onion(onion) => match other {
Onion(other_onion) => onion == other_onion,
_ => false,
},
}
}
}
impl Eq for PeerAddr {}
impl std::fmt::Display for PeerAddr {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Ip(ip) => write!(f, "{}", ip),
Onion(onion) => {
let onion_address = &onion.to_string();
write!(f, "tor://{}", onion_address)
}
}
}
}
impl PeerAddr {
pub fn from_ip(addr: IpAddr) -> PeerAddr {
let port = if global::is_floonet() { 13414 } else { 3414 };
PeerAddr::Ip(SocketAddr::new(addr, port))
}
pub fn from_str(addr: &str) -> PeerAddr {
let socket_addr = SocketAddr::from_str(addr);
if socket_addr.is_err() {
let socket_addrs = addr.to_socket_addrs();
if socket_addrs.is_ok() {
let vec: Vec<SocketAddr> = socket_addrs.unwrap().collect();
PeerAddr::Ip(vec[0])
} else {
PeerAddr::Onion(addr.to_string())
}
} else {
PeerAddr::Ip(socket_addr.unwrap())
}
}
pub fn as_key(&self) -> String {
match self {
Ip(ip) => {
if ip.ip().is_loopback() {
format!("{}:{}", ip.ip(), ip.port())
} else {
format!("{}", ip.ip())
}
}
Onion(onion) => format!("{}", onion),
}
}
pub fn tor_address(&self) -> Result<String, Error> {
match self {
Ip(_ip) => {
return Err(Error::Internal(
"requested TOR pub key from IP address".to_string(),
))
}
Onion(onion) => {
if onion.ends_with(".onion") {
let onion = &onion[..(onion.len() - ".onion".len())];
return Ok(onion.to_string());
} else {
return Ok(onion.clone());
}
}
}
}
pub fn is_loopback(&self) -> bool {
match self {
Ip(ip) => ip.ip().is_loopback(),
Onion(_) => {
false }
}
}
}
#[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 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,
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, peers_sync_mode: bool) -> u32 {
if peers_sync_mode {
PEER_BOOST_OUTBOUND_COUNT
} else {
match self.peer_max_outbound_count {
Some(n) => n,
None => PEER_MAX_OUTBOUND_COUNT,
}
}
}
pub fn peer_min_preferred_outbound_count(&self, peers_sync_mode: bool) -> u32 {
if peers_sync_mode {
PEER_BOOST_OUTBOUND_COUNT
} else {
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 TOR_ADDRESS = 0b0001_0000;
const PIBD_HIST = 0b0010_0000;
const BLOCK_HIST = 0b0100_0000;
const HEADERS_HASH = 0b1000_0000;
}
}
impl Capabilities {
pub fn new(tor: bool, archive_mode: bool) -> Self {
let mut res = Capabilities::HEADER_HIST
| Capabilities::TXHASHSET_HIST
| Capabilities::PEER_LIST
| Capabilities::TX_KERNEL_HASH
| Capabilities::TOR_ADDRESS
| Capabilities::PIBD_HIST
| Capabilities::HEADERS_HASH;
if tor {
res |= Capabilities::TOR_ADDRESS;
}
if archive_mode {
res |= Capabilities::BLOCK_HIST;
}
res
}
}
enum_from_primitive! {
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Direction {
Inbound = 0,
Outbound = 1,
InboundTor = 2,
OutboundTor = 3,
}
}
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,
HeadersHashFailure = 8,
PibdFailure = 9,
BadRequest = 10,
}
}
#[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>>,
pub tx_base_fee: u64,
}
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 || self.direction == Direction::OutboundTor
}
pub fn is_inbound(&self) -> bool {
self.direction == Direction::Inbound || self.direction == Direction::InboundTor
}
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 PeerInfoDisplayLegacy {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: ProtocolVersion,
pub addr: String,
pub direction: Direction,
pub total_difficulty: Difficulty,
pub height: u64,
}
#[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.clone().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],
remaining: u64,
peer_info: &PeerInfo,
) -> Result<(), chain::Error>;
fn header_locator(&self) -> Result<Vec<Hash>, chain::Error>;
fn locate_headers(&self, locator: &[Hash]) -> Result<Vec<core::BlockHeader>, chain::Error>;
fn get_block(&self, h: Hash, peer_info: &PeerInfo) -> Option<core::Block>;
fn txhashset_read(&self, h: Hash) -> Option<TxHashSetRead>;
fn txhashset_archive_header(&self) -> Result<core::BlockHeader, chain::Error>;
fn get_tmp_dir(&self) -> PathBuf;
fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf;
fn prepare_segmenter(&self) -> Result<Segmenter, chain::Error>;
fn get_kernel_segment(
&self,
hash: Hash,
id: SegmentIdentifier,
) -> Result<Segment<TxKernel>, chain::Error>;
fn get_bitmap_segment(
&self,
hash: Hash,
id: SegmentIdentifier,
) -> Result<Segment<BitmapChunk>, chain::Error>;
fn get_output_segment(
&self,
hash: Hash,
id: SegmentIdentifier,
) -> Result<Segment<OutputIdentifier>, chain::Error>;
fn get_rangeproof_segment(
&self,
hash: Hash,
id: SegmentIdentifier,
) -> Result<Segment<RangeProof>, chain::Error>;
fn recieve_pibd_status(
&self,
peer: &PeerAddr,
header_hash: Hash,
header_height: u64,
output_bitmap_root: Hash,
) -> Result<(), chain::Error>;
fn recieve_another_archive_header(
&self,
peer: &PeerAddr,
header_hash: Hash,
header_height: u64,
) -> Result<(), chain::Error>;
fn receive_headers_hash_response(
&self,
peer: &PeerAddr,
archive_height: u64,
headers_hash_root: Hash,
) -> Result<(), chain::Error>;
fn get_header_hashes_segment(
&self,
header_hashes_root: Hash,
id: SegmentIdentifier,
) -> Result<Segment<Hash>, chain::Error>;
fn receive_header_hashes_segment(
&self,
peer: &PeerAddr,
header_hashes_root: Hash,
segment: Segment<Hash>,
) -> Result<(), chain::Error>;
fn receive_bitmap_segment(
&self,
peer: &PeerAddr,
archive_header_hash: Hash,
segment: Segment<BitmapChunk>,
) -> Result<(), chain::Error>;
fn receive_output_segment(
&self,
peer: &PeerAddr,
archive_header_hash: Hash,
segment: Segment<OutputIdentifier>,
) -> Result<(), chain::Error>;
fn receive_rangeproof_segment(
&self,
peer: &PeerAddr,
archive_header_hash: Hash,
segment: Segment<RangeProof>,
) -> Result<(), chain::Error>;
fn receive_kernel_segment(
&self,
peer: &PeerAddr,
archive_header_hash: Hash,
segment: Segment<TxKernel>,
) -> Result<(), chain::Error>;
fn peer_difficulty(&self, peer: &PeerAddr, difficulty: Difficulty, height: u64);
}
pub trait NetAdapter: ChainAdapter {
fn find_peer_addrs(&self, capab: Capabilities) -> Vec<PeerAddr>;
fn peer_addrs_received(&self, _: Vec<PeerAddr>);
fn is_banned(&self, addr: &PeerAddr) -> bool;
fn ban_peer(&self, addr: &PeerAddr, ban_reason: ReasonForBan, message: &str);
}
#[derive(Clone, Debug)]
pub struct AttachmentMeta {
pub size: usize,
pub hash: Hash,
pub height: u64,
pub start_time: DateTime<Utc>,
pub path: PathBuf,
}
#[derive(Clone, Debug)]
pub struct AttachmentUpdate {
pub read: usize,
pub left: usize,
pub meta: Arc<AttachmentMeta>,
}