2016-10-25 07:35:10 +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.
|
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
use std::sync::{Mutex, Arc};
|
2016-10-29 22:36:45 +03:00
|
|
|
|
2016-12-11 06:11:49 +03:00
|
|
|
use futures;
|
2017-01-30 02:52:01 +03:00
|
|
|
use futures::Future;
|
2016-12-11 06:11:49 +03:00
|
|
|
use futures::stream;
|
2017-01-30 02:52:01 +03:00
|
|
|
use futures::sync::mpsc::UnboundedSender;
|
2016-12-11 06:11:49 +03:00
|
|
|
use tokio_core::net::TcpStream;
|
2016-10-29 22:36:45 +03:00
|
|
|
|
2016-11-01 20:42:33 +03:00
|
|
|
use core::core;
|
2017-01-30 02:52:01 +03:00
|
|
|
use core::core::hash::Hash;
|
2016-10-25 07:35:10 +03:00
|
|
|
use core::ser;
|
2017-02-02 06:05:17 +03:00
|
|
|
use conn::TimeoutConnection;
|
2016-10-26 08:06:13 +03:00
|
|
|
use msg::*;
|
|
|
|
use types::*;
|
2017-01-30 02:52:01 +03:00
|
|
|
use util::OneTime;
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
pub struct ProtocolV1 {
|
2017-02-02 06:05:17 +03:00
|
|
|
conn: OneTime<TimeoutConnection>,
|
2016-11-03 00:19:40 +03:00
|
|
|
|
2017-02-02 06:05:17 +03:00
|
|
|
expected_responses: Mutex<Vec<(Type, Hash)>>,
|
2016-10-31 22:29:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ProtocolV1 {
|
2017-02-02 06:05:17 +03:00
|
|
|
pub fn new() -> ProtocolV1 {
|
|
|
|
ProtocolV1 {
|
|
|
|
conn: OneTime::new(),
|
|
|
|
expected_responses: Mutex::new(vec![]),
|
|
|
|
}
|
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2016-10-29 22:36:45 +03:00
|
|
|
impl Protocol for ProtocolV1 {
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Sets up the protocol reading, writing and closing logic.
|
2016-12-11 06:11:49 +03:00
|
|
|
fn handle(&self,
|
|
|
|
conn: TcpStream,
|
2016-12-16 01:57:04 +03:00
|
|
|
adapter: Arc<NetAdapter>)
|
2017-02-27 07:08:40 +03:00
|
|
|
-> Box<Future<Item = (), Error = Error>> {
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2017-02-02 06:05:17 +03:00
|
|
|
let (conn, listener) = TimeoutConnection::listen(conn, move |sender, header, data| {
|
|
|
|
let adapt = adapter.as_ref();
|
|
|
|
handle_payload(adapt, sender, header, data)
|
|
|
|
});
|
2016-12-11 06:11:49 +03:00
|
|
|
|
2017-02-02 06:05:17 +03:00
|
|
|
self.conn.init(conn);
|
2016-12-12 00:04:52 +03:00
|
|
|
|
2017-02-02 06:05:17 +03:00
|
|
|
listener
|
2016-12-12 00:04:52 +03:00
|
|
|
}
|
|
|
|
|
2017-01-30 02:52:01 +03:00
|
|
|
/// Bytes sent and received.
|
2016-12-12 00:04:52 +03:00
|
|
|
fn transmitted_bytes(&self) -> (u64, u64) {
|
2017-02-02 06:05:17 +03:00
|
|
|
self.conn.borrow().transmitted_bytes()
|
2016-12-12 00:04:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a ping message to the remote peer. Will panic if handle has never
|
|
|
|
/// been called on this protocol.
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_ping(&self) -> Result<(), Error> {
|
2017-02-19 05:42:34 +03:00
|
|
|
self.send_request(Type::Ping, Type::Pong, &Empty {}, None)
|
2016-12-12 00:04:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes and sends a block to our remote peer
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_block(&self, b: &core::Block) -> Result<(), Error> {
|
2016-12-12 00:04:52 +03:00
|
|
|
self.send_msg(Type::Block, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Serializes and sends a transaction to our remote peer
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_transaction(&self, tx: &core::Transaction) -> Result<(), Error> {
|
2016-12-12 00:04:52 +03:00
|
|
|
self.send_msg(Type::Transaction, tx)
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_header_request(&self, locator: Vec<Hash>) -> Result<(), Error> {
|
2017-02-19 05:42:34 +03:00
|
|
|
self.send_request(Type::GetHeaders,
|
|
|
|
Type::Headers,
|
|
|
|
&Locator { hashes: locator },
|
|
|
|
None)
|
2017-02-08 00:52:17 +03:00
|
|
|
}
|
|
|
|
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_block_request(&self, h: Hash) -> Result<(), Error> {
|
2017-02-19 05:42:34 +03:00
|
|
|
self.send_request(Type::GetBlock, Type::Block, &h, Some(h))
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:08:40 +03:00
|
|
|
fn send_peer_request(&self, capab: Capabilities) -> Result<(), Error> {
|
2017-02-19 05:42:34 +03:00
|
|
|
self.send_request(Type::GetPeerAddrs,
|
|
|
|
Type::PeerAddrs,
|
|
|
|
&GetPeerAddrs { capabilities: capab },
|
|
|
|
None)
|
2017-02-08 00:52:17 +03:00
|
|
|
}
|
|
|
|
|
2016-12-12 00:04:52 +03:00
|
|
|
/// Close the connection to the remote peer
|
|
|
|
fn close(&self) {
|
|
|
|
// TODO some kind of shutdown signal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProtocolV1 {
|
2017-04-07 08:54:54 +03:00
|
|
|
fn send_msg<W: ser::Writeable>(&self, t: Type, body: &W) -> Result<(), Error> {
|
2017-02-02 06:05:17 +03:00
|
|
|
self.conn.borrow().send_msg(t, body)
|
|
|
|
}
|
2017-01-30 02:52:01 +03:00
|
|
|
|
2017-04-07 08:54:54 +03:00
|
|
|
fn send_request<W: ser::Writeable>(&self,
|
2017-02-02 06:05:17 +03:00
|
|
|
t: Type,
|
2017-02-19 05:42:34 +03:00
|
|
|
rt: Type,
|
2017-04-07 08:54:54 +03:00
|
|
|
body: &W,
|
2017-02-19 05:42:34 +03:00
|
|
|
expect_resp: Option<Hash>)
|
2017-02-27 07:08:40 +03:00
|
|
|
-> Result<(), Error> {
|
2017-02-19 05:42:34 +03:00
|
|
|
self.conn.borrow().send_request(t, rt, body, expect_resp)
|
2017-02-02 06:05:17 +03:00
|
|
|
}
|
2016-11-01 20:42:33 +03:00
|
|
|
}
|
2016-12-16 01:57:04 +03:00
|
|
|
|
2017-01-30 02:52:01 +03:00
|
|
|
fn handle_payload(adapter: &NetAdapter,
|
|
|
|
sender: UnboundedSender<Vec<u8>>,
|
|
|
|
header: MsgHeader,
|
|
|
|
buf: Vec<u8>)
|
2017-02-02 06:05:17 +03:00
|
|
|
-> Result<Option<Hash>, ser::Error> {
|
|
|
|
match header.msg_type {
|
|
|
|
Type::Ping => {
|
2017-02-08 00:52:17 +03:00
|
|
|
let data = ser::ser_vec(&MsgHeader::new(Type::Pong, 0))?;
|
2017-02-02 06:05:17 +03:00
|
|
|
sender.send(data);
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Type::Pong => Ok(None),
|
|
|
|
Type::Transaction => {
|
2017-02-08 00:52:17 +03:00
|
|
|
let tx = ser::deserialize::<core::Transaction>(&mut &buf[..])?;
|
2017-02-02 06:05:17 +03:00
|
|
|
adapter.transaction_received(tx);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-02-08 00:52:17 +03:00
|
|
|
Type::GetBlock => {
|
|
|
|
let h = ser::deserialize::<Hash>(&mut &buf[..])?;
|
|
|
|
let bo = adapter.get_block(h);
|
|
|
|
if let Some(b) = bo {
|
|
|
|
// serialize and send the block over
|
|
|
|
let mut body_data = vec![];
|
|
|
|
try!(ser::serialize(&mut body_data, &b));
|
|
|
|
let mut data = vec![];
|
|
|
|
try!(ser::serialize(&mut data,
|
|
|
|
&MsgHeader::new(Type::Block, body_data.len() as u64)));
|
|
|
|
data.append(&mut body_data);
|
|
|
|
sender.send(data);
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-02-02 06:05:17 +03:00
|
|
|
Type::Block => {
|
2017-02-08 00:52:17 +03:00
|
|
|
let b = ser::deserialize::<core::Block>(&mut &buf[..])?;
|
2017-02-02 06:05:17 +03:00
|
|
|
let bh = b.hash();
|
|
|
|
adapter.block_received(b);
|
|
|
|
Ok(Some(bh))
|
|
|
|
}
|
2017-02-08 00:52:17 +03:00
|
|
|
Type::GetHeaders => {
|
|
|
|
// load headers from the locator
|
|
|
|
let loc = ser::deserialize::<Locator>(&mut &buf[..])?;
|
|
|
|
let headers = adapter.locate_headers(loc.hashes);
|
|
|
|
|
|
|
|
// serialize and send all the headers over
|
|
|
|
let mut body_data = vec![];
|
|
|
|
try!(ser::serialize(&mut body_data, &Headers { headers: headers }));
|
|
|
|
let mut data = vec![];
|
|
|
|
try!(ser::serialize(&mut data,
|
|
|
|
&MsgHeader::new(Type::Headers, body_data.len() as u64)));
|
|
|
|
data.append(&mut body_data);
|
|
|
|
sender.send(data);
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Type::Headers => {
|
|
|
|
let headers = ser::deserialize::<Headers>(&mut &buf[..])?;
|
|
|
|
adapter.headers_received(headers.headers);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-02-19 05:42:34 +03:00
|
|
|
Type::GetPeerAddrs => {
|
|
|
|
let get_peers = ser::deserialize::<GetPeerAddrs>(&mut &buf[..])?;
|
|
|
|
let peer_addrs = adapter.find_peer_addrs(get_peers.capabilities);
|
|
|
|
|
|
|
|
// serialize and send all the headers over
|
|
|
|
let mut body_data = vec![];
|
|
|
|
try!(ser::serialize(&mut body_data,
|
|
|
|
&PeerAddrs {
|
|
|
|
peers: peer_addrs.iter().map(|sa| SockAddr(*sa)).collect(),
|
|
|
|
}));
|
|
|
|
let mut data = vec![];
|
|
|
|
try!(ser::serialize(&mut data,
|
|
|
|
&MsgHeader::new(Type::PeerAddrs, body_data.len() as u64)));
|
|
|
|
data.append(&mut body_data);
|
|
|
|
sender.send(data);
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
Type::PeerAddrs => {
|
|
|
|
let peer_addrs = ser::deserialize::<PeerAddrs>(&mut &buf[..])?;
|
|
|
|
adapter.peer_addrs_received(peer_addrs.peers.iter().map(|pa| pa.0).collect());
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-02-02 06:05:17 +03:00
|
|
|
_ => {
|
|
|
|
debug!("unknown message type {:?}", header.msg_type);
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 01:57:04 +03:00
|
|
|
}
|