remove mocknet stuff, clean up debug
This commit is contained in:
parent
0c69d4a1ce
commit
28cf91c7a5
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -610,14 +610,12 @@ dependencies = [
|
|||||||
"clippy 0.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clippy 0.0.90 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ethcore 1.4.0",
|
"ethcore 1.4.0",
|
||||||
"ethcore-devtools 1.4.0",
|
|
||||||
"ethcore-io 1.4.0",
|
"ethcore-io 1.4.0",
|
||||||
"ethcore-ipc 1.4.0",
|
"ethcore-ipc 1.4.0",
|
||||||
"ethcore-ipc-codegen 1.4.0",
|
"ethcore-ipc-codegen 1.4.0",
|
||||||
"ethcore-ipc-nano 1.4.0",
|
"ethcore-ipc-nano 1.4.0",
|
||||||
"ethcore-network 1.4.0",
|
"ethcore-network 1.4.0",
|
||||||
"ethcore-util 1.4.0",
|
"ethcore-util 1.4.0",
|
||||||
"ethkey 0.2.0",
|
|
||||||
"heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -52,7 +52,7 @@ use blockchain::{BlockChain, BlockProvider, TreeRoute, ImportRoute};
|
|||||||
use client::{
|
use client::{
|
||||||
BlockID, TransactionID, UncleID, TraceId, ClientConfig, BlockChainClient,
|
BlockID, TransactionID, UncleID, TraceId, ClientConfig, BlockChainClient,
|
||||||
MiningBlockChainClient, TraceFilter, CallAnalytics, BlockImportError, Mode,
|
MiningBlockChainClient, TraceFilter, CallAnalytics, BlockImportError, Mode,
|
||||||
ChainNotify, TransactionImportResult
|
ChainNotify
|
||||||
};
|
};
|
||||||
use client::Error as ClientError;
|
use client::Error as ClientError;
|
||||||
use env_info::EnvInfo;
|
use env_info::EnvInfo;
|
||||||
@ -480,16 +480,6 @@ impl Client {
|
|||||||
results.len()
|
results.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: these are only used for tests in sync and only contribute to huge Client
|
|
||||||
/// Import a locally created transaction.
|
|
||||||
pub fn import_own_transaction(&self, transaction: SignedTransaction) -> Result<TransactionImportResult, EthcoreError> {
|
|
||||||
self.miner.import_own_transaction(self, transaction)
|
|
||||||
}
|
|
||||||
/// Set miner author.
|
|
||||||
pub fn set_author(&self, author: Address) {
|
|
||||||
self.miner.set_author(author)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Used by PoA to try sealing on period change.
|
/// Used by PoA to try sealing on period change.
|
||||||
pub fn update_sealing(&self) {
|
pub fn update_sealing(&self) {
|
||||||
self.miner.update_sealing(self)
|
self.miner.update_sealing(self)
|
||||||
@ -1039,13 +1029,8 @@ impl BlockChainClient for Client {
|
|||||||
} else {
|
} else {
|
||||||
let len = transactions.len();
|
let len = transactions.len();
|
||||||
match self.io_channel.send(ClientIoMessage::NewTransactions(transactions)) {
|
match self.io_channel.send(ClientIoMessage::NewTransactions(transactions)) {
|
||||||
Ok(_) => {
|
Ok(_) => { self.queue_transactions.fetch_add(len, AtomicOrdering::SeqCst); },
|
||||||
trace!(target: "external_tx", "Sent IoMessage");
|
Err(e) => debug!("Ignoring {} transactions: error queueing: {}", len, e),
|
||||||
self.queue_transactions.fetch_add(len, AtomicOrdering::SeqCst);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
debug!("Ignoring {} transactions: error queueing: {}", len, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,6 @@ use evm::Schedule;
|
|||||||
use ethjson;
|
use ethjson;
|
||||||
use io::{IoContext, IoHandler, TimerToken, IoService, IoChannel};
|
use io::{IoContext, IoHandler, TimerToken, IoService, IoChannel};
|
||||||
use service::ClientIoMessage;
|
use service::ClientIoMessage;
|
||||||
use time::get_time;
|
|
||||||
|
|
||||||
/// `AuthorityRound` params.
|
/// `AuthorityRound` params.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@ -119,7 +118,10 @@ impl IoHandler<BlockArrived> for TransitionHandler {
|
|||||||
debug!(target: "authorityround", "Timeout step: {}", engine.step.load(AtomicOrdering::Relaxed));
|
debug!(target: "authorityround", "Timeout step: {}", engine.step.load(AtomicOrdering::Relaxed));
|
||||||
engine.step.fetch_add(1, AtomicOrdering::SeqCst);
|
engine.step.fetch_add(1, AtomicOrdering::SeqCst);
|
||||||
if let Some(ref channel) = *engine.message_channel.try_lock().unwrap() {
|
if let Some(ref channel) = *engine.message_channel.try_lock().unwrap() {
|
||||||
channel.send(ClientIoMessage::UpdateSealing);
|
match channel.send(ClientIoMessage::UpdateSealing) {
|
||||||
|
Ok(_) => trace!(target: "authorityround", "timeout: UpdateSealing message sent."),
|
||||||
|
Err(_) => trace!(target: "authorityround", "timeout: Could not send a sealing message."),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.our_params.step_duration).expect("Failed to restart consensus step timer.")
|
io.register_timer_once(ENGINE_TIMEOUT_TOKEN, engine.our_params.step_duration).expect("Failed to restart consensus step timer.")
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ pub trait Engine : Sync + Send {
|
|||||||
/// Panics if `is_builtin(a)` is not true.
|
/// Panics if `is_builtin(a)` is not true.
|
||||||
fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut BytesRef) { self.builtins().get(a).unwrap().execute(input, output); }
|
fn execute_builtin(&self, a: &Address, input: &[u8], output: &mut BytesRef) { self.builtins().get(a).unwrap().execute(input, output); }
|
||||||
|
|
||||||
/// Add a channel for communication with Client.
|
/// Add a channel for communication with Client which can be used for sealing.
|
||||||
fn register_message_channel(&self, message_channel: IoChannel<ClientIoMessage>) {}
|
fn register_message_channel(&self, _message_channel: IoChannel<ClientIoMessage>) {}
|
||||||
// TODO: sealing stuff - though might want to leave this for later.
|
// TODO: sealing stuff - though might want to leave this for later.
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ impl Default for MinerOptions {
|
|||||||
MinerOptions {
|
MinerOptions {
|
||||||
new_work_notify: vec![],
|
new_work_notify: vec![],
|
||||||
force_sealing: false,
|
force_sealing: false,
|
||||||
reseal_on_external_tx: true,
|
reseal_on_external_tx: false,
|
||||||
reseal_on_own_tx: true,
|
reseal_on_own_tx: true,
|
||||||
tx_gas_limit: !U256::zero(),
|
tx_gas_limit: !U256::zero(),
|
||||||
tx_queue_size: 1024,
|
tx_queue_size: 1024,
|
||||||
|
@ -21,7 +21,7 @@ use io::*;
|
|||||||
use spec::Spec;
|
use spec::Spec;
|
||||||
use error::*;
|
use error::*;
|
||||||
use client::{Client, ClientConfig, ChainNotify};
|
use client::{Client, ClientConfig, ChainNotify};
|
||||||
use miner::{Miner, MinerService};
|
use miner::Miner;
|
||||||
use snapshot::ManifestData;
|
use snapshot::ManifestData;
|
||||||
use snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams};
|
use snapshot::service::{Service as SnapshotService, ServiceParams as SnapServiceParams};
|
||||||
use std::sync::atomic::AtomicBool;
|
use std::sync::atomic::AtomicBool;
|
||||||
@ -202,7 +202,7 @@ impl IoHandler<ClientIoMessage> for ClientIoHandler {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
ClientIoMessage::UpdateSealing => {
|
ClientIoMessage::UpdateSealing => {
|
||||||
println!("Message received!");
|
trace!(target: "authorityround", "message: UpdateSealing");
|
||||||
self.client.update_sealing()
|
self.client.update_sealing()
|
||||||
},
|
},
|
||||||
_ => {} // ignore other messages
|
_ => {} // ignore other messages
|
||||||
|
@ -13,12 +13,10 @@ ethcore-ipc-codegen = { path = "../ipc/codegen" }
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ethcore-util = { path = "../util" }
|
ethcore-util = { path = "../util" }
|
||||||
ethkey = { path = "../ethkey" }
|
|
||||||
ethcore-network = { path = "../util/network" }
|
ethcore-network = { path = "../util/network" }
|
||||||
ethcore-io = { path = "../util/io" }
|
ethcore-io = { path = "../util/io" }
|
||||||
ethcore = { path = "../ethcore" }
|
ethcore = { path = "../ethcore" }
|
||||||
rlp = { path = "../util/rlp" }
|
rlp = { path = "../util/rlp" }
|
||||||
ethcore-devtools = { path = "../devtools" }
|
|
||||||
clippy = { version = "0.0.90", optional = true}
|
clippy = { version = "0.0.90", optional = true}
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
env_logger = "0.3"
|
env_logger = "0.3"
|
||||||
|
@ -905,7 +905,6 @@ impl ChainSync {
|
|||||||
|
|
||||||
/// Resume downloading
|
/// Resume downloading
|
||||||
fn continue_sync(&mut self, io: &mut SyncIo) {
|
fn continue_sync(&mut self, io: &mut SyncIo) {
|
||||||
trace!(target:"sync", "continue_sync");
|
|
||||||
let mut peers: Vec<(PeerId, U256, u32)> = self.peers.iter().filter_map(|(k, p)|
|
let mut peers: Vec<(PeerId, U256, u32)> = self.peers.iter().filter_map(|(k, p)|
|
||||||
if p.can_sync() { Some((*k, p.difficulty.unwrap_or_else(U256::zero), p.protocol_version)) } else { None }).collect();
|
if p.can_sync() { Some((*k, p.difficulty.unwrap_or_else(U256::zero), p.protocol_version)) } else { None }).collect();
|
||||||
thread_rng().shuffle(&mut peers); //TODO: sort by rating
|
thread_rng().shuffle(&mut peers); //TODO: sort by rating
|
||||||
@ -1249,7 +1248,6 @@ impl ChainSync {
|
|||||||
|
|
||||||
/// Called when peer sends us new transactions
|
/// Called when peer sends us new transactions
|
||||||
fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
fn on_peer_transactions(&mut self, io: &mut SyncIo, peer_id: PeerId, r: &UntrustedRlp) -> Result<(), PacketDecodeError> {
|
||||||
trace!(target: "sync", "Received tx from {}", peer_id);
|
|
||||||
// accepting transactions once only fully synced
|
// accepting transactions once only fully synced
|
||||||
if !io.is_chain_queue_empty() {
|
if !io.is_chain_queue_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -1701,6 +1699,7 @@ impl ChainSync {
|
|||||||
|
|
||||||
/// propagates new transactions to all peers
|
/// propagates new transactions to all peers
|
||||||
pub fn propagate_new_transactions(&mut self, io: &mut SyncIo) -> usize {
|
pub fn propagate_new_transactions(&mut self, io: &mut SyncIo) -> usize {
|
||||||
|
|
||||||
// Early out of nobody to send to.
|
// Early out of nobody to send to.
|
||||||
if self.peers.is_empty() {
|
if self.peers.is_empty() {
|
||||||
return 0;
|
return 0;
|
||||||
@ -1792,7 +1791,6 @@ impl ChainSync {
|
|||||||
/// called when block is imported to chain - propagates the blocks and updates transactions sent to peers
|
/// called when block is imported to chain - propagates the blocks and updates transactions sent to peers
|
||||||
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, _imported: &[H256], invalid: &[H256], _enacted: &[H256], _retracted: &[H256], sealed: &[H256]) {
|
pub fn chain_new_blocks(&mut self, io: &mut SyncIo, _imported: &[H256], invalid: &[H256], _enacted: &[H256], _retracted: &[H256], sealed: &[H256]) {
|
||||||
if io.is_chain_queue_empty() {
|
if io.is_chain_queue_empty() {
|
||||||
trace!(target: "sync", "Chain not empty!");
|
|
||||||
self.propagate_latest_blocks(io, sealed);
|
self.propagate_latest_blocks(io, sealed);
|
||||||
}
|
}
|
||||||
if !invalid.is_empty() {
|
if !invalid.is_empty() {
|
||||||
|
@ -37,11 +37,6 @@ extern crate semver;
|
|||||||
extern crate parking_lot;
|
extern crate parking_lot;
|
||||||
extern crate rlp;
|
extern crate rlp;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate ethkey;
|
|
||||||
#[cfg(test)]
|
|
||||||
extern crate ethcore_devtools as devtools;
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
@ -1,77 +0,0 @@
|
|||||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
||||||
// This file is part of Parity.
|
|
||||||
|
|
||||||
// Parity is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
|
|
||||||
// Parity is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use util::*;
|
|
||||||
use ethcore::spec::Spec;
|
|
||||||
use super::mocknet::*;
|
|
||||||
use std::thread::sleep;
|
|
||||||
use std::time::Duration;
|
|
||||||
use ethcore::client::BlockChainClient;
|
|
||||||
|
|
||||||
fn authorities() -> Vec<H256> { vec!["1".sha3(), "2".sha3()] }
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn two_auth() {
|
|
||||||
::env_logger::init().ok();
|
|
||||||
let mut net = MockNet::new_with_spec(2, authorities(), &Spec::new_test_round);
|
|
||||||
net.peer(1).issue_rand_tx();
|
|
||||||
net.sync();
|
|
||||||
sleep(Duration::from_secs(1));
|
|
||||||
net.sync();
|
|
||||||
println!("{:?}", net.peer(0).client.chain_info());
|
|
||||||
println!("{:?}", net.peer(1).client.chain_info());
|
|
||||||
net.is_synced(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn one_auth_missing() {
|
|
||||||
::env_logger::init().ok();
|
|
||||||
let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round);
|
|
||||||
net.peer(1).issue_rand_tx();
|
|
||||||
sleep(Duration::from_secs(1));
|
|
||||||
net.sync();
|
|
||||||
sleep(Duration::from_secs(1));
|
|
||||||
net.sync();
|
|
||||||
println!("{:?}", net.peer(0).client.chain_info());
|
|
||||||
println!("{:?}", net.peer(1).client.chain_info());
|
|
||||||
net.is_synced(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn issue_many() {
|
|
||||||
::env_logger::init().ok();
|
|
||||||
let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round);
|
|
||||||
net.peer(1).issue_rand_txs(5);
|
|
||||||
sleep(Duration::from_secs(1));
|
|
||||||
net.sync();
|
|
||||||
net.peer(0).issue_rand_txs(5);
|
|
||||||
net.sync();
|
|
||||||
println!("{:?}", net.peer(0).client.chain_info());
|
|
||||||
println!("{:?}", net.peer(1).client.chain_info());
|
|
||||||
net.is_synced(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rand_simulation() {
|
|
||||||
::env_logger::init().ok();
|
|
||||||
let mut net = MockNet::new_with_spec(3, authorities(), &Spec::new_test_round);
|
|
||||||
|
|
||||||
net.rand_simulation(10);
|
|
||||||
|
|
||||||
println!("{:?}", net.peer(0).client.chain_info());
|
|
||||||
println!("{:?}", net.peer(1).client.chain_info());
|
|
||||||
net.is_synced(10);
|
|
||||||
}
|
|
@ -1,335 +0,0 @@
|
|||||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
||||||
// This file is part of Parity.
|
|
||||||
|
|
||||||
// Parity is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
|
|
||||||
// Parity is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
use util::*;
|
|
||||||
use network::*;
|
|
||||||
use tests::snapshot::*;
|
|
||||||
use ethcore::client::{Client, BlockChainClient, ChainNotify};
|
|
||||||
use ethcore::spec::Spec;
|
|
||||||
use ethcore::snapshot::SnapshotService;
|
|
||||||
use ethcore::transaction::{Transaction, SignedTransaction, Action};
|
|
||||||
use ethcore::account_provider::AccountProvider;
|
|
||||||
use ethkey::{Random, Generator};
|
|
||||||
use sync_io::SyncIo;
|
|
||||||
use chain::ChainSync;
|
|
||||||
use ::SyncConfig;
|
|
||||||
use devtools::RandomTempPath;
|
|
||||||
use ethcore::miner::Miner;
|
|
||||||
use ethcore::service::ClientService;
|
|
||||||
use ethcore::header::BlockNumber;
|
|
||||||
use std::time::Duration;
|
|
||||||
use std::thread::sleep;
|
|
||||||
use rand::{thread_rng, Rng};
|
|
||||||
|
|
||||||
pub struct TestIo<'p> {
|
|
||||||
pub client: Arc<Client>,
|
|
||||||
pub snapshot_service: &'p TestSnapshotService,
|
|
||||||
pub queue: &'p mut VecDeque<TestPacket>,
|
|
||||||
pub sender: Option<PeerId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p> TestIo<'p> {
|
|
||||||
pub fn new(client: Arc<Client>, ss: &'p TestSnapshotService, queue: &'p mut VecDeque<TestPacket>, sender: Option<PeerId>) -> TestIo<'p> {
|
|
||||||
TestIo {
|
|
||||||
client: client,
|
|
||||||
snapshot_service: ss,
|
|
||||||
queue: queue,
|
|
||||||
sender: sender
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'p> SyncIo for TestIo<'p> {
|
|
||||||
fn disable_peer(&mut self, _peer_id: PeerId) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn disconnect_peer(&mut self, _peer_id: PeerId) {
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_expired(&self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn respond(&mut self, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError> {
|
|
||||||
self.queue.push_back(TestPacket {
|
|
||||||
data: data,
|
|
||||||
packet_id: packet_id,
|
|
||||||
recipient: self.sender.unwrap()
|
|
||||||
});
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send(&mut self, peer_id: PeerId, packet_id: PacketId, data: Vec<u8>) -> Result<(), NetworkError> {
|
|
||||||
self.queue.push_back(TestPacket {
|
|
||||||
data: data,
|
|
||||||
packet_id: packet_id,
|
|
||||||
recipient: peer_id,
|
|
||||||
});
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn chain(&self) -> &BlockChainClient {
|
|
||||||
self.client.as_ref()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn snapshot_service(&self) -> &SnapshotService {
|
|
||||||
self.snapshot_service
|
|
||||||
}
|
|
||||||
|
|
||||||
fn eth_protocol_version(&self, _peer: PeerId) -> u8 {
|
|
||||||
64
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TestPacket {
|
|
||||||
pub data: Bytes,
|
|
||||||
pub packet_id: PacketId,
|
|
||||||
pub recipient: PeerId,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transaction() -> Transaction {
|
|
||||||
Transaction {
|
|
||||||
action: Action::Create,
|
|
||||||
value: U256::zero(),
|
|
||||||
data: "3331600055".from_hex().unwrap(),
|
|
||||||
gas: U256::from(100_000),
|
|
||||||
gas_price: U256::zero(),
|
|
||||||
nonce: U256::zero(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn random_transaction() -> SignedTransaction {
|
|
||||||
let keypair = Random.generate().unwrap();
|
|
||||||
transaction().sign(&keypair.secret())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MockPeer {
|
|
||||||
pub client: Arc<Client>,
|
|
||||||
pub snapshot_service: Arc<TestSnapshotService>,
|
|
||||||
pub sync: RwLock<ChainSync>,
|
|
||||||
pub queue: RwLock<VecDeque<TestPacket>>,
|
|
||||||
_service: ClientService,
|
|
||||||
_paths: Vec<RandomTempPath>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChainNotify for MockPeer {
|
|
||||||
fn new_blocks(&self,
|
|
||||||
imported: Vec<H256>,
|
|
||||||
invalid: Vec<H256>,
|
|
||||||
enacted: Vec<H256>,
|
|
||||||
retracted: Vec<H256>,
|
|
||||||
sealed: Vec<H256>,
|
|
||||||
_duration: u64) {
|
|
||||||
println!("New sync blocks");
|
|
||||||
let ref mut q = *self.queue.write();
|
|
||||||
let mut sync_io = TestIo::new(
|
|
||||||
self.client.clone(),
|
|
||||||
&self.snapshot_service,
|
|
||||||
q,
|
|
||||||
None);
|
|
||||||
self.sync.write().chain_new_blocks(
|
|
||||||
&mut sync_io,
|
|
||||||
&imported,
|
|
||||||
&invalid,
|
|
||||||
&enacted,
|
|
||||||
&retracted,
|
|
||||||
&sealed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MockPeer {
|
|
||||||
pub fn new_with_spec<S>(get_spec: &S, author_secret: Option<Secret>) -> Arc<MockPeer> where S: Fn()->Spec {
|
|
||||||
let (accounts, address) = if let Some(secret) = author_secret {
|
|
||||||
let tap = AccountProvider::transient_provider();
|
|
||||||
let addr = tap.insert_account(secret, "").unwrap();
|
|
||||||
tap.unlock_account_permanently(addr, "".into()).unwrap();
|
|
||||||
(Some(Arc::new(tap)), Some(addr))
|
|
||||||
} else {
|
|
||||||
(None, None)
|
|
||||||
};
|
|
||||||
|
|
||||||
let client_path = RandomTempPath::new();
|
|
||||||
let snapshot_path = RandomTempPath::new();
|
|
||||||
let ipc_path = RandomTempPath::new();
|
|
||||||
let spec = get_spec();
|
|
||||||
|
|
||||||
let service = ClientService::start(
|
|
||||||
Default::default(),
|
|
||||||
&spec,
|
|
||||||
client_path.as_path(),
|
|
||||||
snapshot_path.as_path(),
|
|
||||||
ipc_path.as_path(),
|
|
||||||
Arc::new(Miner::with_spec_and_accounts(&spec, accounts.clone())),
|
|
||||||
).unwrap();
|
|
||||||
|
|
||||||
let client = service.client();
|
|
||||||
if let Some(addr) = address { client.set_author(addr) }
|
|
||||||
let sync = ChainSync::new(SyncConfig::default(), &*client);
|
|
||||||
|
|
||||||
let peer = Arc::new(MockPeer {
|
|
||||||
sync: RwLock::new(sync),
|
|
||||||
snapshot_service: Arc::new(TestSnapshotService::new()),
|
|
||||||
client: client,
|
|
||||||
queue: RwLock::new(VecDeque::new()),
|
|
||||||
_service: service,
|
|
||||||
_paths: vec![client_path, snapshot_path, ipc_path]
|
|
||||||
});
|
|
||||||
peer.client.add_notify(peer.clone());
|
|
||||||
peer
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn issue_tx(&self, transaction: SignedTransaction) {
|
|
||||||
self.client.import_own_transaction(transaction).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn issue_rand_tx(&self) {
|
|
||||||
self.issue_tx(random_transaction())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn issue_rand_txs(&self, n: usize) {
|
|
||||||
for _ in 0..n {
|
|
||||||
self.issue_rand_tx();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MockNet {
|
|
||||||
pub peers: Vec<Arc<MockPeer>>,
|
|
||||||
pub started: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MockNet {
|
|
||||||
pub fn new_with_spec<S>(nodes: usize, author_secrets: Vec<H256>, get_spec: &S) -> MockNet where S: Fn()->Spec {
|
|
||||||
let mut net = MockNet {
|
|
||||||
peers: Vec::new(),
|
|
||||||
started: false,
|
|
||||||
};
|
|
||||||
for secret in author_secrets {
|
|
||||||
net.peers.push(MockPeer::new_with_spec(get_spec, Some(secret)));
|
|
||||||
}
|
|
||||||
for _ in net.peers.len()..nodes {
|
|
||||||
net.peers.push(MockPeer::new_with_spec(get_spec, None));
|
|
||||||
}
|
|
||||||
net
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn peer(&self, i: usize) -> Arc<MockPeer> {
|
|
||||||
self.peers.get(i).unwrap().clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn peer_mut(&mut self, i: usize) -> &mut MockPeer {
|
|
||||||
Arc::get_mut(self.peers.get_mut(i).unwrap()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn start(&mut self) {
|
|
||||||
for peer in 0..self.peers.len() {
|
|
||||||
for client in 0..self.peers.len() {
|
|
||||||
if peer != client {
|
|
||||||
let p = self.peers.get_mut(peer).unwrap();
|
|
||||||
let mut q = p.queue.write();
|
|
||||||
p.sync.write().on_peer_connected(&mut TestIo::new(p.client.clone(),
|
|
||||||
&p.snapshot_service,
|
|
||||||
&mut *q,
|
|
||||||
Some(client as PeerId)),
|
|
||||||
client as PeerId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sync_step(&mut self) {
|
|
||||||
for (i, peer0) in self.peers.iter().enumerate() {
|
|
||||||
let mut q0 = peer0.queue.write();
|
|
||||||
if let Some(packet) = q0.pop_front() {
|
|
||||||
let p = self.peers.get(packet.recipient).unwrap();
|
|
||||||
let mut q1 = p.queue.write();
|
|
||||||
trace!(target: "mocknet", "--- {} -> {} ---", i, packet.recipient);
|
|
||||||
ChainSync::dispatch_packet(&p.sync,
|
|
||||||
&mut TestIo::new(p.client.clone(),
|
|
||||||
&p.snapshot_service,
|
|
||||||
&mut *q1,
|
|
||||||
Some(i as PeerId)),
|
|
||||||
i as PeerId,
|
|
||||||
packet.packet_id,
|
|
||||||
&packet.data);
|
|
||||||
trace!(target: "mocknet", "----------------");
|
|
||||||
}
|
|
||||||
let p = self.peers.get(i).unwrap();
|
|
||||||
peer0.client.flush_queue();
|
|
||||||
let mut io = TestIo::new(peer0.client.clone(), &peer0.snapshot_service, &mut *q0, None);
|
|
||||||
p.sync.write().maintain_sync(&mut io);
|
|
||||||
p.sync.write().propagate_new_transactions(&mut io);
|
|
||||||
sleep(Duration::from_millis(10));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sync_step_peer(&mut self, peer_num: usize) {
|
|
||||||
let mut peer = self.peer_mut(peer_num);
|
|
||||||
let ref mut q = *peer.queue.write();
|
|
||||||
peer.sync.write().maintain_sync(&mut TestIo::new(peer.client.clone(), &peer.snapshot_service, q, None));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn restart_peer(&mut self, i: usize) {
|
|
||||||
let peer = self.peer_mut(i);
|
|
||||||
let ref mut q = *peer.queue.write();
|
|
||||||
peer.sync.write().restart(&mut TestIo::new(peer.client.clone(), &peer.snapshot_service, q, None));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sync(&mut self) -> u32 {
|
|
||||||
self.start();
|
|
||||||
let mut total_steps = 0;
|
|
||||||
while !self.done() {
|
|
||||||
self.sync_step();
|
|
||||||
total_steps += 1;
|
|
||||||
}
|
|
||||||
total_steps
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sync_steps(&mut self, count: usize) {
|
|
||||||
if !self.started {
|
|
||||||
self.start();
|
|
||||||
self.started = true;
|
|
||||||
}
|
|
||||||
for _ in 0..count {
|
|
||||||
self.sync_step();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn done(&self) -> bool {
|
|
||||||
self.peers.iter().all(|p| p.queue.try_read().unwrap().is_empty())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rand_peer(&self) -> Arc<MockPeer> {
|
|
||||||
thread_rng().choose(&self.peers).unwrap().clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn rand_simulation(&mut self, steps: usize) {
|
|
||||||
for _ in 0..steps {
|
|
||||||
self.rand_peer().issue_rand_tx();
|
|
||||||
sleep(Duration::from_millis(500));
|
|
||||||
self.sync();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_synced(&self, block: BlockNumber) {
|
|
||||||
let hash = self.peer(0).client.chain_info().best_block_hash;
|
|
||||||
for p in &self.peers {
|
|
||||||
let ci = p.client.chain_info();
|
|
||||||
assert_eq!(ci.best_block_number, block);
|
|
||||||
assert_eq!(ci.best_block_hash, hash);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -16,7 +16,5 @@
|
|||||||
|
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod snapshot;
|
pub mod snapshot;
|
||||||
pub mod mocknet;
|
|
||||||
mod chain;
|
mod chain;
|
||||||
mod rpc;
|
mod rpc;
|
||||||
mod auth_round;
|
|
||||||
|
Loading…
Reference in New Issue
Block a user