Common EngineSigner (#4189)
* remove register_account_provider * build rpc module * new dummy client * common EngineSigner struct * from -> into * return Signature, docs
This commit is contained in:
parent
1f77c4301a
commit
97a60ceab1
@ -36,8 +36,9 @@ use io::{IoContext, IoHandler, TimerToken, IoService};
|
|||||||
use env_info::EnvInfo;
|
use env_info::EnvInfo;
|
||||||
use builtin::Builtin;
|
use builtin::Builtin;
|
||||||
use client::{Client, EngineClient};
|
use client::{Client, EngineClient};
|
||||||
use super::validator_set::{ValidatorSet, new_validator_set};
|
|
||||||
use state::CleanupMode;
|
use state::CleanupMode;
|
||||||
|
use super::signer::EngineSigner;
|
||||||
|
use super::validator_set::{ValidatorSet, new_validator_set};
|
||||||
|
|
||||||
/// `AuthorityRound` params.
|
/// `AuthorityRound` params.
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@ -78,8 +79,7 @@ pub struct AuthorityRound {
|
|||||||
step: AtomicUsize,
|
step: AtomicUsize,
|
||||||
proposed: AtomicBool,
|
proposed: AtomicBool,
|
||||||
client: RwLock<Option<Weak<EngineClient>>>,
|
client: RwLock<Option<Weak<EngineClient>>>,
|
||||||
account_provider: Mutex<Arc<AccountProvider>>,
|
signer: EngineSigner,
|
||||||
password: RwLock<Option<String>>,
|
|
||||||
validators: Box<ValidatorSet + Send + Sync>,
|
validators: Box<ValidatorSet + Send + Sync>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +117,7 @@ impl AuthorityRound {
|
|||||||
step: AtomicUsize::new(initial_step),
|
step: AtomicUsize::new(initial_step),
|
||||||
proposed: AtomicBool::new(false),
|
proposed: AtomicBool::new(false),
|
||||||
client: RwLock::new(None),
|
client: RwLock::new(None),
|
||||||
account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())),
|
signer: Default::default(),
|
||||||
password: RwLock::new(None),
|
|
||||||
validators: new_validator_set(our_params.validators),
|
validators: new_validator_set(our_params.validators),
|
||||||
});
|
});
|
||||||
// Do not initialize timeouts for tests.
|
// Do not initialize timeouts for tests.
|
||||||
@ -234,11 +233,10 @@ impl Engine for AuthorityRound {
|
|||||||
let header = block.header();
|
let header = block.header();
|
||||||
let step = self.step.load(AtomicOrdering::SeqCst);
|
let step = self.step.load(AtomicOrdering::SeqCst);
|
||||||
if self.is_step_proposer(step, header.author()) {
|
if self.is_step_proposer(step, header.author()) {
|
||||||
let ap = self.account_provider.lock();
|
if let Ok(signature) = self.signer.sign(header.bare_hash()) {
|
||||||
if let Ok(signature) = ap.sign(*header.author(), self.password.read().clone(), header.bare_hash()) {
|
|
||||||
trace!(target: "poa", "generate_seal: Issuing a block for step {}.", step);
|
trace!(target: "poa", "generate_seal: Issuing a block for step {}.", step);
|
||||||
self.proposed.store(true, AtomicOrdering::SeqCst);
|
self.proposed.store(true, AtomicOrdering::SeqCst);
|
||||||
return Seal::Regular(vec![encode(&step).to_vec(), encode(&(&*signature as &[u8])).to_vec()]);
|
return Seal::Regular(vec![encode(&step).to_vec(), encode(&(&H520::from(signature) as &[u8])).to_vec()]);
|
||||||
} else {
|
} else {
|
||||||
warn!(target: "poa", "generate_seal: FAIL: Accounts secret key unavailable.");
|
warn!(target: "poa", "generate_seal: FAIL: Accounts secret key unavailable.");
|
||||||
}
|
}
|
||||||
@ -330,9 +328,7 @@ impl Engine for AuthorityRound {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
||||||
*self.account_provider.lock() = ap;
|
self.signer.set(ap, address, password);
|
||||||
*self.password.write() = Some(password);
|
|
||||||
debug!(target: "poa", "Setting Engine signer to {}", address);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ use evm::Schedule;
|
|||||||
use ethjson;
|
use ethjson;
|
||||||
use header::Header;
|
use header::Header;
|
||||||
use client::Client;
|
use client::Client;
|
||||||
|
use super::signer::EngineSigner;
|
||||||
use super::validator_set::{ValidatorSet, new_validator_set};
|
use super::validator_set::{ValidatorSet, new_validator_set};
|
||||||
|
|
||||||
/// `BasicAuthority` params.
|
/// `BasicAuthority` params.
|
||||||
@ -56,8 +57,7 @@ pub struct BasicAuthority {
|
|||||||
params: CommonParams,
|
params: CommonParams,
|
||||||
gas_limit_bound_divisor: U256,
|
gas_limit_bound_divisor: U256,
|
||||||
builtins: BTreeMap<Address, Builtin>,
|
builtins: BTreeMap<Address, Builtin>,
|
||||||
account_provider: Mutex<Arc<AccountProvider>>,
|
signer: EngineSigner,
|
||||||
password: RwLock<Option<String>>,
|
|
||||||
validators: Box<ValidatorSet + Send + Sync>,
|
validators: Box<ValidatorSet + Send + Sync>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +69,7 @@ impl BasicAuthority {
|
|||||||
gas_limit_bound_divisor: our_params.gas_limit_bound_divisor,
|
gas_limit_bound_divisor: our_params.gas_limit_bound_divisor,
|
||||||
builtins: builtins,
|
builtins: builtins,
|
||||||
validators: new_validator_set(our_params.validators),
|
validators: new_validator_set(our_params.validators),
|
||||||
account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())),
|
signer: Default::default(),
|
||||||
password: RwLock::new(None),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -110,14 +109,12 @@ impl Engine for BasicAuthority {
|
|||||||
|
|
||||||
/// Attempt to seal the block internally.
|
/// Attempt to seal the block internally.
|
||||||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
||||||
let ref ap = *self.account_provider.lock();
|
|
||||||
let header = block.header();
|
let header = block.header();
|
||||||
let author = header.author();
|
let author = header.author();
|
||||||
if self.validators.contains(author) {
|
if self.validators.contains(author) {
|
||||||
let message = header.bare_hash();
|
|
||||||
// account should be pernamently unlocked, otherwise sealing will fail
|
// account should be pernamently unlocked, otherwise sealing will fail
|
||||||
if let Ok(signature) = ap.sign(*author, self.password.read().clone(), message) {
|
if let Ok(signature) = self.signer.sign(header.bare_hash()) {
|
||||||
return Seal::Regular(vec![::rlp::encode(&(&*signature as &[u8])).to_vec()]);
|
return Seal::Regular(vec![::rlp::encode(&(&H520::from(signature) as &[u8])).to_vec()]);
|
||||||
} else {
|
} else {
|
||||||
trace!(target: "basicauthority", "generate_seal: FAIL: accounts secret key unavailable");
|
trace!(target: "basicauthority", "generate_seal: FAIL: accounts secret key unavailable");
|
||||||
}
|
}
|
||||||
@ -171,9 +168,8 @@ impl Engine for BasicAuthority {
|
|||||||
self.validators.register_call_contract(client);
|
self.validators.register_call_contract(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_signer(&self, ap: Arc<AccountProvider>, _address: Address, password: String) {
|
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
||||||
*self.password.write() = Some(password);
|
self.signer.set(ap, address, password);
|
||||||
*self.account_provider.lock() = ap;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ mod basic_authority;
|
|||||||
mod authority_round;
|
mod authority_round;
|
||||||
mod tendermint;
|
mod tendermint;
|
||||||
mod validator_set;
|
mod validator_set;
|
||||||
|
mod signer;
|
||||||
|
|
||||||
pub use self::null_engine::NullEngine;
|
pub use self::null_engine::NullEngine;
|
||||||
pub use self::instant_seal::InstantSeal;
|
pub use self::instant_seal::InstantSeal;
|
||||||
|
63
ethcore/src/engines/signer.rs
Normal file
63
ethcore/src/engines/signer.rs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// Copyright 2015, 2016 Parity Technologies (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/>.
|
||||||
|
|
||||||
|
//! A signer used by Engines which need to sign messages.
|
||||||
|
|
||||||
|
use util::{Arc, Mutex, RwLock, H256, Address};
|
||||||
|
use ethkey::Signature;
|
||||||
|
use account_provider::{self, AccountProvider};
|
||||||
|
|
||||||
|
/// Everything that an Engine needs to sign messages.
|
||||||
|
pub struct EngineSigner {
|
||||||
|
account_provider: Mutex<Arc<AccountProvider>>,
|
||||||
|
address: RwLock<Address>,
|
||||||
|
password: RwLock<Option<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EngineSigner {
|
||||||
|
fn default() -> Self {
|
||||||
|
EngineSigner {
|
||||||
|
account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())),
|
||||||
|
address: Default::default(),
|
||||||
|
password: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EngineSigner {
|
||||||
|
/// Set up the signer to sign with given address and password.
|
||||||
|
pub fn set(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
||||||
|
*self.account_provider.lock() = ap;
|
||||||
|
*self.address.write() = address;
|
||||||
|
*self.password.write() = Some(password);
|
||||||
|
debug!(target: "poa", "Setting Engine signer to {}", address);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sign a consensus message hash.
|
||||||
|
pub fn sign(&self, hash: H256) -> Result<Signature, account_provider::Error> {
|
||||||
|
self.account_provider.lock().sign(*self.address.read(), self.password.read().clone(), hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Signing address.
|
||||||
|
pub fn address(&self) -> Address {
|
||||||
|
self.address.read().clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Check if the given address is the signing address.
|
||||||
|
pub fn is_address(&self, address: &Address) -> bool {
|
||||||
|
*self.address.read() == *address
|
||||||
|
}
|
||||||
|
}
|
@ -46,6 +46,7 @@ use views::HeaderView;
|
|||||||
use evm::Schedule;
|
use evm::Schedule;
|
||||||
use state::CleanupMode;
|
use state::CleanupMode;
|
||||||
use io::IoService;
|
use io::IoService;
|
||||||
|
use super::signer::EngineSigner;
|
||||||
use super::validator_set::{ValidatorSet, new_validator_set};
|
use super::validator_set::{ValidatorSet, new_validator_set};
|
||||||
use self::message::*;
|
use self::message::*;
|
||||||
use self::transition::TransitionHandler;
|
use self::transition::TransitionHandler;
|
||||||
@ -81,10 +82,6 @@ pub struct Tendermint {
|
|||||||
step_service: IoService<Step>,
|
step_service: IoService<Step>,
|
||||||
client: RwLock<Option<Weak<EngineClient>>>,
|
client: RwLock<Option<Weak<EngineClient>>>,
|
||||||
block_reward: U256,
|
block_reward: U256,
|
||||||
/// Address to be used as authority.
|
|
||||||
authority: RwLock<Address>,
|
|
||||||
/// Password used for signing messages.
|
|
||||||
password: RwLock<Option<String>>,
|
|
||||||
/// Blockchain height.
|
/// Blockchain height.
|
||||||
height: AtomicUsize,
|
height: AtomicUsize,
|
||||||
/// Consensus round.
|
/// Consensus round.
|
||||||
@ -94,7 +91,7 @@ pub struct Tendermint {
|
|||||||
/// Vote accumulator.
|
/// Vote accumulator.
|
||||||
votes: VoteCollector,
|
votes: VoteCollector,
|
||||||
/// Used to sign messages and proposals.
|
/// Used to sign messages and proposals.
|
||||||
account_provider: Mutex<Arc<AccountProvider>>,
|
signer: EngineSigner,
|
||||||
/// Message for the last PoLC.
|
/// Message for the last PoLC.
|
||||||
lock_change: RwLock<Option<ConsensusMessage>>,
|
lock_change: RwLock<Option<ConsensusMessage>>,
|
||||||
/// Last lock round.
|
/// Last lock round.
|
||||||
@ -116,13 +113,11 @@ impl Tendermint {
|
|||||||
client: RwLock::new(None),
|
client: RwLock::new(None),
|
||||||
step_service: IoService::<Step>::start()?,
|
step_service: IoService::<Step>::start()?,
|
||||||
block_reward: our_params.block_reward,
|
block_reward: our_params.block_reward,
|
||||||
authority: RwLock::new(Address::default()),
|
|
||||||
password: RwLock::new(None),
|
|
||||||
height: AtomicUsize::new(1),
|
height: AtomicUsize::new(1),
|
||||||
round: AtomicUsize::new(0),
|
round: AtomicUsize::new(0),
|
||||||
step: RwLock::new(Step::Propose),
|
step: RwLock::new(Step::Propose),
|
||||||
votes: VoteCollector::new(),
|
votes: VoteCollector::new(),
|
||||||
account_provider: Mutex::new(Arc::new(AccountProvider::transient_provider())),
|
signer: Default::default(),
|
||||||
lock_change: RwLock::new(None),
|
lock_change: RwLock::new(None),
|
||||||
last_lock: AtomicUsize::new(0),
|
last_lock: AtomicUsize::new(0),
|
||||||
proposal: RwLock::new(None),
|
proposal: RwLock::new(None),
|
||||||
@ -158,18 +153,17 @@ impl Tendermint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn generate_message(&self, block_hash: Option<BlockHash>) -> Option<Bytes> {
|
fn generate_message(&self, block_hash: Option<BlockHash>) -> Option<Bytes> {
|
||||||
let ref ap = *self.account_provider.lock();
|
|
||||||
let h = self.height.load(AtomicOrdering::SeqCst);
|
let h = self.height.load(AtomicOrdering::SeqCst);
|
||||||
let r = self.round.load(AtomicOrdering::SeqCst);
|
let r = self.round.load(AtomicOrdering::SeqCst);
|
||||||
let s = self.step.read();
|
let s = self.step.read();
|
||||||
let vote_info = message_info_rlp(&VoteStep::new(h, r, *s), block_hash);
|
let vote_info = message_info_rlp(&VoteStep::new(h, r, *s), block_hash);
|
||||||
let authority = self.authority.read();
|
match self.signer.sign(vote_info.sha3()).map(Into::into) {
|
||||||
match ap.sign(*authority, self.password.read().clone(), vote_info.sha3()).map(Into::into) {
|
|
||||||
Ok(signature) => {
|
Ok(signature) => {
|
||||||
let message_rlp = message_full_rlp(&signature, &vote_info);
|
let message_rlp = message_full_rlp(&signature, &vote_info);
|
||||||
let message = ConsensusMessage::new(signature, h, r, *s, block_hash);
|
let message = ConsensusMessage::new(signature, h, r, *s, block_hash);
|
||||||
self.votes.vote(message.clone(), &*authority);
|
let validator = self.signer.address();
|
||||||
debug!(target: "poa", "Generated {:?} as {}.", message, *authority);
|
self.votes.vote(message.clone(), &validator);
|
||||||
|
debug!(target: "poa", "Generated {:?} as {}.", message, validator);
|
||||||
self.handle_valid_message(&message);
|
self.handle_valid_message(&message);
|
||||||
|
|
||||||
Some(message_rlp)
|
Some(message_rlp)
|
||||||
@ -240,7 +234,7 @@ impl Tendermint {
|
|||||||
let height = self.height.load(AtomicOrdering::SeqCst);
|
let height = self.height.load(AtomicOrdering::SeqCst);
|
||||||
if let Some(block_hash) = *self.proposal.read() {
|
if let Some(block_hash) = *self.proposal.read() {
|
||||||
// Generate seal and remove old votes.
|
// Generate seal and remove old votes.
|
||||||
if self.is_proposer(&*self.authority.read()).is_ok() {
|
if self.is_signer_proposer() {
|
||||||
if let Some(seal) = self.votes.seal_signatures(height, round, &block_hash) {
|
if let Some(seal) = self.votes.seal_signatures(height, round, &block_hash) {
|
||||||
trace!(target: "poa", "Collected seal: {:?}", seal);
|
trace!(target: "poa", "Collected seal: {:?}", seal);
|
||||||
let seal = vec![
|
let seal = vec![
|
||||||
@ -267,11 +261,16 @@ impl Tendermint {
|
|||||||
n > self.validators.count() * 2/3
|
n > self.validators.count() * 2/3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Find the designated for the given round.
|
||||||
|
fn round_proposer(&self, height: Height, round: Round) -> Address {
|
||||||
|
let proposer_nonce = height + round;
|
||||||
|
trace!(target: "poa", "Proposer nonce: {}", proposer_nonce);
|
||||||
|
self.validators.get(proposer_nonce)
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if address is a proposer for given round.
|
/// Check if address is a proposer for given round.
|
||||||
fn is_round_proposer(&self, height: Height, round: Round, address: &Address) -> Result<(), EngineError> {
|
fn is_round_proposer(&self, height: Height, round: Round, address: &Address) -> Result<(), EngineError> {
|
||||||
let proposer_nonce = height + round;
|
let proposer = self.round_proposer(height, round);
|
||||||
trace!(target: "poa", "is_proposer: Proposer nonce: {}", proposer_nonce);
|
|
||||||
let proposer = self.validators.get(proposer_nonce);
|
|
||||||
if proposer == *address {
|
if proposer == *address {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -279,9 +278,10 @@ impl Tendermint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if address is the current proposer.
|
/// Check if current signer is the current proposer.
|
||||||
fn is_proposer(&self, address: &Address) -> Result<(), EngineError> {
|
fn is_signer_proposer(&self) -> bool {
|
||||||
self.is_round_proposer(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), address)
|
let proposer = self.round_proposer(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst));
|
||||||
|
self.signer.is_address(&proposer)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_height(&self, message: &ConsensusMessage) -> bool {
|
fn is_height(&self, message: &ConsensusMessage) -> bool {
|
||||||
@ -416,11 +416,10 @@ impl Engine for Tendermint {
|
|||||||
|
|
||||||
/// Attempt to seal generate a proposal seal.
|
/// Attempt to seal generate a proposal seal.
|
||||||
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
fn generate_seal(&self, block: &ExecutedBlock) -> Seal {
|
||||||
let ref ap = *self.account_provider.lock();
|
|
||||||
let header = block.header();
|
let header = block.header();
|
||||||
let author = header.author();
|
let author = header.author();
|
||||||
// Only proposer can generate seal if None was generated.
|
// Only proposer can generate seal if None was generated.
|
||||||
if self.is_proposer(author).is_err() || self.proposal.read().is_some() {
|
if !self.is_signer_proposer() || self.proposal.read().is_some() {
|
||||||
return Seal::None;
|
return Seal::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,7 +427,7 @@ impl Engine for Tendermint {
|
|||||||
let round = self.round.load(AtomicOrdering::SeqCst);
|
let round = self.round.load(AtomicOrdering::SeqCst);
|
||||||
let bh = Some(header.bare_hash());
|
let bh = Some(header.bare_hash());
|
||||||
let vote_info = message_info_rlp(&VoteStep::new(height, round, Step::Propose), bh.clone());
|
let vote_info = message_info_rlp(&VoteStep::new(height, round, Step::Propose), bh.clone());
|
||||||
if let Ok(signature) = ap.sign(*author, self.password.read().clone(), vote_info.sha3()).map(H520::from) {
|
if let Ok(signature) = self.signer.sign(vote_info.sha3()).map(Into::into) {
|
||||||
// Insert Propose vote.
|
// Insert Propose vote.
|
||||||
debug!(target: "poa", "Submitting proposal {} at height {} round {}.", header.bare_hash(), height, round);
|
debug!(target: "poa", "Submitting proposal {} at height {} round {}.", header.bare_hash(), height, round);
|
||||||
self.votes.vote(ConsensusMessage::new(signature, height, round, Step::Propose, bh), author);
|
self.votes.vote(ConsensusMessage::new(signature, height, round, Step::Propose, bh), author);
|
||||||
@ -557,9 +556,7 @@ impl Engine for Tendermint {
|
|||||||
|
|
||||||
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
fn set_signer(&self, ap: Arc<AccountProvider>, address: Address, password: String) {
|
||||||
{
|
{
|
||||||
*self.authority.write() = address;
|
self.signer.set(ap, address, password);
|
||||||
*self.password.write() = Some(password);
|
|
||||||
*self.account_provider.lock() = ap;
|
|
||||||
}
|
}
|
||||||
self.to_step(Step::Propose);
|
self.to_step(Step::Propose);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user