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
// 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::{
btree_map::{BTreeMap, Entry},
btree_set::BTreeSet,
vec_deque::VecDeque,
};
use sp_io::crypto::secp256k1_ecdsa_recover;
use primitives::{Address, H256, Header, SealedEmptyStep, public_to_address};
use crate::{Storage, ancestry};
use crate::error::Error;
use sp_std::prelude::*;
/// Tries to finalize blocks when given block is imported.
///
@@ -68,8 +68,8 @@ fn is_finalized(
votes: &BTreeMap<Address, u64>,
requires_two_thirds_majority: bool,
) -> bool {
(!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() * 2 > validators.len())
|| (requires_two_thirds_majority && votes.len() * 3 > validators.len() * 2)
}
/// 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 {
*entry.get_mut() -= 1;
}
},
}
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.
fn empty_steps_signers(header: &Header) -> BTreeSet<Address> {
header.empty_steps()
header
.empty_steps()
.into_iter()
.flat_map(|steps| steps)
.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)]
mod tests {
use crate::HeaderToImport;
use crate::tests::{InMemoryStorage, genesis, validator, validators_addresses};
use super::*;
use crate::tests::{genesis, validator, validators_addresses, InMemoryStorage};
use crate::HeaderToImport;
#[test]
fn verifies_header_author() {
+40 -67
View File
@@ -14,13 +14,13 @@
// You should have received a copy of the GNU General Public License
// 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::finality::finalize_blocks;
use crate::validators::{Validators, ValidatorsConfiguration};
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
/// 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 useless = 0;
for (header, receipts) in headers {
let import_result = import_header(
storage,
aura_config,
validators_config,
prune_depth,
header,
receipts,
);
let import_result = import_header(storage, aura_config, validators_config, prune_depth, header, receipts);
match import_result {
Ok(_) => useful += 1,
@@ -85,16 +78,11 @@ pub fn import_header<S: Storage>(
let (hash, prev_finalized_hash) = is_importable_header(storage, &header)?;
// verify header
let import_context = verify_aura_header(
storage,
aura_config,
&header,
)?;
let import_context = verify_aura_header(storage, aura_config, &header)?;
// check if block schedules new validators
let validators = Validators::new(validators_config);
let (scheduled_change, enacted_change) =
validators.extract_validators_change(&header, receipts)?;
let (scheduled_change, enacted_change) = validators.extract_validators_change(&header, receipts)?;
// check if block finalizes some other blocks and corresponding scheduled validators
let finalized_blocks = finalize_blocks(
@@ -105,8 +93,7 @@ pub fn import_header<S: Storage>(
&header,
aura_config.two_thirds_majority_transition,
)?;
let enacted_change = enacted_change
.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));
let enacted_change = enacted_change.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));
// NOTE: we can't return Err() from anywhere below this line
// (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)]
mod tests {
use crate::{kovan_aura_config, kovan_validators_config};
use super::*;
use crate::tests::{
InMemoryStorage,
block_i, custom_block_i, signed_header, genesis,
validator, validators_addresses,
block_i, custom_block_i, genesis, signed_header, validator, validators_addresses, InMemoryStorage,
};
use crate::validators::ValidatorsSource;
use super::*;
use crate::{kovan_aura_config, kovan_validators_config};
#[test]
fn rejects_finalized_block_competitors() {
@@ -208,7 +193,8 @@ mod tests {
PRUNE_DEPTH,
block.clone(),
None,
).map(|_| ()),
)
.map(|_| ()),
Ok(()),
);
assert_eq!(
@@ -219,7 +205,8 @@ mod tests {
PRUNE_DEPTH,
block,
None,
).map(|_| ()),
)
.map(|_| ()),
Err(Error::KnownHeader),
);
}
@@ -235,8 +222,15 @@ mod tests {
let header = block_i(&storage, 1, &validators);
let hash = header.hash();
assert_eq!(
import_header(&mut storage, &kovan_aura_config(), &validators_config, PRUNE_DEPTH, header, None)
.map(|_| ()),
import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
PRUNE_DEPTH,
header,
None
)
.map(|_| ()),
Ok(()),
);
@@ -250,9 +244,8 @@ mod tests {
#[test]
fn headers_are_pruned() {
let validators_config = ValidatorsConfiguration::Single(
ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)),
);
let validators_config =
ValidatorsConfiguration::Single(ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)));
let validators = vec![validator(0), validator(1), validator(2)];
let mut storage = InMemoryStorage::new(genesis(), validators_addresses(3));
@@ -261,14 +254,8 @@ mod tests {
let mut last_block_hash = Default::default();
for i in 1..11 {
let header = block_i(&storage, i, &validators);
last_block_hash = import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
last_block_hash =
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
}
assert!(storage.header(&genesis().hash()).is_some());
@@ -276,7 +263,9 @@ mod tests {
// => we prune header#0
let header = custom_block_i(&storage, 11, &validators, |header| {
header.log_bloom = (&[0xff; 256]).into();
header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951".parse().unwrap();
header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951"
.parse()
.unwrap();
});
last_block_hash = import_header(
&mut storage,
@@ -284,8 +273,11 @@ mod tests {
&validators_config,
10,
header,
Some(vec![crate::validators::tests::validators_change_recept(last_block_hash)]),
).unwrap();
Some(vec![crate::validators::tests::validators_change_recept(
last_block_hash,
)]),
)
.unwrap();
assert!(storage.header(&genesis().hash()).is_none());
// and now let's say validators 1 && 2 went offline
@@ -298,22 +290,13 @@ mod tests {
parent_hash: last_block_hash,
gas_limit: 0x2000.into(),
author: validator(2).address().to_fixed_bytes().into(),
seal: vec![
vec![step].into(),
vec![].into(),
],
seal: vec![vec![step].into(), vec![].into()],
difficulty: i.into(),
..Default::default()
};
let header = signed_header(&validators, header, step as _);
last_block_hash = import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
last_block_hash =
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
step += 3;
}
assert_eq!(storage.oldest_unpruned_block(), 11);
@@ -326,22 +309,12 @@ mod tests {
parent_hash: last_block_hash,
gas_limit: 0x2000.into(),
author: validator(0).address().to_fixed_bytes().into(),
seal: vec![
vec![step].into(),
vec![].into(),
],
seal: vec![vec![step].into(), vec![].into()],
difficulty: 25.into(),
..Default::default()
};
let header = signed_header(&validators, header, step as _);
import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
assert_eq!(storage.oldest_unpruned_block(), 15);
}
}
+206 -145
View File
@@ -16,14 +16,14 @@
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::{prelude::*, iter::from_fn};
use codec::{Decode, Encode};
use frame_support::{decl_module, decl_storage};
use primitives::{Address, Header, Receipt, H256, U256};
use sp_runtime::RuntimeDebug;
use primitives::{Address, U256, H256, Header, Receipt};
use validators::{ValidatorsSource, ValidatorsConfiguration};
use sp_std::{iter::from_fn, prelude::*};
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 finality;
@@ -180,11 +180,7 @@ pub trait Storage {
/// It is the storage duty to ensure that unfinalized headers that have
/// scheduled changes won't be pruned until they or their competitors
/// are finalized.
fn finalize_headers(
&mut self,
finalized: Option<(u64, H256)>,
prune_end: Option<u64>,
);
fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>);
}
/// 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.
pub fn is_import_requires_receipts(header: Header) -> bool {
import::header_import_requires_receipts(
&BridgeStorage,
&kovan_validators_config(),
&header,
)
import::header_import_requires_receipts(&BridgeStorage, &kovan_validators_config(), &header)
}
/// Returns true if header is known to the runtime.
@@ -334,18 +326,17 @@ impl Storage for BridgeStorage {
}
fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> {
Headers::get(parent_hash)
.map(|parent_header| {
let (next_validators_set_start, next_validators) =
ValidatorsSets::get(parent_header.next_validators_set_id)
Headers::get(parent_hash).map(|parent_header| {
let (next_validators_set_start, next_validators) =
ValidatorsSets::get(parent_header.next_validators_set_id)
.expect("validators set is only pruned when last ref is pruned; there is a ref; qed");
ImportContext {
parent_header: parent_header.header,
parent_total_difficulty: parent_header.total_difficulty,
next_validators_set_id: parent_header.next_validators_set_id,
next_validators_set: (next_validators_set_start, next_validators),
}
})
ImportContext {
parent_header: parent_header.header,
parent_total_difficulty: parent_header.total_difficulty,
next_validators_set_id: parent_header.next_validators_set_id,
next_validators_set: (next_validators_set_start, next_validators),
}
})
}
fn scheduled_change(&self, hash: &H256) -> Option<Vec<Address>> {
@@ -369,34 +360,33 @@ impl Storage for BridgeStorage {
ValidatorsSets::insert(next_validators_set_id, (header.hash, enacted_change));
ValidatorsSetsRc::insert(next_validators_set_id, 1);
next_validators_set_id
},
}
None => {
ValidatorsSetsRc::mutate(
header.context.next_validators_set_id,
|rc| {
*rc = Some(rc.map(|rc| rc + 1).unwrap_or(1));
*rc
},
);
ValidatorsSetsRc::mutate(header.context.next_validators_set_id, |rc| {
*rc = Some(rc.map(|rc| rc + 1).unwrap_or(1));
*rc
});
header.context.next_validators_set_id
},
}
};
HeadersByNumber::append_or_insert(header.header.number, vec![header.hash]);
Headers::insert(&header.hash, StoredHeader {
header: header.header,
total_difficulty: header.total_difficulty,
next_validators_set_id,
});
Headers::insert(
&header.hash,
StoredHeader {
header: header.header,
total_difficulty: header.total_difficulty,
next_validators_set_id,
},
);
}
fn finalize_headers(
&mut self,
finalized: Option<(u64, H256)>,
prune_end: Option<u64>,
) {
fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>) {
// 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 {
FinalizedBlock::put(finalized);
}
@@ -423,13 +413,10 @@ impl Storage for BridgeStorage {
let header = Headers::take(&hash);
ScheduledChanges::remove(hash);
if let Some(header) = header {
ValidatorsSetsRc::mutate(
header.next_validators_set_id,
|rc| match *rc {
Some(rc) if rc > 1 => Some(rc - 1),
_ => None,
},
);
ValidatorsSetsRc::mutate(header.next_validators_set_id, |rc| match *rc {
Some(rc) if rc > 1 => Some(rc - 1),
_ => None,
});
}
}
}
@@ -456,50 +443,113 @@ pub fn kovan_aura_config() -> AuraConfiguration {
/// Validators configuration for Kovan chain.
pub fn kovan_validators_config() -> ValidatorsConfiguration {
ValidatorsConfiguration::Multi(vec![
(0, 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, 0x42, 0x7f, 0xea, 0xe2, 0x41, 0x9c, 0x15, 0xb8, 0x9d,
0x1c, 0x21, 0xaf, 0x10, 0xd1, 0xb6, 0x65, 0x0a, 0x4d, 0x3d].into(),
[0x4E, 0xd9, 0xB0, 0x8e, 0x63, 0x54, 0xC7, 0x0f, 0xE6, 0xF8,
0xCB, 0x04, 0x11, 0xb0, 0xd3, 0x24, 0x6b, 0x42, 0x4d, 0x6c].into(),
[0x00, 0x20, 0xee, 0x4B, 0xe0, 0xe2, 0x02, 0x7d, 0x76, 0x60,
0x3c, 0xB7, 0x51, 0xeE, 0x06, 0x95, 0x19, 0xbA, 0x81, 0xA1].into(),
[0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5,
0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0, 0x3a, 0xfd, 0x03, 0x2d].into(),
[0x00, 0x77, 0x33, 0xa1, 0xFE, 0x69, 0xCF, 0x3f, 0x2C, 0xF9,
0x89, 0xF8, 0x1C, 0x7b, 0x4c, 0xAc, 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![
[0xd0, 0x5f, 0x74, 0x78, 0xc6, 0xaa, 0x10, 0x78, 0x12, 0x58,
0xc5, 0xcc, 0x8b, 0x4f, 0x38, 0x5f, 0xc8, 0xfa, 0x98, 0x9c].into(),
[0x03, 0x80, 0x1e, 0xfb, 0x0e, 0xfe, 0x2a, 0x25, 0xed, 0xe5,
0xdd, 0x3a, 0x00, 0x3a, 0xe8, 0x80, 0xc0, 0x29, 0x2e, 0x4d].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(),
],
)),
(
0,
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, 0x42, 0x7f, 0xea, 0xe2, 0x41, 0x9c, 0x15, 0xb8, 0x9d, 0x1c, 0x21, 0xaf, 0x10, 0xd1, 0xb6,
0x65, 0x0a, 0x4d, 0x3d,
]
.into(),
[
0x4E, 0xd9, 0xB0, 0x8e, 0x63, 0x54, 0xC7, 0x0f, 0xE6, 0xF8, 0xCB, 0x04, 0x11, 0xb0, 0xd3, 0x24,
0x6b, 0x42, 0x4d, 0x6c,
]
.into(),
[
0x00, 0x20, 0xee, 0x4B, 0xe0, 0xe2, 0x02, 0x7d, 0x76, 0x60, 0x3c, 0xB7, 0x51, 0xeE, 0x06, 0x95,
0x19, 0xbA, 0x81, 0xA1,
]
.into(),
[
0x00, 0x10, 0xf9, 0x4b, 0x29, 0x6a, 0x85, 0x2a, 0xaa, 0xc5, 0x2e, 0xa6, 0xc5, 0xac, 0x72, 0xe0,
0x3a, 0xfd, 0x03, 0x2d,
]
.into(),
[
0x00, 0x77, 0x33, 0xa1, 0xFE, 0x69, 0xCF, 0x3f, 0x2C, 0xF9, 0x89, 0xF8, 0x1C, 0x7b, 0x4c, 0xAc,
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![
[
0xd0, 0x5f, 0x74, 0x78, 0xc6, 0xaa, 0x10, 0x78, 0x12, 0x58, 0xc5, 0xcc, 0x8b, 0x4f, 0x38, 0x5f,
0xc8, 0xfa, 0x98, 0x9c,
]
.into(),
[
0x03, 0x80, 0x1e, 0xfb, 0x0e, 0xfe, 0x2a, 0x25, 0xed, 0xe5, 0xdd, 0x3a, 0x00, 0x3a, 0xe8, 0x80,
0xc0, 0x29, 0x2e, 0x4d,
]
.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();
parent_hash = header.parent_hash.clone();
Some((hash, header))
},
None => None
}
None => None,
}
})
}
#[cfg(test)]
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 parity_crypto::publickey::{sign, KeyPair, Secret};
use primitives::{rlp_encode, H520};
use std::collections::{hash_map::Entry, HashMap};
pub fn genesis() -> Header {
Header {
seal: vec![
vec![42].into(),
vec![].into(),
],
seal: vec![vec![42].into(), vec![].into()],
..Default::default()
}
}
@@ -556,10 +603,7 @@ pub(crate) mod tests {
parent_hash: storage.headers_by_number[&(number - 1)][0].clone(),
gas_limit: 0x2000.into(),
author: validator(validator_index).address().to_fixed_bytes().into(),
seal: vec![
vec![number as u8 + 42].into(),
vec![].into(),
],
seal: vec![vec![number as u8 + 42].into(), vec![].into()],
difficulty: number.into(),
..Default::default()
};
@@ -582,7 +626,9 @@ pub(crate) mod tests {
}
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 {
@@ -612,7 +658,9 @@ pub(crate) mod tests {
total_difficulty: 0.into(),
next_validators_set_id: 0,
},
)].into_iter().collect(),
)]
.into_iter()
.collect(),
next_validators_set_id: 1,
validators_sets: vec![(0, (hash, initial_validators))].into_iter().collect(),
validators_sets_rc: vec![(0, 1)].into_iter().collect(),
@@ -643,17 +691,16 @@ pub(crate) mod tests {
}
fn import_context(&self, parent_hash: &H256) -> Option<ImportContext> {
self.headers.get(parent_hash)
.map(|parent_header| {
let (next_validators_set_start, next_validators) =
self.validators_sets.get(&parent_header.next_validators_set_id).unwrap();
ImportContext {
parent_header: parent_header.header.clone(),
parent_total_difficulty: parent_header.total_difficulty,
next_validators_set_id: parent_header.next_validators_set_id,
next_validators_set: (*next_validators_set_start, next_validators.clone()),
}
})
self.headers.get(parent_hash).map(|parent_header| {
let (next_validators_set_start, next_validators) =
self.validators_sets.get(&parent_header.next_validators_set_id).unwrap();
ImportContext {
parent_header: parent_header.header.clone(),
parent_total_difficulty: parent_header.total_difficulty,
next_validators_set_id: parent_header.next_validators_set_id,
next_validators_set: (*next_validators_set_start, next_validators.clone()),
}
})
}
fn scheduled_change(&self, hash: &H256) -> Option<Vec<Address>> {
@@ -671,30 +718,39 @@ pub(crate) mod tests {
Some(enacted_change) => {
let next_validators_set_id = self.next_validators_set_id;
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);
next_validators_set_id
},
}
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
},
}
};
self.headers_by_number.entry(header.header.number).or_default().push(header.hash);
self.headers.insert(header.hash, StoredHeader {
header: header.header,
total_difficulty: header.total_difficulty,
next_validators_set_id,
});
self.headers_by_number
.entry(header.header.number)
.or_default()
.push(header.hash);
self.headers.insert(
header.hash,
StoredHeader {
header: header.header,
total_difficulty: header.total_difficulty,
next_validators_set_id,
},
);
}
fn finalize_headers(
&mut self,
finalized: Option<(u64, H256)>,
prune_end: Option<u64>,
) {
let finalized_number = finalized.as_ref().map(|f| f.0).unwrap_or_else(|| self.finalized_block.0);
fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>) {
let finalized_number = finalized
.as_ref()
.map(|f| f.0)
.unwrap_or_else(|| self.finalized_block.0);
if let Some(finalized) = 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
if number > finalized_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.oldest_unpruned_block = number;
return;
@@ -722,12 +781,14 @@ pub(crate) mod tests {
self.scheduled_changes.remove(&hash);
if let Some(header) = header {
match self.validators_sets_rc.entry(header.next_validators_set_id) {
Entry::Occupied(mut entry) => if *entry.get() == 1 {
entry.remove();
} else {
*entry.get_mut() -= 1;
},
Entry::Vacant(_) => unreachable!("there's entry for each header")
Entry::Occupied(mut entry) => {
if *entry.get() == 1 {
entry.remove();
} else {
*entry.get_mut() -= 1;
}
}
Entry::Vacant(_) => unreachable!("there's entry for each header"),
};
}
}
+77 -77
View File
@@ -14,15 +14,16 @@
// You should have received a copy of the GNU General Public License
// 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::Storage;
use primitives::{Address, Header, LogEntry, Receipt, H256, U256};
use sp_std::prelude::*;
/// 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,
0x4e, 0x24, 0xa7, 0x4a, 0x70, 0xe9, 0xc1, 0x1f, 0xd2, 0xc2, 0x28, 0x1d, 0xf8, 0xd6, 0xea,
0x13, 0x12, 0x6f, 0xf8, 0x45, 0xf7, 0x82, 0x5c, 0x89];
const CHANGE_EVENT_HASH: &'static [u8; 32] = &[
0x55, 0x25, 0x2f, 0xa6, 0xee, 0xe4, 0x74, 0x1b, 0x4e, 0x24, 0xa7, 0x4a, 0x70, 0xe9, 0xc1, 0x1f, 0xd2, 0xc2, 0x28,
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.
pub enum ValidatorsConfiguration {
@@ -76,12 +77,10 @@ impl<'a> Validators<'a> {
// that the contract has (probably) emitted epoch change event
let expected_bloom = LogEntry {
address: *contract_address,
topics: vec![
CHANGE_EVENT_HASH.into(),
header.parent_hash,
],
topics: vec![CHANGE_EVENT_HASH.into(), header.parent_hash],
data: Vec::new(), // irrelevant for bloom.
}.bloom();
}
.bloom();
header.log_bloom.contains(&expected_bloom)
}
@@ -120,12 +119,10 @@ impl<'a> Validators<'a> {
// that the contract has (probably) emitted epoch change event
let expected_bloom = LogEntry {
address: *contract_address,
topics: vec![
CHANGE_EVENT_HASH.into(),
header.parent_hash,
],
topics: vec![CHANGE_EVENT_HASH.into(), header.parent_hash],
data: Vec::new(), // irrelevant for bloom.
}.bloom();
}
.bloom();
if !header.log_bloom.contains(&expected_bloom) {
return Ok((None, None));
@@ -138,41 +135,47 @@ impl<'a> Validators<'a> {
// iterate in reverse because only the _last_ change in a given
// block actually has any effect
Ok((receipts.iter()
.rev()
.filter(|r| r.log_bloom.contains(&expected_bloom))
.flat_map(|r| r.logs.iter())
.filter(|l| l.address == *contract_address &&
l.topics.len() == 2 &&
l.topics[0].as_fixed_bytes() == CHANGE_EVENT_HASH &&
l.topics[1] == header.parent_hash
)
.filter_map(|l| {
let data_len = l.data.len();
if data_len < 64 {
return None;
}
Ok((
receipts
.iter()
.rev()
.filter(|r| r.log_bloom.contains(&expected_bloom))
.flat_map(|r| r.logs.iter())
.filter(|l| {
l.address == *contract_address
&& l.topics.len() == 2 && l.topics[0].as_fixed_bytes() == CHANGE_EVENT_HASH
&& l.topics[1] == header.parent_hash
})
.filter_map(|l| {
let data_len = l.data.len();
if data_len < 64 {
return None;
}
let new_validators_len_u256 = U256::from_big_endian(&l.data[32..64]);
let new_validators_len = new_validators_len_u256.low_u64();
if new_validators_len_u256 != new_validators_len.into() {
return None;
}
let new_validators_len_u256 = U256::from_big_endian(&l.data[32..64]);
let new_validators_len = new_validators_len_u256.low_u64();
if new_validators_len_u256 != new_validators_len.into() {
return None;
}
if (data_len - 64) as u64 != new_validators_len.saturating_mul(32) {
return None;
}
if (data_len - 64) as u64 != new_validators_len.saturating_mul(32) {
return None;
}
Some(l.data[64..]
.chunks(32)
.map(|chunk| {
let mut new_validator = Address::default();
new_validator.as_mut().copy_from_slice(&chunk[12..32]);
new_validator
})
.collect())
})
.next(), None))
Some(
l.data[64..]
.chunks(32)
.map(|chunk| {
let mut new_validator = Address::default();
new_validator.as_mut().copy_from_slice(&chunk[12..32]);
new_validator
})
.collect(),
)
})
.next(),
None,
))
}
/// 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) {
match self.config {
ValidatorsConfiguration::Single(ref source) => (0, 0, source),
ValidatorsConfiguration::Multi(ref sources) => sources.iter().rev()
ValidatorsConfiguration::Multi(ref sources) => sources
.iter()
.rev()
.enumerate()
.find(|(_, &(begin, _))| begin < header_number)
.map(|(i, (begin, source))| (sources.len() - 1 - i, *begin, source))
.expect("there's always entry for the initial block;\
we do not touch any headers with number < initial block number; qed"),
.expect(
"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];
(source.0, &source.1)
},
}
}
}
}
@@ -243,32 +250,24 @@ pub fn step_validator(header_validators: &[Address], header_step: u64) -> Addres
#[cfg(test)]
pub(crate) mod tests {
use primitives::TransactionOutcome;
use crate::kovan_validators_config;
use super::*;
use crate::kovan_validators_config;
use primitives::TransactionOutcome;
pub(crate) fn validators_change_recept(parent_hash: H256) -> Receipt {
Receipt {
gas_used: 0.into(),
log_bloom: (&[0xff; 256]).into(),
outcome: TransactionOutcome::Unknown,
logs: vec![
LogEntry {
address: [3; 20].into(),
topics: vec![
CHANGE_EVENT_HASH.into(),
parent_hash,
],
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, 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,
],
},
],
logs: vec![LogEntry {
address: [3; 20].into(),
topics: vec![CHANGE_EVENT_HASH.into(), parent_hash],
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, 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
header.number = 150;
assert_eq!(
validators.extract_validators_change(&header, None),
Ok((None, None)),
);
assert_eq!(validators.extract_validators_change(&header, None), Ok((None, None)),);
// when we're at the block that switches to contract source
header.number = 200;
@@ -370,7 +366,9 @@ pub(crate) mod tests {
// when we're inside contract range and logs bloom signals change
// but there's no change in receipts
header.receipts_root = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421".parse().unwrap();
header.receipts_root = "56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
.parse()
.unwrap();
assert_eq!(
validators.extract_validators_change(&header, Some(Vec::new())),
Ok((None, None)),
@@ -379,7 +377,9 @@ pub(crate) mod tests {
// when we're inside contract range and logs bloom signals change
// and there's change in receipts
let receipts = vec![validators_change_recept(Default::default())];
header.receipts_root = "81ce88dc524403b796222046bf3daf543978329b87ffd50228f1d3987031dc45".parse().unwrap();
header.receipts_root = "81ce88dc524403b796222046bf3daf543978329b87ffd50228f1d3987031dc45"
.parse()
.unwrap();
assert_eq!(
validators.extract_validators_change(&header, Some(receipts)),
Ok((Some(vec![[7; 20].into()]), None)),
+36 -49
View File
@@ -14,11 +14,11 @@
// You should have received a copy of the GNU General Public License
// 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::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.
pub fn verify_aura_header<S: Storage>(
@@ -30,14 +30,15 @@ pub fn verify_aura_header<S: Storage>(
contextless_checks(params, header)?;
// 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 header_step = 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.
if header_step == parent_step
|| (header.number >= params.validate_step_transition && header_step <= parent_step) {
if header_step == parent_step || (header.number >= params.validate_step_transition && header_step <= parent_step) {
return Err(Error::DoubleVote);
}
@@ -69,7 +70,7 @@ pub fn verify_aura_header<S: Storage>(
}
empty_steps_len
},
}
false => 0,
};
@@ -160,20 +161,22 @@ fn verify_signature(expected_validator: &Address, signature: &H520, message: &H2
#[cfg(test)]
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 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 {
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 validator_index = (step % validators.len() as u64) as usize;
let signature: [u8; 65] = sign(
validators[validator_index].secret(),
&message.as_fixed_bytes().into(),
).unwrap().into();
let signature: [u8; 65] = sign(validators[validator_index].secret(), &message.as_fixed_bytes().into())
.unwrap()
.into();
empty_step.signature = signature.into();
empty_step
}
@@ -321,17 +324,11 @@ mod tests {
assert_eq!(default_verify(&header), Err(Error::MissingStep));
// when step is the same as for the parent block
header.seal = vec![
vec![42].into(),
vec![].into(),
];
header.seal = vec![vec![42].into(), vec![].into()];
assert_eq!(default_verify(&header), Err(Error::DoubleVote));
// when step is OK
header.seal = vec![
vec![43].into(),
vec![].into(),
];
header.seal = vec![vec![43].into(), vec![].into()];
assert_ne!(default_verify(&header), Err(Error::DoubleVote));
// now check with validate_step check enabled
@@ -339,17 +336,11 @@ mod tests {
config.validate_step_transition = 0;
// when step is lesser that for the parent block
header.seal = vec![
vec![40].into(),
vec![].into(),
];
header.seal = vec![vec![40].into(), vec![].into()];
assert_eq!(verify_with_config(&config, &header), Err(Error::DoubleVote));
// when step is OK
header.seal = vec![
vec![44].into(),
vec![].into(),
];
header.seal = vec![vec![44].into(), vec![].into()];
assert_ne!(verify_with_config(&config, &header), Err(Error::DoubleVote));
}
@@ -364,9 +355,7 @@ mod tests {
seal: vec![
vec![45].into(),
vec![142].into(),
SealedEmptyStep::rlp_of(&[
sealed_empty_step(&validators, &genesis().hash(), 42),
]),
SealedEmptyStep::rlp_of(&[sealed_empty_step(&validators, &genesis().hash(), 42)]),
],
gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(),
@@ -403,10 +392,7 @@ mod tests {
// when chain score is invalid
let mut header = Header {
seal: vec![
vec![43].into(),
vec![].into(),
],
seal: vec![vec![43].into(), vec![].into()],
gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(),
..Default::default()
@@ -421,16 +407,17 @@ mod tests {
#[test]
fn verifies_validator() {
let validators = vec![validator(0), validator(1), validator(2)];
let good_header = signed_header(&validators, Header {
author: validators[1].address().as_fixed_bytes().into(),
seal: vec![
vec![43].into(),
vec![].into(),
],
gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(),
..Default::default()
}, 43);
let good_header = signed_header(
&validators,
Header {
author: validators[1].address().as_fixed_bytes().into(),
seal: vec![vec![43].into(), vec![].into()],
gas_limit: kovan_aura_config().min_gas_limit,
parent_hash: genesis().hash(),
..Default::default()
},
43,
);
// when header author is invalid
let mut header = good_header.clone();
+36 -35
View File
@@ -37,14 +37,14 @@
mod storage_proof;
use crate::storage_proof::{StorageProof, StorageProofChecker};
use codec::{Encode, Decode};
use sp_finality_grandpa::{AuthorityId, AuthorityWeight, GRANDPA_AUTHORITIES_KEY};
use sp_runtime::traits::Header;
use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchResult, DispatchError},
decl_error, decl_module, decl_storage,
dispatch::{DispatchError, DispatchResult},
};
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;
#[derive(Encode, Decode, Clone, PartialEq)]
@@ -58,12 +58,11 @@ pub struct BridgeInfo<T: Trait> {
impl<T: Trait> BridgeInfo<T> {
pub fn new(
block_number: &T::BlockNumber,
block_hash: &T::Hash,
state_root: &T::Hash,
validator_set: Vec<(AuthorityId, AuthorityWeight)>,
) -> Self
{
block_number: &T::BlockNumber,
block_hash: &T::Hash,
state_root: &T::Hash,
validator_set: Vec<(AuthorityId, AuthorityWeight)>,
) -> Self {
// I don't like how this is done, should come back to...
BridgeInfo {
last_finalized_block_number: *block_number,
@@ -140,11 +139,8 @@ impl<T: Trait> Module<T> {
proof: StorageProof,
validator_set: &Vec<(AuthorityId, AuthorityWeight)>,
) -> DispatchResult {
let checker = <StorageProofChecker<<T::Hashing as sp_runtime::traits::Hash>::Hasher>>::new(
*state_root,
proof.clone()
);
let checker =
<StorageProofChecker<<T::Hashing as sp_runtime::traits::Hash>::Hasher>>::new(*state_root, proof.clone());
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`
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)?;
if encoded_validator_set == actual_validator_set {
@@ -182,7 +180,7 @@ impl<T: Trait> Module<T> {
parent_hash = header.parent_hash();
if *parent_hash == ancestor_hash {
return Ok(())
return Ok(());
}
}
@@ -193,7 +191,8 @@ impl<T: Trait> Module<T> {
match e {
StorageError::StorageRootMismatch => Error::<T>::StorageRootMismatch,
StorageError::StorageValueUnavailable => Error::<T>::StorageValueUnavailable,
}.into()
}
.into()
}
}
@@ -201,11 +200,14 @@ impl<T: Trait> Module<T> {
mod tests {
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::{
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! {
pub enum Origin for Test where system = frame_system {}
@@ -251,9 +253,7 @@ mod tests {
fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap();
GenesisConfig {
num_bridges: 0,
}.assimilate_storage(&mut t).unwrap();
GenesisConfig { num_bridges: 0 }.assimilate_storage(&mut t).unwrap();
t.into()
}
@@ -273,14 +273,15 @@ mod tests {
}
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();
// construct storage proof
let backend = <InMemoryBackend<Blake2Hasher>>::from(vec![
(None, vec![(GRANDPA_AUTHORITIES_KEY.to_vec(), Some(encoded_set))]),
]);
let backend = <InMemoryBackend<Blake2Hasher>>::from(vec![(
None,
vec![(GRANDPA_AUTHORITIES_KEY.to_vec(), Some(encoded_set))],
)]);
let root = backend.storage_root(std::iter::empty()).0;
// Generates a storage read proof
@@ -332,12 +333,11 @@ mod tests {
new_test_ext().execute_with(|| {
assert_eq!(MockBridge::num_bridges(), 0);
dbg!(&test_header);
assert_ok!(
MockBridge::initialize_bridge(
Origin::signed(1),
test_header,
authorities.clone(),
proof,
assert_ok!(MockBridge::initialize_bridge(
Origin::signed(1),
test_header,
authorities.clone(),
proof,
));
assert_eq!(
@@ -347,7 +347,8 @@ mod tests {
last_finalized_block_hash: test_hash,
last_finalized_state_root: root,
current_validator_set: authorities.clone(),
}));
})
);
assert_eq!(MockBridge::num_bridges(), 1);
});
+9 -11
View File
@@ -16,9 +16,9 @@
//! Logic for checking Substrate storage proofs.
use hash_db::{Hasher, HashDB, EMPTY_PREFIX};
use sp_trie::{MemoryDB, Trie, trie_types::TrieDB};
use hash_db::{HashDB, Hasher, EMPTY_PREFIX};
use sp_runtime::RuntimeDebug;
use sp_trie::{trie_types::TrieDB, MemoryDB, Trie};
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
/// authentication against a known Merkle root as well as the values in the database themselves.
pub struct StorageProofChecker<H>
where H: Hasher
where
H: Hasher,
{
root: H::Out,
db: MemoryDB<H>,
}
impl<H> StorageProofChecker<H>
where H: Hasher
where
H: Hasher,
{
/// Constructs a new storage proof checker.
///
@@ -43,10 +45,7 @@ impl<H> StorageProofChecker<H>
for item in proof {
db.insert(EMPTY_PREFIX, &item);
}
let checker = StorageProofChecker {
root,
db,
};
let checker = StorageProofChecker { root, db };
// Return error if trie would be invalid.
let _ = checker.trie()?;
Ok(checker)
@@ -62,8 +61,7 @@ impl<H> StorageProofChecker<H>
}
fn trie(&self) -> Result<TrieDB<H>, Error> {
TrieDB::new(&self.db, &self.root)
.map_err(|_| Error::StorageRootMismatch)
TrieDB::new(&self.db, &self.root).map_err(|_| Error::StorageRootMismatch)
}
}
@@ -78,7 +76,7 @@ mod tests {
use super::*;
use sp_core::{Blake2Hasher, H256};
use sp_state_machine::{prove_read, backend::Backend, InMemoryBackend};
use sp_state_machine::{backend::Backend, prove_read, InMemoryBackend};
#[test]
fn storage_proof_check() {
+24 -24
View File
@@ -22,21 +22,21 @@ pub use primitive_types::{H160, H256, H512, U128, U256};
#[cfg(feature = "test-helpers")]
pub use rlp::encode as rlp_encode;
use sp_std::prelude::*;
use sp_io::hashing::keccak_256;
use codec::{Decode, Encode};
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 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;
#[cfg(feature = "std")]
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); }
impl_fixed_hash_rlp!(H520, 65);
@@ -121,10 +121,7 @@ pub struct LogEntry {
/// Logs bloom.
#[derive(Clone, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Bloom(
#[cfg_attr(feature = "std", serde(with = "BigArray"))]
[u8; 256]
);
pub struct Bloom(#[cfg_attr(feature = "std", serde(with = "BigArray"))] [u8; 256]);
#[cfg(feature = "std")]
big_array! { BigArray; }
@@ -172,7 +169,7 @@ impl Header {
let mut message = self.hash().as_bytes().to_vec();
message.extend_from_slice(self.seal.get(2)?);
keccak_256(&message).into()
},
}
false => keccak_256(&self.rlp(false)).into(),
})
}
@@ -189,7 +186,9 @@ impl Header {
/// Extracts the empty steps from the header seal.
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.
@@ -232,15 +231,15 @@ impl Receipt {
match self.outcome {
TransactionOutcome::Unknown => {
s.begin_list(3);
},
}
TransactionOutcome::StateRoot(ref root) => {
s.begin_list(4);
s.append(root);
},
}
TransactionOutcome::StatusCode(ref status_code) => {
s.begin_list(4);
s.append(status_code);
},
}
}
s.append(&self.gas_used);
s.append(&EthBloom::from(self.log_bloom.0));
@@ -275,9 +274,7 @@ impl SealedEmptyStep {
let mut s = RlpStream::new();
s.begin_list(empty_steps.len());
for empty_step in empty_steps {
s.begin_list(2)
.append(&empty_step.signature)
.append(&empty_step.step);
s.begin_list(2).append(&empty_step.signature).append(&empty_step.step);
}
s.out()
}
@@ -295,10 +292,13 @@ impl Decodable for SealedEmptyStep {
impl LogEntry {
/// Calculates the bloom of this log entry.
pub fn bloom(&self) -> Bloom {
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
});
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
});
Bloom(*eth_bloom.data())
}
}