2017-12-12 19:40:26 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::fs::File;
|
2017-12-12 19:40:26 +03:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
use rand::{thread_rng, Rng};
|
|
|
|
|
|
|
|
use core::core;
|
2018-01-30 17:42:04 +03:00
|
|
|
use core::core::hash::{Hash, Hashed};
|
2017-12-12 19:40:26 +03:00
|
|
|
use core::core::target::Difficulty;
|
|
|
|
use util::LOGGER;
|
2018-01-18 21:39:56 +03:00
|
|
|
use time;
|
2017-12-12 19:40:26 +03:00
|
|
|
|
|
|
|
use peer::Peer;
|
2018-02-02 05:03:12 +03:00
|
|
|
use store::{PeerStore, PeerData, State};
|
2017-12-12 19:40:26 +03:00
|
|
|
use types::*;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Peers {
|
|
|
|
pub adapter: Arc<ChainAdapter>,
|
|
|
|
store: Arc<PeerStore>,
|
|
|
|
peers: Arc<RwLock<HashMap<SocketAddr, Arc<RwLock<Peer>>>>>,
|
2018-01-31 00:44:13 +03:00
|
|
|
config: P2PConfig,
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for Peers {}
|
|
|
|
unsafe impl Sync for Peers {}
|
|
|
|
|
|
|
|
impl Peers {
|
2018-01-31 00:44:13 +03:00
|
|
|
pub fn new(store: PeerStore, adapter: Arc<ChainAdapter>, config: P2PConfig) -> Peers {
|
2017-12-12 19:40:26 +03:00
|
|
|
Peers {
|
2018-01-31 00:44:13 +03:00
|
|
|
adapter,
|
2017-12-12 19:40:26 +03:00
|
|
|
store: Arc::new(store),
|
|
|
|
peers: Arc::new(RwLock::new(HashMap::new())),
|
2018-01-31 00:44:13 +03:00
|
|
|
config,
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the peer to our internal peer mapping. Note that the peer is still
|
|
|
|
/// returned so the server can run it.
|
|
|
|
pub fn add_connected(&self, p: Peer) -> Arc<RwLock<Peer>> {
|
|
|
|
debug!(LOGGER, "Saving newly connected peer {}.", p.info.addr);
|
|
|
|
let peer_data = PeerData {
|
|
|
|
addr: p.info.addr,
|
|
|
|
capabilities: p.info.capabilities,
|
|
|
|
user_agent: p.info.user_agent.clone(),
|
|
|
|
flags: State::Healthy,
|
2018-01-18 21:39:56 +03:00
|
|
|
last_banned: 0,
|
2017-12-12 19:40:26 +03:00
|
|
|
};
|
|
|
|
if let Err(e) = self.save_peer(&peer_data) {
|
|
|
|
error!(LOGGER, "Could not save connected peer: {:?}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
let addr = p.info.addr.clone();
|
|
|
|
let apeer = Arc::new(RwLock::new(p));
|
|
|
|
{
|
|
|
|
let mut peers = self.peers.write().unwrap();
|
|
|
|
peers.insert(addr, apeer.clone());
|
|
|
|
}
|
|
|
|
apeer.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_known(&self, addr: &SocketAddr) -> bool {
|
2018-01-03 04:03:44 +03:00
|
|
|
self.get_connected_peer(addr).is_some()
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:33:22 +03:00
|
|
|
/// Get vec of peers we are currently connected to.
|
2017-12-12 19:40:26 +03:00
|
|
|
pub fn connected_peers(&self) -> Vec<Arc<RwLock<Peer>>> {
|
2018-01-18 21:39:56 +03:00
|
|
|
let mut res = self.peers
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.values()
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
2017-12-14 23:33:22 +03:00
|
|
|
thread_rng().shuffle(&mut res);
|
|
|
|
res
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a peer we're connected to by address.
|
2018-01-03 04:03:44 +03:00
|
|
|
pub fn get_connected_peer(&self, addr: &SocketAddr) -> Option<Arc<RwLock<Peer>>> {
|
2017-12-12 19:40:26 +03:00
|
|
|
self.peers.read().unwrap().get(addr).map(|p| p.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Number of peers we're currently connected to.
|
|
|
|
pub fn peer_count(&self) -> u32 {
|
|
|
|
self.connected_peers().len() as u32
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:39:56 +03:00
|
|
|
// Return vec of connected peers that currently advertise more work
|
|
|
|
// (total_difficulty) than we do.
|
2017-12-29 03:49:27 +03:00
|
|
|
pub fn more_work_peers(&self) -> Vec<Arc<RwLock<Peer>>> {
|
|
|
|
let peers = self.connected_peers();
|
|
|
|
if peers.len() == 0 {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
let total_difficulty = self.total_difficulty();
|
|
|
|
|
|
|
|
let mut max_peers = peers
|
|
|
|
.iter()
|
2018-01-18 21:39:56 +03:00
|
|
|
.filter(|x| match x.try_read() {
|
|
|
|
Ok(peer) => peer.info.total_difficulty > total_difficulty,
|
|
|
|
Err(_) => false,
|
2017-12-29 03:49:27 +03:00
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
thread_rng().shuffle(&mut max_peers);
|
|
|
|
max_peers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns single random peer with more work than us.
|
|
|
|
pub fn more_work_peer(&self) -> Option<Arc<RwLock<Peer>>> {
|
|
|
|
match self.more_work_peers().first() {
|
|
|
|
Some(x) => Some(x.clone()),
|
2018-01-18 21:39:56 +03:00
|
|
|
None => None,
|
2017-12-29 03:49:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return vec of connected peers that currently have the most worked branch,
|
2017-12-12 19:40:26 +03:00
|
|
|
/// showing the highest total difficulty.
|
|
|
|
pub fn most_work_peers(&self) -> Vec<Arc<RwLock<Peer>>> {
|
|
|
|
let peers = self.connected_peers();
|
|
|
|
if peers.len() == 0 {
|
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
|
|
|
let max_total_difficulty = peers
|
|
|
|
.iter()
|
2018-01-18 21:39:56 +03:00
|
|
|
.map(|x| match x.try_read() {
|
|
|
|
Ok(peer) => peer.info.total_difficulty.clone(),
|
|
|
|
Err(_) => Difficulty::zero(),
|
2017-12-12 19:40:26 +03:00
|
|
|
})
|
|
|
|
.max()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut max_peers = peers
|
|
|
|
.iter()
|
2018-01-18 21:39:56 +03:00
|
|
|
.filter(|x| match x.try_read() {
|
|
|
|
Ok(peer) => peer.info.total_difficulty == max_total_difficulty,
|
|
|
|
Err(_) => false,
|
2017-12-12 19:40:26 +03:00
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
thread_rng().shuffle(&mut max_peers);
|
|
|
|
max_peers
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns single random peer with the most worked branch, showing the highest total
|
|
|
|
/// difficulty.
|
|
|
|
pub fn most_work_peer(&self) -> Option<Arc<RwLock<Peer>>> {
|
|
|
|
match self.most_work_peers().first() {
|
|
|
|
Some(x) => Some(x.clone()),
|
2018-01-18 21:39:56 +03:00
|
|
|
None => None,
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_banned(&self, peer_addr: SocketAddr) -> bool {
|
|
|
|
if let Ok(peer_data) = self.store.get_peer(peer_addr) {
|
|
|
|
if peer_data.flags == State::Banned {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Bans a peer, disconnecting it if we're currently connected
|
|
|
|
pub fn ban_peer(&self, peer_addr: &SocketAddr) {
|
|
|
|
if let Err(e) = self.update_state(peer_addr.clone(), State::Banned) {
|
|
|
|
error!(LOGGER, "Couldn't ban {}: {:?}", peer_addr, e);
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:39:56 +03:00
|
|
|
if let Err(e) =
|
|
|
|
self.update_last_banned(peer_addr.clone(), time::now_utc().to_timespec().sec)
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
LOGGER,
|
|
|
|
"Couldn't update last_banned time {}: {:?}", peer_addr, e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-03 04:03:44 +03:00
|
|
|
if let Some(peer) = self.get_connected_peer(peer_addr) {
|
2017-12-12 19:40:26 +03:00
|
|
|
debug!(LOGGER, "Banning peer {}", peer_addr);
|
|
|
|
// setting peer status will get it removed at the next clean_peer
|
|
|
|
let peer = peer.write().unwrap();
|
|
|
|
peer.set_banned();
|
|
|
|
peer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 06:25:14 +03:00
|
|
|
/// Unbans a peer, checks if it exists and banned then unban
|
|
|
|
pub fn unban_peer(&self, peer_addr: &SocketAddr) {
|
|
|
|
match self.get_peer(peer_addr.clone()) {
|
|
|
|
Ok(_) => {
|
|
|
|
if self.is_banned(peer_addr.clone()) {
|
|
|
|
if let Err(e) = self.update_state(peer_addr.clone(), State::Healthy) {
|
|
|
|
error!(LOGGER, "Couldn't unban {}: {:?}", peer_addr, e)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!(LOGGER, "Couldn't unban {}: peer is not banned", peer_addr)
|
|
|
|
}
|
2018-01-18 21:39:56 +03:00
|
|
|
}
|
|
|
|
Err(e) => error!(LOGGER, "Couldn't unban {}: {:?}", peer_addr, e),
|
2018-01-04 06:25:14 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:33:22 +03:00
|
|
|
/// Broadcasts the provided block to PEER_PREFERRED_COUNT of our peers.
|
|
|
|
/// We may be connected to PEER_MAX_COUNT peers so we only
|
|
|
|
/// want to broadcast to a random subset of peers.
|
|
|
|
/// A peer implementation may drop the broadcast request
|
|
|
|
/// if it knows the remote peer already has the block.
|
2017-12-12 19:40:26 +03:00
|
|
|
pub fn broadcast_block(&self, b: &core::Block) {
|
|
|
|
let peers = self.connected_peers();
|
2017-12-19 00:18:36 +03:00
|
|
|
let preferred_peers = 8;
|
2017-12-12 19:40:26 +03:00
|
|
|
let mut count = 0;
|
2017-12-19 00:18:36 +03:00
|
|
|
for p in peers.iter().take(preferred_peers) {
|
2017-12-12 19:40:26 +03:00
|
|
|
let p = p.read().unwrap();
|
|
|
|
if p.is_connected() {
|
|
|
|
if let Err(e) = p.send_block(b) {
|
|
|
|
debug!(LOGGER, "Error sending block to peer: {:?}", e);
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 00:18:36 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2018-01-30 17:42:04 +03:00
|
|
|
"broadcast_block: {}, {} at {}, to {} peers, done.",
|
2017-12-19 00:18:36 +03:00
|
|
|
b.hash(),
|
|
|
|
b.header.total_difficulty,
|
|
|
|
b.header.height,
|
|
|
|
count,
|
|
|
|
);
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 23:39:55 +03:00
|
|
|
pub fn broadcast_compact_block(&self, b: &core::CompactBlock) {
|
|
|
|
let peers = self.connected_peers();
|
|
|
|
let preferred_peers = 8;
|
|
|
|
let mut count = 0;
|
|
|
|
for p in peers.iter().take(preferred_peers) {
|
|
|
|
let p = p.read().unwrap();
|
|
|
|
if p.is_connected() {
|
|
|
|
if let Err(e) = p.send_compact_block(b) {
|
|
|
|
debug!(LOGGER, "Error sending compact block to peer: {:?}", e);
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"broadcast_compact_block: {}, {} at {}, to {} peers, done.",
|
|
|
|
b.hash(),
|
|
|
|
b.header.total_difficulty,
|
|
|
|
b.header.height,
|
|
|
|
count,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-30 17:42:04 +03:00
|
|
|
/// Broadcasts the provided block to PEER_PREFERRED_COUNT of our peers.
|
|
|
|
/// We may be connected to PEER_MAX_COUNT peers so we only
|
|
|
|
/// want to broadcast to a random subset of peers.
|
|
|
|
/// A peer implementation may drop the broadcast request
|
|
|
|
/// if it knows the remote peer already has the block.
|
|
|
|
pub fn broadcast_header(&self, bh: &core::BlockHeader) {
|
|
|
|
let peers = self.connected_peers();
|
|
|
|
let preferred_peers = 8;
|
|
|
|
let mut count = 0;
|
|
|
|
for p in peers.iter().take(preferred_peers) {
|
|
|
|
let p = p.read().unwrap();
|
|
|
|
if p.is_connected() {
|
|
|
|
if let Err(e) = p.send_header(bh) {
|
|
|
|
debug!(LOGGER, "Error sending header to peer: {:?}", e);
|
|
|
|
} else {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"broadcast_header: {}, {} at {}, to {} peers, done.",
|
|
|
|
bh.hash(),
|
|
|
|
bh.total_difficulty,
|
|
|
|
bh.height,
|
|
|
|
count,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:33:22 +03:00
|
|
|
/// Broadcasts the provided transaction to PEER_PREFERRED_COUNT of our peers.
|
|
|
|
/// We may be connected to PEER_MAX_COUNT peers so we only
|
|
|
|
/// want to broadcast to a random subset of peers.
|
|
|
|
/// A peer implementation may drop the broadcast request
|
|
|
|
/// if it knows the remote peer already has the transaction.
|
2017-12-12 19:40:26 +03:00
|
|
|
pub fn broadcast_transaction(&self, tx: &core::Transaction) {
|
|
|
|
let peers = self.connected_peers();
|
2017-12-14 23:33:22 +03:00
|
|
|
for p in peers.iter().take(8) {
|
2017-12-12 19:40:26 +03:00
|
|
|
let p = p.read().unwrap();
|
|
|
|
if p.is_connected() {
|
|
|
|
if let Err(e) = p.send_transaction(tx) {
|
|
|
|
debug!(LOGGER, "Error sending block to peer: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ping all our connected peers. Always automatically expects a pong back or
|
|
|
|
/// disconnects. This acts as a liveness test.
|
|
|
|
pub fn check_all(&self, total_difficulty: Difficulty, height: u64) {
|
|
|
|
let peers_map = self.peers.read().unwrap();
|
|
|
|
for p in peers_map.values() {
|
|
|
|
let p = p.read().unwrap();
|
|
|
|
if p.is_connected() {
|
|
|
|
let _ = p.send_ping(total_difficulty.clone(), height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// All peer information we have in storage
|
|
|
|
pub fn all_peers(&self) -> Vec<PeerData> {
|
|
|
|
self.store.all_peers()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find peers in store (not necessarily connected) and return their data
|
|
|
|
pub fn find_peers(&self, state: State, cap: Capabilities, count: usize) -> Vec<PeerData> {
|
|
|
|
self.store.find_peers(state, cap, count)
|
|
|
|
}
|
|
|
|
|
2018-01-03 04:03:44 +03:00
|
|
|
/// Get peer in store by address
|
|
|
|
pub fn get_peer(&self, peer_addr: SocketAddr) -> Result<PeerData, Error> {
|
|
|
|
self.store.get_peer(peer_addr).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
2017-12-12 19:40:26 +03:00
|
|
|
/// Whether we've already seen a peer with the provided address
|
|
|
|
pub fn exists_peer(&self, peer_addr: SocketAddr) -> Result<bool, Error> {
|
|
|
|
self.store.exists_peer(peer_addr).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Saves updated information about a peer
|
|
|
|
pub fn save_peer(&self, p: &PeerData) -> Result<(), Error> {
|
|
|
|
self.store.save_peer(p).map_err(From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the state of a peer in store
|
|
|
|
pub fn update_state(&self, peer_addr: SocketAddr, new_state: State) -> Result<(), Error> {
|
2018-01-18 21:39:56 +03:00
|
|
|
self.store
|
|
|
|
.update_state(peer_addr, new_state)
|
|
|
|
.map_err(From::from)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the last banned time of a peer in store
|
|
|
|
pub fn update_last_banned(&self, peer_addr: SocketAddr, last_banned: i64) -> Result<(), Error> {
|
|
|
|
self.store
|
|
|
|
.update_last_banned(peer_addr, last_banned)
|
|
|
|
.map_err(From::from)
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterate over the peer list and prune all peers we have
|
|
|
|
/// lost connection to or have been deemed problematic.
|
|
|
|
/// Also avoid connected peer count getting too high.
|
2017-12-14 20:23:35 +03:00
|
|
|
pub fn clean_peers(&self, max_count: usize) {
|
2017-12-12 19:40:26 +03:00
|
|
|
let mut rm = vec![];
|
|
|
|
|
|
|
|
// build a list of peers to be cleaned up
|
|
|
|
for peer in self.connected_peers() {
|
|
|
|
let peer_inner = peer.read().unwrap();
|
|
|
|
if peer_inner.is_banned() {
|
|
|
|
debug!(LOGGER, "cleaning {:?}, peer banned", peer_inner.info.addr);
|
|
|
|
rm.push(peer.clone());
|
|
|
|
} else if !peer_inner.is_connected() {
|
|
|
|
debug!(LOGGER, "cleaning {:?}, not connected", peer_inner.info.addr);
|
|
|
|
rm.push(peer.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now clean up peer map based on the list to remove
|
|
|
|
{
|
|
|
|
let mut peers = self.peers.write().unwrap();
|
|
|
|
for p in rm.clone() {
|
|
|
|
let p = p.read().unwrap();
|
|
|
|
peers.remove(&p.info.addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we do not have too many connected peers
|
|
|
|
let excess_count = {
|
|
|
|
let peer_count = self.peer_count().clone() as usize;
|
2017-12-14 20:23:35 +03:00
|
|
|
if peer_count > max_count {
|
|
|
|
peer_count - max_count
|
2017-12-12 19:40:26 +03:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// map peers to addrs in a block to bound how long we keep the read lock for
|
|
|
|
let addrs = {
|
2018-01-18 21:39:56 +03:00
|
|
|
self.connected_peers()
|
|
|
|
.iter()
|
|
|
|
.map(|x| {
|
|
|
|
let p = x.read().unwrap();
|
|
|
|
p.info.addr.clone()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
2017-12-12 19:40:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// now remove them taking a short-lived write lock each time
|
|
|
|
// maybe better to take write lock once and remove them all?
|
2018-01-18 21:39:56 +03:00
|
|
|
for x in addrs.iter().take(excess_count) {
|
|
|
|
let mut peers = self.peers.write().unwrap();
|
|
|
|
peers.remove(x);
|
|
|
|
}
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop(self) {
|
|
|
|
let peers = self.connected_peers();
|
|
|
|
for peer in peers {
|
|
|
|
let peer = peer.read().unwrap();
|
|
|
|
peer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChainAdapter for Peers {
|
|
|
|
fn total_difficulty(&self) -> Difficulty {
|
|
|
|
self.adapter.total_difficulty()
|
|
|
|
}
|
|
|
|
fn total_height(&self) -> u64 {
|
|
|
|
self.adapter.total_height()
|
|
|
|
}
|
|
|
|
fn transaction_received(&self, tx: core::Transaction) {
|
|
|
|
self.adapter.transaction_received(tx)
|
|
|
|
}
|
|
|
|
fn block_received(&self, b: core::Block, peer_addr: SocketAddr) -> bool {
|
|
|
|
if !self.adapter.block_received(b, peer_addr) {
|
2018-01-30 17:42:04 +03:00
|
|
|
// if the peer sent us a block that's intrinsically bad
|
|
|
|
// they are either mistaken or manevolent, both of which require a ban
|
|
|
|
self.ban_peer(&peer_addr);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 23:39:55 +03:00
|
|
|
fn compact_block_received(&self, cb: core::CompactBlock, peer_addr: SocketAddr) -> bool {
|
|
|
|
if !self.adapter.compact_block_received(cb, peer_addr) {
|
|
|
|
// if the peer sent us a block that's intrinsically bad
|
|
|
|
// they are either mistaken or manevolent, both of which require a ban
|
|
|
|
self.ban_peer(&peer_addr);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 17:42:04 +03:00
|
|
|
fn header_received(&self, bh: core::BlockHeader, peer_addr: SocketAddr) -> bool {
|
|
|
|
if !self.adapter.header_received(bh, peer_addr) {
|
|
|
|
// if the peer sent us a block header that's intrinsically bad
|
|
|
|
// they are either mistaken or manevolent, both of which require a ban
|
2017-12-12 19:40:26 +03:00
|
|
|
self.ban_peer(&peer_addr);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 21:39:56 +03:00
|
|
|
fn headers_received(&self, headers: Vec<core::BlockHeader>, peer_addr: SocketAddr) {
|
2017-12-12 19:40:26 +03:00
|
|
|
self.adapter.headers_received(headers, peer_addr)
|
|
|
|
}
|
|
|
|
fn locate_headers(&self, hs: Vec<Hash>) -> Vec<core::BlockHeader> {
|
|
|
|
self.adapter.locate_headers(hs)
|
|
|
|
}
|
|
|
|
fn get_block(&self, h: Hash) -> Option<core::Block> {
|
|
|
|
self.adapter.get_block(h)
|
|
|
|
}
|
2018-02-10 01:32:16 +03:00
|
|
|
fn sumtrees_read(&self, h: Hash) -> Option<SumtreesRead> {
|
|
|
|
self.adapter.sumtrees_read(h)
|
|
|
|
}
|
|
|
|
fn sumtrees_write(
|
|
|
|
&self,
|
|
|
|
h: Hash,
|
|
|
|
rewind_to_output: u64,
|
|
|
|
rewind_to_kernel: u64,
|
|
|
|
sumtree_data: File,
|
|
|
|
peer_addr: SocketAddr,
|
|
|
|
) -> bool {
|
|
|
|
if !self.adapter.sumtrees_write(h, rewind_to_output, rewind_to_kernel,
|
|
|
|
sumtree_data, peer_addr) {
|
|
|
|
self.ban_peer(&peer_addr);
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 19:40:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NetAdapter for Peers {
|
|
|
|
/// Find good peers we know with the provided capability and return their
|
|
|
|
/// addresses.
|
|
|
|
fn find_peer_addrs(&self, capab: Capabilities) -> Vec<SocketAddr> {
|
|
|
|
let peers = self.find_peers(State::Healthy, capab, MAX_PEER_ADDRS as usize);
|
|
|
|
debug!(LOGGER, "Got {} peer addrs to send.", peers.len());
|
|
|
|
map_vec!(peers, |p| p.addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A list of peers has been received from one of our peers.
|
|
|
|
fn peer_addrs_received(&self, peer_addrs: Vec<SocketAddr>) {
|
|
|
|
debug!(LOGGER, "Received {} peer addrs, saving.", peer_addrs.len());
|
|
|
|
for pa in peer_addrs {
|
|
|
|
if let Ok(e) = self.exists_peer(pa) {
|
|
|
|
if e {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let peer = PeerData {
|
|
|
|
addr: pa,
|
2018-02-01 21:14:32 +03:00
|
|
|
capabilities: Capabilities::UNKNOWN,
|
2017-12-12 19:40:26 +03:00
|
|
|
user_agent: "".to_string(),
|
|
|
|
flags: State::Healthy,
|
2018-01-18 21:39:56 +03:00
|
|
|
last_banned: 0,
|
2017-12-12 19:40:26 +03:00
|
|
|
};
|
|
|
|
if let Err(e) = self.save_peer(&peer) {
|
|
|
|
error!(LOGGER, "Could not save received peer address: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peer_difficulty(&self, addr: SocketAddr, diff: Difficulty, height: u64) {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"peer total_diff @ height (ping/pong): {}: {} @ {} \
|
2018-01-18 21:39:56 +03:00
|
|
|
vs us: {} @ {}",
|
2017-12-12 19:40:26 +03:00
|
|
|
addr,
|
|
|
|
diff,
|
|
|
|
height,
|
|
|
|
self.total_difficulty(),
|
|
|
|
self.total_height()
|
|
|
|
);
|
|
|
|
|
|
|
|
if diff.into_num() > 0 {
|
2018-01-03 04:03:44 +03:00
|
|
|
if let Some(peer) = self.get_connected_peer(&addr) {
|
2017-12-12 19:40:26 +03:00
|
|
|
let mut peer = peer.write().unwrap();
|
|
|
|
peer.info.total_difficulty = diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|