Migrate network, primitives and rpc to the 2018 edition (#1710)

This commit is contained in:
Stanislav Tkach
2019-02-06 20:07:48 +02:00
committed by Gav Wood
parent 3a4dda7beb
commit e60be1ad12
43 changed files with 203 additions and 278 deletions
+11 -12
View File
@@ -4,6 +4,7 @@ name = "substrate-network"
version = "0.1.0"
license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[lib]
@@ -18,24 +19,22 @@ linked-hash-map = "0.5"
lru-cache = "0.1.1"
rustc-hex = "2.0"
rand = "0.6"
substrate-primitives = { path = "../../core/primitives" }
substrate-consensus-common = { path = "../../core/consensus/common" }
substrate-client = { path = "../../core/client" }
sr-primitives = { path = "../../core/sr-primitives" }
primitives = { package = "substrate-primitives", path = "../../core/primitives" }
consensus = { package = "substrate-consensus-common", path = "../../core/consensus/common" }
client = { package = "substrate-client", path = "../../core/client" }
runtime_primitives = { package = "sr-primitives", path = "../../core/sr-primitives" }
parity-codec = "3.0"
parity-codec-derive = "3.0"
substrate-network-libp2p = { path = "../../core/network-libp2p" }
network_libp2p = { package = "substrate-network-libp2p", path = "../../core/network-libp2p" }
tokio = "0.1.11"
env_logger = { version = "0.6", optional = true }
substrate-keyring = { path = "../../core/keyring", optional = true }
substrate-test-client = { path = "../../core/test-client", optional = true }
keyring = { package = "substrate-keyring", path = "../../core/keyring", optional = true }
test_client = { package = "substrate-test-client", path = "../../core/test-client", optional = true }
[dev-dependencies]
env_logger = { version = "0.6" }
substrate-keyring = { path = "../../core/keyring" }
substrate-test-client = { path = "../../core/test-client" }
keyring = { package = "substrate-keyring", path = "../../core/keyring" }
test_client = { package = "substrate-test-client", path = "../../core/test-client" }
[features]
default = []
test-helpers = ["env_logger", "substrate-keyring", "substrate-test-client"]
test-helpers = ["keyring", "test_client"]
+3 -2
View File
@@ -19,9 +19,10 @@ use std::cmp;
use std::ops::Range;
use std::collections::{HashMap, BTreeMap};
use std::collections::hash_map::Entry;
use log::trace;
use network_libp2p::NodeIndex;
use runtime_primitives::traits::{Block as BlockT, NumberFor, As};
use message;
use crate::message;
const MAX_PARALLEL_DOWNLOADS: u32 = 1;
@@ -194,7 +195,7 @@ impl<B: BlockT> BlockCollection<B> {
#[cfg(test)]
mod test {
use super::{BlockCollection, BlockData, BlockRangeState};
use message;
use crate::message;
use runtime_primitives::testing::{Block as RawBlock, ExtrinsicWrapper};
use primitives::H256;
+9 -8
View File
@@ -18,11 +18,12 @@
pub use network_libp2p::{NonReservedPeerMode, NetworkConfiguration, Secret};
use chain::Client;
use codec;
use on_demand::OnDemandService;
use bitflags::bitflags;
use crate::chain::Client;
use parity_codec;
use crate::on_demand::OnDemandService;
use runtime_primitives::traits::{Block as BlockT};
use service::{ExHashT, TransactionPool};
use crate::service::{ExHashT, TransactionPool};
use std::sync::Arc;
/// Service initialization parameters.
@@ -70,14 +71,14 @@ bitflags! {
}
}
impl codec::Encode for Roles {
fn encode_to<T: codec::Output>(&self, dest: &mut T) {
impl parity_codec::Encode for Roles {
fn encode_to<T: parity_codec::Output>(&self, dest: &mut T) {
dest.push_byte(self.bits())
}
}
impl codec::Decode for Roles {
fn decode<I: codec::Input>(input: &mut I) -> Option<Self> {
impl parity_codec::Decode for Roles {
fn decode<I: parity_codec::Input>(input: &mut I) -> Option<Self> {
Self::from_bits(input.read_byte()?)
}
}
@@ -19,14 +19,15 @@
use std::collections::{HashMap, HashSet};
use std::time::{Instant, Duration};
use log::{trace, debug};
use futures::sync::mpsc;
use rand::{self, seq::SliceRandom};
use lru_cache::LruCache;
use network_libp2p::NodeIndex;
use runtime_primitives::traits::{Block as BlockT, Hash, HashFor};
pub use message::generic::{Message, ConsensusMessage};
use protocol::Context;
use config::Roles;
pub use crate::message::generic::{Message, ConsensusMessage};
use crate::protocol::Context;
use crate::config::Roles;
// FIXME: Add additional spam/DoS attack protection: https://github.com/paritytech/substrate/issues/1115
const MESSAGE_LIFETIME: Duration = Duration::from_secs(120);
+1
View File
@@ -20,6 +20,7 @@
// https://github.com/paritytech/substrate/issues/1547
#![allow(deprecated)]
use error_chain::*;
use std::io::Error as IoError;
use network_libp2p::Error as NetworkError;
use client;
-29
View File
@@ -20,35 +20,6 @@
//! Substrate-specific P2P networking: synchronizing blocks, propagating BFT messages.
//! Allows attachment of an optional subprotocol for chain-specific requests.
#[macro_use]
extern crate crossbeam_channel;
extern crate linked_hash_map;
extern crate lru_cache;
extern crate parking_lot;
extern crate substrate_primitives as primitives;
extern crate substrate_client as client;
extern crate sr_primitives as runtime_primitives;
extern crate substrate_network_libp2p as network_libp2p;
extern crate substrate_consensus_common as consensus;
extern crate parity_codec as codec;
extern crate futures;
extern crate rustc_hex;
extern crate rand;
extern crate tokio;
#[macro_use] extern crate log;
#[macro_use] extern crate bitflags;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate parity_codec_derive;
#[cfg(test)]
extern crate env_logger;
#[cfg(any(test, feature = "test-helpers"))]
extern crate substrate_keyring as keyring;
#[cfg(any(test, feature = "test-helpers"))]
extern crate substrate_test_client as test_client;
mod service;
mod sync;
#[macro_use]
+5 -2
View File
@@ -16,8 +16,10 @@
//! Network packet message types. These get serialized and put into the lower level protocol payload.
use bitflags::bitflags;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT};
use codec::{Encode, Decode, Input, Output};
use parity_codec::{Encode, Decode, Input, Output};
use parity_codec_derive::{Encode, Decode};
pub use self::generic::{
BlockAnnounce, RemoteCallRequest, RemoteReadRequest,
RemoteHeaderRequest, RemoteHeaderResponse,
@@ -124,7 +126,8 @@ pub struct RemoteReadResponse {
/// Generic types.
pub mod generic {
use runtime_primitives::Justification;
use config::Roles;
use parity_codec_derive::{Encode, Decode};
use crate::config::Roles;
use super::{
BlockAttributes, RemoteCallResponse, RemoteReadResponse,
RequestId, Transactions, Direction
+8 -7
View File
@@ -16,10 +16,11 @@
//! On-demand requests service.
use codec::Encode;
use parity_codec::Encode;
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{Instant, Duration};
use log::trace;
use futures::{Async, Future, Poll};
use futures::sync::oneshot::{channel, Receiver, Sender as OneShotSender};
use linked_hash_map::LinkedHashMap;
@@ -28,10 +29,10 @@ use parking_lot::Mutex;
use client::{error::{Error as ClientError, ErrorKind as ClientErrorKind}};
use client::light::fetcher::{Fetcher, FetchChecker, RemoteHeaderRequest,
RemoteCallRequest, RemoteReadRequest, RemoteChangesRequest, ChangesProof};
use message;
use crate::message;
use network_libp2p::{Severity, NodeIndex};
use config::Roles;
use service::{NetworkChan, NetworkMsg};
use crate::config::Roles;
use crate::service::{NetworkChan, NetworkMsg};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, NumberFor};
/// Remote request timeout.
@@ -530,10 +531,10 @@ pub mod tests {
use client::{error::{ErrorKind as ClientErrorKind, Result as ClientResult}};
use client::light::fetcher::{Fetcher, FetchChecker, RemoteHeaderRequest,
RemoteCallRequest, RemoteReadRequest, RemoteChangesRequest, ChangesProof};
use config::Roles;
use message;
use crate::config::Roles;
use crate::message;
use network_libp2p::{NodeIndex, ProtocolId, Severity};
use service::{network_channel, NetworkPort, NetworkMsg};
use crate::service::{network_channel, NetworkPort, NetworkMsg};
use super::{REQUEST_TIMEOUT, OnDemand, OnDemandService};
use test_client::runtime::{changes_trie_config, Block, Header};
+15 -14
View File
@@ -14,30 +14,31 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use codec::Encode;
use crossbeam_channel::{self as channel, Receiver, Sender};
use parity_codec::Encode;
use crossbeam_channel::{self as channel, Receiver, Sender, select};
use network_libp2p::{NodeIndex, Severity};
use primitives::storage::StorageKey;
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{As, Block as BlockT, Header as HeaderT, NumberFor, Zero};
use consensus::import_queue::ImportQueue;
use message::{self, Message};
use message::generic::Message as GenericMessage;
use consensus_gossip::ConsensusGossip;
use on_demand::OnDemandService;
use specialization::NetworkSpecialization;
use sync::{ChainSync, Status as SyncStatus, SyncState};
use service::{NetworkChan, NetworkMsg, TransactionPool, ExHashT};
use config::{ProtocolConfig, Roles};
use crate::message::{self, Message};
use crate::message::generic::Message as GenericMessage;
use crate::consensus_gossip::ConsensusGossip;
use crate::on_demand::OnDemandService;
use crate::specialization::NetworkSpecialization;
use crate::sync::{ChainSync, Status as SyncStatus, SyncState};
use crate::service::{NetworkChan, NetworkMsg, TransactionPool, ExHashT};
use crate::config::{ProtocolConfig, Roles};
use rustc_hex::ToHex;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::sync::Arc;
use std::thread;
use std::time;
use std::cmp;
use chain::Client;
use log::{trace, debug, warn};
use crate::chain::Client;
use client::light::fetcher::ChangesProof;
use error;
use crate::error;
const REQUEST_TIMEOUT_SEC: u64 = 40;
const TICK_TIMEOUT: time::Duration = time::Duration::from_millis(1000);
@@ -134,7 +135,7 @@ pub struct PeerInfo<B: BlockT> {
/// Context for a network-specific handler.
pub trait Context<B: BlockT> {
/// Get a reference to the client.
fn client(&self) -> &::chain::Client<B>;
fn client(&self) -> &crate::chain::Client<B>;
/// Point out that a peer has been malign or irresponsible or appeared lazy.
fn report_peer(&mut self, who: NodeIndex, reason: Severity);
@@ -143,7 +144,7 @@ pub trait Context<B: BlockT> {
fn peer_info(&self, peer: NodeIndex) -> Option<PeerInfo<B>>;
/// Send a message to a peer.
fn send_message(&mut self, who: NodeIndex, data: ::message::Message<B>);
fn send_message(&mut self, who: NodeIndex, data: crate::message::Message<B>);
}
/// Protocol context.
+7 -6
View File
@@ -17,20 +17,21 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::{io, thread};
use log::{warn, debug, error, trace};
use futures::{Async, Future, Stream, stream, sync::oneshot};
use parking_lot::Mutex;
use network_libp2p::{ProtocolId, PeerId, NetworkConfiguration, NodeIndex, ErrorKind, Severity};
use network_libp2p::{start_service, parse_str_addr, Service as NetworkService, ServiceEvent as NetworkServiceEvent};
use network_libp2p::{Protocol as Libp2pProtocol, RegisteredProtocol};
use consensus::import_queue::{ImportQueue, Link};
use consensus_gossip::ConsensusGossip;
use protocol::{self, Context, Protocol, ProtocolMsg, ProtocolStatus, PeerInfo};
use codec::Decode;
use config::Params;
use crate::consensus_gossip::ConsensusGossip;
use crate::protocol::{self, Context, Protocol, ProtocolMsg, ProtocolStatus, PeerInfo};
use parity_codec::Decode;
use crate::config::Params;
use crossbeam_channel::{self as channel, Receiver, Sender, TryRecvError};
use error::Error;
use crate::error::Error;
use runtime_primitives::traits::{Block as BlockT, NumberFor};
use specialization::NetworkSpecialization;
use crate::specialization::NetworkSpecialization;
use tokio::prelude::task::AtomicTask;
use tokio::runtime::Runtime;
+4 -4
View File
@@ -16,9 +16,9 @@
//! Specializations of the substrate network protocol to allow more complex forms of communication.
use ::NodeIndex;
use crate::NodeIndex;
use runtime_primitives::traits::Block as BlockT;
use protocol::Context;
use crate::protocol::Context;
/// A specialization of the substrate network protocol. Handles events and sends messages.
pub trait NetworkSpecialization<B: BlockT>: Send + Sync + 'static {
@@ -26,13 +26,13 @@ pub trait NetworkSpecialization<B: BlockT>: Send + Sync + 'static {
fn status(&self) -> Vec<u8>;
/// Called when a peer successfully handshakes.
fn on_connect(&mut self, ctx: &mut Context<B>, who: NodeIndex, status: ::message::Status<B>);
fn on_connect(&mut self, ctx: &mut Context<B>, who: NodeIndex, status: crate::message::Status<B>);
/// Called when a peer is disconnected. If the peer ID is unknown, it should be ignored.
fn on_disconnect(&mut self, ctx: &mut Context<B>, who: NodeIndex);
/// Called when a network-specific message arrives.
fn on_message(&mut self, ctx: &mut Context<B>, who: NodeIndex, message: &mut Option<::message::Message<B>>);
fn on_message(&mut self, ctx: &mut Context<B>, who: NodeIndex, message: &mut Option<crate::message::Message<B>>);
/// Called on abort.
fn on_abort(&mut self) { }
+6 -5
View File
@@ -17,18 +17,19 @@
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};
use protocol::Context;
use log::{trace, debug};
use crate::protocol::Context;
use network_libp2p::{Severity, NodeIndex};
use client::{BlockStatus, ClientInfo};
use consensus::BlockOrigin;
use consensus::import_queue::{ImportQueue, IncomingBlock};
use client::error::Error as ClientError;
use blocks::BlockCollection;
use crate::blocks::BlockCollection;
use runtime_primitives::Justification;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor};
use runtime_primitives::generic::BlockId;
use message::{self, generic::Message as GenericMessage};
use config::Roles;
use crate::message::{self, generic::Message as GenericMessage};
use crate::config::Roles;
// Maximum blocks to request in a single packet.
const MAX_BLOCKS_TO_REQUEST: usize = 128;
@@ -790,7 +791,7 @@ impl<B: BlockT> ChainSync<B> {
/// Get block status, taking into account import queue.
fn block_status<B: BlockT>(
chain: &::chain::Client<B>,
chain: &crate::chain::Client<B>,
queue: &ImportQueue<B>,
hash: B::Hash) -> Result<BlockStatus, ClientError>
{
+10 -9
View File
@@ -26,28 +26,29 @@ use std::sync::Arc;
use std::thread;
use std::time::Duration;
use log::trace;
use client;
use client::block_builder::BlockBuilder;
use codec::{Decode, Encode};
use config::ProtocolConfig;
use parity_codec::{Decode, Encode};
use crate::config::ProtocolConfig;
use consensus::import_queue::{import_many_blocks, ImportQueue, ImportQueueStatus, IncomingBlock};
use consensus::import_queue::{Link, SharedBlockImport, SharedJustificationImport, Verifier};
use consensus::{Error as ConsensusError, ErrorKind as ConsensusErrorKind};
use consensus::{BlockOrigin, ForkChoiceStrategy, ImportBlock, JustificationImport};
use consensus_gossip::{ConsensusGossip, ConsensusMessage};
use crossbeam_channel::{self as channel, Sender};
use crate::consensus_gossip::{ConsensusGossip, ConsensusMessage};
use crossbeam_channel::{self as channel, Sender, select};
use futures::Future;
use futures::sync::{mpsc, oneshot};
use keyring::Keyring;
use network_libp2p::{NodeIndex, ProtocolId, Severity};
use parking_lot::Mutex;
use primitives::{H256, Ed25519AuthorityId};
use protocol::{Context, Protocol, ProtocolMsg, ProtocolStatus};
use crate::protocol::{Context, Protocol, ProtocolMsg, ProtocolStatus};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Digest, DigestItem, Header, Zero, NumberFor};
use runtime_primitives::Justification;
use service::{network_channel, NetworkChan, NetworkLink, NetworkMsg, NetworkPort, TransactionPool};
use specialization::NetworkSpecialization;
use crate::service::{network_channel, NetworkChan, NetworkLink, NetworkMsg, NetworkPort, TransactionPool};
use crate::specialization::NetworkSpecialization;
use test_client;
pub use test_client::runtime::{Block, Extrinsic, Hash, Transfer};
@@ -216,7 +217,7 @@ impl NetworkSpecialization<Block> for DummySpecialization {
vec![]
}
fn on_connect(&mut self, _ctx: &mut Context<Block>, _peer_id: NodeIndex, _status: ::message::Status<Block>) {
fn on_connect(&mut self, _ctx: &mut Context<Block>, _peer_id: NodeIndex, _status: crate::message::Status<Block>) {
}
fn on_disconnect(&mut self, _ctx: &mut Context<Block>, _peer_id: NodeIndex) {
@@ -226,7 +227,7 @@ impl NetworkSpecialization<Block> for DummySpecialization {
&mut self,
_ctx: &mut Context<Block>,
_peer_id: NodeIndex,
_message: &mut Option<::message::Message<Block>>,
_message: &mut Option<crate::message::Message<Block>>,
) {
}
}
+2 -2
View File
@@ -16,10 +16,10 @@
use client::backend::Backend;
use client::blockchain::HeaderBackend as BlockchainHeaderBackend;
use config::Roles;
use crate::config::Roles;
use consensus::BlockOrigin;
use network_libp2p::NodeIndex;
use sync::SyncState;
use crate::sync::SyncState;
use std::{thread, time};
use std::collections::HashSet;
use super::*;