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

Miner tests #1597

Merged
merged 3 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ethcore/src/client/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl MiningBlockChainClient for TestBlockChainClient {
let mut db_result = get_temp_journal_db();
let mut db = db_result.take();
self.spec.ensure_db_good(db.as_hashdb_mut());

let last_hashes = vec![genesis_header.hash()];
OpenBlock::new(
engine.deref(),
Expand Down
89 changes: 87 additions & 2 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,9 @@ impl MinerService for Miner {
let imported = {
// Be sure to release the lock before we call enable_and_prepare_sealing
let mut transaction_queue = self.transaction_queue.lock();
let import = self.add_transactions_to_queue(chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue).pop().unwrap();
let import = self.add_transactions_to_queue(
chain, vec![transaction], TransactionOrigin::Local, &mut transaction_queue
).pop().unwrap();

match import {
Ok(ref res) => {
Expand Down Expand Up @@ -834,10 +836,13 @@ impl MinerService for Miner {
#[cfg(test)]
mod tests {

use std::time::Duration;
use super::super::MinerService;
use super::Miner;
use super::*;
use util::*;
use client::{TestBlockChainClient, EachBlockWith};
use client::{TransactionImportResult};
use types::transaction::{Transaction, Action};
use block::*;
use spec::Spec;

Expand Down Expand Up @@ -872,4 +877,84 @@ mod tests {
// solution to original work submitted.
assert!(miner.submit_seal(&client, res.unwrap(), vec![]).is_ok());
}

fn miner() -> Miner {
Arc::try_unwrap(Miner::new(
MinerOptions {
new_work_notify: Vec::new(),
force_sealing: false,
reseal_on_external_tx: false,
reseal_on_own_tx: true,
reseal_min_period: Duration::from_secs(5),
tx_gas_limit: !U256::zero(),
tx_queue_size: 1024,
pending_set: PendingSet::AlwaysSealing,
work_queue_size: 5,
enable_resubmission: true,
},
GasPricer::new_fixed(0u64.into()),
Spec::new_test(),
None, // accounts provider
)).ok().expect("Miner was just created.")
}

#[test]
fn should_make_pending_block_when_importing_own_transaction() {
// given
let client = TestBlockChainClient::default();
let miner = miner();
let transaction = {
let keypair = KeyPair::create().unwrap();
Transaction {
action: Action::Create,
value: U256::zero(),
data: "3331600055".from_hex().unwrap(),
gas: U256::from(100_000),
gas_price: U256::zero(),
nonce: U256::zero(),
}.sign(keypair.secret())
};

// when
let res = miner.import_own_transaction(&client, transaction);

// then
assert_eq!(res.unwrap(), TransactionImportResult::Current);
assert_eq!(miner.all_transactions().len(), 1);
assert_eq!(miner.pending_transactions().len(), 1);
assert_eq!(miner.pending_transactions_hashes().len(), 1);
assert_eq!(miner.pending_receipts().len(), 1);
// This method will let us know if pending block was created (before calling that method)
assert_eq!(miner.enable_and_prepare_sealing(&client), false);
}

#[test]
fn should_import_external_transaction() {
// given
let client = TestBlockChainClient::default();
let miner = miner();
let transaction = {
let keypair = KeyPair::create().unwrap();
Transaction {
action: Action::Create,
value: U256::zero(),
data: "3331600055".from_hex().unwrap(),
gas: U256::from(100_000),
gas_price: U256::zero(),
nonce: U256::zero(),
}.sign(keypair.secret())
};

// when
let res = miner.import_external_transactions(&client, vec![transaction]).pop().unwrap();

// then
assert_eq!(res.unwrap(), TransactionImportResult::Current);
assert_eq!(miner.all_transactions().len(), 1);
assert_eq!(miner.pending_transactions_hashes().len(), 0);
assert_eq!(miner.pending_transactions().len(), 0);
assert_eq!(miner.pending_receipts().len(), 0);
// This method will let us know if pending block was created (before calling that method)
assert_eq!(miner.enable_and_prepare_sealing(&client), true);
}
}
4 changes: 2 additions & 2 deletions ethcore/src/miner/price_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl PriceInfo {
}
}

//#[ignore]
#[test]
fn should_get_price_info() {
use std::sync::Arc;
Expand All @@ -88,9 +87,10 @@ fn should_get_price_info() {
init_log();
let done = Arc::new((Mutex::new(PriceInfo { ethusd: 0f32 }), Condvar::new()));
let rdone = done.clone();

PriceInfo::get(move |price| { let mut p = rdone.0.lock(); *p = price; rdone.1.notify_one(); }).unwrap();
let mut p = done.0.lock();
let t = done.1.wait_for(&mut p, Duration::from_millis(10000));
assert!(!t.timed_out());
assert!(p.ethusd != 0f32);
}
}
2 changes: 1 addition & 1 deletion rpc/src/v1/tests/helpers/miner_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl MinerService for TestMinerService {
}

fn map_sealing_work<F, T>(&self, _chain: &MiningBlockChainClient, _f: F) -> Option<T> where F: FnOnce(&ClosedBlock) -> T {
unimplemented!();
None
}

fn transaction(&self, hash: &H256) -> Option<SignedTransaction> {
Expand Down
5 changes: 2 additions & 3 deletions rpc/src/v1/tests/mocked/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct EthTester {
pub client: Arc<TestBlockChainClient>,
pub sync: Arc<TestSyncProvider>,
pub accounts_provider: Arc<AccountProvider>,
miner: Arc<TestMinerService>,
pub miner: Arc<TestMinerService>,
hashrates: Arc<RwLock<HashMap<H256, U256>>>,
pub io: IoHandler,
}
Expand Down Expand Up @@ -742,13 +742,12 @@ fn returns_no_work_if_cant_mine() {
assert_eq!(eth_tester.io.handle_request(request), Some(response.to_owned()));
}

#[ignore]
// enable once TestMinerService supports the mining API.
#[test]
fn returns_error_if_can_mine_and_no_closed_block() {
use ethsync::{SyncState};

let eth_tester = EthTester::default();
eth_tester.miner.set_author(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap());
eth_tester.sync.status.write().state = SyncState::Idle;

let request = r#"{"jsonrpc": "2.0", "method": "eth_getWork", "params": [], "id": 1}"#;
Expand Down
2 changes: 1 addition & 1 deletion util/src/io/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Worker {
if deleting.load(AtomicOrdering::Acquire) {
return;
}
let _ = wait.wait(&mut lock);
wait.wait(&mut lock);
}

if deleting.load(AtomicOrdering::Acquire) {
Expand Down
12 changes: 1 addition & 11 deletions util/table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

//! A collection associating pair of keys (row and column) with a single value.

use std::default::Default;
use std::hash::Hash;
use std::collections::HashMap;

Expand All @@ -25,22 +24,13 @@ use std::collections::HashMap;
/// You can obviously use `HashMap<(Row,Col), Val>`, but this structure gives
/// you better access to all `Columns` in Specific `Row`. Namely you can get sub-hashmap
/// `HashMap<Col, Val>` for specific `Row`
#[derive(Default, Debug, PartialEq)]
pub struct Table<Row, Col, Val>
where Row: Eq + Hash + Clone,
Col: Eq + Hash {
map: HashMap<Row, HashMap<Col, Val>>,
}

impl<Row, Col, Val> Default for Table<Row, Col, Val>
where Row: Eq + Hash + Clone,
Col: Eq + Hash {
fn default() -> Self {
Table::new()
}
}

// There is default but clippy does not detect it?
#[cfg_attr(feature="dev", allow(new_without_default))]
impl<Row, Col, Val> Table<Row, Col, Val>
where Row: Eq + Hash + Clone,
Col: Eq + Hash {
Expand Down