* Miner tweaks (#1797) * Mining fixes. - Use queue to determine whether we're mining - Kick stale hash rates Fixes #1794 Fixes #1641 * Fix tests. * Address grumbles. * Fixed tests
This commit is contained in:
@@ -283,7 +283,7 @@ impl<C, S, M, EM> Eth for EthClient<C, S, M, EM> where
|
||||
|
||||
fn is_mining(&self, params: Params) -> Result<Value, Error> {
|
||||
match params {
|
||||
Params::None => to_value(&self.external_miner.is_mining()),
|
||||
Params::None => to_value(&(take_weak!(self.miner).is_sealing())),
|
||||
_ => Err(Error::invalid_params())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,6 +205,10 @@ impl MinerService for TestMinerService {
|
||||
self.last_nonces.read().unwrap().get(address).cloned()
|
||||
}
|
||||
|
||||
fn is_sealing(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Submit `seal` as a valid solution for the header of `pow_hash`.
|
||||
/// Will check the seal, but not actually insert the block into the chain.
|
||||
fn submit_seal(&self, _chain: &MiningBlockChainClient, _pow_hash: H256, _seal: Vec<Bytes>) -> Result<(), Error> {
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use std::sync::{Arc, RwLock, Mutex};
|
||||
use std::time::{Instant, Duration};
|
||||
use jsonrpc_core::IoHandler;
|
||||
use util::hash::{Address, H256, FixedHash};
|
||||
use util::numbers::{Uint, U256};
|
||||
@@ -55,8 +56,8 @@ struct EthTester {
|
||||
pub client: Arc<TestBlockChainClient>,
|
||||
pub sync: Arc<TestSyncProvider>,
|
||||
pub accounts_provider: Arc<AccountProvider>,
|
||||
miner: Arc<TestMinerService>,
|
||||
hashrates: Arc<RwLock<HashMap<H256, U256>>>,
|
||||
pub miner: Arc<TestMinerService>,
|
||||
hashrates: Arc<Mutex<HashMap<H256, (Instant, U256)>>>,
|
||||
pub io: IoHandler,
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ impl Default for EthTester {
|
||||
let sync = sync_provider();
|
||||
let ap = accounts_provider();
|
||||
let miner = miner_service();
|
||||
let hashrates = Arc::new(RwLock::new(HashMap::new()));
|
||||
let hashrates = Arc::new(Mutex::new(HashMap::new()));
|
||||
let external_miner = Arc::new(ExternalMiner::new(hashrates.clone()));
|
||||
let eth = EthClient::new(&client, &sync, &ap, &miner, &external_miner, true).to_delegate();
|
||||
let sign = EthSigningUnsafeClient::new(&client, &ap, &miner).to_delegate();
|
||||
@@ -132,9 +133,9 @@ fn rpc_eth_syncing() {
|
||||
#[test]
|
||||
fn rpc_eth_hashrate() {
|
||||
let tester = EthTester::default();
|
||||
tester.hashrates.write().unwrap().insert(H256::from(0), U256::from(0xfffa));
|
||||
tester.hashrates.write().unwrap().insert(H256::from(0), U256::from(0xfffb));
|
||||
tester.hashrates.write().unwrap().insert(H256::from(1), U256::from(0x1));
|
||||
tester.hashrates.lock().unwrap().insert(H256::from(0), (Instant::now() + Duration::from_secs(2), U256::from(0xfffa)));
|
||||
tester.hashrates.lock().unwrap().insert(H256::from(0), (Instant::now() + Duration::from_secs(2), U256::from(0xfffb)));
|
||||
tester.hashrates.lock().unwrap().insert(H256::from(1), (Instant::now() + Duration::from_secs(2), U256::from(0x1)));
|
||||
|
||||
let request = r#"{"jsonrpc": "2.0", "method": "eth_hashrate", "params": [], "id": 1}"#;
|
||||
let response = r#"{"jsonrpc":"2.0","result":"0xfffc","id":1}"#;
|
||||
@@ -157,8 +158,8 @@ fn rpc_eth_submit_hashrate() {
|
||||
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
|
||||
|
||||
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
|
||||
assert_eq!(tester.hashrates.read().unwrap().get(&H256::from("0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c")).cloned(),
|
||||
Some(U256::from(0x500_000)));
|
||||
assert_eq!(tester.hashrates.lock().unwrap().get(&H256::from("0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c")).cloned().unwrap().1,
|
||||
U256::from(0x500_000));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -209,16 +210,11 @@ fn rpc_eth_author() {
|
||||
#[test]
|
||||
fn rpc_eth_mining() {
|
||||
let tester = EthTester::default();
|
||||
tester.miner.set_author(Address::from_str("d46e8dd67c5d32be8058bb8eb970870f07244567").unwrap());
|
||||
|
||||
let request = r#"{"jsonrpc": "2.0", "method": "eth_mining", "params": [], "id": 1}"#;
|
||||
let response = r#"{"jsonrpc":"2.0","result":false,"id":1}"#;
|
||||
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
|
||||
|
||||
tester.hashrates.write().unwrap().insert(H256::from(1), U256::from(0x1));
|
||||
|
||||
let request = r#"{"jsonrpc": "2.0", "method": "eth_mining", "params": [], "id": 1}"#;
|
||||
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
|
||||
assert_eq!(tester.io.handle_request(request), Some(response.to_owned()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user