seals_internally (#4613)

This commit is contained in:
keorn 2017-02-20 15:35:53 +00:00 committed by Gav Wood
parent 44769fcd4a
commit ac6180a6fe
6 changed files with 33 additions and 32 deletions

View File

@ -220,8 +220,8 @@ impl Engine for AuthorityRound {
}); });
} }
fn is_sealer(&self, author: &Address) -> Option<bool> { fn seals_internally(&self) -> Option<bool> {
Some(self.validators.contains(author)) Some(self.validators.contains(&self.signer.address()))
} }
/// Attempt to seal the block internally. /// Attempt to seal the block internally.

View File

@ -103,8 +103,8 @@ impl Engine for BasicAuthority {
}); });
} }
fn is_sealer(&self, author: &Address) -> Option<bool> { fn seals_internally(&self) -> Option<bool> {
Some(self.validators.contains(author)) Some(self.validators.contains(&self.signer.address()))
} }
/// Attempt to seal the block internally. /// Attempt to seal the block internally.
@ -268,7 +268,8 @@ mod tests {
let authority = tap.insert_account(Secret::from_slice(&"".sha3()).unwrap(), "").unwrap(); let authority = tap.insert_account(Secret::from_slice(&"".sha3()).unwrap(), "").unwrap();
let engine = new_test_authority().engine; let engine = new_test_authority().engine;
assert!(!engine.is_sealer(&Address::default()).unwrap()); assert!(!engine.seals_internally().unwrap());
assert!(engine.is_sealer(&authority).unwrap()); engine.set_signer(Arc::new(tap), authority, "".into());
assert!(engine.seals_internally().unwrap());
} }
} }

View File

@ -56,7 +56,7 @@ impl Engine for InstantSeal {
Schedule::new_post_eip150(usize::max_value(), true, true, true) Schedule::new_post_eip150(usize::max_value(), true, true, true)
} }
fn is_sealer(&self, _author: &Address) -> Option<bool> { Some(true) } fn seals_internally(&self) -> Option<bool> { Some(true) }
fn generate_seal(&self, _block: &ExecutedBlock) -> Seal { fn generate_seal(&self, _block: &ExecutedBlock) -> Seal {
Seal::Regular(Vec::new()) Seal::Regular(Vec::new())

View File

@ -128,11 +128,10 @@ pub trait Engine : Sync + Send {
/// Block transformation functions, after the transactions. /// Block transformation functions, after the transactions.
fn on_close_block(&self, _block: &mut ExecutedBlock) {} fn on_close_block(&self, _block: &mut ExecutedBlock) {}
/// If Some(true) this author is able to generate seals, generate_seal has to be implemented. /// None means that it requires external input (e.g. PoW) to seal a block.
/// None indicates that this Engine never seals internally regardless of author (e.g. PoW). /// Some(true) means the engine is currently prime for seal generation (i.e. node is the current validator).
fn is_sealer(&self, _author: &Address) -> Option<bool> { None } /// Some(false) means that the node might seal internally but is not qualified now.
/// Checks if default address is able to seal. fn seals_internally(&self) -> Option<bool> { None }
fn is_default_sealer(&self) -> Option<bool> { self.is_sealer(&Default::default()) }
/// Attempt to seal the block internally. /// Attempt to seal the block internally.
/// ///
/// If `Some` is returned, then you get a valid seal. /// If `Some` is returned, then you get a valid seal.

View File

@ -410,8 +410,8 @@ impl Engine for Tendermint {
} }
/// Should this node participate. /// Should this node participate.
fn is_sealer(&self, address: &Address) -> Option<bool> { fn seals_internally(&self) -> Option<bool> {
Some(self.is_authority(address)) Some(self.is_authority(&self.signer.address()))
} }
/// Attempt to seal generate a proposal seal. /// Attempt to seal generate a proposal seal.

View File

@ -215,8 +215,6 @@ pub struct Miner {
sealing_block_last_request: Mutex<u64>, sealing_block_last_request: Mutex<u64>,
// for sealing... // for sealing...
options: MinerOptions, options: MinerOptions,
/// Does the node perform internal (without work) sealing.
pub seals_internally: bool,
gas_range_target: RwLock<(U256, U256)>, gas_range_target: RwLock<(U256, U256)>,
author: RwLock<Address>, author: RwLock<Address>,
@ -275,9 +273,8 @@ impl Miner {
queue: UsingQueue::new(options.work_queue_size), queue: UsingQueue::new(options.work_queue_size),
enabled: options.force_sealing enabled: options.force_sealing
|| !options.new_work_notify.is_empty() || !options.new_work_notify.is_empty()
|| spec.engine.is_default_sealer().unwrap_or(false) || spec.engine.seals_internally().is_some()
}), }),
seals_internally: spec.engine.is_default_sealer().is_some(),
gas_range_target: RwLock::new((U256::zero(), U256::zero())), gas_range_target: RwLock::new((U256::zero(), U256::zero())),
author: RwLock::new(Address::default()), author: RwLock::new(Address::default()),
extra_data: RwLock::new(Vec::new()), extra_data: RwLock::new(Vec::new()),
@ -455,7 +452,7 @@ impl Miner {
let last_request = *self.sealing_block_last_request.lock(); let last_request = *self.sealing_block_last_request.lock();
let should_disable_sealing = !self.forced_sealing() let should_disable_sealing = !self.forced_sealing()
&& !has_local_transactions && !has_local_transactions
&& !self.seals_internally && self.engine.seals_internally().is_none()
&& best_block > last_request && best_block > last_request
&& best_block - last_request > SEALING_TIMEOUT_IN_BLOCKS; && best_block - last_request > SEALING_TIMEOUT_IN_BLOCKS;
@ -765,21 +762,21 @@ impl MinerService for Miner {
} }
fn set_author(&self, author: Address) { fn set_author(&self, author: Address) {
if self.seals_internally { if self.engine.seals_internally().is_some() {
let mut sealing_work = self.sealing_work.lock(); let mut sealing_work = self.sealing_work.lock();
sealing_work.enabled = self.engine.is_sealer(&author).unwrap_or(false); sealing_work.enabled = true;
} }
*self.author.write() = author; *self.author.write() = author;
} }
fn set_engine_signer(&self, address: Address, password: String) -> Result<(), AccountError> { fn set_engine_signer(&self, address: Address, password: String) -> Result<(), AccountError> {
if self.seals_internally { if self.engine.seals_internally().is_some() {
if let Some(ref ap) = self.accounts { if let Some(ref ap) = self.accounts {
ap.sign(address.clone(), Some(password.clone()), Default::default())?; ap.sign(address.clone(), Some(password.clone()), Default::default())?;
// Limit the scope of the locks. // Limit the scope of the locks.
{ {
let mut sealing_work = self.sealing_work.lock(); let mut sealing_work = self.sealing_work.lock();
sealing_work.enabled = self.engine.is_sealer(&address).unwrap_or(false); sealing_work.enabled = true;
*self.author.write() = address; *self.author.write() = address;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -914,7 +911,7 @@ impl MinerService for Miner {
if imported.is_ok() && self.options.reseal_on_own_tx && self.tx_reseal_allowed() { if imported.is_ok() && self.options.reseal_on_own_tx && self.tx_reseal_allowed() {
// Make sure to do it after transaction is imported and lock is droped. // Make sure to do it after transaction is imported and lock is droped.
// We need to create pending block and enable sealing. // We need to create pending block and enable sealing.
if self.seals_internally || !self.prepare_work_sealing(chain) { if self.engine.seals_internally().unwrap_or(false) || !self.prepare_work_sealing(chain) {
// If new block has not been prepared (means we already had one) // If new block has not been prepared (means we already had one)
// or Engine might be able to seal internally, // or Engine might be able to seal internally,
// we need to update sealing. // we need to update sealing.
@ -1071,14 +1068,18 @@ impl MinerService for Miner {
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
trace!(target: "miner", "update_sealing: preparing a block"); trace!(target: "miner", "update_sealing: preparing a block");
let (block, original_work_hash) = self.prepare_block(chain); let (block, original_work_hash) = self.prepare_block(chain);
if self.seals_internally { match self.engine.seals_internally() {
Some(true) => {
trace!(target: "miner", "update_sealing: engine indicates internal sealing"); trace!(target: "miner", "update_sealing: engine indicates internal sealing");
if self.seal_and_import_block_internally(chain, block) { if self.seal_and_import_block_internally(chain, block) {
trace!(target: "miner", "update_sealing: imported internally sealed block"); trace!(target: "miner", "update_sealing: imported internally sealed block");
} }
} else { },
None => {
trace!(target: "miner", "update_sealing: engine does not seal internally, preparing work"); trace!(target: "miner", "update_sealing: engine does not seal internally, preparing work");
self.prepare_work(block, original_work_hash); self.prepare_work(block, original_work_hash)
},
_ => trace!(target: "miner", "update_sealing: engine is not keen to seal internally right now")
} }
} }
} }