2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2016-03-08 15:46:44 +01:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
2016-03-09 14:26:28 +01:00
|
|
|
//! Miner module
|
2018-04-13 17:34:27 +02:00
|
|
|
//! Keeps track of transactions and currently sealed pending block.
|
2016-03-09 14:26:28 +01:00
|
|
|
|
2016-11-16 10:45:55 +01:00
|
|
|
mod miner;
|
2018-03-03 18:42:13 +01:00
|
|
|
mod service_transaction_checker;
|
2016-03-08 15:46:44 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
pub mod pool_client;
|
|
|
|
pub mod stratum;
|
2016-03-09 13:28:37 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
pub use self::miner::{Miner, MinerOptions, Penalization, PendingSet, AuthoringParams};
|
2018-06-12 08:22:54 +02:00
|
|
|
pub use ethcore_miner::pool::PendingOrdering;
|
2018-01-11 17:49:10 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
use std::sync::Arc;
|
2018-07-04 17:37:55 +02:00
|
|
|
use std::collections::{BTreeSet, BTreeMap};
|
2018-01-11 17:49:10 +01:00
|
|
|
|
2018-03-03 18:42:13 +01:00
|
|
|
use bytes::Bytes;
|
2018-04-13 17:34:27 +02:00
|
|
|
use ethereum_types::{H256, U256, Address};
|
|
|
|
use ethcore_miner::pool::{VerifiedTransaction, QueueStatus, local_transactions};
|
|
|
|
|
|
|
|
use block::{Block, SealedBlock};
|
2018-03-03 18:42:13 +01:00
|
|
|
use client::{
|
2018-04-13 17:34:27 +02:00
|
|
|
CallContract, RegistryInfo, ScheduleInfo,
|
|
|
|
BlockChain, BlockProducer, SealedBlockImporter, ChainInfo,
|
|
|
|
AccountData, Nonce,
|
2018-03-03 18:42:13 +01:00
|
|
|
};
|
2018-04-13 17:34:27 +02:00
|
|
|
use error::Error;
|
2018-03-03 18:42:13 +01:00
|
|
|
use header::{BlockNumber, Header};
|
2016-08-17 19:25:02 +02:00
|
|
|
use receipt::{RichReceipt, Receipt};
|
2018-04-13 17:34:27 +02:00
|
|
|
use transaction::{self, UnverifiedTransaction, SignedTransaction, PendingTransaction};
|
2018-03-03 18:42:13 +01:00
|
|
|
use state::StateInfo;
|
2018-06-22 15:09:15 +02:00
|
|
|
use ethkey::Password;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Provides methods to verify incoming external transactions
|
|
|
|
pub trait TransactionVerifierClient: Send + Sync
|
|
|
|
// Required for ServiceTransactionChecker
|
|
|
|
+ CallContract + RegistryInfo
|
|
|
|
// Required for verifiying transactions
|
|
|
|
+ BlockChain + ScheduleInfo + AccountData
|
|
|
|
{}
|
|
|
|
|
|
|
|
/// Extended client interface used for mining
|
|
|
|
pub trait BlockChainClient: TransactionVerifierClient + BlockProducer + SealedBlockImporter {}
|
|
|
|
|
2016-03-11 14:48:30 +01:00
|
|
|
/// Miner client API
|
|
|
|
pub trait MinerService : Send + Sync {
|
2018-03-03 18:42:13 +01:00
|
|
|
/// Type representing chain state
|
|
|
|
type State: StateInfo + 'static;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Sealing
|
2016-04-13 00:04:40 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// 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, pow_hash: H256, seal: Vec<Bytes>) -> Result<SealedBlock, Error>;
|
2016-04-13 00:04:40 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Is it currently sealing?
|
|
|
|
fn is_currently_sealing(&self) -> bool;
|
2016-03-22 19:12:17 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get the sealing work package preparing it if doesn't exist yet.
|
|
|
|
///
|
|
|
|
/// Returns `None` if engine seals internally.
|
|
|
|
fn work_package<C>(&self, chain: &C) -> Option<(H256, BlockNumber, u64, U256)>
|
|
|
|
where C: BlockChain + CallContract + BlockProducer + SealedBlockImporter + Nonce + Sync;
|
2016-04-11 21:06:32 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Update current pending block
|
|
|
|
fn update_sealing<C>(&self, chain: &C)
|
|
|
|
where C: BlockChain + CallContract + BlockProducer + SealedBlockImporter + Nonce + Sync;
|
2016-06-23 14:29:16 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Notifications
|
2016-06-23 14:29:16 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Called when blocks are imported to chain, updates transactions queue.
|
|
|
|
/// `is_internal_import` indicates that the block has just been created in miner and internally sealed by the engine,
|
|
|
|
/// so we shouldn't attempt creating new block again.
|
|
|
|
fn chain_new_blocks<C>(&self, chain: &C, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256], is_internal_import: bool)
|
|
|
|
where C: BlockChainClient;
|
2016-04-18 23:03:41 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Pending block
|
2016-06-27 20:19:01 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get a list of all pending receipts from pending block.
|
|
|
|
fn pending_receipts(&self, best_block: BlockNumber) -> Option<BTreeMap<H256, Receipt>>;
|
2016-04-17 18:26:15 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get a particular receipt from pending block.
|
|
|
|
fn pending_receipt(&self, best_block: BlockNumber, hash: &H256) -> Option<RichReceipt>;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get `Some` `clone()` of the current pending block's state or `None` if we're not sealing.
|
|
|
|
fn pending_state(&self, latest_block_number: BlockNumber) -> Option<Self::State>;
|
2016-03-11 19:27:09 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get `Some` `clone()` of the current pending block header or `None` if we're not sealing.
|
|
|
|
fn pending_block_header(&self, latest_block_number: BlockNumber) -> Option<Header>;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get `Some` `clone()` of the current pending block or `None` if we're not sealing.
|
|
|
|
fn pending_block(&self, latest_block_number: BlockNumber) -> Option<Block>;
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get `Some` `clone()` of the current pending block transactions or `None` if we're not sealing.
|
|
|
|
fn pending_transactions(&self, latest_block_number: BlockNumber) -> Option<Vec<SignedTransaction>>;
|
2017-07-12 08:52:18 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Block authoring
|
2016-03-11 14:48:30 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get current authoring parameters.
|
|
|
|
fn authoring_params(&self) -> AuthoringParams;
|
2016-03-24 23:03:22 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Set the lower and upper bound of gas limit we wish to target when sealing a new block.
|
|
|
|
fn set_gas_range_target(&self, gas_range_target: (U256, U256));
|
2016-03-27 22:16:24 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Set the extra_data that we will seal blocks with.
|
|
|
|
fn set_extra_data(&self, extra_data: Bytes);
|
2016-03-28 18:53:33 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Set info necessary to sign consensus messages and block authoring.
|
|
|
|
///
|
|
|
|
/// On PoW password is optional.
|
2018-06-22 15:09:15 +02:00
|
|
|
fn set_author(&self, address: Address, password: Option<Password>) -> Result<(), ::account_provider::SignError>;
|
2017-03-19 08:46:51 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Transaction Pool
|
2016-05-24 21:56:32 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Imports transactions to transaction queue.
|
|
|
|
fn import_external_transactions<C>(&self, client: &C, transactions: Vec<UnverifiedTransaction>)
|
|
|
|
-> Vec<Result<(), transaction::Error>>
|
|
|
|
where C: BlockChainClient;
|
2016-12-15 18:19:19 +01:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Imports own (node owner) transaction to queue.
|
|
|
|
fn import_own_transaction<C>(&self, chain: &C, transaction: PendingTransaction)
|
|
|
|
-> Result<(), transaction::Error>
|
2018-06-18 15:32:18 +02:00
|
|
|
where C: BlockChainClient;
|
|
|
|
|
|
|
|
/// Imports transactions from potentially external sources, with behaviour determined
|
|
|
|
/// by the config flag `tx_queue_allow_unfamiliar_locals`
|
|
|
|
fn import_claimed_local_transaction<C>(&self, chain: &C, transaction: PendingTransaction, trusted: bool)
|
|
|
|
-> Result<(), transaction::Error>
|
2018-04-13 17:34:27 +02:00
|
|
|
where C: BlockChainClient;
|
|
|
|
|
|
|
|
/// Removes transaction from the pool.
|
|
|
|
///
|
|
|
|
/// Attempts to "cancel" a transaction. If it was not propagated yet (or not accepted by other peers)
|
|
|
|
/// there is a good chance that the transaction will actually be removed.
|
|
|
|
/// NOTE: The transaction is not removed from pending block if there is one.
|
|
|
|
fn remove_transaction(&self, hash: &H256) -> Option<Arc<VerifiedTransaction>>;
|
|
|
|
|
|
|
|
/// Query transaction from the pool given it's hash.
|
|
|
|
fn transaction(&self, hash: &H256) -> Option<Arc<VerifiedTransaction>>;
|
|
|
|
|
|
|
|
/// Returns next valid nonce for given address.
|
|
|
|
///
|
|
|
|
/// This includes nonces of all transactions from this address in the pending queue
|
|
|
|
/// if they are consecutive.
|
|
|
|
/// NOTE: pool may contain some future transactions that will become pending after
|
|
|
|
/// transaction with nonce returned from this function is signed on.
|
|
|
|
fn next_nonce<C>(&self, chain: &C, address: &Address) -> U256
|
|
|
|
where C: Nonce + Sync;
|
|
|
|
|
2018-07-04 17:37:55 +02:00
|
|
|
/// Get a set of all pending transaction hashes.
|
|
|
|
///
|
|
|
|
/// Depending on the settings may look in transaction pool or only in pending block.
|
|
|
|
fn pending_transaction_hashes<C>(&self, chain: &C) -> BTreeSet<H256> where
|
|
|
|
C: ChainInfo + Sync;
|
|
|
|
|
2018-06-12 08:22:54 +02:00
|
|
|
/// Get a list of all ready transactions either ordered by priority or unordered (cheaper).
|
2018-04-13 17:34:27 +02:00
|
|
|
///
|
|
|
|
/// Depending on the settings may look in transaction pool or only in pending block.
|
2018-06-12 08:22:54 +02:00
|
|
|
/// If you don't need a full set of transactions, you can add `max_len` and create only a limited set of
|
|
|
|
/// transactions.
|
|
|
|
fn ready_transactions<C>(&self, chain: &C, max_len: usize, ordering: PendingOrdering) -> Vec<Arc<VerifiedTransaction>>
|
2018-04-13 17:34:27 +02:00
|
|
|
where C: ChainInfo + Nonce + Sync;
|
|
|
|
|
|
|
|
/// Get a list of all transactions in the pool (some of them might not be ready for inclusion yet).
|
|
|
|
fn queued_transactions(&self) -> Vec<Arc<VerifiedTransaction>>;
|
2016-04-06 23:03:07 +02:00
|
|
|
|
2016-11-16 17:54:54 +01:00
|
|
|
/// Get a list of local transactions with statuses.
|
2018-04-13 17:34:27 +02:00
|
|
|
fn local_transactions(&self) -> BTreeMap<H256, local_transactions::Status>;
|
2016-05-24 21:56:32 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
/// Get current queue status.
|
|
|
|
///
|
|
|
|
/// Status includes verification thresholds and current pool utilization and limits.
|
|
|
|
fn queue_status(&self) -> QueueStatus;
|
2016-08-17 19:25:02 +02:00
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
// Misc
|
2016-08-02 18:53:32 +02:00
|
|
|
|
2016-04-14 21:01:12 +02:00
|
|
|
/// Suggested gas price.
|
2016-10-31 12:57:48 +01:00
|
|
|
fn sensible_gas_price(&self) -> U256;
|
2016-04-14 21:01:12 +02:00
|
|
|
|
|
|
|
/// Suggested gas limit.
|
2018-04-13 17:34:27 +02:00
|
|
|
fn sensible_gas_limit(&self) -> U256;
|
2016-03-11 14:48:30 +01:00
|
|
|
}
|