Merge pull request #1091 from ethcore/miner-spec-refact

Miner holds it's own copy of spec/engine
This commit is contained in:
Marek Kotewicz 2016-05-19 09:40:30 +02:00
commit 7d873b2c81
9 changed files with 24 additions and 27 deletions

View File

@ -433,10 +433,6 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
block.try_seal(self.engine.deref().deref(), seal)
}
fn engine(&self) -> &Engine {
self.engine.deref().deref()
}
fn vm_factory(&self) -> &EvmFactory {
&self.vm_factory
}

View File

@ -33,17 +33,15 @@ use std::collections::HashSet;
use util::bytes::Bytes;
use util::hash::{Address, H256, H2048};
use util::numbers::U256;
use util::keys::store::AccountProvider;
use blockchain::TreeRoute;
use block_queue::BlockQueueInfo;
use block::{ExecutedBlock, ClosedBlock, LockedBlock, SealedBlock};
use block::{ClosedBlock, LockedBlock, SealedBlock};
use header::{BlockNumber, Header};
use transaction::{LocalizedTransaction, SignedTransaction};
use log_entry::LocalizedLogEntry;
use filter::Filter;
use error::{ImportResult, ExecutionError};
use receipt::LocalizedReceipt;
use engine::{Engine};
use trace::LocalizedTrace;
use evm::Factory as EvmFactory;
@ -135,12 +133,6 @@ pub trait BlockChainClient : Sync + Send {
/// Makes a non-persistent transaction call.
fn call(&self, t: &SignedTransaction) -> Result<Executed, ExecutionError>;
/// Attempt to seal the block internally. See `Engine`.
fn generate_seal(&self, block: &ExecutedBlock, accounts: Option<&AccountProvider>) -> Option<Vec<Bytes>> { self.engine().generate_seal(block, accounts) }
/// Executes a function providing it with a reference to an engine.
fn engine(&self) -> &Engine;
/// Returns EvmFactory.
fn vm_factory(&self) -> &EvmFactory;

View File

@ -33,7 +33,6 @@ use block_queue::BlockQueueInfo;
use block::{SealedBlock, ClosedBlock, LockedBlock};
use executive::Executed;
use error::{ExecutionError};
use engine::Engine;
use trace::LocalizedTrace;
/// Test client.
@ -431,10 +430,6 @@ impl BlockChainClient for TestBlockChainClient {
}
}
fn engine(&self) -> &Engine {
unimplemented!();
}
fn vm_factory(&self) -> &EvmFactory {
unimplemented!();
}

View File

@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Consensus engine specification
use common::*;
use util::keys::store::AccountProvider;
use block::ExecutedBlock;

View File

@ -107,6 +107,7 @@ pub mod trace;
pub mod spec;
pub mod views;
pub mod pod_state;
pub mod engine;
mod db;
mod common;
@ -116,7 +117,6 @@ mod env_info;
mod pod_account;
mod account_diff;
mod state_diff;
mod engine;
mod state;
mod account;
mod account_db;

View File

@ -25,6 +25,8 @@ use ethcore::block::{ClosedBlock, IsBlock};
use ethcore::error::*;
use ethcore::client::{Executive, Executed, EnvInfo, TransactOptions};
use ethcore::transaction::SignedTransaction;
use ethcore::spec::Spec;
use ethcore::engine::Engine;
use super::{MinerService, MinerStatus, TransactionQueue, AccountDetails, TransactionImportResult, TransactionOrigin};
/// Keeps track of transactions using priority queue and holds currently mined block.
@ -39,6 +41,7 @@ pub struct Miner {
gas_floor_target: RwLock<U256>,
author: RwLock<Address>,
extra_data: RwLock<Bytes>,
spec: Spec,
accounts: RwLock<Option<Arc<AccountService>>>, // TODO: this is horrible since AccountService already contains a single RwLock field. refactor.
}
@ -55,13 +58,14 @@ impl Default for Miner {
author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(None),
spec: Spec::new_test(),
}
}
}
impl Miner {
/// Creates new instance of miner
pub fn new(force_sealing: bool) -> Arc<Miner> {
pub fn new(force_sealing: bool, spec: Spec) -> Arc<Miner> {
Arc::new(Miner {
transaction_queue: Mutex::new(TransactionQueue::new()),
force_sealing: force_sealing,
@ -72,11 +76,12 @@ impl Miner {
author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(None),
spec: spec,
})
}
/// Creates new instance of miner
pub fn with_accounts(force_sealing: bool, accounts: Arc<AccountService>) -> Arc<Miner> {
pub fn with_accounts(force_sealing: bool, spec: Spec, accounts: Arc<AccountService>) -> Arc<Miner> {
Arc::new(Miner {
transaction_queue: Mutex::new(TransactionQueue::new()),
force_sealing: force_sealing,
@ -87,9 +92,14 @@ impl Miner {
author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()),
accounts: RwLock::new(Some(accounts)),
spec: spec,
})
}
fn engine(&self) -> &Engine {
self.spec.engine.deref()
}
/// Prepares new block for sealing including top transactions from queue.
#[cfg_attr(feature="dev", allow(match_same_arms))]
fn prepare_sealing(&self, chain: &BlockChainClient) {
@ -111,7 +121,7 @@ impl Miner {
Some(old_block) => {
trace!(target: "miner", "Already have previous work; updating and returning");
// add transactions to old_block
let e = chain.engine();
let e = self.engine();
let mut invalid_transactions = HashSet::new();
let mut block = old_block.reopen(e, chain.vm_factory());
let block_number = block.block().fields().header.number();
@ -166,7 +176,7 @@ impl Miner {
trace!(target: "miner", "prepare_sealing: block has transaction - attempting internal seal.");
// block with transactions - see if we can seal immediately.
let a = self.accounts.read().unwrap();
let s = chain.generate_seal(block.block(), match *a.deref() {
let s = self.engine().generate_seal(block.block(), match *a.deref() {
Some(ref x) => Some(x.deref() as &AccountProvider),
None => None,
});
@ -267,7 +277,8 @@ impl MinerService for Miner {
state.sub_balance(&sender, &balance);
state.add_balance(&sender, &U256::max_value());
let options = TransactOptions { tracing: false, check_nonce: false };
Executive::new(&mut state, &env_info, chain.engine(), chain.vm_factory()).transact(t, options)
Executive::new(&mut state, &env_info, self.engine(), chain.vm_factory()).transact(t, options)
},
None => {
chain.call(t)

View File

@ -145,7 +145,7 @@ fn execute_client(conf: Configuration) {
let client = service.client();
// Miner
let miner = Miner::with_accounts(conf.args.flag_force_sealing, account_service.clone());
let miner = Miner::with_accounts(conf.args.flag_force_sealing, conf.spec(), account_service.clone());
miner.set_author(conf.author());
miner.set_gas_floor_target(conf.gas_floor_target());
miner.set_extra_data(conf.extra_data());

View File

@ -45,7 +45,7 @@
//! let mut service = NetworkService::start(NetworkConfiguration::new()).unwrap();
//! let dir = env::temp_dir();
//! let client = Client::new(ClientConfig::default(), ethereum::new_frontier(), &dir, service.io().channel());
//! let miner = Miner::new(false);
//! let miner = Miner::new(false, ethereum::new_frontier());
//! EthSync::register(&mut service, SyncConfig::default(), client, miner);
//! }
//! ```

View File

@ -16,6 +16,7 @@
use util::*;
use ethcore::client::{TestBlockChainClient, BlockChainClient};
use ethcore::spec::Spec;
use io::SyncIo;
use chain::ChainSync;
use ethminer::Miner;
@ -93,7 +94,7 @@ impl TestNet {
for _ in 0..n {
net.peers.push(TestPeer {
chain: TestBlockChainClient::new(),
sync: ChainSync::new(SyncConfig::default(), Miner::new(false)),
sync: ChainSync::new(SyncConfig::default(), Miner::new(false, Spec::new_test())),
queue: VecDeque::new(),
});
}