RustFmt the repo (sorry not sorry)

This commit is contained in:
Hernando Castano
2020-02-02 22:13:28 -05:00
committed by Bastian Köcher
parent ffdb987a54
commit 987888e163
8 changed files with 440 additions and 419 deletions
+12 -11
View File
@@ -14,16 +14,16 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>. // along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.
use sp_std::prelude::*; use crate::error::Error;
use crate::{ancestry, Storage};
use primitives::{public_to_address, Address, Header, SealedEmptyStep, H256};
use sp_io::crypto::secp256k1_ecdsa_recover;
use sp_std::collections::{ use sp_std::collections::{
btree_map::{BTreeMap, Entry}, btree_map::{BTreeMap, Entry},
btree_set::BTreeSet, btree_set::BTreeSet,
vec_deque::VecDeque, vec_deque::VecDeque,
}; };
use sp_io::crypto::secp256k1_ecdsa_recover; use sp_std::prelude::*;
use primitives::{Address, H256, Header, SealedEmptyStep, public_to_address};
use crate::{Storage, ancestry};
use crate::error::Error;
/// Tries to finalize blocks when given block is imported. /// Tries to finalize blocks when given block is imported.
/// ///
@@ -68,8 +68,8 @@ fn is_finalized(
votes: &BTreeMap<Address, u64>, votes: &BTreeMap<Address, u64>,
requires_two_thirds_majority: bool, requires_two_thirds_majority: bool,
) -> bool { ) -> bool {
(!requires_two_thirds_majority && votes.len() * 2 > validators.len()) || (!requires_two_thirds_majority && votes.len() * 2 > validators.len())
(requires_two_thirds_majority && votes.len() * 3 > validators.len() * 2) || (requires_two_thirds_majority && votes.len() * 3 > validators.len() * 2)
} }
/// Prepare 'votes' of header and its ancestors' signers. /// Prepare 'votes' of header and its ancestors' signers.
@@ -157,7 +157,7 @@ fn remove_signers_votes(signers_to_remove: &BTreeSet<Address>, votes: &mut BTree
} else { } else {
*entry.get_mut() -= 1; *entry.get_mut() -= 1;
} }
}, }
Entry::Vacant(_) => unreachable!("we only remove signers that have been added; qed"), Entry::Vacant(_) => unreachable!("we only remove signers that have been added; qed"),
} }
} }
@@ -165,7 +165,8 @@ fn remove_signers_votes(signers_to_remove: &BTreeSet<Address>, votes: &mut BTree
/// Returns unique set of empty steps signers. /// Returns unique set of empty steps signers.
fn empty_steps_signers(header: &Header) -> BTreeSet<Address> { fn empty_steps_signers(header: &Header) -> BTreeSet<Address> {
header.empty_steps() header
.empty_steps()
.into_iter() .into_iter()
.flat_map(|steps| steps) .flat_map(|steps| steps)
.filter_map(|step| empty_step_signer(&step, &header.parent_hash)) .filter_map(|step| empty_step_signer(&step, &header.parent_hash))
@@ -182,9 +183,9 @@ fn empty_step_signer(empty_step: &SealedEmptyStep, parent_hash: &H256) -> Option
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::HeaderToImport;
use crate::tests::{InMemoryStorage, genesis, validator, validators_addresses};
use super::*; use super::*;
use crate::tests::{genesis, validator, validators_addresses, InMemoryStorage};
use crate::HeaderToImport;
#[test] #[test]
fn verifies_header_author() { fn verifies_header_author() {
+39 -66
View File
@@ -14,13 +14,13 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>. // along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.
use sp_std::prelude::*;
use primitives::{H256, Header, Receipt};
use crate::{AuraConfiguration, Storage};
use crate::error::Error; use crate::error::Error;
use crate::finality::finalize_blocks; use crate::finality::finalize_blocks;
use crate::validators::{Validators, ValidatorsConfiguration}; use crate::validators::{Validators, ValidatorsConfiguration};
use crate::verification::verify_aura_header; use crate::verification::verify_aura_header;
use crate::{AuraConfiguration, Storage};
use primitives::{Header, Receipt, H256};
use sp_std::prelude::*;
/// Maximal number of headers behind best blocks that we are aiming to store. When there /// Maximal number of headers behind best blocks that we are aiming to store. When there
/// are too many unfinalized headers, it slows down finalization tracking significantly. /// are too many unfinalized headers, it slows down finalization tracking significantly.
@@ -50,14 +50,7 @@ pub fn import_headers<S: Storage>(
let mut useful = 0; let mut useful = 0;
let mut useless = 0; let mut useless = 0;
for (header, receipts) in headers { for (header, receipts) in headers {
let import_result = import_header( let import_result = import_header(storage, aura_config, validators_config, prune_depth, header, receipts);
storage,
aura_config,
validators_config,
prune_depth,
header,
receipts,
);
match import_result { match import_result {
Ok(_) => useful += 1, Ok(_) => useful += 1,
@@ -85,16 +78,11 @@ pub fn import_header<S: Storage>(
let (hash, prev_finalized_hash) = is_importable_header(storage, &header)?; let (hash, prev_finalized_hash) = is_importable_header(storage, &header)?;
// verify header // verify header
let import_context = verify_aura_header( let import_context = verify_aura_header(storage, aura_config, &header)?;
storage,
aura_config,
&header,
)?;
// check if block schedules new validators // check if block schedules new validators
let validators = Validators::new(validators_config); let validators = Validators::new(validators_config);
let (scheduled_change, enacted_change) = let (scheduled_change, enacted_change) = validators.extract_validators_change(&header, receipts)?;
validators.extract_validators_change(&header, receipts)?;
// check if block finalizes some other blocks and corresponding scheduled validators // check if block finalizes some other blocks and corresponding scheduled validators
let finalized_blocks = finalize_blocks( let finalized_blocks = finalize_blocks(
@@ -105,8 +93,7 @@ pub fn import_header<S: Storage>(
&header, &header,
aura_config.two_thirds_majority_transition, aura_config.two_thirds_majority_transition,
)?; )?;
let enacted_change = enacted_change let enacted_change = enacted_change.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));
.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));
// NOTE: we can't return Err() from anywhere below this line // NOTE: we can't return Err() from anywhere below this line
// (because otherwise we'll have inconsistent storage if transaction will fail) // (because otherwise we'll have inconsistent storage if transaction will fail)
@@ -169,14 +156,12 @@ fn is_importable_header<S: Storage>(storage: &S, header: &Header) -> Result<(H25
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{kovan_aura_config, kovan_validators_config}; use super::*;
use crate::tests::{ use crate::tests::{
InMemoryStorage, block_i, custom_block_i, genesis, signed_header, validator, validators_addresses, InMemoryStorage,
block_i, custom_block_i, signed_header, genesis,
validator, validators_addresses,
}; };
use crate::validators::ValidatorsSource; use crate::validators::ValidatorsSource;
use super::*; use crate::{kovan_aura_config, kovan_validators_config};
#[test] #[test]
fn rejects_finalized_block_competitors() { fn rejects_finalized_block_competitors() {
@@ -208,7 +193,8 @@ mod tests {
PRUNE_DEPTH, PRUNE_DEPTH,
block.clone(), block.clone(),
None, None,
).map(|_| ()), )
.map(|_| ()),
Ok(()), Ok(()),
); );
assert_eq!( assert_eq!(
@@ -219,7 +205,8 @@ mod tests {
PRUNE_DEPTH, PRUNE_DEPTH,
block, block,
None, None,
).map(|_| ()), )
.map(|_| ()),
Err(Error::KnownHeader), Err(Error::KnownHeader),
); );
} }
@@ -235,7 +222,14 @@ mod tests {
let header = block_i(&storage, 1, &validators); let header = block_i(&storage, 1, &validators);
let hash = header.hash(); let hash = header.hash();
assert_eq!( assert_eq!(
import_header(&mut storage, &kovan_aura_config(), &validators_config, PRUNE_DEPTH, header, None) import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
PRUNE_DEPTH,
header,
None
)
.map(|_| ()), .map(|_| ()),
Ok(()), Ok(()),
); );
@@ -250,9 +244,8 @@ mod tests {
#[test] #[test]
fn headers_are_pruned() { fn headers_are_pruned() {
let validators_config = ValidatorsConfiguration::Single( let validators_config =
ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)), ValidatorsConfiguration::Single(ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)));
);
let validators = vec![validator(0), validator(1), validator(2)]; let validators = vec![validator(0), validator(1), validator(2)];
let mut storage = InMemoryStorage::new(genesis(), validators_addresses(3)); let mut storage = InMemoryStorage::new(genesis(), validators_addresses(3));
@@ -261,14 +254,8 @@ mod tests {
let mut last_block_hash = Default::default(); let mut last_block_hash = Default::default();
for i in 1..11 { for i in 1..11 {
let header = block_i(&storage, i, &validators); let header = block_i(&storage, i, &validators);
last_block_hash = import_header( last_block_hash =
&mut storage, import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
} }
assert!(storage.header(&genesis().hash()).is_some()); assert!(storage.header(&genesis().hash()).is_some());
@@ -276,7 +263,9 @@ mod tests {
// => we prune header#0 // => we prune header#0
let header = custom_block_i(&storage, 11, &validators, |header| { let header = custom_block_i(&storage, 11, &validators, |header| {
header.log_bloom = (&[0xff; 256]).into(); header.log_bloom = (&[0xff; 256]).into();
header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951".parse().unwrap(); header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951"
.parse()
.unwrap();
}); });
last_block_hash = import_header( last_block_hash = import_header(
&mut storage, &mut storage,
@@ -284,8 +273,11 @@ mod tests {
&validators_config, &validators_config,
10, 10,
header, header,
Some(vec![crate::validators::tests::validators_change_recept(last_block_hash)]), Some(vec![crate::validators::tests::validators_change_recept(
).unwrap(); last_block_hash,
)]),
)
.unwrap();
assert!(storage.header(&genesis().hash()).is_none()); assert!(storage.header(&genesis().hash()).is_none());
// and now let's say validators 1 && 2 went offline // and now let's say validators 1 && 2 went offline
@@ -298,22 +290,13 @@ mod tests {
parent_hash: last_block_hash, parent_hash: last_block_hash,
gas_limit: 0x2000.into(), gas_limit: 0x2000.into(),
author: validator(2).address().to_fixed_bytes().into(), author: validator(2).address().to_fixed_bytes().into(),
seal: vec![ seal: vec![vec![step].into(), vec![].into()],
vec![step].into(),
vec![].into(),
],
difficulty: i.into(), difficulty: i.into(),
..Default::default() ..Default::default()
}; };
let header = signed_header(&validators, header, step as _); let header = signed_header(&validators, header, step as _);
last_block_hash = import_header( last_block_hash =
&mut storage, import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
step += 3; step += 3;
} }
assert_eq!(storage.oldest_unpruned_block(), 11); assert_eq!(storage.oldest_unpruned_block(), 11);
@@ -326,22 +309,12 @@ mod tests {
parent_hash: last_block_hash, parent_hash: last_block_hash,
gas_limit: 0x2000.into(), gas_limit: 0x2000.into(),
author: validator(0).address().to_fixed_bytes().into(), author: validator(0).address().to_fixed_bytes().into(),
seal: vec![ seal: vec![vec![step].into(), vec![].into()],
vec![step].into(),
vec![].into(),
],
difficulty: 25.into(), difficulty: 25.into(),
..Default::default() ..Default::default()
}; };
let header = signed_header(&validators, header, step as _); let header = signed_header(&validators, header, step as _);
import_header( import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
assert_eq!(storage.oldest_unpruned_block(), 15); assert_eq!(storage.oldest_unpruned_block(), 15);
} }
} }
+173 -112
View File
@@ -16,14 +16,14 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
use sp_std::{prelude::*, iter::from_fn};
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{decl_module, decl_storage}; use frame_support::{decl_module, decl_storage};
use primitives::{Address, Header, Receipt, H256, U256};
use sp_runtime::RuntimeDebug; use sp_runtime::RuntimeDebug;
use primitives::{Address, U256, H256, Header, Receipt}; use sp_std::{iter::from_fn, prelude::*};
use validators::{ValidatorsSource, ValidatorsConfiguration}; use validators::{ValidatorsConfiguration, ValidatorsSource};
pub use import::{import_header, header_import_requires_receipts}; pub use import::{header_import_requires_receipts, import_header};
mod error; mod error;
mod finality; mod finality;
@@ -180,11 +180,7 @@ pub trait Storage {
/// It is the storage duty to ensure that unfinalized headers that have /// It is the storage duty to ensure that unfinalized headers that have
/// scheduled changes won't be pruned until they or their competitors /// scheduled changes won't be pruned until they or their competitors
/// are finalized. /// are finalized.
fn finalize_headers( fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>);
&mut self,
finalized: Option<(u64, H256)>,
prune_end: Option<u64>,
);
} }
/// Decides whether the session should be ended. /// Decides whether the session should be ended.
@@ -304,11 +300,7 @@ impl<T: Trait> Module<T> {
/// Returns true if the import of given block requires transactions receipts. /// Returns true if the import of given block requires transactions receipts.
pub fn is_import_requires_receipts(header: Header) -> bool { pub fn is_import_requires_receipts(header: Header) -> bool {
import::header_import_requires_receipts( import::header_import_requires_receipts(&BridgeStorage, &kovan_validators_config(), &header)
&BridgeStorage,
&kovan_validators_config(),
&header,
)
} }
/// Returns true if header is known to the runtime. /// Returns true if header is known to the runtime.
@@ -334,8 +326,7 @@ impl Storage for BridgeStorage {
} }
fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> { fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> {
Headers::get(parent_hash) Headers::get(parent_hash).map(|parent_header| {
.map(|parent_header| {
let (next_validators_set_start, next_validators) = let (next_validators_set_start, next_validators) =
ValidatorsSets::get(parent_header.next_validators_set_id) ValidatorsSets::get(parent_header.next_validators_set_id)
.expect("validators set is only pruned when last ref is pruned; there is a ref; qed"); .expect("validators set is only pruned when last ref is pruned; there is a ref; qed");
@@ -369,34 +360,33 @@ impl Storage for BridgeStorage {
ValidatorsSets::insert(next_validators_set_id, (header.hash, enacted_change)); ValidatorsSets::insert(next_validators_set_id, (header.hash, enacted_change));
ValidatorsSetsRc::insert(next_validators_set_id, 1); ValidatorsSetsRc::insert(next_validators_set_id, 1);
next_validators_set_id next_validators_set_id
}, }
None => { None => {
ValidatorsSetsRc::mutate( ValidatorsSetsRc::mutate(header.context.next_validators_set_id, |rc| {
header.context.next_validators_set_id,
|rc| {
*rc = Some(rc.map(|rc| rc + 1).unwrap_or(1)); *rc = Some(rc.map(|rc| rc + 1).unwrap_or(1));
*rc *rc
}, });
);
header.context.next_validators_set_id header.context.next_validators_set_id
}, }
}; };
HeadersByNumber::append_or_insert(header.header.number, vec![header.hash]); HeadersByNumber::append_or_insert(header.header.number, vec![header.hash]);
Headers::insert(&header.hash, StoredHeader { Headers::insert(
&header.hash,
StoredHeader {
header: header.header, header: header.header,
total_difficulty: header.total_difficulty, total_difficulty: header.total_difficulty,
next_validators_set_id, next_validators_set_id,
}); },
);
} }
fn finalize_headers( fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>) {
&mut self,
finalized: Option<(u64, H256)>,
prune_end: Option<u64>,
) {
// remember just finalized block // remember just finalized block
let finalized_number = finalized.as_ref().map(|f| f.0).unwrap_or_else(|| FinalizedBlock::get().0); let finalized_number = finalized
.as_ref()
.map(|f| f.0)
.unwrap_or_else(|| FinalizedBlock::get().0);
if let Some(finalized) = finalized { if let Some(finalized) = finalized {
FinalizedBlock::put(finalized); FinalizedBlock::put(finalized);
} }
@@ -423,13 +413,10 @@ impl Storage for BridgeStorage {
let header = Headers::take(&hash); let header = Headers::take(&hash);
ScheduledChanges::remove(hash); ScheduledChanges::remove(hash);
if let Some(header) = header { if let Some(header) = header {
ValidatorsSetsRc::mutate( ValidatorsSetsRc::mutate(header.next_validators_set_id, |rc| match *rc {
header.next_validators_set_id,
|rc| match *rc {
Some(rc) if rc > 1 => Some(rc - 1), Some(rc) if rc > 1 => Some(rc - 1),
_ => None, _ => None,
}, });
);
} }
} }
} }
@@ -456,50 +443,113 @@ pub fn kovan_aura_config() -> AuraConfiguration {
/// Validators configuration for Kovan chain. /// Validators configuration for Kovan chain.
pub fn kovan_validators_config() -> ValidatorsConfiguration { pub fn kovan_validators_config() -> ValidatorsConfiguration {
ValidatorsConfiguration::Multi(vec![ ValidatorsConfiguration::Multi(vec![
(0, ValidatorsSource::List(vec![ (
[0x00, 0xD6, 0xCc, 0x1B, 0xA9, 0xcf, 0x89, 0xBD, 0x2e, 0x58, 0,
0x00, 0x97, 0x41, 0xf4, 0xF7, 0x32, 0x5B, 0xAd, 0xc0, 0xED].into(), ValidatorsSource::List(vec![
[0x00, 0x42, 0x7f, 0xea, 0xe2, 0x41, 0x9c, 0x15, 0xb8, 0x9d, [
0x1c, 0x21, 0xaf, 0x10, 0xd1, 0xb6, 0x65, 0x0a, 0x4d, 0x3d].into(), 0x00, 0xD6, 0xCc, 0x1B, 0xA9, 0xcf, 0x89, 0xBD, 0x2e, 0x58, 0x00, 0x97, 0x41, 0xf4, 0xF7, 0x32,
[0x4E, 0xd9, 0xB0, 0x8e, 0x63, 0x54, 0xC7, 0x0f, 0xE6, 0xF8, 0x5B, 0xAd, 0xc0, 0xED,
0xCB, 0x04, 0x11, 0xb0, 0xd3, 0x24, 0x6b, 0x42, 0x4d, 0x6c].into(), ]
[0x00, 0x20, 0xee, 0x4B, 0xe0, 0xe2, 0x02, 0x7d, 0x76, 0x60, .into(),
0x3c, 0xB7, 0x51, 0xeE, 0x06, 0x95, 0x19, 0xbA, 0x81, 0xA1].into(), [
[0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5, 0x00, 0x42, 0x7f, 0xea, 0xe2, 0x41, 0x9c, 0x15, 0xb8, 0x9d, 0x1c, 0x21, 0xaf, 0x10, 0xd1, 0xb6,
0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0, 0x3a, 0xfd, 0x03, 0x2d].into(), 0x65, 0x0a, 0x4d, 0x3d,
[0x00, 0x77, 0x33, 0xa1, 0xFE, 0x69, 0xCF, 0x3f, 0x2C, 0xF9, ]
0x89, 0xF8, 0x1C, 0x7b, 0x4c, 0xAc, 0x16, 0x93, 0x38, 0x7A].into(), .into(),
[0x00, 0xE6, 0xd2, 0xb9, 0x31, 0xF5, 0x5a, 0x3f, 0x17, 0x01, [
0xc7, 0x38, 0x9d, 0x59, 0x2a, 0x77, 0x78, 0x89, 0x78, 0x79].into(), 0x4E, 0xd9, 0xB0, 0x8e, 0x63, 0x54, 0xC7, 0x0f, 0xE6, 0xF8, 0xCB, 0x04, 0x11, 0xb0, 0xd3, 0x24,
[0x00, 0xe4, 0xa1, 0x06, 0x50, 0xe5, 0xa6, 0xD6, 0x00, 0x1C, 0x6b, 0x42, 0x4d, 0x6c,
0x38, 0xff, 0x8E, 0x64, 0xF9, 0x70, 0x16, 0xa1, 0x64, 0x5c].into(), ]
[0x00, 0xa0, 0xa2, 0x4b, 0x9f, 0x0e, 0x5e, 0xc7, 0xaa, 0x4c, .into(),
0x73, 0x89, 0xb8, 0x30, 0x2f, 0xd0, 0x12, 0x31, 0x94, 0xde].into(), [
])), 0x00, 0x20, 0xee, 0x4B, 0xe0, 0xe2, 0x02, 0x7d, 0x76, 0x60, 0x3c, 0xB7, 0x51, 0xeE, 0x06, 0x95,
(10960440, ValidatorsSource::List(vec![ 0x19, 0xbA, 0x81, 0xA1,
[0x00, 0xD6, 0xCc, 0x1B, 0xA9, 0xcf, 0x89, 0xBD, 0x2e, 0x58, ]
0x00, 0x97, 0x41, 0xf4, 0xF7, 0x32, 0x5B, 0xAd, 0xc0, 0xED].into(), .into(),
[0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5, [
0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0, 0x3a, 0xfd, 0x03, 0x2d].into(), 0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5, 0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0,
[0x00, 0xa0, 0xa2, 0x4b, 0x9f, 0x0e, 0x5e, 0xc7, 0xaa, 0x4c, 0x3a, 0xfd, 0x03, 0x2d,
0x73, 0x89, 0xb8, 0x30, 0x2f, 0xd0, 0x12, 0x31, 0x94, 0xde].into(), ]
])), .into(),
(10960500, ValidatorsSource::Contract( [
[0xaE, 0x71, 0x80, 0x7C, 0x1B, 0x0a, 0x09, 0x3c, 0xB1, 0x54, 0x00, 0x77, 0x33, 0xa1, 0xFE, 0x69, 0xCF, 0x3f, 0x2C, 0xF9, 0x89, 0xF8, 0x1C, 0x7b, 0x4c, 0xAc,
0x7b, 0x68, 0x2D, 0xC7, 0x83, 0x16, 0xD9, 0x45, 0xc9, 0xB8].into(), 0x16, 0x93, 0x38, 0x7A,
]
.into(),
[
0x00, 0xE6, 0xd2, 0xb9, 0x31, 0xF5, 0x5a, 0x3f, 0x17, 0x01, 0xc7, 0x38, 0x9d, 0x59, 0x2a, 0x77,
0x78, 0x89, 0x78, 0x79,
]
.into(),
[
0x00, 0xe4, 0xa1, 0x06, 0x50, 0xe5, 0xa6, 0xD6, 0x00, 0x1C, 0x38, 0xff, 0x8E, 0x64, 0xF9, 0x70,
0x16, 0xa1, 0x64, 0x5c,
]
.into(),
[
0x00, 0xa0, 0xa2, 0x4b, 0x9f, 0x0e, 0x5e, 0xc7, 0xaa, 0x4c, 0x73, 0x89, 0xb8, 0x30, 0x2f, 0xd0,
0x12, 0x31, 0x94, 0xde,
]
.into(),
]),
),
(
10960440,
ValidatorsSource::List(vec![
[
0x00, 0xD6, 0xCc, 0x1B, 0xA9, 0xcf, 0x89, 0xBD, 0x2e, 0x58, 0x00, 0x97, 0x41, 0xf4, 0xF7, 0x32,
0x5B, 0xAd, 0xc0, 0xED,
]
.into(),
[
0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5, 0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0,
0x3a, 0xfd, 0x03, 0x2d,
]
.into(),
[
0x00, 0xa0, 0xa2, 0x4b, 0x9f, 0x0e, 0x5e, 0xc7, 0xaa, 0x4c, 0x73, 0x89, 0xb8, 0x30, 0x2f, 0xd0,
0x12, 0x31, 0x94, 0xde,
]
.into(),
]),
),
(
10960500,
ValidatorsSource::Contract(
[
0xaE, 0x71, 0x80, 0x7C, 0x1B, 0x0a, 0x09, 0x3c, 0xB1, 0x54, 0x7b, 0x68, 0x2D, 0xC7, 0x83, 0x16,
0xD9, 0x45, 0xc9, 0xB8,
]
.into(),
vec![ vec![
[0xd0, 0x5f, 0x74, 0x78, 0xc6, 0xaa, 0x10, 0x78, 0x12, 0x58, [
0xc5, 0xcc, 0x8b, 0x4f, 0x38, 0x5f, 0xc8, 0xfa, 0x98, 0x9c].into(), 0xd0, 0x5f, 0x74, 0x78, 0xc6, 0xaa, 0x10, 0x78, 0x12, 0x58, 0xc5, 0xcc, 0x8b, 0x4f, 0x38, 0x5f,
[0x03, 0x80, 0x1e, 0xfb, 0x0e, 0xfe, 0x2a, 0x25, 0xed, 0xe5, 0xc8, 0xfa, 0x98, 0x9c,
0xdd, 0x3a, 0x00, 0x3a, 0xe8, 0x80, 0xc0, 0x29, 0x2e, 0x4d].into(), ]
[0xa4, 0xdf, 0x25, 0x5e, 0xcf, 0x08, 0xbb, 0xf2, 0xc2, 0x80, .into(),
0x55, 0xc6, 0x52, 0x25, 0xc9, 0xa9, 0x84, 0x7a, 0xbd, 0x94].into(), [
[0x59, 0x6e, 0x82, 0x21, 0xa3, 0x0b, 0xfe, 0x6e, 0x7e, 0xff, 0x03, 0x80, 0x1e, 0xfb, 0x0e, 0xfe, 0x2a, 0x25, 0xed, 0xe5, 0xdd, 0x3a, 0x00, 0x3a, 0xe8, 0x80,
0x67, 0xfe, 0xe6, 0x64, 0xa0, 0x1c, 0x73, 0xba, 0x3c, 0x56].into(), 0xc0, 0x29, 0x2e, 0x4d,
[0xfa, 0xad, 0xfa, 0xce, 0x3f, 0xbd, 0x81, 0xce, 0x37, 0xb0, ]
0xe1, 0x9c, 0x0b, 0x65, 0xff, 0x42, 0x34, 0x14, 0x81, 0x32].into(), .into(),
[
0xa4, 0xdf, 0x25, 0x5e, 0xcf, 0x08, 0xbb, 0xf2, 0xc2, 0x80, 0x55, 0xc6, 0x52, 0x25, 0xc9, 0xa9,
0x84, 0x7a, 0xbd, 0x94,
]
.into(),
[
0x59, 0x6e, 0x82, 0x21, 0xa3, 0x0b, 0xfe, 0x6e, 0x7e, 0xff, 0x67, 0xfe, 0xe6, 0x64, 0xa0, 0x1c,
0x73, 0xba, 0x3c, 0x56,
]
.into(),
[
0xfa, 0xad, 0xfa, 0xce, 0x3f, 0xbd, 0x81, 0xce, 0x37, 0xb0, 0xe1, 0x9c, 0x0b, 0x65, 0xff, 0x42,
0x34, 0x14, 0x81, 0x32,
]
.into(),
], ],
)), ),
),
]) ])
} }
@@ -517,25 +567,22 @@ pub(crate) fn ancestry<'a, S: Storage>(storage: &'a S, header: &Header) -> impl
let hash = parent_hash.clone(); let hash = parent_hash.clone();
parent_hash = header.parent_hash.clone(); parent_hash = header.parent_hash.clone();
Some((hash, header)) Some((hash, header))
}, }
None => None None => None,
} }
}) })
} }
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use std::collections::{HashMap, hash_map::Entry};
use parity_crypto::publickey::{KeyPair, Secret, sign};
use primitives::{H520, rlp_encode};
use super::*; use super::*;
use parity_crypto::publickey::{sign, KeyPair, Secret};
use primitives::{rlp_encode, H520};
use std::collections::{hash_map::Entry, HashMap};
pub fn genesis() -> Header { pub fn genesis() -> Header {
Header { Header {
seal: vec![ seal: vec![vec![42].into(), vec![].into()],
vec![42].into(),
vec![].into(),
],
..Default::default() ..Default::default()
} }
} }
@@ -556,10 +603,7 @@ pub(crate) mod tests {
parent_hash: storage.headers_by_number[&(number - 1)][0].clone(), parent_hash: storage.headers_by_number[&(number - 1)][0].clone(),
gas_limit: 0x2000.into(), gas_limit: 0x2000.into(),
author: validator(validator_index).address().to_fixed_bytes().into(), author: validator(validator_index).address().to_fixed_bytes().into(),
seal: vec![ seal: vec![vec![number as u8 + 42].into(), vec![].into()],
vec![number as u8 + 42].into(),
vec![].into(),
],
difficulty: number.into(), difficulty: number.into(),
..Default::default() ..Default::default()
}; };
@@ -582,7 +626,9 @@ pub(crate) mod tests {
} }
pub fn validators_addresses(count: u8) -> Vec<Address> { pub fn validators_addresses(count: u8) -> Vec<Address> {
(0..count as usize).map(|i| validator(i as u8).address().as_fixed_bytes().into()).collect() (0..count as usize)
.map(|i| validator(i as u8).address().as_fixed_bytes().into())
.collect()
} }
pub struct InMemoryStorage { pub struct InMemoryStorage {
@@ -612,7 +658,9 @@ pub(crate) mod tests {
total_difficulty: 0.into(), total_difficulty: 0.into(),
next_validators_set_id: 0, next_validators_set_id: 0,
}, },
)].into_iter().collect(), )]
.into_iter()
.collect(),
next_validators_set_id: 1, next_validators_set_id: 1,
validators_sets: vec![(0, (hash, initial_validators))].into_iter().collect(), validators_sets: vec![(0, (hash, initial_validators))].into_iter().collect(),
validators_sets_rc: vec![(0, 1)].into_iter().collect(), validators_sets_rc: vec![(0, 1)].into_iter().collect(),
@@ -643,8 +691,7 @@ pub(crate) mod tests {
} }
fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> { fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> {
self.headers.get(parent_hash) self.headers.get(parent_hash).map(|parent_header| {
.map(|parent_header| {
let (next_validators_set_start, next_validators) = let (next_validators_set_start, next_validators) =
self.validators_sets.get(&parent_header.next_validators_set_id).unwrap(); self.validators_sets.get(&parent_header.next_validators_set_id).unwrap();
ImportContext { ImportContext {
@@ -671,30 +718,39 @@ pub(crate) mod tests {
Some(enacted_change) => { Some(enacted_change) => {
let next_validators_set_id = self.next_validators_set_id; let next_validators_set_id = self.next_validators_set_id;
self.next_validators_set_id += 1; self.next_validators_set_id += 1;
self.validators_sets.insert(next_validators_set_id, (header.hash, enacted_change)); self.validators_sets
.insert(next_validators_set_id, (header.hash, enacted_change));
self.validators_sets_rc.insert(next_validators_set_id, 1); self.validators_sets_rc.insert(next_validators_set_id, 1);
next_validators_set_id next_validators_set_id
}, }
None => { None => {
*self.validators_sets_rc.entry(header.context.next_validators_set_id).or_default() += 1; *self
.validators_sets_rc
.entry(header.context.next_validators_set_id)
.or_default() += 1;
header.context.next_validators_set_id header.context.next_validators_set_id
}, }
}; };
self.headers_by_number.entry(header.header.number).or_default().push(header.hash); self.headers_by_number
self.headers.insert(header.hash, StoredHeader { .entry(header.header.number)
.or_default()
.push(header.hash);
self.headers.insert(
header.hash,
StoredHeader {
header: header.header, header: header.header,
total_difficulty: header.total_difficulty, total_difficulty: header.total_difficulty,
next_validators_set_id, next_validators_set_id,
}); },
);
} }
fn finalize_headers( fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>) {
&mut self, let finalized_number = finalized
finalized: Option<(u64, H256)>, .as_ref()
prune_end: Option<u64>, .map(|f| f.0)
) { .unwrap_or_else(|| self.finalized_block.0);
let finalized_number = finalized.as_ref().map(|f| f.0).unwrap_or_else(|| self.finalized_block.0);
if let Some(finalized) = finalized { if let Some(finalized) = finalized {
self.finalized_block = finalized; self.finalized_block = finalized;
} }
@@ -708,7 +764,10 @@ pub(crate) mod tests {
// ensure that unfinalized headers we want to prune do not have scheduled changes // ensure that unfinalized headers we want to prune do not have scheduled changes
if number > finalized_number { if number > finalized_number {
if let Some(ref blocks_at_number) = blocks_at_number { if let Some(ref blocks_at_number) = blocks_at_number {
if blocks_at_number.iter().any(|block| self.scheduled_changes.contains_key(block)) { if blocks_at_number
.iter()
.any(|block| self.scheduled_changes.contains_key(block))
{
self.headers_by_number.insert(number, blocks_at_number.clone()); self.headers_by_number.insert(number, blocks_at_number.clone());
self.oldest_unpruned_block = number; self.oldest_unpruned_block = number;
return; return;
@@ -722,12 +781,14 @@ pub(crate) mod tests {
self.scheduled_changes.remove(&hash); self.scheduled_changes.remove(&hash);
if let Some(header) = header { if let Some(header) = header {
match self.validators_sets_rc.entry(header.next_validators_set_id) { match self.validators_sets_rc.entry(header.next_validators_set_id) {
Entry::Occupied(mut entry) => if *entry.get() == 1 { Entry::Occupied(mut entry) => {
if *entry.get() == 1 {
entry.remove(); entry.remove();
} else { } else {
*entry.get_mut() -= 1; *entry.get_mut() -= 1;
}, }
Entry::Vacant(_) => unreachable!("there's entry for each header") }
Entry::Vacant(_) => unreachable!("there's entry for each header"),
}; };
} }
} }
+51 -51
View File
@@ -14,15 +14,16 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>. // along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.
use sp_std::prelude::*;
use primitives::{Address, H256, Header, LogEntry, Receipt, U256};
use crate::Storage;
use crate::error::Error; use crate::error::Error;
use crate::Storage;
use primitives::{Address, Header, LogEntry, Receipt, H256, U256};
use sp_std::prelude::*;
/// The hash of InitiateChange event of the validators set contract. /// The hash of InitiateChange event of the validators set contract.
const CHANGE_EVENT_HASH: &'static [u8; 32] = &[0x55, 0x25, 0x2f, 0xa6, 0xee, 0xe4, 0x74, 0x1b, const CHANGE_EVENT_HASH: &'static [u8; 32] = &[
0x4e, 0x24, 0xa7, 0x4a, 0x70, 0xe9, 0xc1, 0x1f, 0xd2, 0xc2, 0x28, 0x1d, 0xf8, 0xd6, 0xea, 0x55, 0x25, 0x2f, 0xa6, 0xee, 0xe4, 0x74, 0x1b, 0x4e, 0x24, 0xa7, 0x4a, 0x70, 0xe9, 0xc1, 0x1f, 0xd2, 0xc2, 0x28,
0x13, 0x12, 0x6f, 0xf8, 0x45, 0xf7, 0x82, 0x5c, 0x89]; 0x1d, 0xf8, 0xd6, 0xea, 0x13, 0x12, 0x6f, 0xf8, 0x45, 0xf7, 0x82, 0x5c, 0x89,
];
/// Where source of validators addresses come from. This covers the chain lifetime. /// Where source of validators addresses come from. This covers the chain lifetime.
pub enum ValidatorsConfiguration { pub enum ValidatorsConfiguration {
@@ -76,12 +77,10 @@ impl<'a> Validators<'a> {
// that the contract has (probably) emitted epoch change event // that the contract has (probably) emitted epoch change event
let expected_bloom = LogEntry { let expected_bloom = LogEntry {
address: *contract_address, address: *contract_address,
topics: vec![ topics: vec![CHANGE_EVENT_HASH.into(), header.parent_hash],
CHANGE_EVENT_HASH.into(),
header.parent_hash,
],
data: Vec::new(), // irrelevant for bloom. data: Vec::new(), // irrelevant for bloom.
}.bloom(); }
.bloom();
header.log_bloom.contains(&expected_bloom) header.log_bloom.contains(&expected_bloom)
} }
@@ -120,12 +119,10 @@ impl<'a> Validators<'a> {
// that the contract has (probably) emitted epoch change event // that the contract has (probably) emitted epoch change event
let expected_bloom = LogEntry { let expected_bloom = LogEntry {
address: *contract_address, address: *contract_address,
topics: vec![ topics: vec![CHANGE_EVENT_HASH.into(), header.parent_hash],
CHANGE_EVENT_HASH.into(),
header.parent_hash,
],
data: Vec::new(), // irrelevant for bloom. data: Vec::new(), // irrelevant for bloom.
}.bloom(); }
.bloom();
if !header.log_bloom.contains(&expected_bloom) { if !header.log_bloom.contains(&expected_bloom) {
return Ok((None, None)); return Ok((None, None));
@@ -138,15 +135,17 @@ impl<'a> Validators<'a> {
// iterate in reverse because only the _last_ change in a given // iterate in reverse because only the _last_ change in a given
// block actually has any effect // block actually has any effect
Ok((receipts.iter() Ok((
receipts
.iter()
.rev() .rev()
.filter(|r| r.log_bloom.contains(&expected_bloom)) .filter(|r| r.log_bloom.contains(&expected_bloom))
.flat_map(|r| r.logs.iter()) .flat_map(|r| r.logs.iter())
.filter(|l| l.address == *contract_address && .filter(|l| {
l.topics.len() == 2 && l.address == *contract_address
l.topics[0].as_fixed_bytes() == CHANGE_EVENT_HASH && && l.topics.len() == 2 && l.topics[0].as_fixed_bytes() == CHANGE_EVENT_HASH
l.topics[1] == header.parent_hash && l.topics[1] == header.parent_hash
) })
.filter_map(|l| { .filter_map(|l| {
let data_len = l.data.len(); let data_len = l.data.len();
if data_len < 64 { if data_len < 64 {
@@ -163,16 +162,20 @@ impl<'a> Validators<'a> {
return None; return None;
} }
Some(l.data[64..] Some(
l.data[64..]
.chunks(32) .chunks(32)
.map(|chunk| { .map(|chunk| {
let mut new_validator = Address::default(); let mut new_validator = Address::default();
new_validator.as_mut().copy_from_slice(&chunk[12..32]); new_validator.as_mut().copy_from_slice(&chunk[12..32]);
new_validator new_validator
}) })
.collect()) .collect(),
)
}) })
.next(), None)) .next(),
None,
))
} }
/// Finalize changes when blocks are finalized. /// Finalize changes when blocks are finalized.
@@ -193,12 +196,16 @@ impl<'a> Validators<'a> {
fn source_at<'b>(&'b self, header_number: u64) -> (usize, u64, &'b ValidatorsSource) { fn source_at<'b>(&'b self, header_number: u64) -> (usize, u64, &'b ValidatorsSource) {
match self.config { match self.config {
ValidatorsConfiguration::Single(ref source) => (0, 0, source), ValidatorsConfiguration::Single(ref source) => (0, 0, source),
ValidatorsConfiguration::Multi(ref sources) => sources.iter().rev() ValidatorsConfiguration::Multi(ref sources) => sources
.iter()
.rev()
.enumerate() .enumerate()
.find(|(_, &(begin, _))| begin < header_number) .find(|(_, &(begin, _))| begin < header_number)
.map(|(i, (begin, source))| (sources.len() - 1 - i, *begin, source)) .map(|(i, (begin, source))| (sources.len() - 1 - i, *begin, source))
.expect("there's always entry for the initial block;\ .expect(
we do not touch any headers with number < initial block number; qed"), "there's always entry for the initial block;\
we do not touch any headers with number < initial block number; qed",
),
} }
} }
@@ -221,7 +228,7 @@ impl<'a> Validators<'a> {
let source = &sources[header_source_index]; let source = &sources[header_source_index];
(source.0, &source.1) (source.0, &source.1)
}, }
} }
} }
} }
@@ -243,32 +250,24 @@ pub fn step_validator(header_validators: &[Address], header_step: u64) -> Addres
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use primitives::TransactionOutcome;
use crate::kovan_validators_config;
use super::*; use super::*;
use crate::kovan_validators_config;
use primitives::TransactionOutcome;
pub(crate) fn validators_change_recept(parent_hash: H256) -> Receipt { pub(crate) fn validators_change_recept(parent_hash: H256) -> Receipt {
Receipt { Receipt {
gas_used: 0.into(), gas_used: 0.into(),
log_bloom: (&[0xff; 256]).into(), log_bloom: (&[0xff; 256]).into(),
outcome: TransactionOutcome::Unknown, outcome: TransactionOutcome::Unknown,
logs: vec![ logs: vec![LogEntry {
LogEntry {
address: [3; 20].into(), address: [3; 20].into(),
topics: vec![ topics: vec![CHANGE_EVENT_HASH.into(), parent_hash],
CHANGE_EVENT_HASH.into(),
parent_hash,
],
data: vec![ data: vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
],
},
], ],
}],
} }
} }
@@ -347,10 +346,7 @@ pub(crate) mod tests {
// when we're inside list range // when we're inside list range
header.number = 150; header.number = 150;
assert_eq!( assert_eq!(validators.extract_validators_change(&header, None), Ok((None, None)),);
validators.extract_validators_change(&header, None),
Ok((None, None)),
);
// when we're at the block that switches to contract source // when we're at the block that switches to contract source
header.number = 200; header.number = 200;
@@ -370,7 +366,9 @@ pub(crate) mod tests {
// when we're inside contract range and logs bloom signals change // when we're inside contract range and logs bloom signals change
// but there's no change in receipts // but there's no change in receipts
header.receipts_root = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".parse().unwrap(); header.receipts_root = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
.parse()
.unwrap();
assert_eq!( assert_eq!(
validators.extract_validators_change(&header, Some(Vec::new())), validators.extract_validators_change(&header, Some(Vec::new())),
Ok((None, None)), Ok((None, None)),
@@ -379,7 +377,9 @@ pub(crate) mod tests {
// when we're inside contract range and logs bloom signals change // when we're inside contract range and logs bloom signals change
// and there's change in receipts // and there's change in receipts
let receipts = vec![validators_change_recept(Default::default())]; let receipts = vec![validators_change_recept(Default::default())];
header.receipts_root = "81ce88dc524403b796222046bf3daf543978329b87ffd50228f1d3987031dc45".parse().unwrap(); header.receipts_root = "81ce88dc524403b796222046bf3daf543978329b87ffd50228f1d3987031dc45"
.parse()
.unwrap();
assert_eq!( assert_eq!(
validators.extract_validators_change(&header, Some(receipts)), validators.extract_validators_change(&header, Some(receipts)),
Ok((Some(vec![[7; 20].into()]), None)), Ok((Some(vec![[7; 20].into()]), None)),
+32 -45
View File
@@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>. // along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.
use sp_io::crypto::secp256k1_ecdsa_recover;
use primitives::{Address, Header, H256, H520, SealedEmptyStep, U128, U256, public_to_address};
use crate::{AuraConfiguration, ImportContext, Storage};
use crate::error::Error; use crate::error::Error;
use crate::validators::step_validator; use crate::validators::step_validator;
use crate::{AuraConfiguration, ImportContext, Storage};
use primitives::{public_to_address, Address, Header, SealedEmptyStep, H256, H520, U128, U256};
use sp_io::crypto::secp256k1_ecdsa_recover;
/// Verify header by Aura rules. /// Verify header by Aura rules.
pub fn verify_aura_header<S: Storage>( pub fn verify_aura_header<S: Storage>(
@@ -30,14 +30,15 @@ pub fn verify_aura_header<S: Storage>(
contextless_checks(params, header)?; contextless_checks(params, header)?;
// the rest of checks requires parent // the rest of checks requires parent
let context = storage.import_context(&header.parent_hash).ok_or(Error::MissingParentBlock)?; let context = storage
.import_context(&header.parent_hash)
.ok_or(Error::MissingParentBlock)?;
let validators = context.validators(); let validators = context.validators();
let header_step = header.step().ok_or(Error::MissingStep)?; let header_step = header.step().ok_or(Error::MissingStep)?;
let parent_step = context.parent_header().step().ok_or(Error::MissingStep)?; let parent_step = context.parent_header().step().ok_or(Error::MissingStep)?;
// Ensure header is from the step after context. // Ensure header is from the step after context.
if header_step == parent_step if header_step == parent_step || (header.number >= params.validate_step_transition && header_step <= parent_step) {
|| (header.number >= params.validate_step_transition && header_step <= parent_step) {
return Err(Error::DoubleVote); return Err(Error::DoubleVote);
} }
@@ -69,7 +70,7 @@ pub fn verify_aura_header<S: Storage>(
} }
empty_steps_len empty_steps_len
}, }
false => 0, false => 0,
}; };
@@ -160,20 +161,22 @@ fn verify_signature(expected_validator: &Address, signature: &H520, message: &H2
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use parity_crypto::publickey::{KeyPair, sign};
use primitives::{H520, rlp_encode};
use crate::kovan_aura_config;
use crate::tests::{InMemoryStorage, genesis, signed_header, validator, validators_addresses};
use super::*; use super::*;
use crate::kovan_aura_config;
use crate::tests::{genesis, signed_header, validator, validators_addresses, InMemoryStorage};
use parity_crypto::publickey::{sign, KeyPair};
use primitives::{rlp_encode, H520};
fn sealed_empty_step(validators: &[KeyPair], parent_hash: &H256, step: u64) -> SealedEmptyStep { fn sealed_empty_step(validators: &[KeyPair], parent_hash: &H256, step: u64) -> SealedEmptyStep {
let mut empty_step = SealedEmptyStep { step, signature: Default::default() }; let mut empty_step = SealedEmptyStep {
step,
signature: Default::default(),
};
let message = empty_step.message(parent_hash); let message = empty_step.message(parent_hash);
let validator_index = (step % validators.len() as u64) as usize; let validator_index = (step % validators.len() as u64) as usize;
let signature: [u8; 65] = sign( let signature: [u8; 65] = sign(validators[validator_index].secret(), &message.as_fixed_bytes().into())
validators[validator_index].secret(), .unwrap()
&message.as_fixed_bytes().into(), .into();
).unwrap().into();
empty_step.signature = signature.into(); empty_step.signature = signature.into();
empty_step empty_step
} }
@@ -321,17 +324,11 @@ mod tests {
assert_eq!(default_verify(&header), Err(Error::MissingStep)); assert_eq!(default_verify(&header), Err(Error::MissingStep));
// when step is the same as for the parent block // when step is the same as for the parent block
header.seal = vec![ header.seal = vec![vec![42].into(), vec![].into()];
vec![42].into(),
vec![].into(),
];
assert_eq!(default_verify(&header), Err(Error::DoubleVote)); assert_eq!(default_verify(&header), Err(Error::DoubleVote));
// when step is OK // when step is OK
header.seal = vec![ header.seal = vec![vec![43].into(), vec![].into()];
vec![43].into(),
vec![].into(),
];
assert_ne!(default_verify(&header), Err(Error::DoubleVote)); assert_ne!(default_verify(&header), Err(Error::DoubleVote));
// now check with validate_step check enabled // now check with validate_step check enabled
@@ -339,17 +336,11 @@ mod tests {
config.validate_step_transition = 0; config.validate_step_transition = 0;
// when step is lesser that for the parent block // when step is lesser that for the parent block
header.seal = vec![ header.seal = vec![vec![40].into(), vec![].into()];
vec![40].into(),
vec![].into(),
];
assert_eq!(verify_with_config(&config, &header), Err(Error::DoubleVote)); assert_eq!(verify_with_config(&config, &header), Err(Error::DoubleVote));
// when step is OK // when step is OK
header.seal = vec![ header.seal = vec![vec![44].into(), vec![].into()];
vec![44].into(),
vec![].into(),
];
assert_ne!(verify_with_config(&config, &header), Err(Error::DoubleVote)); assert_ne!(verify_with_config(&config, &header), Err(Error::DoubleVote));
} }
@@ -364,9 +355,7 @@ mod tests {
seal: vec![ seal: vec![
vec![45].into(), vec![45].into(),
vec![142].into(), vec![142].into(),
SealedEmptyStep::rlp_of(&[ SealedEmptyStep::rlp_of(&[sealed_empty_step(&validators, &genesis().hash(), 42)]),
sealed_empty_step(&validators, &genesis().hash(), 42),
]),
], ],
gas_limit: kovan_aura_config().min_gas_limit, gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().hash(),
@@ -403,10 +392,7 @@ mod tests {
// when chain score is invalid // when chain score is invalid
let mut header = Header { let mut header = Header {
seal: vec![ seal: vec![vec![43].into(), vec![].into()],
vec![43].into(),
vec![].into(),
],
gas_limit: kovan_aura_config().min_gas_limit, gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().hash(),
..Default::default() ..Default::default()
@@ -421,16 +407,17 @@ mod tests {
#[test] #[test]
fn verifies_validator() { fn verifies_validator() {
let validators = vec![validator(0), validator(1), validator(2)]; let validators = vec![validator(0), validator(1), validator(2)];
let good_header = signed_header(&validators, Header { let good_header = signed_header(
&validators,
Header {
author: validators[1].address().as_fixed_bytes().into(), author: validators[1].address().as_fixed_bytes().into(),
seal: vec![ seal: vec![vec![43].into(), vec![].into()],
vec![43].into(),
vec![].into(),
],
gas_limit: kovan_aura_config().min_gas_limit, gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().hash(),
..Default::default() ..Default::default()
}, 43); },
43,
);
// when header author is invalid // when header author is invalid
let mut header = good_header.clone(); let mut header = good_header.clone();
+28 -27
View File
@@ -37,14 +37,14 @@
mod storage_proof; mod storage_proof;
use crate::storage_proof::{StorageProof, StorageProofChecker}; use crate::storage_proof::{StorageProof, StorageProofChecker};
use codec::{Encode, Decode}; use codec::{Decode, Encode};
use sp_finality_grandpa::{AuthorityId, AuthorityWeight, GRANDPA_AUTHORITIES_KEY};
use sp_runtime::traits::Header;
use frame_support::{ use frame_support::{
dispatch::{DispatchResult, DispatchError},
decl_error, decl_module, decl_storage, decl_error, decl_module, decl_storage,
dispatch::{DispatchError, DispatchResult},
}; };
use frame_system::{self as system, ensure_signed}; use frame_system::{self as system, ensure_signed};
use sp_finality_grandpa::{AuthorityId, AuthorityWeight, GRANDPA_AUTHORITIES_KEY};
use sp_runtime::traits::Header;
use storage_proof::Error as StorageError; use storage_proof::Error as StorageError;
#[derive(Encode, Decode, Clone, PartialEq)] #[derive(Encode, Decode, Clone, PartialEq)]
@@ -62,8 +62,7 @@ impl<T: Trait> BridgeInfo<T> {
block_hash: &T::Hash, block_hash: &T::Hash,
state_root: &T::Hash, state_root: &T::Hash,
validator_set: Vec<(AuthorityId, AuthorityWeight)>, validator_set: Vec<(AuthorityId, AuthorityWeight)>,
) -> Self ) -> Self {
{
// I don't like how this is done, should come back to... // I don't like how this is done, should come back to...
BridgeInfo { BridgeInfo {
last_finalized_block_number: *block_number, last_finalized_block_number: *block_number,
@@ -140,11 +139,8 @@ impl<T: Trait> Module<T> {
proof: StorageProof, proof: StorageProof,
validator_set: &Vec<(AuthorityId, AuthorityWeight)>, validator_set: &Vec<(AuthorityId, AuthorityWeight)>,
) -> DispatchResult { ) -> DispatchResult {
let checker =
let checker = <StorageProofChecker<<T::Hashing as sp_runtime::traits::Hash>::Hasher>>::new( <StorageProofChecker<<T::Hashing as sp_runtime::traits::Hash>::Hasher>>::new(*state_root, proof.clone());
*state_root,
proof.clone()
);
let checker = checker.map_err(Self::map_storage_err)?; let checker = checker.map_err(Self::map_storage_err)?;
@@ -152,7 +148,9 @@ impl<T: Trait> Module<T> {
// with the stuff we get out of storage via `read_value` // with the stuff we get out of storage via `read_value`
let encoded_validator_set = validator_set.encode(); let encoded_validator_set = validator_set.encode();
let c = checker.read_value(GRANDPA_AUTHORITIES_KEY).map_err(Self::map_storage_err)?; let c = checker
.read_value(GRANDPA_AUTHORITIES_KEY)
.map_err(Self::map_storage_err)?;
let actual_validator_set = c.ok_or(Error::<T>::StorageValueUnavailable)?; let actual_validator_set = c.ok_or(Error::<T>::StorageValueUnavailable)?;
if encoded_validator_set == actual_validator_set { if encoded_validator_set == actual_validator_set {
@@ -182,7 +180,7 @@ impl<T: Trait> Module<T> {
parent_hash = header.parent_hash(); parent_hash = header.parent_hash();
if *parent_hash == ancestor_hash { if *parent_hash == ancestor_hash {
return Ok(()) return Ok(());
} }
} }
@@ -193,7 +191,8 @@ impl<T: Trait> Module<T> {
match e { match e {
StorageError::StorageRootMismatch => Error::<T>::StorageRootMismatch, StorageError::StorageRootMismatch => Error::<T>::StorageRootMismatch,
StorageError::StorageValueUnavailable => Error::<T>::StorageValueUnavailable, StorageError::StorageValueUnavailable => Error::<T>::StorageValueUnavailable,
}.into() }
.into()
} }
} }
@@ -201,11 +200,14 @@ impl<T: Trait> Module<T> {
mod tests { mod tests {
use super::*; use super::*;
use sp_core::{Blake2Hasher, H256, Public}; use frame_support::{assert_err, assert_ok, impl_outer_origin, parameter_types};
use sp_core::{Blake2Hasher, Public, H256};
use sp_runtime::{ use sp_runtime::{
Perbill, traits::{Header as HeaderT, IdentityLookup}, testing::Header, generic::Digest, generic::Digest,
testing::Header,
traits::{Header as HeaderT, IdentityLookup},
Perbill,
}; };
use frame_support::{assert_ok, assert_err, impl_outer_origin, parameter_types};
impl_outer_origin! { impl_outer_origin! {
pub enum Origin for Test where system = frame_system {} pub enum Origin for Test where system = frame_system {}
@@ -251,9 +253,7 @@ mod tests {
fn new_test_ext() -> sp_io::TestExternalities { fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap(); let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig { GenesisConfig { num_bridges: 0 }.assimilate_storage(&mut t).unwrap();
num_bridges: 0,
}.assimilate_storage(&mut t).unwrap();
t.into() t.into()
} }
@@ -273,14 +273,15 @@ mod tests {
} }
fn create_dummy_validator_proof(validator_set: Vec<(AuthorityId, AuthorityWeight)>) -> (H256, StorageProof) { fn create_dummy_validator_proof(validator_set: Vec<(AuthorityId, AuthorityWeight)>) -> (H256, StorageProof) {
use sp_state_machine::{prove_read, backend::Backend, InMemoryBackend}; use sp_state_machine::{backend::Backend, prove_read, InMemoryBackend};
let encoded_set = validator_set.encode(); let encoded_set = validator_set.encode();
// construct storage proof // construct storage proof
let backend = <InMemoryBackend<Blake2Hasher>>::from(vec![ let backend = <InMemoryBackend<Blake2Hasher>>::from(vec![(
(None, vec![(GRANDPA_AUTHORITIES_KEY.to_vec(), Some(encoded_set))]), None,
]); vec![(GRANDPA_AUTHORITIES_KEY.to_vec(), Some(encoded_set))],
)]);
let root = backend.storage_root(std::iter::empty()).0; let root = backend.storage_root(std::iter::empty()).0;
// Generates a storage read proof // Generates a storage read proof
@@ -332,8 +333,7 @@ mod tests {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
assert_eq!(MockBridge::num_bridges(), 0); assert_eq!(MockBridge::num_bridges(), 0);
dbg!(&test_header); dbg!(&test_header);
assert_ok!( assert_ok!(MockBridge::initialize_bridge(
MockBridge::initialize_bridge(
Origin::signed(1), Origin::signed(1),
test_header, test_header,
authorities.clone(), authorities.clone(),
@@ -347,7 +347,8 @@ mod tests {
last_finalized_block_hash: test_hash, last_finalized_block_hash: test_hash,
last_finalized_state_root: root, last_finalized_state_root: root,
current_validator_set: authorities.clone(), current_validator_set: authorities.clone(),
})); })
);
assert_eq!(MockBridge::num_bridges(), 1); assert_eq!(MockBridge::num_bridges(), 1);
}); });
+9 -11
View File
@@ -16,9 +16,9 @@
//! Logic for checking Substrate storage proofs. //! Logic for checking Substrate storage proofs.
use hash_db::{Hasher, HashDB, EMPTY_PREFIX}; use hash_db::{HashDB, Hasher, EMPTY_PREFIX};
use sp_trie::{MemoryDB, Trie, trie_types::TrieDB};
use sp_runtime::RuntimeDebug; use sp_runtime::RuntimeDebug;
use sp_trie::{trie_types::TrieDB, MemoryDB, Trie};
pub(crate) type StorageProof = Vec<Vec<u8>>; pub(crate) type StorageProof = Vec<Vec<u8>>;
@@ -26,14 +26,16 @@ pub(crate) type StorageProof = Vec<Vec<u8>>;
/// is a subset of the nodes in the Merkle structure of the database, so that it provides /// is a subset of the nodes in the Merkle structure of the database, so that it provides
/// authentication against a known Merkle root as well as the values in the database themselves. /// authentication against a known Merkle root as well as the values in the database themselves.
pub struct StorageProofChecker<H> pub struct StorageProofChecker<H>
where H: Hasher where
H: Hasher,
{ {
root: H::Out, root: H::Out,
db: MemoryDB<H>, db: MemoryDB<H>,
} }
impl<H> StorageProofChecker<H> impl<H> StorageProofChecker<H>
where H: Hasher where
H: Hasher,
{ {
/// Constructs a new storage proof checker. /// Constructs a new storage proof checker.
/// ///
@@ -43,10 +45,7 @@ impl<H> StorageProofChecker<H>
for item in proof { for item in proof {
db.insert(EMPTY_PREFIX, &item); db.insert(EMPTY_PREFIX, &item);
} }
let checker = StorageProofChecker { let checker = StorageProofChecker { root, db };
root,
db,
};
// Return error if trie would be invalid. // Return error if trie would be invalid.
let _ = checker.trie()?; let _ = checker.trie()?;
Ok(checker) Ok(checker)
@@ -62,8 +61,7 @@ impl<H> StorageProofChecker<H>
} }
fn trie(&self) -> Result<TrieDB<H>, Error> { fn trie(&self) -> Result<TrieDB<H>, Error> {
TrieDB::new(&self.db, &self.root) TrieDB::new(&self.db, &self.root).map_err(|_| Error::StorageRootMismatch)
.map_err(|_| Error::StorageRootMismatch)
} }
} }
@@ -78,7 +76,7 @@ mod tests {
use super::*; use super::*;
use sp_core::{Blake2Hasher, H256}; use sp_core::{Blake2Hasher, H256};
use sp_state_machine::{prove_read, backend::Backend, InMemoryBackend}; use sp_state_machine::{backend::Backend, prove_read, InMemoryBackend};
#[test] #[test]
fn storage_proof_check() { fn storage_proof_check() {
+21 -21
View File
@@ -22,21 +22,21 @@ pub use primitive_types::{H160, H256, H512, U128, U256};
#[cfg(feature = "test-helpers")] #[cfg(feature = "test-helpers")]
pub use rlp::encode as rlp_encode; pub use rlp::encode as rlp_encode;
use sp_std::prelude::*;
use sp_io::hashing::keccak_256;
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use ethbloom::{Bloom as EthBloom, Input as BloomInput}; use ethbloom::{Bloom as EthBloom, Input as BloomInput};
use rlp::{Decodable, DecoderError, Rlp, RlpStream};
use sp_runtime::RuntimeDebug;
use fixed_hash::construct_fixed_hash; use fixed_hash::construct_fixed_hash;
use rlp::{Decodable, DecoderError, Rlp, RlpStream};
use sp_io::hashing::keccak_256;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "std")]
use serde_big_array::big_array;
use impl_rlp::impl_fixed_hash_rlp; use impl_rlp::impl_fixed_hash_rlp;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use impl_serde::impl_fixed_hash_serde; use impl_serde::impl_fixed_hash_serde;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use serde_big_array::big_array;
construct_fixed_hash! { pub struct H520(65); } construct_fixed_hash! { pub struct H520(65); }
impl_fixed_hash_rlp!(H520, 65); impl_fixed_hash_rlp!(H520, 65);
@@ -121,10 +121,7 @@ pub struct LogEntry {
/// Logs bloom. /// Logs bloom.
#[derive(Clone, Encode, Decode)] #[derive(Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Bloom( pub struct Bloom(#[cfg_attr(feature = "std", serde(with = "BigArray"))] [u8; 256]);
#[cfg_attr(feature = "std", serde(with = "BigArray"))]
[u8; 256]
);
#[cfg(feature = "std")] #[cfg(feature = "std")]
big_array! { BigArray; } big_array! { BigArray; }
@@ -172,7 +169,7 @@ impl Header {
let mut message = self.hash().as_bytes().to_vec(); let mut message = self.hash().as_bytes().to_vec();
message.extend_from_slice(self.seal.get(2)?); message.extend_from_slice(self.seal.get(2)?);
keccak_256(&message).into() keccak_256(&message).into()
}, }
false => keccak_256(&self.rlp(false)).into(), false => keccak_256(&self.rlp(false)).into(),
}) })
} }
@@ -189,7 +186,9 @@ impl Header {
/// Extracts the empty steps from the header seal. /// Extracts the empty steps from the header seal.
pub fn empty_steps(&self) -> Option<Vec<SealedEmptyStep>> { pub fn empty_steps(&self) -> Option<Vec<SealedEmptyStep>> {
self.seal.get(2).and_then(|x| Rlp::new(x).as_list::<SealedEmptyStep>().ok()) self.seal
.get(2)
.and_then(|x| Rlp::new(x).as_list::<SealedEmptyStep>().ok())
} }
/// Returns header RLP with or without seals. /// Returns header RLP with or without seals.
@@ -232,15 +231,15 @@ impl Receipt {
match self.outcome { match self.outcome {
TransactionOutcome::Unknown => { TransactionOutcome::Unknown => {
s.begin_list(3); s.begin_list(3);
}, }
TransactionOutcome::StateRoot(ref root) => { TransactionOutcome::StateRoot(ref root) => {
s.begin_list(4); s.begin_list(4);
s.append(root); s.append(root);
}, }
TransactionOutcome::StatusCode(ref status_code) => { TransactionOutcome::StatusCode(ref status_code) => {
s.begin_list(4); s.begin_list(4);
s.append(status_code); s.append(status_code);
}, }
} }
s.append(&self.gas_used); s.append(&self.gas_used);
s.append(&EthBloom::from(self.log_bloom.0)); s.append(&EthBloom::from(self.log_bloom.0));
@@ -275,9 +274,7 @@ impl SealedEmptyStep {
let mut s = RlpStream::new(); let mut s = RlpStream::new();
s.begin_list(empty_steps.len()); s.begin_list(empty_steps.len());
for empty_step in empty_steps { for empty_step in empty_steps {
s.begin_list(2) s.begin_list(2).append(&empty_step.signature).append(&empty_step.step);
.append(&empty_step.signature)
.append(&empty_step.step);
} }
s.out() s.out()
} }
@@ -295,7 +292,10 @@ impl Decodable for SealedEmptyStep {
impl LogEntry { impl LogEntry {
/// Calculates the bloom of this log entry. /// Calculates the bloom of this log entry.
pub fn bloom(&self) -> Bloom { pub fn bloom(&self) -> Bloom {
let eth_bloom = self.topics.iter().fold(EthBloom::from(BloomInput::Raw(self.address.as_bytes())), |mut b, t| { let eth_bloom =
self.topics
.iter()
.fold(EthBloom::from(BloomInput::Raw(self.address.as_bytes())), |mut b, t| {
b.accrue(BloomInput::Raw(t.as_bytes())); b.accrue(BloomInput::Raw(t.as_bytes()));
b b
}); });