refactor Miner to not wrap accounts in an RwLock, and to take a generalized AccountProvider

This commit is contained in:
Robert Habermeier 2016-05-30 13:10:33 +02:00
parent b036f1de98
commit 1465b0d34c

View File

@ -18,14 +18,14 @@ use rayon::prelude::*;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
use util::*; use util::*;
use util::keys::store::{AccountService, AccountProvider}; use util::keys::store::AccountProvider;
use ethcore::views::{BlockView, HeaderView}; use ethcore::views::{BlockView, HeaderView};
use ethcore::client::{BlockChainClient, BlockID}; use ethcore::client::{BlockChainClient, BlockID};
use ethcore::block::{ClosedBlock, IsBlock}; use ethcore::block::{ClosedBlock, IsBlock};
use ethcore::error::*; use ethcore::error::*;
use ethcore::client::{Executive, Executed, EnvInfo, TransactOptions}; use ethcore::client::{Executive, Executed, EnvInfo, TransactOptions};
use ethcore::transaction::SignedTransaction; use ethcore::transaction::SignedTransaction;
use ethcore::receipt::{Receipt}; use ethcore::receipt::Receipt;
use ethcore::spec::Spec; use ethcore::spec::Spec;
use ethcore::engine::Engine; use ethcore::engine::Engine;
use super::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin}; use super::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin};
@ -44,7 +44,7 @@ pub struct Miner {
extra_data: RwLock<Bytes>, extra_data: RwLock<Bytes>,
spec: Spec, spec: Spec,
accounts: RwLock<Option<Arc<AccountService>>>, // TODO: this is horrible since AccountService already contains a single RwLock field. refactor. accounts: Option<Arc<AccountProvider>>,
} }
impl Default for Miner { impl Default for Miner {
@ -58,7 +58,7 @@ impl Default for Miner {
gas_floor_target: RwLock::new(U256::zero()), gas_floor_target: RwLock::new(U256::zero()),
author: RwLock::new(Address::default()), author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()), extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(None), accounts: None,
spec: Spec::new_test(), spec: Spec::new_test(),
} }
} }
@ -76,13 +76,13 @@ impl Miner {
gas_floor_target: RwLock::new(U256::zero()), gas_floor_target: RwLock::new(U256::zero()),
author: RwLock::new(Address::default()), author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()), extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(None), accounts: None,
spec: spec, spec: spec,
}) })
} }
/// Creates new instance of miner /// Creates new instance of miner
pub fn with_accounts(force_sealing: bool, spec: Spec, accounts: Arc<AccountService>) -> Arc<Miner> { pub fn with_accounts(force_sealing: bool, spec: Spec, accounts: Arc<AccountProvider>) -> Arc<Miner> {
Arc::new(Miner { Arc::new(Miner {
transaction_queue: Mutex::new(TransactionQueue::new()), transaction_queue: Mutex::new(TransactionQueue::new()),
force_sealing: force_sealing, force_sealing: force_sealing,
@ -92,7 +92,7 @@ impl Miner {
gas_floor_target: RwLock::new(U256::zero()), gas_floor_target: RwLock::new(U256::zero()),
author: RwLock::new(Address::default()), author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()), extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(Some(accounts)), accounts: Some(accounts),
spec: spec, spec: spec,
}) })
} }
@ -177,9 +177,8 @@ impl Miner {
if !block.transactions().is_empty() { if !block.transactions().is_empty() {
trace!(target: "miner", "prepare_sealing: block has transaction - attempting internal seal."); trace!(target: "miner", "prepare_sealing: block has transaction - attempting internal seal.");
// block with transactions - see if we can seal immediately. // block with transactions - see if we can seal immediately.
let a = self.accounts.read().unwrap(); let s = self.engine().generate_seal(block.block(), match self.accounts {
let s = self.engine().generate_seal(block.block(), match *a.deref() { Some(ref x) => Some(&**x),
Some(ref x) => Some(x.deref() as &AccountProvider),
None => None, None => None,
}); });
if let Some(seal) = s { if let Some(seal) = s {