Skip to content

Commit

Permalink
Merge pull request #202 from poanetwork/afck-aura-pos-randomness-cont…
Browse files Browse the repository at this point in the history
…ract-map

Make the randomness contract option a map; disable Clique (doesn't bu…
  • Loading branch information
varasev authored Dec 5, 2019
2 parents 152012b + c483bfb commit 1112121
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
19 changes: 12 additions & 7 deletions ethcore/src/engines/authority_round/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ pub struct AuthorityRoundParams {
pub maximum_empty_steps: usize,
/// Transition block to strict empty steps validation.
pub strict_empty_steps_transition: u64,
/// If set, enables random number contract integration.
pub randomness_contract_address: Option<Address>,
/// If set, enables random number contract integration. It maps the transition block to the contract address.
pub randomness_contract_address: BTreeMap<u64, Address>,
/// If set, this is the block number at which the consensus engine switches from AuRa to AuRa
/// with POSDAO modifications.
pub posdao_transition: Option<BlockNumber>,
Expand Down Expand Up @@ -173,6 +173,11 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
.into_iter()
.map(|(block_num, address)| (block_num.into(), address.into()))
.collect();
let randomness_contract_address = p.randomness_contract_address.map_or_else(BTreeMap::new, |transitions| {
transitions.into_iter().map(|(ethjson::uint::Uint(block), addr)| {
(block.as_u64(), addr.into())
}).collect()
});
AuthorityRoundParams {
step_durations,
validators: new_validator_set_posdao(p.validators, p.posdao_transition.map(Into::into)),
Expand All @@ -188,9 +193,9 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams {
maximum_empty_steps: p.maximum_empty_steps.map_or(0, Into::into),
two_thirds_majority_transition: p.two_thirds_majority_transition.map_or_else(BlockNumber::max_value, Into::into),
strict_empty_steps_transition: p.strict_empty_steps_transition.map_or(0, Into::into),
randomness_contract_address: p.randomness_contract_address.map(Into::into),
posdao_transition: p.posdao_transition.map(Into::into),
block_gas_limit_contract_transitions,
randomness_contract_address,
}
}
}
Expand Down Expand Up @@ -553,15 +558,15 @@ pub struct AuthorityRound {
two_thirds_majority_transition: BlockNumber,
maximum_empty_steps: usize,
machine: EthereumMachine,
/// If set, enables random number contract integration.
randomness_contract_address: Option<Address>,
/// The block number at which the consensus engine switches from AuRa to AuRa with POSDAO
/// modifications.
posdao_transition: Option<BlockNumber>,
/// The addresses of a contracts that determine the block gas limit.
block_gas_limit_contract_transitions: BTreeMap<u64, Address>,
/// History of step hashes recently received from peers.
received_step_hashes: RwLock<BTreeMap<(u64, Address), H256>>,
/// If set, enables random number contract integration. It maps the transition block to the contract address.
randomness_contract_address: BTreeMap<u64, Address>,
}

// header-chain validator.
Expand Down Expand Up @@ -1479,7 +1484,7 @@ impl Engine<EthereumMachine> for AuthorityRound {
};

// Random number generation
if let Some(contract_addr) = self.randomness_contract_address {
if let Some((_, &contract_addr)) = self.randomness_contract_address.range(..=block.header.number()).last() {
let contract = util::BoundContract::bind(&*client, BlockId::Latest, contract_addr);
// TODO: How should these errors be handled?
let phase = randomness::RandomnessPhase::load(&contract, our_addr)
Expand Down Expand Up @@ -1917,7 +1922,7 @@ mod tests {
block_reward_contract_transitions: Default::default(),
strict_empty_steps_transition: 0,
two_thirds_majority_transition: 0,
randomness_contract_address: None,
randomness_contract_address: BTreeMap::new(),
posdao_transition: Some(0),
block_gas_limit_contract_transitions: BTreeMap::new(),
};
Expand Down
21 changes: 2 additions & 19 deletions ethcore/src/engines/clique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,8 @@ pub struct Clique {

impl Clique {
/// Initialize Clique engine from empty state.
pub fn new(our_params: CliqueParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> {
let mut engine = Clique {
epoch_length: our_params.epoch,
period: our_params.period,
client: Default::default(),
block_state_by_hash: RwLock::new(LruCache::new(STATE_CACHE_NUM)),
proposals: Default::default(),
signer: Default::default(),
machine,
step_service: None,
};

let res = Arc::new(engine);

if our_params.period > 0 {
engine.step_service = Some(StepService::start(Arc::downgrade(&res) as Weak<Engine<_>>));
}

Ok(res)
pub fn new(_our_params: CliqueParams, _machine: EthereumMachine) -> Result<Arc<Self>, Error> {
unimplemented!()
}

#[cfg(test)]
Expand Down
15 changes: 11 additions & 4 deletions json/src/spec/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ pub struct AuthorityRoundParams {
pub strict_empty_steps_transition: Option<Uint>,
/// First block for which a 2/3 quorum (instead of 1/2) is required.
pub two_thirds_majority_transition: Option<Uint>,
/// If set, enables random number contract integration, and use Proof of
/// Stake (PoS) consensus. Otherwise, use Proof of Authority (PoA)
/// consensus.
pub randomness_contract_address: Option<Address>,
/// A map of randomness contract transitions.
pub randomness_contract_address: Option<BTreeMap<Uint, Address>>,
/// The block number at which the consensus engine switches from AuRa to AuRa with POSDAO
/// modifications.
pub posdao_transition: Option<Uint>,
Expand Down Expand Up @@ -117,6 +115,10 @@ mod tests {
"blockGasLimitContractTransitions": {
"10": "0x1000000000000000000000000000000000000001",
"20": "0x2000000000000000000000000000000000000002"
},
"randomnessContractAddress": {
"10": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"20": "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
}
}
}"#;
Expand All @@ -133,5 +135,10 @@ mod tests {
(Uint(20.into()), Address(H160::from_str("2000000000000000000000000000000000000002").unwrap()))];
assert_eq!(deserialized.params.block_gas_limit_contract_transitions,
Some(expected_bglc.to_vec().into_iter().collect()));
assert_eq!(deserialized.params.randomness_contract_address.unwrap(),
vec![
(Uint(10.into()), Address(H160::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap())),
(Uint(20.into()), Address(H160::from_str("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb").unwrap())),
].into_iter().collect());
}
}

0 comments on commit 1112121

Please sign in to comment.