2018-01-30 17:42:04 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2016-10-25 07:35:10 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::env;
|
|
|
|
use std::fs::File;
|
2017-11-21 17:24:29 +03:00
|
|
|
use std::net::SocketAddr;
|
2018-02-10 01:32:16 +03:00
|
|
|
use std::sync::Arc;
|
2016-10-29 22:36:45 +03:00
|
|
|
|
2018-06-14 15:16:14 +03:00
|
|
|
use conn::{Message, MessageHandler, Response};
|
2016-11-01 20:42:33 +03:00
|
|
|
use core::core;
|
2018-01-30 17:42:04 +03:00
|
|
|
use core::core::hash::{Hash, Hashed};
|
2018-06-14 15:16:14 +03:00
|
|
|
use msg::{BanReason, GetPeerAddrs, Headers, Locator, PeerAddrs, Ping, Pong, SockAddr,
|
|
|
|
TxHashSetArchive, TxHashSetRequest, Type};
|
|
|
|
use rand::{self, Rng};
|
|
|
|
use types::{Error, NetAdapter};
|
2017-10-12 19:56:44 +03:00
|
|
|
use util::LOGGER;
|
2016-10-25 07:35:10 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
pub struct Protocol {
|
|
|
|
adapter: Arc<NetAdapter>,
|
|
|
|
addr: SocketAddr,
|
2016-10-31 22:29:08 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
impl Protocol {
|
|
|
|
pub fn new(adapter: Arc<NetAdapter>, addr: SocketAddr) -> Protocol {
|
2018-03-04 03:19:54 +03:00
|
|
|
Protocol { adapter, addr }
|
2017-02-02 06:05:17 +03:00
|
|
|
}
|
2016-10-25 07:35:10 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
impl MessageHandler for Protocol {
|
2018-02-10 01:32:16 +03:00
|
|
|
fn consume<'a>(&self, mut msg: Message<'a>) -> Result<Option<Response<'a>>, Error> {
|
2018-02-02 05:03:12 +03:00
|
|
|
let adapter = &self.adapter;
|
2018-01-30 17:42:04 +03:00
|
|
|
|
2018-03-27 19:09:41 +03:00
|
|
|
// If we received a msg from a banned peer then log and drop it.
|
|
|
|
// If we are getting a lot of these then maybe we are not cleaning
|
|
|
|
// banned peers up correctly?
|
|
|
|
if adapter.is_banned(self.addr.clone()) {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"handler: consume: peer {:?} banned, received: {:?}, dropping.",
|
|
|
|
self.addr,
|
|
|
|
msg.header.msg_type,
|
|
|
|
);
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
match msg.header.msg_type {
|
|
|
|
Type::Ping => {
|
|
|
|
let ping: Ping = msg.body()?;
|
|
|
|
adapter.peer_difficulty(self.addr, ping.total_difficulty, ping.height);
|
2017-02-08 00:52:17 +03:00
|
|
|
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(Some(msg.respond(
|
|
|
|
Type::Pong,
|
|
|
|
Pong {
|
|
|
|
total_difficulty: adapter.total_difficulty(),
|
|
|
|
height: adapter.total_height(),
|
|
|
|
},
|
|
|
|
)))
|
2018-02-02 05:03:12 +03:00
|
|
|
}
|
2018-01-31 23:39:55 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::Pong => {
|
|
|
|
let pong: Pong = msg.body()?;
|
|
|
|
adapter.peer_difficulty(self.addr, pong.total_difficulty, pong.height);
|
|
|
|
Ok(None)
|
2018-03-04 03:19:54 +03:00
|
|
|
}
|
2017-12-14 15:19:43 +03:00
|
|
|
|
2018-05-29 05:45:31 +03:00
|
|
|
Type::BanReason => {
|
|
|
|
let ban_reason: BanReason = msg.body()?;
|
|
|
|
error!(LOGGER, "handle_payload: BanReason {:?}", ban_reason);
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::Transaction => {
|
|
|
|
let tx: core::Transaction = msg.body()?;
|
2018-03-20 06:18:54 +03:00
|
|
|
adapter.transaction_received(tx, false);
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
Type::StemTransaction => {
|
|
|
|
let tx: core::Transaction = msg.body()?;
|
|
|
|
adapter.transaction_received(tx, true);
|
2018-02-02 05:03:12 +03:00
|
|
|
Ok(None)
|
2017-12-14 15:19:43 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::GetBlock => {
|
|
|
|
let h: Hash = msg.body()?;
|
2018-03-25 19:41:12 +03:00
|
|
|
trace!(LOGGER, "handle_payload: GetBlock {}", h);
|
2018-01-30 17:42:04 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
let bo = adapter.get_block(h);
|
|
|
|
if let Some(b) = bo {
|
2018-02-10 01:32:16 +03:00
|
|
|
return Ok(Some(msg.respond(Type::Block, b)));
|
2017-12-14 15:19:43 +03:00
|
|
|
}
|
2018-02-02 05:03:12 +03:00
|
|
|
Ok(None)
|
2017-02-08 00:52:17 +03:00
|
|
|
}
|
2018-01-31 23:39:55 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::Block => {
|
|
|
|
let b: core::Block = msg.body()?;
|
|
|
|
let bh = b.hash();
|
2018-01-31 23:39:55 +03:00
|
|
|
|
2018-03-25 19:41:12 +03:00
|
|
|
trace!(LOGGER, "handle_payload: Block {}", bh);
|
2018-01-31 23:39:55 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
adapter.block_received(b, self.addr);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2018-02-07 19:26:52 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::GetCompactBlock => {
|
|
|
|
let h: Hash = msg.body()?;
|
|
|
|
debug!(LOGGER, "handle_payload: GetCompactBlock: {}", h);
|
|
|
|
|
|
|
|
if let Some(b) = adapter.get_block(h) {
|
|
|
|
let cb = b.as_compact_block();
|
|
|
|
|
|
|
|
// serialize and send the block over in compact representation
|
|
|
|
|
|
|
|
// if we have txs in the block send a compact block
|
|
|
|
// but if block is empty -
|
|
|
|
// to allow us to test all code paths, randomly choose to send
|
|
|
|
// either the block or the compact block
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
|
|
|
if cb.kern_ids.is_empty() && rng.gen() {
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"handle_payload: GetCompactBlock: empty block, sending full block",
|
2018-03-04 03:19:54 +03:00
|
|
|
);
|
2018-02-02 05:03:12 +03:00
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(Some(msg.respond(Type::Block, b)))
|
2018-02-02 05:03:12 +03:00
|
|
|
} else {
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(Some(msg.respond(Type::CompactBlock, cb)))
|
2018-02-02 05:03:12 +03:00
|
|
|
}
|
2018-01-31 23:39:55 +03:00
|
|
|
} else {
|
2018-02-02 05:03:12 +03:00
|
|
|
Ok(None)
|
2018-01-31 23:39:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::CompactBlock => {
|
|
|
|
let b: core::CompactBlock = msg.body()?;
|
|
|
|
let bh = b.hash();
|
|
|
|
debug!(LOGGER, "handle_payload: CompactBlock: {}", bh);
|
2017-02-08 00:52:17 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
adapter.compact_block_received(b, self.addr);
|
|
|
|
Ok(None)
|
2017-12-14 15:19:43 +03:00
|
|
|
}
|
2017-02-08 00:52:17 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::GetHeaders => {
|
|
|
|
// load headers from the locator
|
|
|
|
let loc: Locator = msg.body()?;
|
|
|
|
let headers = adapter.locate_headers(loc.hashes);
|
2018-01-30 17:42:04 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
// serialize and send all the headers over
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(Some(msg.respond(
|
|
|
|
Type::Headers,
|
|
|
|
Headers { headers: headers },
|
|
|
|
)))
|
2018-02-02 05:03:12 +03:00
|
|
|
}
|
2018-01-30 17:42:04 +03:00
|
|
|
|
2018-02-07 19:26:52 +03:00
|
|
|
// "header first" block propagation - if we have not yet seen this block
|
|
|
|
// we can go request it from some of our peers
|
|
|
|
Type::Header => {
|
|
|
|
let header: core::BlockHeader = msg.body()?;
|
|
|
|
|
|
|
|
adapter.header_received(header, self.addr);
|
|
|
|
|
|
|
|
// we do not return a hash here as we never request a single header
|
|
|
|
// a header will always arrive unsolicited
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::Headers => {
|
|
|
|
let headers: Headers = msg.body()?;
|
|
|
|
adapter.headers_received(headers.headers, self.addr);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2018-01-30 17:42:04 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::GetPeerAddrs => {
|
|
|
|
let get_peers: GetPeerAddrs = msg.body()?;
|
|
|
|
let peer_addrs = adapter.find_peer_addrs(get_peers.capabilities);
|
2018-03-04 03:19:54 +03:00
|
|
|
Ok(Some(msg.respond(
|
|
|
|
Type::PeerAddrs,
|
|
|
|
PeerAddrs {
|
|
|
|
peers: peer_addrs.iter().map(|sa| SockAddr(*sa)).collect(),
|
|
|
|
},
|
|
|
|
)))
|
2018-02-02 05:03:12 +03:00
|
|
|
}
|
2017-02-19 05:42:34 +03:00
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
Type::PeerAddrs => {
|
|
|
|
let peer_addrs: PeerAddrs = msg.body()?;
|
|
|
|
adapter.peer_addrs_received(peer_addrs.peers.iter().map(|pa| pa.0).collect());
|
|
|
|
Ok(None)
|
2017-12-14 15:19:43 +03:00
|
|
|
}
|
2017-02-19 05:42:34 +03:00
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
Type::TxHashSetRequest => {
|
|
|
|
let sm_req: TxHashSetRequest = msg.body()?;
|
2018-03-04 03:19:54 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2018-03-05 22:33:44 +03:00
|
|
|
"handle_payload: txhashset req for {} at {}", sm_req.hash, sm_req.height
|
2018-03-04 03:19:54 +03:00
|
|
|
);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
let txhashset = self.adapter.txhashset_read(sm_req.hash);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
if let Some(txhashset) = txhashset {
|
|
|
|
let file_sz = txhashset.reader.metadata()?.len();
|
2018-02-10 01:32:16 +03:00
|
|
|
let mut resp = msg.respond(
|
2018-03-05 22:33:44 +03:00
|
|
|
Type::TxHashSetArchive,
|
|
|
|
&TxHashSetArchive {
|
2018-02-10 01:32:16 +03:00
|
|
|
height: sm_req.height as u64,
|
|
|
|
hash: sm_req.hash,
|
|
|
|
bytes: file_sz,
|
2018-03-04 03:19:54 +03:00
|
|
|
},
|
|
|
|
);
|
2018-03-05 22:33:44 +03:00
|
|
|
resp.add_attachment(txhashset.reader);
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(Some(resp))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:33:44 +03:00
|
|
|
Type::TxHashSetArchive => {
|
|
|
|
let sm_arch: TxHashSetArchive = msg.body()?;
|
2018-03-04 03:19:54 +03:00
|
|
|
debug!(
|
|
|
|
LOGGER,
|
2018-07-02 02:23:24 +03:00
|
|
|
"handle_payload: txhashset archive for {} at {}",
|
2018-03-04 03:19:54 +03:00
|
|
|
sm_arch.hash,
|
|
|
|
sm_arch.height,
|
|
|
|
);
|
2018-02-10 01:32:16 +03:00
|
|
|
|
|
|
|
let mut tmp = env::temp_dir();
|
2018-03-05 22:33:44 +03:00
|
|
|
tmp.push("txhashset.zip");
|
2018-02-10 01:32:16 +03:00
|
|
|
{
|
|
|
|
let mut tmp_zip = File::create(tmp.clone())?;
|
2018-03-04 03:19:54 +03:00
|
|
|
msg.copy_attachment(sm_arch.bytes as usize, &mut tmp_zip)?;
|
2018-02-10 01:32:16 +03:00
|
|
|
tmp_zip.sync_all()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tmp_zip = File::open(tmp)?;
|
2018-03-05 22:33:44 +03:00
|
|
|
self.adapter.txhashset_write(
|
2018-03-04 03:19:54 +03:00
|
|
|
sm_arch.hash,
|
|
|
|
tmp_zip,
|
|
|
|
self.addr,
|
|
|
|
);
|
2018-04-06 20:14:50 +03:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
LOGGER,
|
|
|
|
"handle_payload: txhashset archive for {} at {}, DONE",
|
|
|
|
sm_arch.hash,
|
|
|
|
sm_arch.height
|
|
|
|
);
|
|
|
|
|
2018-02-10 01:32:16 +03:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2018-02-02 05:03:12 +03:00
|
|
|
_ => {
|
|
|
|
debug!(LOGGER, "unknown message type {:?}", msg.header.msg_type);
|
|
|
|
Ok(None)
|
|
|
|
}
|
2017-02-02 06:05:17 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-16 01:57:04 +03:00
|
|
|
}
|