more simulation methods

This commit is contained in:
keorn 2016-09-20 15:48:17 +02:00
parent 44c4845d84
commit 9d23915caf
2 changed files with 35 additions and 19 deletions

View File

@ -22,15 +22,12 @@ use std::time::Duration;
use ethcore::client::BlockChainClient;
#[test]
fn 2_peer_1_tx_seal() {
fn two_peer_tx_seal() {
::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();
net.sync();
net.sync();
net.sync();
println!("{:?}", net.peer(0).client.chain_info());
println!("{:?}", net.peer(1).client.chain_info());
}
@ -39,21 +36,22 @@ fn 2_peer_1_tx_seal() {
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_tx();
net.peer(1).issue_rand_tx();
net.peer(1).issue_rand_tx();
net.peer(1).issue_rand_tx();
net.peer(1).issue_rand_tx();
net.peer(1).issue_rand_txs(5);
sleep(Duration::from_secs(1));
net.sync();
net.peer(0).issue_rand_txs(5);
net.sync();
net.peer(0).issue_rand_tx();
net.peer(0).issue_rand_tx();
net.peer(0).issue_rand_tx();
net.peer(0).issue_rand_tx();
net.peer(0).issue_rand_tx();
net.sync();
net.sync();
//println!("{:?}", net.peer(0).client.chain_info());
//println!("{:?}", net.peer(1).client.chain_info());
println!("{:?}", net.peer(0).client.chain_info());
println!("{:?}", net.peer(1).client.chain_info());
}
#[test]
fn rand_simulation() {
::env_logger::init().ok();
let mut net = MockNet::new_with_spec(2, vec!["1".sha3()], &Spec::new_test_round);
net.rand_simulation(10);
println!("{:?}", net.peer(0).client.chain_info());
println!("{:?}", net.peer(1).client.chain_info());
}

View File

@ -31,6 +31,7 @@ use ethcore::miner::Miner;
use ethcore::service::ClientService;
use std::time::Duration;
use std::thread::sleep;
use rand::{thread_rng, Rng};
pub struct TestIo<'p> {
pub client: Arc<Client>,
@ -196,6 +197,12 @@ impl MockPeer {
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 {
@ -264,7 +271,6 @@ impl MockNet {
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_secs(2));
}
}
@ -303,4 +309,16 @@ impl MockNet {
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(100));
self.sync();
}
}
}