Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
[sync]: rust 2018 (#11067)
Browse files Browse the repository at this point in the history
* [sync]: rust 2018

* fix(grumble): explicit use for RlpResponseResult

* fix(grumble): types -> common_types

* fix: bad rebase

* fix: wildcard import

* fix(grumble): rename crate hash to `keccak_hash`
  • Loading branch information
niklasad1 authored Sep 19, 2019
1 parent b6415c6 commit 19184e8
Show file tree
Hide file tree
Showing 24 changed files with 404 additions and 384 deletions.
9 changes: 5 additions & 4 deletions ethcore/sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ name = "ethcore-sync"
version = "1.12.0"
license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"

[lib]

[dependencies]
bytes = { package = "parity-bytes", version = "0.1" }
client-traits = { path = "../client-traits" }
common-types = { path = "../types" }
devp2p = { package = "ethcore-network-devp2p", path = "../../util/network-devp2p" }
enum_primitive = "0.1.1"
ethcore-io = { path = "../../util/io" }
ethcore-light = { path = "../light" }
ethcore-network = { path = "../../util/network" }
ethcore-network-devp2p = { path = "../../util/network-devp2p" }
ethcore-private-tx = { path = "../private-tx" }
ethereum-types = "0.6.0"
ethkey = { path = "../../accounts/ethkey" }
fastmap = { path = "../../util/fastmap" }
futures = "0.1"
keccak-hash = "0.2.0"
light = { package = "ethcore-light", path = "../light" }
log = "0.4"
macros = { path = "../../util/macros" }
parity-bytes = "0.1"
network = { package = "ethcore-network", path = "../../util/network" }
parity-runtime = { path = "../../util/runtime" }
parity-util-mem = "0.2.0"
parking_lot = "0.8"
Expand Down
66 changes: 37 additions & 29 deletions ethcore/sync/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,54 @@ use std::collections::{HashMap, BTreeMap};
use std::io;
use std::ops::RangeInclusive;
use std::time::Duration;
use std::net::{SocketAddr, AddrParseError};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::sync_io::NetSyncIo;
use crate::light_sync::{self, SyncInfo};
use crate::private_tx::PrivateTxHandler;
use crate::chain::{
sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket},
ChainSyncApi, SyncState, SyncStatus as EthSyncStatus, ETH_PROTOCOL_VERSION_62,
ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2,
PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4,
};

use bytes::Bytes;
use client_traits::{BlockChainClient, ChainNotify};
use devp2p::NetworkService;
use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error,
ConnectionFilter};
use network::client_version::ClientVersion;
use ethcore_io::TimerToken;
use ethcore_private_tx::PrivateStateDB;
use ethereum_types::{H256, H512, U256};
use ethkey::Secret;
use futures::sync::mpsc as futures_mpsc;
use futures::Stream;
use io::{TimerToken};
use ethkey::Secret;
use client_traits::{BlockChainClient, ChainNotify};
use snapshot::SnapshotService;
use ethcore_private_tx::PrivateStateDB;
use types::BlockNumber;
use sync_io::NetSyncIo;
use chain::{ChainSyncApi, SyncStatus as EthSyncStatus};
use std::net::{SocketAddr, AddrParseError};
use std::str::FromStr;
use parking_lot::{RwLock, Mutex};
use chain::{ETH_PROTOCOL_VERSION_63, ETH_PROTOCOL_VERSION_62,
PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_2, PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4, SyncState};
use chain::sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket};
use light::client::AsLightClient;
use light::Provider;
use light::net::{
self as light_net, LightProtocol, Params as LightParams,
Capabilities, Handler as LightHandler, EventContext, SampleStore,
};
use log::{trace, warn};
use macros::hash_map;
use network::{
client_version::ClientVersion,
NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error,
ConnectionFilter, IpFilter
};
use snapshot::SnapshotService;
use parking_lot::{RwLock, Mutex};
use parity_runtime::Executor;
use std::sync::atomic::{AtomicBool, Ordering};
use network::IpFilter;
use private_tx::PrivateTxHandler;
use types::{
use trace_time::trace_time;
use common_types::{
BlockNumber,
chain_notify::{NewBlocks, ChainMessageType},
pruning_info::PruningInfo,
transaction::UnverifiedTransaction,
};

use super::light_sync::SyncInfo;

/// Parity sync protocol
pub const WARP_SYNC_PROTOCOL_ID: ProtocolId = *b"par";
Expand Down Expand Up @@ -646,14 +654,14 @@ impl ChainNotify for EthSync {
struct TxRelay(Arc<dyn BlockChainClient>);

impl LightHandler for TxRelay {
fn on_transactions(&self, ctx: &dyn EventContext, relay: &[::types::transaction::UnverifiedTransaction]) {
fn on_transactions(&self, ctx: &dyn EventContext, relay: &[UnverifiedTransaction]) {
trace!(target: "pip", "Relaying {} transactions from peer {}", relay.len(), ctx.peer());
self.0.queue_transactions(relay.iter().map(|tx| ::rlp::encode(tx)).collect(), ctx.peer())
self.0.queue_transactions(relay.iter().map(|tx| rlp::encode(tx)).collect(), ctx.peer())
}
}

/// Trait for managing network
pub trait ManageNetwork : Send + Sync {
pub trait ManageNetwork: Send + Sync {
/// Set to allow unreserved peers to connect
fn accept_unreserved_peers(&self);
/// Set to deny unreserved peers to connect
Expand Down Expand Up @@ -945,15 +953,15 @@ impl LightSync {

}

impl ::std::ops::Deref for LightSync {
type Target = dyn (::light_sync::SyncInfo);
impl std::ops::Deref for LightSync {
type Target = dyn (light_sync::SyncInfo);

fn deref(&self) -> &Self::Target { &*self.sync }
}


impl LightNetworkDispatcher for LightSync {
fn with_context<F, T>(&self, f: F) -> Option<T> where F: FnOnce(&dyn (::light::net::BasicContext)) -> T {
fn with_context<F, T>(&self, f: F) -> Option<T> where F: FnOnce(&dyn (light::net::BasicContext)) -> T {
self.network.with_context_eval(
self.subprotocol_name,
move |ctx| self.proto.with_context(&ctx, f),
Expand Down
53 changes: 31 additions & 22 deletions ethcore/sync/src/block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@

use std::collections::{HashSet, VecDeque};
use std::cmp;
use parity_util_mem::MallocSizeOf;

use crate::{
blocks::{BlockCollection, SyncBody, SyncHeader},
chain::BlockSet,
sync_io::SyncIo
};

use ethereum_types::H256;
use rlp::{self, Rlp};
use types::{
use log::{debug, trace};
use network::{client_version::ClientCapabilities, PeerId};
use rlp::Rlp;
use parity_util_mem::MallocSizeOf;
use common_types::{
BlockNumber,
block_status::BlockStatus,
ids::BlockId,
errors::{EthcoreError, BlockError, ImportError},
};
use sync_io::SyncIo;
use blocks::{BlockCollection, SyncBody, SyncHeader};
use chain::BlockSet;
use network::PeerId;
use network::client_version::ClientCapabilities;

const MAX_HEADERS_TO_REQUEST: usize = 128;
const MAX_BODIES_TO_REQUEST_LARGE: usize = 128;
Expand Down Expand Up @@ -635,18 +639,23 @@ fn all_expected<A, B, F>(values: &[A], expected_values: &[B], is_expected: F) ->

#[cfg(test)]
mod tests {
use super::*;
use super::{
BlockSet, BlockDownloader, BlockDownloaderImportError, DownloadAction, SyncIo, H256,
MAX_HEADERS_TO_REQUEST, MAX_USELESS_HEADERS_PER_ROUND, SUBCHAIN_SIZE, State, Rlp, VecDeque
};

use crate::tests::{helpers::TestIo, snapshot::TestSnapshotService};

use ethcore::test_helpers::TestBlockChainClient;
use spec;
use ethkey::{Generator, Random};
use hash::keccak;
use ethkey::{Random, Generator};
use keccak_hash::keccak;
use parking_lot::RwLock;
use rlp::{encode_list, RlpStream};
use tests::helpers::TestIo;
use tests::snapshot::TestSnapshotService;
use types::transaction::{Transaction, SignedTransaction};
use triehash_ethereum::ordered_trie_root;
use types::header::Header as BlockHeader;
use common_types::{
transaction::{Transaction, SignedTransaction},
header::Header as BlockHeader,
};

fn dummy_header(number: u64, parent_hash: H256) -> BlockHeader {
let mut header = BlockHeader::new();
Expand Down Expand Up @@ -680,7 +689,7 @@ mod tests {

#[test]
fn import_headers_in_chain_head_state() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash();
Expand Down Expand Up @@ -752,7 +761,7 @@ mod tests {

#[test]
fn import_headers_in_blocks_state() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new();
Expand Down Expand Up @@ -802,7 +811,7 @@ mod tests {

#[test]
fn import_bodies() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new();
Expand Down Expand Up @@ -870,7 +879,7 @@ mod tests {

#[test]
fn import_receipts() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new();
Expand Down Expand Up @@ -929,7 +938,7 @@ mod tests {

#[test]
fn reset_after_multiple_sets_of_useless_headers() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash();
Expand Down Expand Up @@ -969,7 +978,7 @@ mod tests {

#[test]
fn dont_reset_after_multiple_sets_of_useless_headers_for_chain_head() {
::env_logger::try_init().ok();
env_logger::try_init().ok();

let spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash();
Expand Down
32 changes: 11 additions & 21 deletions ethcore/sync/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashSet, HashMap, hash_map};
use hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP};
use parity_util_mem::MallocSizeOf;
use ethereum_types::H256;
use triehash_ethereum::ordered_trie_root;

use bytes::Bytes;
use ethereum_types::H256;
use keccak_hash::{keccak, KECCAK_NULL_RLP, KECCAK_EMPTY_LIST_RLP};
use log::{trace, warn};
use parity_util_mem::MallocSizeOf;
use rlp::{Rlp, RlpStream, DecoderError};
use network;
use types::{
use triehash_ethereum::ordered_trie_root;
use common_types::{
transaction::UnverifiedTransaction,
header::Header as BlockHeader,
verification::Unverified,
Expand All @@ -40,7 +41,7 @@ pub struct SyncHeader {
impl SyncHeader {
pub fn from_rlp(bytes: Bytes) -> Result<Self, DecoderError> {
let result = SyncHeader {
header: ::rlp::decode(&bytes)?,
header: rlp::decode(&bytes)?,
bytes,
};

Expand Down Expand Up @@ -151,18 +152,7 @@ pub struct BlockCollection {
impl BlockCollection {
/// Create a new instance.
pub fn new(download_receipts: bool) -> BlockCollection {
BlockCollection {
need_receipts: download_receipts,
blocks: HashMap::new(),
header_ids: HashMap::new(),
receipt_ids: HashMap::new(),
heads: Vec::new(),
parents: HashMap::new(),
head: None,
downloading_headers: HashSet::new(),
downloading_bodies: HashSet::new(),
downloading_receipts: HashSet::new(),
}
Self { need_receipts: download_receipts, ..Default::default() }
}

/// Clear everything.
Expand Down Expand Up @@ -545,12 +535,12 @@ mod test {
use super::{BlockCollection, SyncHeader};
use client_traits::BlockChainClient;
use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith};
use types::{
use common_types::{
ids::BlockId,
BlockNumber,
verification::Unverified,
};
use rlp::*;
use rlp::Rlp;

fn is_empty(bc: &BlockCollection) -> bool {
bc.heads.is_empty() &&
Expand Down
Loading

0 comments on commit 19184e8

Please sign in to comment.