[sync]: rust 2018 (#11067)

* [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`
This commit is contained in:
Niklas Adolfsson 2019-09-19 13:12:07 +02:00 committed by GitHub
parent b6415c6196
commit 19184e8529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 413 additions and 393 deletions

View File

@ -4,26 +4,27 @@ name = "ethcore-sync"
version = "1.12.0" version = "1.12.0"
license = "GPL-3.0" license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[lib] [lib]
[dependencies] [dependencies]
bytes = { package = "parity-bytes", version = "0.1" }
client-traits = { path = "../client-traits" } client-traits = { path = "../client-traits" }
common-types = { path = "../types" } common-types = { path = "../types" }
devp2p = { package = "ethcore-network-devp2p", path = "../../util/network-devp2p" }
enum_primitive = "0.1.1" enum_primitive = "0.1.1"
ethcore-io = { path = "../../util/io" } 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" } ethcore-private-tx = { path = "../private-tx" }
ethereum-types = "0.6.0" ethereum-types = "0.6.0"
ethkey = { path = "../../accounts/ethkey" } ethkey = { path = "../../accounts/ethkey" }
fastmap = { path = "../../util/fastmap" } fastmap = { path = "../../util/fastmap" }
futures = "0.1" futures = "0.1"
keccak-hash = "0.2.0" keccak-hash = "0.2.0"
light = { package = "ethcore-light", path = "../light" }
log = "0.4" log = "0.4"
macros = { path = "../../util/macros" } macros = { path = "../../util/macros" }
parity-bytes = "0.1" network = { package = "ethcore-network", path = "../../util/network" }
parity-runtime = { path = "../../util/runtime" } parity-runtime = { path = "../../util/runtime" }
parity-util-mem = "0.2.0" parity-util-mem = "0.2.0"
parking_lot = "0.8" parking_lot = "0.8"

View File

@ -19,46 +19,54 @@ use std::collections::{HashMap, BTreeMap};
use std::io; use std::io;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
use std::time::Duration; use std::time::Duration;
use bytes::Bytes;
use devp2p::NetworkService;
use network::{NetworkProtocolHandler, NetworkContext, PeerId, ProtocolId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode, Error,
ConnectionFilter};
use network::client_version::ClientVersion;
use ethereum_types::{H256, H512, U256};
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::net::{SocketAddr, AddrParseError};
use std::str::FromStr; use std::str::FromStr;
use parking_lot::{RwLock, Mutex}; use std::sync::atomic::{AtomicBool, Ordering};
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 crate::sync_io::NetSyncIo;
use chain::sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket}; 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 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 light::client::AsLightClient; use light::client::AsLightClient;
use light::Provider; use light::Provider;
use light::net::{ use light::net::{
self as light_net, LightProtocol, Params as LightParams, self as light_net, LightProtocol, Params as LightParams,
Capabilities, Handler as LightHandler, EventContext, SampleStore, 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 parity_runtime::Executor;
use std::sync::atomic::{AtomicBool, Ordering}; use trace_time::trace_time;
use network::IpFilter; use common_types::{
use private_tx::PrivateTxHandler; BlockNumber,
use types::{
chain_notify::{NewBlocks, ChainMessageType}, chain_notify::{NewBlocks, ChainMessageType},
pruning_info::PruningInfo, pruning_info::PruningInfo,
transaction::UnverifiedTransaction, transaction::UnverifiedTransaction,
}; };
use super::light_sync::SyncInfo;
/// Parity sync protocol /// Parity sync protocol
pub const WARP_SYNC_PROTOCOL_ID: ProtocolId = *b"par"; pub const WARP_SYNC_PROTOCOL_ID: ProtocolId = *b"par";
@ -646,14 +654,14 @@ impl ChainNotify for EthSync {
struct TxRelay(Arc<dyn BlockChainClient>); struct TxRelay(Arc<dyn BlockChainClient>);
impl LightHandler for TxRelay { 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()); 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 /// Trait for managing network
pub trait ManageNetwork : Send + Sync { pub trait ManageNetwork: Send + Sync {
/// Set to allow unreserved peers to connect /// Set to allow unreserved peers to connect
fn accept_unreserved_peers(&self); fn accept_unreserved_peers(&self);
/// Set to deny unreserved peers to connect /// Set to deny unreserved peers to connect
@ -945,15 +953,15 @@ impl LightSync {
} }
impl ::std::ops::Deref for LightSync { impl std::ops::Deref for LightSync {
type Target = dyn (::light_sync::SyncInfo); type Target = dyn (light_sync::SyncInfo);
fn deref(&self) -> &Self::Target { &*self.sync } fn deref(&self) -> &Self::Target { &*self.sync }
} }
impl LightNetworkDispatcher for LightSync { 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.network.with_context_eval(
self.subprotocol_name, self.subprotocol_name,
move |ctx| self.proto.with_context(&ctx, f), move |ctx| self.proto.with_context(&ctx, f),

View File

@ -20,20 +20,24 @@
use std::collections::{HashSet, VecDeque}; use std::collections::{HashSet, VecDeque};
use std::cmp; use std::cmp;
use parity_util_mem::MallocSizeOf;
use crate::{
blocks::{BlockCollection, SyncBody, SyncHeader},
chain::BlockSet,
sync_io::SyncIo
};
use ethereum_types::H256; use ethereum_types::H256;
use rlp::{self, Rlp}; use log::{debug, trace};
use types::{ use network::{client_version::ClientCapabilities, PeerId};
use rlp::Rlp;
use parity_util_mem::MallocSizeOf;
use common_types::{
BlockNumber, BlockNumber,
block_status::BlockStatus, block_status::BlockStatus,
ids::BlockId, ids::BlockId,
errors::{EthcoreError, BlockError, ImportError}, 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_HEADERS_TO_REQUEST: usize = 128;
const MAX_BODIES_TO_REQUEST_LARGE: usize = 128; const MAX_BODIES_TO_REQUEST_LARGE: usize = 128;
@ -635,18 +639,23 @@ fn all_expected<A, B, F>(values: &[A], expected_values: &[B], is_expected: F) ->
#[cfg(test)] #[cfg(test)]
mod tests { 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 ethcore::test_helpers::TestBlockChainClient;
use spec; use ethkey::{Random, Generator};
use ethkey::{Generator, Random}; use keccak_hash::keccak;
use hash::keccak;
use parking_lot::RwLock; use parking_lot::RwLock;
use rlp::{encode_list, RlpStream}; 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 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 { fn dummy_header(number: u64, parent_hash: H256) -> BlockHeader {
let mut header = BlockHeader::new(); let mut header = BlockHeader::new();
@ -680,7 +689,7 @@ mod tests {
#[test] #[test]
fn import_headers_in_chain_head_state() { fn import_headers_in_chain_head_state() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let spec = spec::new_test(); let spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash(); let genesis_hash = spec.genesis_header().hash();
@ -752,7 +761,7 @@ mod tests {
#[test] #[test]
fn import_headers_in_blocks_state() { fn import_headers_in_blocks_state() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut chain = TestBlockChainClient::new(); let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new(); let snapshot_service = TestSnapshotService::new();
@ -802,7 +811,7 @@ mod tests {
#[test] #[test]
fn import_bodies() { fn import_bodies() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut chain = TestBlockChainClient::new(); let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new(); let snapshot_service = TestSnapshotService::new();
@ -870,7 +879,7 @@ mod tests {
#[test] #[test]
fn import_receipts() { fn import_receipts() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut chain = TestBlockChainClient::new(); let mut chain = TestBlockChainClient::new();
let snapshot_service = TestSnapshotService::new(); let snapshot_service = TestSnapshotService::new();
@ -929,7 +938,7 @@ mod tests {
#[test] #[test]
fn reset_after_multiple_sets_of_useless_headers() { fn reset_after_multiple_sets_of_useless_headers() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let spec = spec::new_test(); let spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash(); let genesis_hash = spec.genesis_header().hash();
@ -969,7 +978,7 @@ mod tests {
#[test] #[test]
fn dont_reset_after_multiple_sets_of_useless_headers_for_chain_head() { 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 spec = spec::new_test();
let genesis_hash = spec.genesis_header().hash(); let genesis_hash = spec.genesis_header().hash();

View File

@ -15,14 +15,15 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::collections::{HashSet, HashMap, hash_map}; 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 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 rlp::{Rlp, RlpStream, DecoderError};
use network; use triehash_ethereum::ordered_trie_root;
use types::{ use common_types::{
transaction::UnverifiedTransaction, transaction::UnverifiedTransaction,
header::Header as BlockHeader, header::Header as BlockHeader,
verification::Unverified, verification::Unverified,
@ -40,7 +41,7 @@ pub struct SyncHeader {
impl SyncHeader { impl SyncHeader {
pub fn from_rlp(bytes: Bytes) -> Result<Self, DecoderError> { pub fn from_rlp(bytes: Bytes) -> Result<Self, DecoderError> {
let result = SyncHeader { let result = SyncHeader {
header: ::rlp::decode(&bytes)?, header: rlp::decode(&bytes)?,
bytes, bytes,
}; };
@ -151,18 +152,7 @@ pub struct BlockCollection {
impl BlockCollection { impl BlockCollection {
/// Create a new instance. /// Create a new instance.
pub fn new(download_receipts: bool) -> BlockCollection { pub fn new(download_receipts: bool) -> BlockCollection {
BlockCollection { Self { need_receipts: download_receipts, ..Default::default() }
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(),
}
} }
/// Clear everything. /// Clear everything.
@ -545,12 +535,12 @@ mod test {
use super::{BlockCollection, SyncHeader}; use super::{BlockCollection, SyncHeader};
use client_traits::BlockChainClient; use client_traits::BlockChainClient;
use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith}; use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith};
use types::{ use common_types::{
ids::BlockId, ids::BlockId,
BlockNumber, BlockNumber,
verification::Unverified, verification::Unverified,
}; };
use rlp::*; use rlp::Rlp;
fn is_empty(bc: &BlockCollection) -> bool { fn is_empty(bc: &BlockCollection) -> bool {
bc.heads.is_empty() && bc.heads.is_empty() &&

View File

@ -14,22 +14,38 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use api::WARP_SYNC_PROTOCOL_ID; use std::time::Instant;
use block_sync::{BlockDownloaderImportError as DownloaderImportError, DownloadAction}; use std::{mem, cmp};
use bytes::Bytes;
use enum_primitive::FromPrimitive;
use ethereum_types::{H256, U256};
use hash::keccak;
use network::PeerId;
use network::client_version::ClientVersion;
use rlp::Rlp;
use crate::{ use crate::{
snapshot_sync::ChunkType, snapshot_sync::ChunkType,
sync_io::SyncIo, sync_io::SyncIo,
api::WARP_SYNC_PROTOCOL_ID,
block_sync::{BlockDownloaderImportError as DownloaderImportError, DownloadAction},
chain::{
sync_packet::{
PacketInfo,
SyncPacket::{
self, BlockBodiesPacket, BlockHeadersPacket, NewBlockHashesPacket, NewBlockPacket,
PrivateStatePacket, PrivateTransactionPacket, ReceiptsPacket, SignedPrivateTransactionPacket,
SnapshotDataPacket, SnapshotManifestPacket, StatusPacket,
}
},
BlockSet, ChainSync, ForkConfirmation, PacketDecodeError, PeerAsking, PeerInfo, SyncRequester,
SyncState, ETH_PROTOCOL_VERSION_62, ETH_PROTOCOL_VERSION_63, MAX_NEW_BLOCK_AGE, MAX_NEW_HASHES,
PAR_PROTOCOL_VERSION_1, PAR_PROTOCOL_VERSION_3, PAR_PROTOCOL_VERSION_4,
}
}; };
use std::time::Instant;
use std::{mem, cmp}; use bytes::Bytes;
use types::{ use enum_primitive::FromPrimitive;
use ethereum_types::{H256, U256};
use keccak_hash::keccak;
use network::PeerId;
use network::client_version::ClientVersion;
use log::{debug, trace, error};
use rlp::Rlp;
use common_types::{
BlockNumber, BlockNumber,
block_status::BlockStatus, block_status::BlockStatus,
ids::BlockId, ids::BlockId,
@ -38,38 +54,6 @@ use types::{
snapshot::{ManifestData, RestorationStatus}, snapshot::{ManifestData, RestorationStatus},
}; };
use super::sync_packet::{PacketInfo, SyncPacket};
use super::sync_packet::SyncPacket::{
StatusPacket,
NewBlockHashesPacket,
BlockHeadersPacket,
BlockBodiesPacket,
NewBlockPacket,
ReceiptsPacket,
SnapshotManifestPacket,
SnapshotDataPacket,
PrivateTransactionPacket,
SignedPrivateTransactionPacket,
PrivateStatePacket,
};
use super::{
BlockSet,
ChainSync,
ForkConfirmation,
PacketDecodeError,
PeerAsking,
PeerInfo,
SyncRequester,
SyncState,
ETH_PROTOCOL_VERSION_62,
ETH_PROTOCOL_VERSION_63,
MAX_NEW_BLOCK_AGE,
MAX_NEW_HASHES,
PAR_PROTOCOL_VERSION_1,
PAR_PROTOCOL_VERSION_3,
PAR_PROTOCOL_VERSION_4,
};
/// The Chain Sync Handler: handles responses from peers /// The Chain Sync Handler: handles responses from peers
pub struct SyncHandler; pub struct SyncHandler;
@ -800,21 +784,19 @@ impl SyncHandler {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::VecDeque;
use super::{
super::tests::{dummy_sync_with_peer, get_dummy_block, get_dummy_blocks, get_dummy_hashes},
SyncHandler
};
use crate::tests::{helpers::TestIo, snapshot::TestSnapshotService};
use client_traits::ChainInfo; use client_traits::ChainInfo;
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use parking_lot::RwLock; use parking_lot::RwLock;
use rlp::Rlp; use rlp::Rlp;
use std::collections::VecDeque;
use tests::helpers::TestIo;
use tests::snapshot::TestSnapshotService;
use super::*;
use super::super::tests::{
dummy_sync_with_peer,
get_dummy_block,
get_dummy_blocks,
get_dummy_hashes,
};
#[test] #[test]
fn handles_peer_new_hashes() { fn handles_peer_new_hashes() {

View File

@ -88,38 +88,41 @@
//! All other messages are ignored. //! All other messages are ignored.
mod handler; mod handler;
pub mod sync_packet;
mod propagator; mod propagator;
mod requester; mod requester;
mod supplier; mod supplier;
pub mod sync_packet;
use std::sync::{Arc, mpsc}; use std::sync::{Arc, mpsc};
use std::collections::{HashSet, HashMap, BTreeMap}; use std::collections::{HashSet, HashMap, BTreeMap};
use std::cmp; use std::cmp;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use hash::keccak;
use parity_util_mem::MallocSizeOfExt;
use futures::sync::mpsc as futures_mpsc;
use api::Notification;
use ethereum_types::{H256, U256};
use fastmap::{H256FastMap, H256FastSet};
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use bytes::Bytes;
use rlp::{RlpStream, DecoderError};
use network::{self, PeerId, PacketId};
use network::client_version::ClientVersion;
use client_traits::BlockChainClient;
use crate::{ use crate::{
EthProtocolInfo as PeerInfoDigest, PriorityTask, SyncConfig, WarpSync, WARP_SYNC_PROTOCOL_ID,
api::{Notification, PRIORITY_TIMER_INTERVAL},
block_sync::{BlockDownloader, DownloadAction},
sync_io::SyncIo, sync_io::SyncIo,
snapshot_sync::Snapshot, snapshot_sync::Snapshot,
transactions_stats::{TransactionsStats, Stats as TransactionStats},
private_tx::PrivateTxHandler,
}; };
use super::{WarpSync, SyncConfig};
use block_sync::{BlockDownloader, DownloadAction}; use bytes::Bytes;
use client_traits::BlockChainClient;
use ethereum_types::{H256, U256};
use fastmap::{H256FastMap, H256FastSet};
use futures::sync::mpsc as futures_mpsc;
use keccak_hash::keccak;
use log::{error, trace, debug};
use network::client_version::ClientVersion;
use network::{self, PeerId, PacketId};
use parity_util_mem::{MallocSizeOfExt, malloc_size_of_is_0};
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use rand::{Rng, seq::SliceRandom}; use rand::{Rng, seq::SliceRandom};
use api::{EthProtocolInfo as PeerInfoDigest, WARP_SYNC_PROTOCOL_ID, PriorityTask}; use rlp::{RlpStream, DecoderError};
use private_tx::PrivateTxHandler; use common_types::{
use transactions_stats::{TransactionsStats, Stats as TransactionStats};
use types::{
BlockNumber, BlockNumber,
ids::BlockId, ids::BlockId,
transaction::UnverifiedTransaction, transaction::UnverifiedTransaction,
@ -434,7 +437,7 @@ impl ChainSyncApi {
} }
/// Returns transactions propagation statistics /// Returns transactions propagation statistics
pub fn transactions_stats(&self) -> BTreeMap<H256, ::TransactionStats> { pub fn transactions_stats(&self) -> BTreeMap<H256, crate::api::TransactionStats> {
self.sync.read().transactions_stats() self.sync.read().transactions_stats()
.iter() .iter()
.map(|(hash, stats)| (*hash, stats.into())) .map(|(hash, stats)| (*hash, stats.into()))
@ -463,7 +466,7 @@ impl ChainSyncApi {
} }
// deadline to get the task from the queue // deadline to get the task from the queue
let deadline = Instant::now() + ::api::PRIORITY_TIMER_INTERVAL; let deadline = Instant::now() + PRIORITY_TIMER_INTERVAL;
let mut work = || { let mut work = || {
let task = { let task = {
let tasks = self.priority_tasks.try_lock_until(deadline)?; let tasks = self.priority_tasks.try_lock_until(deadline)?;
@ -1405,22 +1408,27 @@ impl ChainSync {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use std::collections::{VecDeque}; use std::{collections::VecDeque, time::Instant};
use ethkey;
use network::PeerId; use super::{
use tests::helpers::TestIo; BlockId, BlockQueueInfo, ChainSync, ClientVersion, PeerInfo, PeerAsking,
use tests::snapshot::TestSnapshotService; SyncHandler, SyncState, SyncStatus, SyncPropagator, UnverifiedTransaction
use ethereum_types::{H256, U256, Address}; };
use parking_lot::RwLock;
use crate::{
api::SyncConfig,
tests::{helpers::TestIo, snapshot::TestSnapshotService},
};
use bytes::Bytes; use bytes::Bytes;
use rlp::{Rlp, RlpStream};
use super::*;
use ::SyncConfig;
use super::{PeerInfo, PeerAsking};
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use client_traits::{BlockInfo, BlockChainClient, ChainInfo}; use client_traits::{BlockInfo, BlockChainClient, ChainInfo};
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use ethcore::miner::{MinerService, PendingOrdering}; use ethcore::miner::{MinerService, PendingOrdering};
use types::header::Header; use ethereum_types::{H256, U256, Address};
use network::PeerId;
use parking_lot::RwLock;
use rlp::{Rlp, RlpStream};
use common_types::header::Header;
pub fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes { pub fn get_dummy_block(order: u32, parent_hash: H256) -> Bytes {
let mut header = Header::new(); let mut header = Header::new();

View File

@ -17,19 +17,18 @@
use std::cmp; use std::cmp;
use std::collections::HashSet; use std::collections::HashSet;
use crate::{sync_io::SyncIo, chain::sync_packet::SyncPacket};
use bytes::Bytes; use bytes::Bytes;
use ethereum_types::H256; use ethereum_types::H256;
use fastmap::H256FastSet; use fastmap::H256FastSet;
use log::{debug, error, trace};
use network::client_version::ClientCapabilities; use network::client_version::ClientCapabilities;
use network::PeerId; use network::PeerId;
use rand::RngCore; use rand::RngCore;
use rlp::{Encodable, RlpStream}; use rlp::{Encodable, RlpStream};
use sync_io::SyncIo; use common_types::{blockchain_info::BlockChainInfo, transaction::SignedTransaction, BlockNumber};
use types::transaction::SignedTransaction;
use types::BlockNumber;
use types::blockchain_info::BlockChainInfo;
use super::sync_packet::SyncPacket;
use super::sync_packet::SyncPacket::{ use super::sync_packet::SyncPacket::{
NewBlockHashesPacket, NewBlockHashesPacket,
TransactionsPacket, TransactionsPacket,
@ -335,17 +334,26 @@ impl SyncPropagator {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use client_traits::{BlockInfo, ChainInfo}; use std::{collections::VecDeque, time::Instant};
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use parking_lot::RwLock; use crate::{
use rlp::Rlp; api::SyncConfig,
use std::collections::VecDeque; chain::{ChainSync, ForkConfirmation, PeerAsking, PeerInfo},
use tests::{ tests::{helpers::TestIo, snapshot::TestSnapshotService},
helpers::TestIo,
snapshot::TestSnapshotService,
}; };
use super::{*, super::{*, tests::*}}; use super::{
super::tests::{dummy_sync_with_peer, insert_dummy_peer},
SyncPropagator,
};
use client_traits::{BlockChainClient, BlockInfo, ChainInfo};
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use ethereum_types::{H256, U256};
use network::client_version::ClientVersion;
use parking_lot::RwLock;
use rlp::Rlp;
use common_types::{ids::BlockId, transaction::UnverifiedTransaction};
#[test] #[test]
fn sends_new_hashes_to_lagging_peer() { fn sends_new_hashes_to_lagging_peer() {

View File

@ -14,14 +14,19 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use block_sync::BlockRequest; use std::time::Instant;
use crate::{
block_sync::BlockRequest,
sync_io::SyncIo
};
use bytes::Bytes; use bytes::Bytes;
use ethereum_types::H256; use ethereum_types::H256;
use log::{debug, trace, warn};
use network::{PeerId}; use network::{PeerId};
use rlp::RlpStream; use rlp::RlpStream;
use std::time::Instant; use common_types::BlockNumber;
use sync_io::SyncIo;
use types::BlockNumber;
use super::sync_packet::SyncPacket; use super::sync_packet::SyncPacket;
use super::sync_packet::SyncPacket::{ use super::sync_packet::SyncPacket::{

View File

@ -14,17 +14,18 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::cmp;
use crate::sync_io::SyncIo;
use bytes::Bytes; use bytes::Bytes;
use enum_primitive::FromPrimitive; use enum_primitive::FromPrimitive;
use ethereum_types::H256; use ethereum_types::H256;
use log::{debug, trace};
use network::{self, PeerId}; use network::{self, PeerId};
use parking_lot::RwLock; use parking_lot::RwLock;
use rlp::{Rlp, RlpStream}; use rlp::{Rlp, RlpStream};
use std::cmp; use common_types::{ids::BlockId, BlockNumber};
use types::BlockNumber;
use types::ids::BlockId;
use sync_io::SyncIo;
use super::sync_packet::{PacketInfo, SyncPacket}; use super::sync_packet::{PacketInfo, SyncPacket};
use super::sync_packet::SyncPacket::{ use super::sync_packet::SyncPacket::{
@ -394,18 +395,27 @@ impl SyncSupplier {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::collections::VecDeque; use std::{collections::VecDeque, str::FromStr};
use tests::helpers::TestIo;
use tests::snapshot::TestSnapshotService; use crate::{
use ethereum_types::H256; blocks::SyncHeader,
use parking_lot::RwLock; chain::RlpResponseResult,
tests::{helpers::TestIo, snapshot::TestSnapshotService}
};
use super::{
SyncPacket::{GetReceiptsPacket, GetNodeDataPacket},
BlockNumber, BlockId, SyncSupplier, PacketInfo
};
use super::super::tests::dummy_sync_with_peer;
use bytes::Bytes; use bytes::Bytes;
use rlp::{Rlp, RlpStream};
use super::{*, super::tests::*};
use blocks::SyncHeader;
use client_traits::BlockChainClient; use client_traits::BlockChainClient;
use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient}; use ethcore::test_helpers::{EachBlockWith, TestBlockChainClient};
use std::str::FromStr; use ethereum_types::H256;
use parking_lot::RwLock;
use rlp::{Rlp, RlpStream};
#[test] #[test]
fn return_block_headers() { fn return_block_headers() {
@ -426,7 +436,8 @@ mod test {
rlp.append(&if reverse {1u32} else {0u32}); rlp.append(&if reverse {1u32} else {0u32});
rlp.out() rlp.out()
} }
fn to_header_vec(rlp: ::chain::RlpResponseResult) -> Vec<SyncHeader> {
fn to_header_vec(rlp: RlpResponseResult) -> Vec<SyncHeader> {
Rlp::new(&rlp.unwrap().unwrap().1.out()).iter().map(|r| SyncHeader::from_rlp(r.as_raw().to_vec()).unwrap()).collect() Rlp::new(&rlp.unwrap().unwrap().1.out()).iter().map(|r| SyncHeader::from_rlp(r.as_raw().to_vec()).unwrap()).collect()
} }

View File

@ -22,7 +22,10 @@
//! to convert to/from the packet id values transmitted over the //! to convert to/from the packet id values transmitted over the
//! wire. //! wire.
use api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID}; use crate::api::{ETH_PROTOCOL, WARP_SYNC_PROTOCOL_ID};
use self::SyncPacket::*;
use enum_primitive::{enum_from_primitive, enum_from_primitive_impl, enum_from_primitive_impl_ty};
use network::{PacketId, ProtocolId}; use network::{PacketId, ProtocolId};
enum_from_primitive! { enum_from_primitive! {
@ -60,12 +63,14 @@ enum_from_primitive! {
} }
} }
use self::SyncPacket::*;
/// Provide both subprotocol and packet id information within the /// Provide both subprotocol and packet id information within the
/// same object. /// same object.
pub trait PacketInfo { pub trait PacketInfo {
/// Get packet id
fn id(&self) -> PacketId; fn id(&self) -> PacketId;
/// Get protocol id
fn protocol(&self) -> ProtocolId; fn protocol(&self) -> ProtocolId;
} }
@ -113,7 +118,6 @@ impl PacketInfo for SyncPacket {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use enum_primitive::FromPrimitive; use enum_primitive::FromPrimitive;
#[test] #[test]

View File

@ -21,47 +21,10 @@
//! https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol //! https://github.com/ethereum/wiki/wiki/Ethereum-Wire-Protocol
//! //!
extern crate client_traits; // needed to make the procedural macro `MallocSizeOf` to work
extern crate common_types as types; #[macro_use] extern crate parity_util_mem as malloc_size_of;
extern crate ethcore_io as io;
extern crate ethcore_light as light;
extern crate ethcore_network as network;
extern crate ethcore_network_devp2p as devp2p;
extern crate ethcore_private_tx;
extern crate ethereum_types;
extern crate ethkey;
extern crate fastmap;
extern crate futures;
extern crate keccak_hash as hash;
extern crate parity_bytes as bytes;
extern crate parity_runtime;
extern crate parking_lot;
extern crate rand;
extern crate rlp;
extern crate snapshot;
extern crate triehash_ethereum;
#[cfg(test)] extern crate engine;
#[cfg(test)] extern crate env_logger;
#[cfg(test)] extern crate ethcore;
#[cfg(test)] extern crate kvdb_memorydb;
#[cfg(test)] extern crate machine;
#[cfg(test)] extern crate rand_xorshift;
#[cfg(test)] extern crate rustc_hex;
#[cfg(test)] extern crate spec;
#[macro_use]
extern crate enum_primitive;
#[macro_use]
extern crate macros;
#[macro_use]
extern crate log;
extern crate parity_util_mem;
#[macro_use]
extern crate parity_util_mem as malloc_size_of;
#[macro_use]
extern crate trace_time;
mod api;
mod chain; mod chain;
mod blocks; mod blocks;
mod block_sync; mod block_sync;
@ -75,8 +38,6 @@ pub mod light_sync;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
mod api;
pub use api::*; pub use api::*;
pub use chain::{SyncStatus, SyncState}; pub use chain::{SyncStatus, SyncState};
pub use devp2p::validate_node_url; pub use devp2p::validate_node_url;

View File

@ -38,15 +38,20 @@ use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use std::time::{Instant, Duration}; use std::time::{Instant, Duration};
use types::encoded; use crate::{
api::Notification,
chain::SyncState as ChainSyncState,
};
use common_types::encoded;
use light::client::{AsLightClient, LightChainClient}; use light::client::{AsLightClient, LightChainClient};
use light::net::{ use light::net::{
PeerStatus, Announcement, Handler, BasicContext, PeerStatus, Announcement, Handler, BasicContext,
EventContext, Capabilities, ReqId, Status, EventContext, Capabilities, ReqId, Status,
Error as NetError, Error as NetError,
}; };
use chain::SyncState as ChainSyncState;
use light::request::{self, CompleteHeadersRequest as HeadersRequest}; use light::request::{self, CompleteHeadersRequest as HeadersRequest};
use log::{debug, trace};
use network::PeerId; use network::PeerId;
use ethereum_types::{H256, U256}; use ethereum_types::{H256, U256};
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
@ -54,7 +59,6 @@ use rand::{rngs::OsRng, seq::SliceRandom};
use futures::sync::mpsc; use futures::sync::mpsc;
use self::sync_round::{AbortReason, SyncRound, ResponseContext}; use self::sync_round::{AbortReason, SyncRound, ResponseContext};
use api::Notification;
mod response; mod response;
mod sync_round; mod sync_round;
@ -78,13 +82,13 @@ struct ChainInfo {
} }
impl PartialOrd for ChainInfo { impl PartialOrd for ChainInfo {
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.head_td.partial_cmp(&other.head_td) self.head_td.partial_cmp(&other.head_td)
} }
} }
impl Ord for ChainInfo { impl Ord for ChainInfo {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering { fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.head_td.cmp(&other.head_td) self.head_td.cmp(&other.head_td)
} }
} }
@ -102,14 +106,20 @@ impl Peer {
} }
} }
// search for a common ancestor with the best chain. /// Search for a common ancestor with the best chain.
#[derive(Debug)] #[derive(Debug)]
enum AncestorSearch { enum AncestorSearch {
Queued(u64), // queued to search for blocks starting from here. /// Queued to search for blocks starting from here.
Awaiting(ReqId, u64, HeadersRequest), // awaiting response for this request. Queued(u64), //
Prehistoric, // prehistoric block found. TODO: start to roll back CHTs. /// Awaiting response for this request.
FoundCommon(u64, H256), // common block found. Awaiting(ReqId, u64, HeadersRequest),
Genesis, // common ancestor is the genesis. /// Pre-historic block found.
// TODO: start to roll back CHTs.
Prehistoric,
/// Common block found.
FoundCommon(u64, H256),
/// Common ancestor is the genesis.
Genesis,
} }
impl AncestorSearch { impl AncestorSearch {
@ -493,7 +503,7 @@ impl<L: AsLightClient> LightSync<L> {
// handles request dispatch, block import, state machine transitions, and timeouts. // handles request dispatch, block import, state machine transitions, and timeouts.
fn maintain_sync(&self, ctx: &dyn BasicContext) { fn maintain_sync(&self, ctx: &dyn BasicContext) {
use types::errors::{EthcoreError, ImportError}; use common_types::errors::{EthcoreError, ImportError};
const DRAIN_AMOUNT: usize = 128; const DRAIN_AMOUNT: usize = 128;

View File

@ -16,7 +16,7 @@
//! Helpers for decoding and verifying responses for headers. //! Helpers for decoding and verifying responses for headers.
use types::{encoded, header::Header}; use common_types::{encoded, header::Header};
use ethereum_types::H256; use ethereum_types::H256;
use light::request::{HashOrNumber, CompleteHeadersRequest as HeadersRequest}; use light::request::{HashOrNumber, CompleteHeadersRequest as HeadersRequest};
use rlp::DecoderError; use rlp::DecoderError;
@ -153,8 +153,8 @@ impl Constraint for Max {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use types::encoded; use common_types::encoded;
use types::header::Header; use common_types::header::Header;
use light::request::CompleteHeadersRequest as HeadersRequest; use light::request::CompleteHeadersRequest as HeadersRequest;
use super::*; use super::*;

View File

@ -20,11 +20,11 @@ use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque}; use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
use std::fmt; use std::fmt;
use types::encoded; use common_types::{encoded, header::Header};
use types::header::Header;
use light::net::ReqId; use light::net::ReqId;
use light::request::CompleteHeadersRequest as HeadersRequest; use light::request::CompleteHeadersRequest as HeadersRequest;
use log::trace;
use network::PeerId; use network::PeerId;
use ethereum_types::H256; use ethereum_types::H256;
@ -37,7 +37,7 @@ const SCAFFOLD_ATTEMPTS: usize = 3;
/// Context for a headers response. /// Context for a headers response.
pub trait ResponseContext { pub trait ResponseContext {
/// Get the peer who sent this response. /// Get the peer who sent this response.
fn responder(&self) -> PeerId; fn responder(&self) -> PeerId;
/// Get the request ID this response corresponds to. /// Get the request ID this response corresponds to.
fn req_id(&self) -> &ReqId; fn req_id(&self) -> &ReqId;
/// Get the (unverified) response data. /// Get the (unverified) response data.

View File

@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use tests::helpers::TestNet; use crate::tests::helpers::TestNet;
use ethcore::test_helpers::EachBlockWith; use ethcore::test_helpers::EachBlockWith;
use client_traits::BlockInfo; use client_traits::BlockInfo;
use types::ids::BlockId; use common_types::ids::BlockId;
mod test_net; mod test_net;

View File

@ -18,26 +18,27 @@
use std::collections::{HashSet, VecDeque}; use std::collections::{HashSet, VecDeque};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration;
use light_sync::*; use crate::{
use tests::helpers::{TestNet, Peer as PeerLike, TestPacket}; light_sync::LightSync,
tests::helpers::{TestNet, Peer as PeerLike, TestPacket}
};
use ethcore::test_helpers::TestBlockChainClient; use ethcore::test_helpers::TestBlockChainClient;
use spec; use ethcore_io::IoChannel;
use io::IoChannel; use light::{
use kvdb_memorydb; cache::Cache,
use light::client::fetch::{self, Unavailable}; client::fetch::{self, Unavailable},
use light::net::{LightProtocol, IoContext, Capabilities, Params as LightParams}; net::{LightProtocol, IoContext, Capabilities, Params as LightParams},
use light::provider::LightProvider; provider::LightProvider
};
use network::{NodeId, PeerId}; use network::{NodeId, PeerId};
use parking_lot::RwLock; use parking_lot::{Mutex, RwLock};
use std::time::Duration;
use light::cache::Cache;
const NETWORK_ID: u64 = 0xcafebabe; const NETWORK_ID: u64 = 0xcafebabe;
pub type LightClient = ::light::client::Client<Unavailable>; pub type LightClient = light::client::Client<Unavailable>;
struct TestIoContext<'a> { struct TestIoContext<'a> {
queue: &'a RwLock<VecDeque<TestPacket>>, queue: &'a RwLock<VecDeque<TestPacket>>,
@ -49,7 +50,7 @@ impl<'a> IoContext for TestIoContext<'a> {
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) { fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) {
self.queue.write().push_back(TestPacket { self.queue.write().push_back(TestPacket {
data: packet_body, data: packet_body,
packet_id: packet_id, packet_id,
recipient: peer, recipient: peer,
}) })
} }
@ -64,11 +65,21 @@ impl<'a> IoContext for TestIoContext<'a> {
self.to_disconnect.write().insert(peer); self.to_disconnect.write().insert(peer);
} }
fn disable_peer(&self, peer: PeerId) { self.disconnect_peer(peer) } fn disable_peer(&self, peer: PeerId) {
fn protocol_version(&self, _peer: PeerId) -> Option<u8> { Some(::light::net::MAX_PROTOCOL_VERSION) } self.disconnect_peer(peer)
}
fn persistent_peer_id(&self, _peer: PeerId) -> Option<NodeId> { unimplemented!() } fn protocol_version(&self, _peer: PeerId) -> Option<u8> {
fn is_reserved_peer(&self, _peer: PeerId) -> bool { false } Some(light::net::MAX_PROTOCOL_VERSION)
}
fn persistent_peer_id(&self, _peer: PeerId) -> Option<NodeId> {
unimplemented!()
}
fn is_reserved_peer(&self, _peer: PeerId) -> bool {
false
}
} }
// peer-specific data. // peer-specific data.
@ -219,7 +230,7 @@ impl TestNet<Peer> {
pub fn light(n_light: usize, n_full: usize) -> Self { pub fn light(n_light: usize, n_full: usize) -> Self {
let mut peers = Vec::with_capacity(n_light + n_full); let mut peers = Vec::with_capacity(n_light + n_full);
for _ in 0..n_light { for _ in 0..n_light {
let mut config = ::light::client::Config::default(); let mut config = light::client::Config::default();
// skip full verification because the blocks are bad. // skip full verification because the blocks are bad.
config.verify_full = false; config.verify_full = false;
@ -242,8 +253,8 @@ impl TestNet<Peer> {
peers.push(Arc::new(Peer::new_full(Arc::new(TestBlockChainClient::new())))) peers.push(Arc::new(Peer::new_full(Arc::new(TestBlockChainClient::new()))))
} }
TestNet { Self {
peers: peers, peers,
started: false, started: false,
disconnect_events: Vec::new(), disconnect_events: Vec::new(),
} }

View File

@ -14,21 +14,22 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use snapshot::SnapshotService;
use ethereum_types::H256;
use hash::keccak;
use types::snapshot::ManifestData;
use std::collections::HashSet; use std::collections::HashSet;
use std::iter::FromIterator; use std::iter::FromIterator;
use ethereum_types::H256;
use keccak_hash::keccak;
use log::trace;
use snapshot::SnapshotService;
use common_types::snapshot::ManifestData;
#[derive(PartialEq, Eq, Debug)] #[derive(PartialEq, Eq, Debug)]
pub enum ChunkType { pub enum ChunkType {
State(H256), State(H256),
Block(H256), Block(H256),
} }
#[derive(MallocSizeOf)] #[derive(Default, MallocSizeOf)]
pub struct Snapshot { pub struct Snapshot {
pending_state_chunks: Vec<H256>, pending_state_chunks: Vec<H256>,
pending_block_chunks: Vec<H256>, pending_block_chunks: Vec<H256>,
@ -41,16 +42,8 @@ pub struct Snapshot {
impl Snapshot { impl Snapshot {
/// Create a new instance. /// Create a new instance.
pub fn new() -> Snapshot { pub fn new() -> Self {
Snapshot { Default::default()
pending_state_chunks: Vec::new(),
pending_block_chunks: Vec::new(),
downloading_chunks: HashSet::new(),
completed_chunks: HashSet::new(),
snapshot_hash: None,
bad_hashes: HashSet::new(),
initialized: false,
}
} }
/// Sync the Snapshot completed chunks with the Snapshot Service /// Sync the Snapshot completed chunks with the Snapshot Service
@ -176,10 +169,11 @@ impl Snapshot {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use hash::keccak; use super::{ChunkType, H256, Snapshot};
use bytes::Bytes; use bytes::Bytes;
use super::*; use keccak_hash::keccak;
use types::snapshot::ManifestData; use common_types::snapshot::ManifestData;
fn is_empty(snapshot: &Snapshot) -> bool { fn is_empty(snapshot: &Snapshot) -> bool {
snapshot.pending_block_chunks.is_empty() && snapshot.pending_block_chunks.is_empty() &&

View File

@ -16,15 +16,17 @@
use std::sync::Arc; use std::sync::Arc;
use std::collections::HashMap; use std::collections::HashMap;
use chain::sync_packet::{PacketInfo, SyncPacket};
use network::{NetworkContext, PeerId, PacketId, Error, SessionInfo, ProtocolId}; use crate::chain::sync_packet::{PacketInfo, SyncPacket};
use network::client_version::ClientVersion;
use bytes::Bytes; use bytes::Bytes;
use client_traits::BlockChainClient; use client_traits::BlockChainClient;
use ethcore_private_tx::PrivateStateDB; use ethcore_private_tx::PrivateStateDB;
use types::BlockNumber; use network::client_version::ClientVersion;
use snapshot::SnapshotService; use network::{NetworkContext, PeerId, PacketId, Error, SessionInfo, ProtocolId};
use parking_lot::RwLock; use parking_lot::RwLock;
use snapshot::SnapshotService;
use common_types::BlockNumber;
/// IO interface for the syncing handler. /// IO interface for the syncing handler.
/// Provides peer connection management and an interface to the blockchain client. /// Provides peer connection management and an interface to the blockchain client.

View File

@ -15,18 +15,20 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc; use std::sync::Arc;
use types::ids::BlockId;
use client_traits::{BlockChainClient, ChainInfo}; use crate::{
api::{SyncConfig, WarpSync},
chain::SyncState,
tests::helpers::TestNet,
};
use client_traits::{BlockChainClient, BlockInfo, ChainInfo};
use common_types::ids::BlockId;
use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith}; use ethcore::test_helpers::{TestBlockChainClient, EachBlockWith};
use client_traits::BlockInfo;
use chain::SyncState;
use super::helpers::*;
use {SyncConfig, WarpSync};
use spec;
#[test] #[test]
fn two_peers() { fn two_peers() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
@ -37,7 +39,7 @@ fn two_peers() {
#[test] #[test]
fn long_chain() { fn long_chain() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(2); let mut net = TestNet::new(2);
net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing); net.peer(1).chain.add_blocks(50000, EachBlockWith::Nothing);
net.sync(); net.sync();
@ -47,7 +49,7 @@ fn long_chain() {
#[test] #[test]
fn status_after_sync() { fn status_after_sync() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
@ -67,7 +69,7 @@ fn takes_few_steps() {
#[test] #[test]
fn empty_blocks() { fn empty_blocks() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
for n in 0..200 { for n in 0..200 {
let with = if n % 2 == 0 { EachBlockWith::Nothing } else { EachBlockWith::Uncle }; let with = if n % 2 == 0 { EachBlockWith::Nothing } else { EachBlockWith::Uncle };
@ -81,7 +83,7 @@ fn empty_blocks() {
#[test] #[test]
fn forked() { fn forked() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer(0).chain.add_blocks(30, EachBlockWith::Uncle); net.peer(0).chain.add_blocks(30, EachBlockWith::Uncle);
net.peer(1).chain.add_blocks(30, EachBlockWith::Uncle); net.peer(1).chain.add_blocks(30, EachBlockWith::Uncle);
@ -102,7 +104,7 @@ fn forked() {
#[test] #[test]
fn forked_with_misbehaving_peer() { fn forked_with_misbehaving_peer() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
let mut alt_spec = spec::new_test(); let mut alt_spec = spec::new_test();
@ -126,7 +128,7 @@ fn forked_with_misbehaving_peer() {
#[test] #[test]
fn net_hard_fork() { fn net_hard_fork() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let ref_client = TestBlockChainClient::new(); let ref_client = TestBlockChainClient::new();
ref_client.add_blocks(50, EachBlockWith::Uncle); ref_client.add_blocks(50, EachBlockWith::Uncle);
{ {
@ -145,7 +147,7 @@ fn net_hard_fork() {
#[test] #[test]
fn restart() { fn restart() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(3); let mut net = TestNet::new(3);
net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(1).chain.add_blocks(1000, EachBlockWith::Uncle);
net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle); net.peer(2).chain.add_blocks(1000, EachBlockWith::Uncle);
@ -229,7 +231,7 @@ fn propagate_blocks() {
#[test] #[test]
fn restart_on_malformed_block() { fn restart_on_malformed_block() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(2); let mut net = TestNet::new(2);
net.peer(1).chain.add_blocks(5, EachBlockWith::Nothing); net.peer(1).chain.add_blocks(5, EachBlockWith::Nothing);
net.peer(1).chain.add_block(EachBlockWith::Nothing, |mut header| { net.peer(1).chain.add_block(EachBlockWith::Nothing, |mut header| {
@ -255,7 +257,7 @@ fn reject_on_broken_chain() {
#[test] #[test]
fn disconnect_on_unrelated_chain() { fn disconnect_on_unrelated_chain() {
::env_logger::try_init().ok(); env_logger::try_init().ok();
let mut net = TestNet::new(2); let mut net = TestNet::new(2);
net.peer(0).chain.set_history(Some(20)); net.peer(0).chain.set_history(Some(20));
net.peer(1).chain.set_history(Some(20)); net.peer(1).chain.set_history(Some(20));

View File

@ -15,21 +15,24 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc; use std::sync::Arc;
use hash::keccak;
use ethereum_types::{U256, Address}; use crate::{
use io::{IoHandler, IoChannel}; api::SyncConfig,
tests::helpers::{TestIoHandler, TestNet},
};
use client_traits::ChainInfo; use client_traits::ChainInfo;
use engine::signer; use engine::signer;
use spec;
use ethcore::client::Client; use ethcore::client::Client;
use ethcore::miner::{self, MinerService}; use ethcore::miner::{self, MinerService};
use ethcore_io::{IoHandler, IoChannel};
use ethereum_types::{U256, Address};
use ethkey::{KeyPair, Secret}; use ethkey::{KeyPair, Secret};
use types::{ use keccak_hash::keccak;
use common_types::{
io_message::ClientIoMessage, io_message::ClientIoMessage,
transaction::{Action, PendingTransaction, Transaction} transaction::{Action, PendingTransaction, Transaction}
}; };
use super::helpers::*;
use SyncConfig;
fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction { fn new_tx(secret: &Secret, nonce: U256, chain_id: u64) -> PendingTransaction {
let signed = Transaction { let signed = Transaction {

View File

@ -16,36 +16,42 @@
use std::collections::{VecDeque, HashSet, HashMap}; use std::collections::{VecDeque, HashSet, HashMap};
use std::sync::Arc; use std::sync::Arc;
use crate::{
api::{SyncConfig, WARP_SYNC_PROTOCOL_ID},
chain::{
sync_packet::{
PacketInfo,
SyncPacket::{self, PrivateTransactionPacket, SignedPrivateTransactionPacket}
},
ChainSync, SyncSupplier, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_4
},
private_tx::SimplePrivateTxHandler,
sync_io::SyncIo,
tests::snapshot::TestSnapshotService,
};
use client_traits::{BlockChainClient, ChainNotify};
use common_types::{
chain_notify::{NewBlocks, ChainMessageType},
io_message::ClientIoMessage,
BlockNumber,
};
use ethcore::{
client::{Client as EthcoreClient, ClientConfig},
test_helpers::{self, TestBlockChainClient},
};
use ethcore::miner::Miner;
use ethcore_io::{IoChannel, IoContext, IoHandler};
use ethcore_private_tx::PrivateStateDB;
use ethereum_types::H256; use ethereum_types::H256;
use parking_lot::{RwLock, Mutex};
use bytes::Bytes; use bytes::Bytes;
use network::{self, PeerId, ProtocolId, PacketId, SessionInfo}; use network::{self, PeerId, ProtocolId, PacketId, SessionInfo};
use network::client_version::ClientVersion; use network::client_version::ClientVersion;
use tests::snapshot::*; use log::trace;
use types::{
chain_notify::{NewBlocks, ChainMessageType},
io_message::ClientIoMessage,
};
use client_traits::{BlockChainClient, ChainNotify};
use ethcore::{
client::{Client as EthcoreClient, ClientConfig},
test_helpers::TestBlockChainClient
};
use snapshot::SnapshotService; use snapshot::SnapshotService;
use spec::{self, Spec}; use spec::Spec;
use ethcore_private_tx::PrivateStateDB; use parking_lot::{RwLock, Mutex};
use ethcore::miner::Miner;
use ethcore::test_helpers;
use sync_io::SyncIo;
use io::{IoChannel, IoContext, IoHandler};
use api::WARP_SYNC_PROTOCOL_ID;
use chain::{ChainSync, SyncSupplier, ETH_PROTOCOL_VERSION_63, PAR_PROTOCOL_VERSION_4};
use chain::sync_packet::{PacketInfo, SyncPacket};
use chain::sync_packet::SyncPacket::{PrivateTransactionPacket, SignedPrivateTransactionPacket};
use SyncConfig;
use private_tx::SimplePrivateTxHandler;
use types::BlockNumber;
pub trait FlushingBlockChainClient: BlockChainClient { pub trait FlushingBlockChainClient: BlockChainClient {
fn flush(&self) {} fn flush(&self) {}
@ -123,7 +129,7 @@ impl<'p, C> SyncIo for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
fn send(&mut self,peer_id: PeerId, packet_id: SyncPacket, data: Vec<u8>) -> Result<(), network::Error> { fn send(&mut self,peer_id: PeerId, packet_id: SyncPacket, data: Vec<u8>) -> Result<(), network::Error> {
self.packets.push(TestPacket { self.packets.push(TestPacket {
data: data, data,
packet_id: packet_id.id(), packet_id: packet_id.id(),
recipient: peer_id, recipient: peer_id,
}); });
@ -135,11 +141,10 @@ impl<'p, C> SyncIo for TestIo<'p, C> where C: FlushingBlockChainClient, C: 'p {
} }
fn peer_version(&self, peer_id: PeerId) -> ClientVersion { fn peer_version(&self, peer_id: PeerId) -> ClientVersion {
let client_id = self.peers_info.get(&peer_id) self.peers_info.get(&peer_id)
.cloned() .cloned()
.unwrap_or_else(|| peer_id.to_string()); .unwrap_or_else(|| peer_id.to_string())
.into()
ClientVersion::from(client_id)
} }
fn snapshot_service(&self) -> &dyn SnapshotService { fn snapshot_service(&self) -> &dyn SnapshotService {

View File

@ -15,32 +15,38 @@
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use std::sync::Arc; use std::sync::Arc;
use hash::keccak;
use io::{IoHandler, IoChannel}; use crate::{
use types::transaction::{Transaction, Action}; api::SyncConfig,
use types::{ tests::helpers::{TestIoHandler, TestNet}
};
use client_traits::BlockChainClient;
use common_types::{
ids::BlockId, ids::BlockId,
io_message::ClientIoMessage, io_message::ClientIoMessage,
transaction::{Transaction, Action},
}; };
use client_traits::BlockChainClient;
use engine::signer; use engine::signer;
use ethcore::{ use ethcore::{
client::Client, client::Client,
miner::{self, MinerService}, miner::{self, MinerService},
test_helpers::{CreateContractAddress, push_block_with_transactions, new_db}, test_helpers::{CreateContractAddress, push_block_with_transactions, new_db},
}; };
use ethcore_private_tx::{Provider, ProviderConfig, NoopEncryptor, Importer, SignedPrivateTransaction, StoringKeyProvider}; use ethcore_io::{IoHandler, IoChannel};
use ethcore_private_tx::{
Provider, ProviderConfig, NoopEncryptor, Importer, SignedPrivateTransaction, StoringKeyProvider
};
use ethkey::KeyPair; use ethkey::KeyPair;
use keccak_hash::keccak;
use machine::executive::contract_address; use machine::executive::contract_address;
use tests::helpers::{TestNet, TestIoHandler};
use rustc_hex::FromHex; use rustc_hex::FromHex;
use rlp::Rlp; use rlp::Rlp;
use spec::Spec; use spec::Spec;
use SyncConfig;
fn seal_spec() -> Spec { fn seal_spec() -> Spec {
let spec_data = include_str!("../res/private_spec.json"); let spec_data = include_str!("../res/private_spec.json");
Spec::load(&::std::env::temp_dir(), spec_data.as_bytes()).unwrap() Spec::load(&std::env::temp_dir(), spec_data.as_bytes()).unwrap()
} }
#[test] #[test]

View File

@ -16,37 +16,35 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use hash::keccak;
use ethereum_types::H256; use crate::{
use parking_lot::Mutex; api::{SyncConfig, WarpSync},
tests::helpers::TestNet
};
use bytes::Bytes; use bytes::Bytes;
use snapshot::SnapshotService;
use ethcore::test_helpers::EachBlockWith; use ethcore::test_helpers::EachBlockWith;
use types::{ use ethereum_types::H256;
use keccak_hash::keccak;
use parking_lot::Mutex;
use snapshot::SnapshotService;
use common_types::{
BlockNumber, BlockNumber,
snapshot::{ManifestData, RestorationStatus}, snapshot::{ManifestData, RestorationStatus},
}; };
use super::helpers::*;
use {SyncConfig, WarpSync};
#[derive(Default)]
pub struct TestSnapshotService { pub struct TestSnapshotService {
manifest: Option<ManifestData>, manifest: Option<ManifestData>,
chunks: HashMap<H256, Bytes>, chunks: HashMap<H256, Bytes>,
restoration_manifest: Mutex<Option<ManifestData>>, restoration_manifest: Mutex<Option<ManifestData>>,
state_restoration_chunks: Mutex<HashMap<H256, Bytes>>, state_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
block_restoration_chunks: Mutex<HashMap<H256, Bytes>>, block_restoration_chunks: Mutex<HashMap<H256, Bytes>>,
} }
impl TestSnapshotService { impl TestSnapshotService {
pub fn new() -> TestSnapshotService { pub fn new() -> Self {
TestSnapshotService { Default::default()
manifest: None,
chunks: HashMap::new(),
restoration_manifest: Mutex::new(None),
state_restoration_chunks: Mutex::new(HashMap::new()),
block_restoration_chunks: Mutex::new(HashMap::new()),
}
} }
pub fn new_with_snapshot(num_chunks: usize, block_hash: H256, block_number: BlockNumber) -> TestSnapshotService { pub fn new_with_snapshot(num_chunks: usize, block_hash: H256, block_number: BlockNumber) -> TestSnapshotService {

View File

@ -14,12 +14,14 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>. // along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
use api::TransactionStats;
use std::hash::BuildHasher; use std::hash::BuildHasher;
use std::collections::{HashSet, HashMap}; use std::collections::{HashSet, HashMap};
use crate::api::TransactionStats;
use ethereum_types::{H256, H512}; use ethereum_types::{H256, H512};
use fastmap::H256FastMap; use fastmap::H256FastMap;
use types::BlockNumber; use common_types::BlockNumber;
type NodeId = H512; type NodeId = H512;
@ -89,9 +91,9 @@ impl TransactionsStats {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use super::{Stats, TransactionsStats, NodeId, H256}; use super::{Stats, TransactionsStats, NodeId, H256};
use macros::hash_map;
#[test] #[test]
fn should_keep_track_of_propagations() { fn should_keep_track_of_propagations() {