Revert "dont keep account provider in miner"

This reverts commit 11ccacd6d0.
This commit is contained in:
keorn 2016-11-22 16:05:27 +00:00
parent 32bcd08b94
commit 340d377930
6 changed files with 94 additions and 56 deletions

View File

@ -12,16 +12,16 @@
} }
}, },
"params": { "params": {
"accountStartNonce": "0x0100000", "accountStartNonce": "0x0",
"maximumExtraDataSize": "0x20", "maximumExtraDataSize": "0x20",
"minGasLimit": "0x1388", "minGasLimit": "0x1388",
"networkID" : "0x69" "networkID" : "0x2323"
}, },
"genesis": { "genesis": {
"seal": { "seal": {
"generic": { "generic": {
"fields": 1, "fields": 3,
"rlp": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa" "rlp": "0x40010"
} }
}, },
"difficulty": "0x20000", "difficulty": "0x20000",
@ -32,10 +32,10 @@
"gasLimit": "0x2fefd8" "gasLimit": "0x2fefd8"
}, },
"accounts": { "accounts": {
"0000000000000000000000000000000000000001": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } }, "0000000000000000000000000000000000000001": { "balance": "1", "builtin": { "name": "ecrecover", "pricing": { "linear": { "base": 3000, "word": 0 } } } },
"0000000000000000000000000000000000000002": { "balance": "1", "nonce": "1048576", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } }, "0000000000000000000000000000000000000002": { "balance": "1", "builtin": { "name": "sha256", "pricing": { "linear": { "base": 60, "word": 12 } } } },
"0000000000000000000000000000000000000003": { "balance": "1", "nonce": "1048576", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } }, "0000000000000000000000000000000000000003": { "balance": "1", "builtin": { "name": "ripemd160", "pricing": { "linear": { "base": 600, "word": 120 } } } },
"0000000000000000000000000000000000000004": { "balance": "1", "nonce": "1048576", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } }, "0000000000000000000000000000000000000004": { "balance": "1", "builtin": { "name": "identity", "pricing": { "linear": { "base": 15, "word": 3 } } } },
"9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376", "nonce": "1048576" } "9cce34f7ab185c7aba1b7c8140d620b4bda941d6": { "balance": "1606938044258990275541962092341162602522202993782792835301376" }
} }
} }

View File

@ -68,9 +68,9 @@ pub struct AuthorityRound {
builtins: BTreeMap<Address, Builtin>, builtins: BTreeMap<Address, Builtin>,
transition_service: IoService<BlockArrived>, transition_service: IoService<BlockArrived>,
message_channel: Mutex<Option<IoChannel<ClientIoMessage>>>, message_channel: Mutex<Option<IoChannel<ClientIoMessage>>>,
account_provider: Mutex<Option<Arc<AccountProvider>>>,
step: AtomicUsize, step: AtomicUsize,
proposed: AtomicBool, proposed: AtomicBool,
account_provider: Mutex<Option<Arc<AccountProvider>>>,
} }
fn header_step(header: &Header) -> Result<usize, ::rlp::DecoderError> { fn header_step(header: &Header) -> Result<usize, ::rlp::DecoderError> {
@ -102,9 +102,9 @@ impl AuthorityRound {
builtins: builtins, builtins: builtins,
transition_service: try!(IoService::<BlockArrived>::start()), transition_service: try!(IoService::<BlockArrived>::start()),
message_channel: Mutex::new(None), message_channel: Mutex::new(None),
account_provider: Mutex::new(None),
step: AtomicUsize::new(initial_step), step: AtomicUsize::new(initial_step),
proposed: AtomicBool::new(false) proposed: AtomicBool::new(false),
account_provider: Mutex::new(None),
}); });
let handler = TransitionHandler { engine: Arc::downgrade(&engine) }; let handler = TransitionHandler { engine: Arc::downgrade(&engine) };
try!(engine.transition_service.register_handler(Arc::new(handler))); try!(engine.transition_service.register_handler(Arc::new(handler)));

View File

@ -58,7 +58,7 @@ pub struct BasicAuthority {
params: CommonParams, params: CommonParams,
our_params: BasicAuthorityParams, our_params: BasicAuthorityParams,
builtins: BTreeMap<Address, Builtin>, builtins: BTreeMap<Address, Builtin>,
account_provider: Mutex<Option<Arc<AccountProvider>>> account_provider: Mutex<Option<Arc<AccountProvider>>>,
} }
impl BasicAuthority { impl BasicAuthority {

View File

@ -174,11 +174,11 @@ pub trait Engine : Sync + Send {
/// Add a channel for communication with Client which can be used for sealing. /// Add a channel for communication with Client which can be used for sealing.
fn register_message_channel(&self, _message_channel: IoChannel<ClientIoMessage>) {} fn register_message_channel(&self, _message_channel: IoChannel<ClientIoMessage>) {}
/// Add an account provider useful for Engines that sign stuff.
fn register_account_provider(&self, _account_provider: Arc<AccountProvider>) {}
/// Check if new block should be chosen as the one in chain. /// Check if new block should be chosen as the one in chain.
fn is_new_best_block(&self, best_total_difficulty: U256, _best_header: HeaderView, parent_details: &BlockDetails, new_header: &HeaderView) -> bool { fn is_new_best_block(&self, best_total_difficulty: U256, _best_header: HeaderView, parent_details: &BlockDetails, new_header: &HeaderView) -> bool {
ethash::is_new_best_block(best_total_difficulty, parent_details, new_header) ethash::is_new_best_block(best_total_difficulty, parent_details, new_header)
} }
/// Add an account provider useful for Engines that sign stuff.
fn register_account_provider(&self, _account_provider: Arc<AccountProvider>) {}
} }

View File

@ -202,26 +202,22 @@ impl Tendermint {
} }
} }
fn nonce_proposer(&self, proposer_nonce: usize) -> &Address {
let ref p = self.our_params;
p.authorities.get(proposer_nonce % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed")
}
fn is_nonce_proposer(&self, proposer_nonce: usize, address: &Address) -> bool {
self.nonce_proposer(proposer_nonce) == address
}
fn is_authority(&self, address: &Address) -> bool { fn is_authority(&self, address: &Address) -> bool {
self.our_params.authorities.contains(address) self.our_params.authorities.contains(address)
} }
fn threshold(&self) -> usize { fn is_above_threshold(&self, n: usize) -> bool {
self.our_params.authority_n * 2/3 n > self.our_params.authority_n * 2/3
} }
/// Round proposer switching. /// Round proposer switching.
fn is_proposer(&self, address: &Address) -> bool { fn is_proposer(&self, address: &Address) -> bool {
self.is_nonce_proposer(self.proposer_nonce.load(AtomicOrdering::SeqCst), address) let ref p = self.our_params;
let proposer_nonce = self.proposer_nonce.load(AtomicOrdering::SeqCst);
let proposer = p.authorities.get(proposer_nonce % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed");
println!("{:?}", &proposer);
println!("{:?}", &address);
proposer == address
} }
fn is_height(&self, message: &ConsensusMessage) -> bool { fn is_height(&self, message: &ConsensusMessage) -> bool {
@ -251,16 +247,22 @@ impl Tendermint {
fn has_enough_any_votes(&self) -> bool { fn has_enough_any_votes(&self) -> bool {
self.votes.count_step_votes(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), *self.step.read()) > self.threshold() let step_votes = self.votes.count_step_votes(self.height.load(AtomicOrdering::SeqCst), self.round.load(AtomicOrdering::SeqCst), *self.step.read());
self.is_above_threshold(step_votes)
} }
fn has_enough_future_step_votes(&self, message: &ConsensusMessage) -> bool { fn has_enough_future_step_votes(&self, message: &ConsensusMessage) -> bool {
message.round > self.round.load(AtomicOrdering::SeqCst) if message.round > self.round.load(AtomicOrdering::SeqCst) {
&& self.votes.count_step_votes(message.height, message.round, message.step) > self.threshold() let step_votes = self.votes.count_step_votes(message.height, message.round, message.step);
self.is_above_threshold(step_votes)
} else {
false
}
} }
fn has_enough_aligned_votes(&self, message: &ConsensusMessage) -> bool { fn has_enough_aligned_votes(&self, message: &ConsensusMessage) -> bool {
self.votes.aligned_votes(&message).len() > self.threshold() let aligned_votes = self.votes.aligned_votes(&message).len();
self.is_above_threshold(aligned_votes)
} }
} }
@ -476,6 +478,13 @@ mod tests {
use super::params::TendermintParams; use super::params::TendermintParams;
use super::message::*; use super::message::*;
fn setup() -> (Spec, Arc<AccountProvider>) {
let tap = Arc::new(AccountProvider::transient_provider());
let spec = Spec::new_test_tendermint();
spec.engine.register_account_provider(tap.clone());
(spec, tap)
}
fn propose_default(engine: &Arc<Engine>, db: &StateDB, proposer: Address) -> Option<Vec<Bytes>> { fn propose_default(engine: &Arc<Engine>, db: &StateDB, proposer: Address) -> Option<Vec<Bytes>> {
let mut header = Header::default(); let mut header = Header::default();
let last_hashes = Arc::new(vec![]); let last_hashes = Arc::new(vec![]);
@ -489,9 +498,7 @@ mod tests {
engine.handle_message(UntrustedRlp::new(&m)).unwrap(); engine.handle_message(UntrustedRlp::new(&m)).unwrap();
} }
fn proposal_seal(header: &Header, round: Round) -> Vec<Bytes> { fn proposal_seal(tap: &Arc<AccountProvider>, header: &Header, round: Round) -> Vec<Bytes> {
let tap = AccountProvider::transient_provider();
let author = header.author(); let author = header.author();
let vote_info = message_info_rlp(header.number() as Height, round, Step::Propose, Some(header.bare_hash())); let vote_info = message_info_rlp(header.number() as Height, round, Step::Propose, Some(header.bare_hash()));
let signature = tap.sign(*author, None, vote_info.sha3()).unwrap(); let signature = tap.sign(*author, None, vote_info.sha3()).unwrap();
@ -502,6 +509,12 @@ mod tests {
] ]
} }
fn insert_and_unlock(tap: &Arc<AccountProvider>, acc: &str) -> Address {
let addr = tap.insert_account(acc.sha3(), acc).unwrap();
tap.unlock_account_permanently(addr, acc.into()).unwrap();
addr
}
fn default_seal() -> Vec<Bytes> { fn default_seal() -> Vec<Bytes> {
vec![vec![160, 39, 191, 179, 126, 80, 124, 233, 13, 161, 65, 48, 114, 4, 177, 198, 186, 36, 25, 67, 128, 97, 53, 144, 172, 80, 202, 75, 29, 113, 152, 255, 101]] vec![vec![160, 39, 191, 179, 126, 80, 124, 233, 13, 161, 65, 48, 114, 4, 177, 198, 186, 36, 25, 67, 128, 97, 53, 144, 172, 80, 202, 75, 29, 113, 152, 255, 101]]
} }
@ -544,21 +557,44 @@ mod tests {
} }
#[test] #[test]
fn verification_fails_on_wrong_signatures() { fn allows_correct_proposer() {
let engine = Spec::new_test_tendermint().engine; ::env_logger::init().unwrap();
let mut header = Header::default(); let (spec, tap) = setup();
let tap = AccountProvider::transient_provider(); let engine = spec.engine;
let mut header = Header::default();
let validator = insert_and_unlock(&tap, "0");
header.set_author(validator);
let seal = proposal_seal(&tap, &header, 0);
header.set_seal(seal);
// Good proposer.
assert!(engine.verify_block_unordered(&header, None).is_ok());
let mut header = Header::default();
let random = insert_and_unlock(&tap, "101");
header.set_author(random);
let seal = proposal_seal(&tap, &header, 0);
header.set_seal(seal);
// Bad proposer.
assert!(engine.verify_block_unordered(&header, None).is_err());
}
#[test]
fn verification_fails_on_wrong_signatures() {
let (spec, tap) = setup();
let engine = spec.engine;
let mut header = Header::default();
let mut seal = Vec::new();
let v1 = tap.insert_account("0".sha3(), "0").unwrap(); let v1 = tap.insert_account("0".sha3(), "0").unwrap();
let sig1 = tap.sign(v1, Some("0".into()), header.bare_hash()).unwrap();
seal.push(::rlp::encode(&(&*sig1 as &[u8])).to_vec()); header.set_author(v1);
let mut seal = proposal_seal(&tap, &header, 0);
header.set_seal(seal.clone()); header.set_seal(seal.clone());
// Not enough signatures. // Not enough signatures.
assert!(engine.verify_block_basic(&header, None).is_err()); assert!(engine.verify_block_unordered(&header, None).is_err());
let v2 = tap.insert_account("101".sha3(), "101").unwrap(); let v2 = tap.insert_account("101".sha3(), "101").unwrap();
let sig2 = tap.sign(v2, Some("101".into()), header.bare_hash()).unwrap(); let sig2 = tap.sign(v2, Some("101".into()), header.bare_hash()).unwrap();
@ -567,7 +603,7 @@ mod tests {
header.set_seal(seal); header.set_seal(seal);
// Enough signatures. // Enough signatures.
assert!(engine.verify_block_basic(&header, None).is_ok()); assert!(engine.verify_block_unordered(&header, None).is_ok());
let verify_result = engine.verify_block_unordered(&header, None); let verify_result = engine.verify_block_unordered(&header, None);
@ -581,10 +617,11 @@ mod tests {
#[test] #[test]
fn seal_with_enough_signatures_is_ok() { fn seal_with_enough_signatures_is_ok() {
let engine = Spec::new_test_tendermint().engine; let (spec, tap) = setup();
let engine = spec.engine;
let mut header = Header::default(); let mut header = Header::default();
let seal = proposal_seal(&header, 0); let seal = proposal_seal(&tap, &header, 0);
header.set_seal(seal); header.set_seal(seal);
// Enough signatures. // Enough signatures.
@ -596,20 +633,18 @@ mod tests {
#[test] #[test]
fn can_generate_seal() { fn can_generate_seal() {
let spec = Spec::new_test_tendermint(); let (spec, _) = setup();
let ref engine = *spec.engine;
let tender = Tendermint::new(engine.params().clone(), TendermintParams::default(), BTreeMap::new()).unwrap();
let genesis_header = spec.genesis_header(); let genesis_header = spec.genesis_header();
let mut db_result = get_temp_state_db(); let mut db_result = get_temp_state_db();
let mut db = db_result.take(); let mut db = db_result.take();
spec.ensure_db_good(&mut db).unwrap(); spec.ensure_db_good(&mut db).unwrap();
let last_hashes = Arc::new(vec![genesis_header.hash()]); let last_hashes = Arc::new(vec![genesis_header.hash()]);
let b = OpenBlock::new(engine, Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap(); let b = OpenBlock::new(spec.engine.as_ref(), Default::default(), false, db, &genesis_header, last_hashes, Address::default(), (3141562.into(), 31415620.into()), vec![]).unwrap();
let b = b.close_and_lock(); let b = b.close_and_lock();
let seal = tender.generate_seal(b.block()).unwrap(); let seal = spec.engine.generate_seal(b.block()).unwrap();
assert!(b.try_seal(engine, seal).is_ok()); assert!(b.try_seal(spec.engine.as_ref(), seal).is_ok());
} }
#[test] #[test]

View File

@ -203,8 +203,14 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
sync_config.fork_block = spec.fork_block(); sync_config.fork_block = spec.fork_block();
sync_config.warp_sync = cmd.warp_sync; sync_config.warp_sync = cmd.warp_sync;
// prepare account provider
let account_provider = Arc::new(try!(prepare_account_provider(&cmd.dirs, cmd.acc_conf)));
// let the Engine access the accounts
spec.engine.register_account_provider(account_provider);
// create miner // create miner
let miner = Miner::new(cmd.miner_options, cmd.gas_pricer.into(), &spec); let miner = Miner::new(cmd.miner_options, cmd.gas_pricer.into(), &spec, Some(account_provider.clone()));
miner.set_author(cmd.miner_extras.author); miner.set_author(cmd.miner_extras.author);
miner.set_gas_floor_target(cmd.miner_extras.gas_floor_target); miner.set_gas_floor_target(cmd.miner_extras.gas_floor_target);
miner.set_gas_ceil_target(cmd.miner_extras.gas_ceil_target); miner.set_gas_ceil_target(cmd.miner_extras.gas_ceil_target);
@ -238,9 +244,6 @@ pub fn execute(cmd: RunCmd, logger: Arc<RotatingLogger>) -> Result<(), String> {
// create supervisor // create supervisor
let mut hypervisor = modules::hypervisor(&cmd.dirs.ipc_path()); let mut hypervisor = modules::hypervisor(&cmd.dirs.ipc_path());
// prepare account provider
let account_provider = Arc::new(try!(prepare_account_provider(&cmd.dirs, cmd.acc_conf)));
// create client service. // create client service.
let service = try!(ClientService::start( let service = try!(ClientService::start(
client_config, client_config,