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-10-29 22:36:45 +03:00
|
|
|
use std::cell::RefCell;
|
2016-12-11 06:11:49 +03:00
|
|
|
use std::net::SocketAddr;
|
2016-10-30 18:24:19 +03:00
|
|
|
use std::ops::Deref;
|
2016-10-29 22:36:45 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
2016-12-11 06:11:49 +03:00
|
|
|
use std::time::Duration;
|
2016-10-26 08:06:13 +03:00
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
use futures;
|
|
|
|
use futures::{Future, Stream};
|
|
|
|
use tokio_core::net::{TcpListener, TcpStream};
|
|
|
|
use tokio_core::reactor;
|
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-12-11 06:11:49 +03:00
|
|
|
peers: Arc<RwLock<Vec<Arc<Peer>>>>,
|
|
|
|
stop: RefCell<Option<futures::sync::oneshot::Sender<()>>>,
|
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-12-11 06:11:49 +03:00
|
|
|
peers: Arc::new(RwLock::new(Vec::new())),
|
|
|
|
stop: RefCell::new(None),
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2016-10-31 04:23:52 +03:00
|
|
|
/// 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-12-11 06:11:49 +03:00
|
|
|
pub fn start(&self, h: reactor::Handle) -> Box<Future<Item = (), Error = Error>> {
|
2016-11-30 05:49:03 +03:00
|
|
|
let addr = SocketAddr::new(self.config.host, self.config.port);
|
2016-12-11 06:11:49 +03:00
|
|
|
let socket = TcpListener::bind(&addr, &h.clone()).unwrap();
|
2016-10-31 04:23:52 +03:00
|
|
|
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());
|
2016-12-11 06:11:49 +03:00
|
|
|
let peers = self.peers.clone();
|
|
|
|
|
|
|
|
// main peer acceptance future handling handshake
|
|
|
|
let hp = h.clone();
|
|
|
|
let peers = socket.incoming().map_err(|e| Error::IOErr(e)).map(move |(conn, addr)| {
|
|
|
|
let peers = peers.clone();
|
|
|
|
// accept the peer and add it to the server map
|
|
|
|
let peer_accept = Peer::accept(conn, &hs.clone()).map(move |(conn, peer)| {
|
|
|
|
let apeer = Arc::new(peer);
|
|
|
|
let mut peers = peers.write().unwrap();
|
|
|
|
peers.push(apeer.clone());
|
|
|
|
Ok((conn, apeer))
|
2016-11-30 05:49:03 +03:00
|
|
|
});
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
// wire in a future to timeout the accept after 5 secs
|
|
|
|
let timeout = reactor::Timeout::new(Duration::new(5, 0), &hp).unwrap();
|
|
|
|
let timed_peer = peer_accept.select(timeout.map(Err).map_err(|e| Error::IOErr(e)))
|
|
|
|
.then(|res| {
|
|
|
|
match res {
|
|
|
|
Ok((Ok((conn, p)), _timeout)) => Ok((conn, p)),
|
|
|
|
Ok((_, _accept)) => Err(Error::TooLargeReadErr),
|
|
|
|
Err((e, _other)) => Err(e),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// run the main peer protocol
|
|
|
|
timed_peer.and_then(|(conn, peer)| peer.clone().run(conn, &DummyAdapter {}))
|
|
|
|
});
|
|
|
|
|
|
|
|
// spawn each peer future to its own task
|
|
|
|
let hs = h.clone();
|
|
|
|
let server = peers.for_each(move |peer| {
|
|
|
|
hs.spawn(peer.then(|res| {
|
|
|
|
match res {
|
|
|
|
Err(e) => info!("Client error: {}", e),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
futures::finished(())
|
|
|
|
}));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
// setup the stopping oneshot on the server and join it with the peer future
|
|
|
|
let (stop, stop_rx) = futures::sync::oneshot::channel();
|
|
|
|
{
|
|
|
|
let mut stop_mut = self.stop.borrow_mut();
|
|
|
|
*stop_mut = Some(stop);
|
2016-11-01 20:42:33 +03:00
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
Box::new(server.select(stop_rx.map_err(|_| Error::CorruptedData)).then(|res| {
|
|
|
|
match res {
|
|
|
|
Ok((_, _)) => Ok(()),
|
|
|
|
Err((e, _)) => Err(e),
|
|
|
|
}
|
|
|
|
}))
|
2016-11-06 02:31:45 +03:00
|
|
|
}
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn connect_peer(&self,
|
|
|
|
addr: SocketAddr,
|
|
|
|
h: reactor::Handle)
|
|
|
|
-> Box<Future<Item = (), Error = Error>> {
|
|
|
|
let socket = TcpStream::connect(&addr, &h).map_err(|e| Error::IOErr(e));
|
|
|
|
let peers = self.peers.clone();
|
|
|
|
let request = socket.and_then(move |socket| {
|
|
|
|
let peers = peers.clone();
|
|
|
|
Peer::connect(socket, &Handshake::new()).map(move |(conn, peer)| {
|
|
|
|
let apeer = Arc::new(peer);
|
|
|
|
let mut peers = peers.write().unwrap();
|
|
|
|
peers.push(apeer.clone());
|
|
|
|
(conn, apeer)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.and_then(|(socket, peer)| peer.run(socket, &DummyAdapter {}));
|
|
|
|
Box::new(request)
|
2016-11-06 02:31:45 +03:00
|
|
|
}
|
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
/// Stops the server. Disconnect from all peers at the same time.
|
2016-12-11 06:11:49 +03:00
|
|
|
pub fn stop(self) {
|
2016-10-31 04:23:52 +03:00
|
|
|
let peers = self.peers.write().unwrap();
|
|
|
|
for p in peers.deref() {
|
|
|
|
p.stop();
|
|
|
|
}
|
2016-12-11 06:11:49 +03:00
|
|
|
self.stop.into_inner().unwrap().complete(());
|
2016-10-31 04:23:52 +03:00
|
|
|
}
|
2016-10-24 00:02:02 +03:00
|
|
|
}
|