2016-03-08 15:46:44 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use rayon::prelude::*;
|
2016-03-11 14:48:30 +01:00
|
|
|
use std::sync::{Mutex, RwLock, Arc};
|
|
|
|
use std::sync::atomic;
|
|
|
|
use std::sync::atomic::AtomicBool;
|
2016-03-15 23:01:36 +01:00
|
|
|
use std::collections::HashSet;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2016-03-14 02:00:22 +01:00
|
|
|
use util::{H256, U256, Address, Bytes, Uint};
|
2016-03-09 13:28:37 +01:00
|
|
|
use ethcore::views::{BlockView};
|
|
|
|
use ethcore::client::{BlockChainClient, BlockId};
|
2016-03-17 11:19:12 +01:00
|
|
|
use ethcore::block::{ClosedBlock, IsBlock};
|
2016-03-11 14:48:30 +01:00
|
|
|
use ethcore::error::{Error};
|
2016-03-08 15:46:44 +01:00
|
|
|
use ethcore::transaction::SignedTransaction;
|
2016-03-16 10:40:33 +01:00
|
|
|
use super::{MinerService, MinerStatus, TransactionQueue, AccountDetails};
|
2016-03-08 15:46:44 +01:00
|
|
|
|
2016-03-09 14:26:28 +01:00
|
|
|
/// Keeps track of transactions using priority queue and holds currently mined block.
|
2016-03-08 15:46:44 +01:00
|
|
|
pub struct Miner {
|
|
|
|
transaction_queue: Mutex<TransactionQueue>,
|
|
|
|
|
|
|
|
// for sealing...
|
|
|
|
sealing_enabled: AtomicBool,
|
|
|
|
sealing_block: Mutex<Option<ClosedBlock>>,
|
2016-03-14 02:00:22 +01:00
|
|
|
gas_floor_target: RwLock<U256>,
|
2016-03-08 15:46:44 +01:00
|
|
|
author: RwLock<Address>,
|
|
|
|
extra_data: RwLock<Bytes>,
|
2016-03-14 02:00:22 +01:00
|
|
|
|
2016-03-08 15:46:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 14:26:28 +01:00
|
|
|
impl Default for Miner {
|
|
|
|
fn default() -> Miner {
|
2016-03-08 15:46:44 +01:00
|
|
|
Miner {
|
|
|
|
transaction_queue: Mutex::new(TransactionQueue::new()),
|
|
|
|
sealing_enabled: AtomicBool::new(false),
|
|
|
|
sealing_block: Mutex::new(None),
|
2016-03-14 02:00:22 +01:00
|
|
|
gas_floor_target: RwLock::new(U256::zero()),
|
2016-03-11 14:48:30 +01:00
|
|
|
author: RwLock::new(Address::default()),
|
2016-03-08 15:46:44 +01:00
|
|
|
extra_data: RwLock::new(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 14:26:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Miner {
|
|
|
|
/// Creates new instance of miner
|
|
|
|
pub fn new() -> Arc<Miner> {
|
|
|
|
Arc::new(Miner::default())
|
|
|
|
}
|
2016-03-08 15:46:44 +01:00
|
|
|
|
2016-03-09 13:28:37 +01:00
|
|
|
/// Get the author that we will seal blocks as.
|
|
|
|
fn author(&self) -> Address {
|
|
|
|
*self.author.read().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the extra_data that we will seal blocks wuth.
|
|
|
|
fn extra_data(&self) -> Bytes {
|
|
|
|
self.extra_data.read().unwrap().clone()
|
|
|
|
}
|
2016-03-09 15:22:06 +01:00
|
|
|
|
2016-03-14 02:00:22 +01:00
|
|
|
/// Get the extra_data that we will seal blocks wuth.
|
|
|
|
fn gas_floor_target(&self) -> U256 {
|
2016-03-16 10:40:33 +01:00
|
|
|
*self.gas_floor_target.read().unwrap()
|
2016-03-14 02:00:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 15:22:06 +01:00
|
|
|
/// Set the author that we will seal blocks as.
|
|
|
|
pub fn set_author(&self, author: Address) {
|
|
|
|
*self.author.write().unwrap() = author;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the extra_data that we will seal blocks with.
|
|
|
|
pub fn set_extra_data(&self, extra_data: Bytes) {
|
|
|
|
*self.extra_data.write().unwrap() = extra_data;
|
|
|
|
}
|
|
|
|
|
2016-03-14 02:00:22 +01:00
|
|
|
/// Set the gas limit we wish to target when sealing a new block.
|
|
|
|
pub fn set_gas_floor_target(&self, target: U256) {
|
|
|
|
*self.gas_floor_target.write().unwrap() = target;
|
|
|
|
}
|
|
|
|
|
2016-03-09 15:22:06 +01:00
|
|
|
/// Set minimal gas price of transaction to be accepted for mining.
|
|
|
|
pub fn set_minimal_gas_price(&self, min_gas_price: U256) {
|
|
|
|
self.transaction_queue.lock().unwrap().set_minimal_gas_price(min_gas_price);
|
|
|
|
}
|
2016-03-09 13:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MinerService for Miner {
|
|
|
|
|
2016-03-09 14:26:28 +01:00
|
|
|
fn clear_and_reset(&self, chain: &BlockChainClient) {
|
|
|
|
self.transaction_queue.lock().unwrap().clear();
|
|
|
|
self.prepare_sealing(chain);
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:28:37 +01:00
|
|
|
fn status(&self) -> MinerStatus {
|
2016-03-08 16:23:32 +01:00
|
|
|
let status = self.transaction_queue.lock().unwrap().status();
|
2016-03-17 11:19:12 +01:00
|
|
|
let block = self.sealing_block.lock().unwrap();
|
2016-03-08 16:23:32 +01:00
|
|
|
MinerStatus {
|
2016-03-17 11:19:12 +01:00
|
|
|
transactions_in_pending_queue: status.pending,
|
|
|
|
transactions_in_future_queue: status.future,
|
|
|
|
transactions_in_pending_block: block.as_ref().map_or(0, |b| b.transactions().len()),
|
2016-03-08 16:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:40:33 +01:00
|
|
|
fn import_transactions<T>(&self, transactions: Vec<SignedTransaction>, fetch_account: T) -> Result<(), Error>
|
|
|
|
where T: Fn(&Address) -> AccountDetails {
|
2016-03-08 15:46:44 +01:00
|
|
|
let mut transaction_queue = self.transaction_queue.lock().unwrap();
|
2016-03-16 10:40:33 +01:00
|
|
|
transaction_queue.add_all(transactions, fetch_account)
|
2016-03-08 15:46:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:00:55 +01:00
|
|
|
fn pending_transactions_hashes(&self) -> Vec<H256> {
|
|
|
|
let transaction_queue = self.transaction_queue.lock().unwrap();
|
|
|
|
transaction_queue.pending_hashes()
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:28:37 +01:00
|
|
|
fn prepare_sealing(&self, chain: &BlockChainClient) {
|
2016-03-17 13:41:08 +01:00
|
|
|
let transactions = self.transaction_queue.lock().unwrap().top_transactions();
|
2016-03-09 12:54:07 +01:00
|
|
|
let b = chain.prepare_sealing(
|
|
|
|
self.author(),
|
2016-03-14 02:00:22 +01:00
|
|
|
self.gas_floor_target(),
|
2016-03-09 12:54:07 +01:00
|
|
|
self.extra_data(),
|
2016-03-14 02:02:32 +01:00
|
|
|
transactions,
|
2016-03-09 12:54:07 +01:00
|
|
|
);
|
2016-03-08 15:46:44 +01:00
|
|
|
*self.sealing_block.lock().unwrap() = b;
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:28:37 +01:00
|
|
|
fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex<Option<ClosedBlock>> {
|
2016-03-08 15:46:44 +01:00
|
|
|
if self.sealing_block.lock().unwrap().is_none() {
|
|
|
|
self.sealing_enabled.store(true, atomic::Ordering::Relaxed);
|
|
|
|
// TODO: Above should be on a timer that resets after two blocks have arrived without being asked for.
|
|
|
|
self.prepare_sealing(chain);
|
|
|
|
}
|
|
|
|
&self.sealing_block
|
|
|
|
}
|
|
|
|
|
2016-03-09 13:28:37 +01:00
|
|
|
fn submit_seal(&self, chain: &BlockChainClient, pow_hash: H256, seal: Vec<Bytes>) -> Result<(), Error> {
|
2016-03-08 15:46:44 +01:00
|
|
|
let mut maybe_b = self.sealing_block.lock().unwrap();
|
|
|
|
match *maybe_b {
|
|
|
|
Some(ref b) if b.hash() == pow_hash => {}
|
|
|
|
_ => { return Err(Error::PowHashInvalid); }
|
|
|
|
}
|
|
|
|
|
|
|
|
let b = maybe_b.take();
|
|
|
|
match chain.try_seal(b.unwrap(), seal) {
|
|
|
|
Err(old) => {
|
|
|
|
*maybe_b = Some(old);
|
|
|
|
Err(Error::PowInvalid)
|
|
|
|
}
|
|
|
|
Ok(sealed) => {
|
|
|
|
// TODO: commit DB from `sealed.drain` and make a VerifiedBlock to skip running the transactions twice.
|
|
|
|
try!(chain.import_block(sealed.rlp_bytes()));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-13 15:36:03 +01:00
|
|
|
fn chain_new_blocks(&self, chain: &BlockChainClient, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]) {
|
2016-03-08 15:46:44 +01:00
|
|
|
fn fetch_transactions(chain: &BlockChainClient, hash: &H256) -> Vec<SignedTransaction> {
|
|
|
|
let block = chain
|
2016-03-10 14:06:47 +01:00
|
|
|
.block(BlockId::Hash(*hash))
|
2016-03-08 15:46:44 +01:00
|
|
|
// Client should send message after commit to db and inserting to chain.
|
|
|
|
.expect("Expected in-chain blocks.");
|
|
|
|
let block = BlockView::new(&block);
|
|
|
|
block.transactions()
|
|
|
|
}
|
|
|
|
{
|
2016-03-13 15:29:55 +01:00
|
|
|
let out_of_chain = retracted
|
|
|
|
.par_iter()
|
2016-03-13 15:36:03 +01:00
|
|
|
.map(|h| fetch_transactions(chain, h));
|
2016-03-13 15:29:55 +01:00
|
|
|
out_of_chain.for_each(|txs| {
|
2016-03-08 15:46:44 +01:00
|
|
|
// populate sender
|
|
|
|
for tx in &txs {
|
|
|
|
let _sender = tx.sender();
|
|
|
|
}
|
|
|
|
let mut transaction_queue = self.transaction_queue.lock().unwrap();
|
2016-03-16 10:40:33 +01:00
|
|
|
let _ = transaction_queue.add_all(txs, |a| AccountDetails {
|
|
|
|
nonce: chain.nonce(a),
|
|
|
|
balance: chain.balance(a)
|
|
|
|
});
|
2016-03-08 15:46:44 +01:00
|
|
|
});
|
|
|
|
}
|
2016-03-15 23:01:36 +01:00
|
|
|
// First import all transactions and after that remove old ones
|
|
|
|
{
|
|
|
|
let in_chain = {
|
|
|
|
let mut in_chain = HashSet::new();
|
|
|
|
in_chain.extend(imported);
|
|
|
|
in_chain.extend(enacted);
|
|
|
|
in_chain.extend(invalid);
|
|
|
|
in_chain
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<H256>>()
|
|
|
|
};
|
|
|
|
|
|
|
|
let in_chain = in_chain
|
|
|
|
.par_iter()
|
|
|
|
.map(|h: &H256| fetch_transactions(chain, h));
|
|
|
|
|
|
|
|
in_chain.for_each(|txs| {
|
|
|
|
let hashes = txs.iter().map(|tx| tx.hash()).collect::<Vec<H256>>();
|
|
|
|
let mut transaction_queue = self.transaction_queue.lock().unwrap();
|
2016-03-16 10:40:33 +01:00
|
|
|
transaction_queue.remove_all(&hashes, |a| AccountDetails {
|
|
|
|
nonce: chain.nonce(a),
|
|
|
|
balance: chain.balance(a)
|
|
|
|
});
|
2016-03-15 23:01:36 +01:00
|
|
|
});
|
|
|
|
}
|
2016-03-08 15:46:44 +01:00
|
|
|
|
|
|
|
if self.sealing_enabled.load(atomic::Ordering::Relaxed) {
|
|
|
|
self.prepare_sealing(chain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|