2016-10-24 00:02:02 +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.
|
|
|
|
|
|
|
|
//! Grin server implementation, accepts incoming connections and connects to
|
2016-10-25 07:35:10 +03:00
|
|
|
//! other peers in the network.
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-11-06 02:31:45 +03:00
|
|
|
use rand::{self, Rng};
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::cell::RefCell;
|
2016-10-26 08:06:13 +03:00
|
|
|
use std::io;
|
2016-11-30 05:49:03 +03:00
|
|
|
use std::net::{SocketAddr, ToSocketAddrs};
|
2016-10-30 18:24:19 +03:00
|
|
|
use std::ops::Deref;
|
2016-10-26 08:06:13 +03:00
|
|
|
use std::str::FromStr;
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
2016-10-26 08:06:13 +03:00
|
|
|
|
|
|
|
use mioco;
|
2016-10-31 04:23:52 +03:00
|
|
|
use mioco::sync::mpsc::{sync_channel, SyncSender};
|
2016-10-28 00:28:02 +03:00
|
|
|
use mioco::tcp::{TcpListener, TcpStream};
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
use core::core;
|
2016-10-28 00:28:02 +03:00
|
|
|
use core::ser::Error;
|
2016-10-26 08:06:13 +03:00
|
|
|
use handshake::Handshake;
|
2016-10-29 22:36:45 +03:00
|
|
|
use peer::Peer;
|
2016-10-26 08:06:13 +03:00
|
|
|
use types::*;
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
pub struct DummyAdapter {}
|
2016-11-03 00:19:40 +03:00
|
|
|
impl NetAdapter for DummyAdapter {
|
|
|
|
fn transaction_received(&self, tx: core::Transaction) {}
|
|
|
|
fn block_received(&self, b: core::Block) {}
|
|
|
|
}
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
/// P2P server implementation, handling bootstrapping to find and connect to
|
|
|
|
/// peers, receiving connections from other peers and keep track of all of them.
|
2016-10-24 00:02:02 +03:00
|
|
|
pub struct Server {
|
2016-11-30 05:49:03 +03:00
|
|
|
config: P2PConfig,
|
2016-10-31 04:23:52 +03:00
|
|
|
peers: RwLock<Vec<Arc<Peer>>>,
|
|
|
|
stop_send: RefCell<Option<SyncSender<u8>>>,
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
unsafe impl Sync for Server {}
|
|
|
|
unsafe impl Send for Server {}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
// TODO TLS
|
2016-10-24 00:02:02 +03:00
|
|
|
impl Server {
|
2016-11-01 20:42:33 +03:00
|
|
|
/// Creates a new idle p2p server with no peers
|
2016-11-30 05:49:03 +03:00
|
|
|
pub fn new(config: P2PConfig) -> Server {
|
2016-10-31 04:23:52 +03:00
|
|
|
Server {
|
2016-11-30 05:49:03 +03:00
|
|
|
config: config,
|
2016-10-31 04:23:52 +03:00
|
|
|
peers: RwLock::new(Vec::new()),
|
|
|
|
stop_send: RefCell::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Starts the p2p server. Opens a TCP port to allow incoming
|
2016-10-24 00:02:02 +03:00
|
|
|
/// connections and starts the bootstrapping process to find peers.
|
2016-10-31 04:23:52 +03:00
|
|
|
pub fn start(&self) -> Result<(), Error> {
|
2016-11-30 05:49:03 +03:00
|
|
|
let addr = SocketAddr::new(self.config.host, self.config.port);
|
2016-10-31 04:23:52 +03:00
|
|
|
let listener = try!(TcpListener::bind(&addr).map_err(&Error::IOErr));
|
|
|
|
warn!("P2P server started on {}", addr);
|
2016-10-24 00:02:02 +03:00
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
let hs = Arc::new(Handshake::new());
|
|
|
|
let (stop_send, stop_recv) = sync_channel(1);
|
|
|
|
{
|
|
|
|
let mut stop_mut = self.stop_send.borrow_mut();
|
|
|
|
*stop_mut = Some(stop_send);
|
|
|
|
}
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
loop {
|
|
|
|
select!(
|
|
|
|
r:listener => {
|
|
|
|
let conn = try!(listener.accept().map_err(&Error::IOErr));
|
|
|
|
let hs = hs.clone();
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-11-30 05:49:03 +03:00
|
|
|
let peer = try!(Peer::accept(conn, &hs));
|
2016-10-29 22:36:45 +03:00
|
|
|
let wpeer = Arc::new(peer);
|
2016-10-31 04:23:52 +03:00
|
|
|
{
|
|
|
|
let mut peers = self.peers.write().unwrap();
|
|
|
|
peers.push(wpeer.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
mioco::spawn(move || -> io::Result<()> {
|
2016-11-01 20:42:33 +03:00
|
|
|
if let Err(err) = wpeer.run(&DummyAdapter{}) {
|
2016-10-31 04:23:52 +03:00
|
|
|
error!("{:?}", err);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
},
|
|
|
|
r:stop_recv => {
|
|
|
|
stop_recv.recv();
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-10-28 00:28:02 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 05:49:03 +03:00
|
|
|
pub fn connect_peer<A: ToSocketAddrs>(&self, addr: A) -> Result<(), Error> {
|
|
|
|
for sock_addr in addr.to_socket_addrs().unwrap() {
|
|
|
|
info!("Connecting to peer {}", sock_addr);
|
|
|
|
let tcp_client = TcpStream::connect(&sock_addr).unwrap();
|
|
|
|
let peer = try!(Peer::connect(tcp_client, &Handshake::new())
|
|
|
|
.map_err(|_| io::Error::last_os_error()));
|
|
|
|
|
|
|
|
let peer = Arc::new(peer);
|
|
|
|
let in_peer = peer.clone();
|
|
|
|
mioco::spawn(move || -> io::Result<()> {
|
|
|
|
in_peer.run(&DummyAdapter {});
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
self.peers.write().unwrap().push(peer);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
/// Asks all the peers to relay the provided block. A peer may choose to
|
|
|
|
/// ignore the relay request if it has knowledge that the remote peer
|
|
|
|
/// already knows the block.
|
|
|
|
pub fn relay_block(&self, b: &core::Block) -> Result<(), Error> {
|
|
|
|
let peers = self.peers.write().unwrap();
|
|
|
|
for p in peers.deref() {
|
|
|
|
try!(p.send_block(b));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Asks all the peers to relay the provided transaction. A peer may choose
|
|
|
|
/// to ignore the relay request if it has knowledge that the remote peer
|
|
|
|
/// already knows the transaction.
|
|
|
|
pub fn relay_transaction(&self, tx: &core::Transaction) -> Result<(), Error> {
|
|
|
|
let peers = self.peers.write().unwrap();
|
|
|
|
for p in peers.deref() {
|
|
|
|
try!(p.send_transaction(tx));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-11-06 02:31:45 +03:00
|
|
|
/// Number of peers this server is connected to.
|
|
|
|
pub fn peers_count(&self) -> u32 {
|
|
|
|
self.peers.read().unwrap().len() as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a random peer from our set of connected peers.
|
|
|
|
pub fn get_any_peer(&self) -> Arc<Peer> {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let peers = self.peers.read().unwrap();
|
|
|
|
peers[rng.gen_range(0, peers.len())].clone()
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
/// Stops the server. Disconnect from all peers at the same time.
|
2016-10-31 04:23:52 +03:00
|
|
|
pub fn stop(&self) {
|
|
|
|
let peers = self.peers.write().unwrap();
|
|
|
|
for p in peers.deref() {
|
|
|
|
p.stop();
|
|
|
|
}
|
|
|
|
let stop_send = self.stop_send.borrow();
|
|
|
|
stop_send.as_ref().unwrap().send(0);
|
|
|
|
}
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|