Finality votes cache (#116)

* removeInMemoryStorage + extract Kovan stuff to runtime

* removed comment from the future

* remove redundant conversions

* remove redundant `u8 as usize`

* remove redundant `u8 as usize`

* Update modules/ethereum/src/mock.rs

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>

* use hex-literal in kovan config

* cargo fmt --all

* extracted insert_header

* cargo fmt --all

* finality cache

* cargo fmt --all

* cargo fmt --all

* impl Default for FinalityVotes

Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
Co-authored-by: Hernando Castano <castano.ha@gmail.com>
This commit is contained in:
Svyatoslav Nikolsky
2020-06-10 10:56:47 +03:00
committed by Bastian Köcher
parent ca8b370de2
commit 643075f7fa
8 changed files with 818 additions and 276 deletions
+4 -1
View File
@@ -216,12 +216,14 @@ impl pallet_aura::Trait for Runtime {
} }
parameter_types! { parameter_types! {
pub const FinalityVotesCachingInterval: Option<u64> = Some(16);
pub const KovanAuraConfiguration: pallet_bridge_eth_poa::AuraConfiguration = kovan::kovan_aura_configuration(); pub const KovanAuraConfiguration: pallet_bridge_eth_poa::AuraConfiguration = kovan::kovan_aura_configuration();
pub const KovanValidatorsConfiguration: pallet_bridge_eth_poa::ValidatorsConfiguration = kovan::kovan_validators_configuration(); pub const KovanValidatorsConfiguration: pallet_bridge_eth_poa::ValidatorsConfiguration = kovan::kovan_validators_configuration();
} }
impl pallet_bridge_eth_poa::Trait for Runtime { impl pallet_bridge_eth_poa::Trait for Runtime {
type AuraConfiguration = KovanAuraConfiguration; type AuraConfiguration = KovanAuraConfiguration;
type FinalityVotesCachingInterval = FinalityVotesCachingInterval;
type ValidatorsConfiguration = KovanValidatorsConfiguration; type ValidatorsConfiguration = KovanValidatorsConfiguration;
type OnHeadersSubmitted = (); type OnHeadersSubmitted = ();
} }
@@ -495,7 +497,8 @@ impl_runtime_apis! {
impl sp_bridge_eth_poa::EthereumHeadersApi<Block> for Runtime { impl sp_bridge_eth_poa::EthereumHeadersApi<Block> for Runtime {
fn best_block() -> (u64, sp_bridge_eth_poa::H256) { fn best_block() -> (u64, sp_bridge_eth_poa::H256) {
BridgeEthPoA::best_block() let best_block = BridgeEthPoA::best_block();
(best_block.number, best_block.hash)
} }
fn is_import_requires_receipts(header: sp_bridge_eth_poa::Header) -> bool { fn is_import_requires_receipts(header: sp_bridge_eth_poa::Header) -> bool {
+329 -114
View File
@@ -16,55 +16,108 @@
use crate::error::Error; use crate::error::Error;
use crate::Storage; use crate::Storage;
use primitives::{public_to_address, Address, Header, SealedEmptyStep, H256}; use codec::{Decode, Encode};
use primitives::{public_to_address, Address, Header, HeaderId, SealedEmptyStep, H256};
use sp_io::crypto::secp256k1_ecdsa_recover; use sp_io::crypto::secp256k1_ecdsa_recover;
use sp_std::prelude::*; use sp_runtime::RuntimeDebug;
use sp_std::{ use sp_std::collections::{
collections::{ btree_map::{BTreeMap, Entry},
btree_map::{BTreeMap, Entry}, btree_set::BTreeSet,
btree_set::BTreeSet, vec_deque::VecDeque,
vec_deque::VecDeque,
},
iter::from_fn,
}; };
use sp_std::prelude::*;
/// Cached finality votes for given block.
#[derive(RuntimeDebug, Default)]
#[cfg_attr(test, derive(PartialEq))]
pub struct CachedFinalityVotes<Submitter> {
/// Header ancestors that were read while we have been searching for
/// cached votes entry. Newest header has index 0.
pub unaccounted_ancestry: VecDeque<(HeaderId, Option<Submitter>, Header)>,
/// Cached finality votes, if they have been found. The associated
/// header is not included into `unaccounted_ancestry`.
pub votes: Option<FinalityVotes<Submitter>>,
}
/// Finality effects.
#[derive(RuntimeDebug)]
#[cfg_attr(test, derive(PartialEq))]
pub struct FinalityEffects<Submitter> {
/// Finalized headers.
pub finalized_headers: Vec<(HeaderId, Option<Submitter>)>,
/// Finality votes used in computation.
pub votes: FinalityVotes<Submitter>,
}
/// Finality votes for given block.
#[derive(RuntimeDebug, Decode, Encode)]
#[cfg_attr(test, derive(Clone, PartialEq))]
pub struct FinalityVotes<Submitter> {
/// Number of votes per each validator.
pub votes: BTreeMap<Address, u64>,
/// Ancestry blocks with oldest ancestors at the beginning and newest at the
/// end of the queue.
pub ancestry: VecDeque<FinalityAncestor<Submitter>>,
}
/// Information about block ancestor that is used in computations.
#[derive(RuntimeDebug, Decode, Encode)]
#[cfg_attr(test, derive(Clone, Default, PartialEq))]
pub struct FinalityAncestor<Submitter> {
/// Bock id.
pub id: HeaderId,
/// Block submitter.
pub submitter: Option<Submitter>,
/// Validators that have signed this block and empty steps on top
/// of this block.
pub signers: BTreeSet<Address>,
}
/// Tries to finalize blocks when given block is imported. /// Tries to finalize blocks when given block is imported.
/// ///
/// Returns numbers and hashes of finalized blocks in ascending order. /// Returns numbers and hashes of finalized blocks in ascending order.
pub fn finalize_blocks<S: Storage>( pub fn finalize_blocks<S: Storage>(
storage: &S, storage: &S,
best_finalized_hash: &H256, best_finalized: HeaderId,
header_validators: (&H256, &[Address]), header_validators: (HeaderId, &[Address]),
hash: &H256, id: HeaderId,
submitter: Option<&S::Submitter>, submitter: Option<&S::Submitter>,
header: &Header, header: &Header,
two_thirds_majority_transition: u64, two_thirds_majority_transition: u64,
) -> Result<Vec<(u64, H256, Option<S::Submitter>)>, Error> { ) -> Result<FinalityEffects<S::Submitter>, Error> {
// compute count of voters for every unfinalized block in ancestry // compute count of voters for every unfinalized block in ancestry
let validators = header_validators.1.iter().collect(); let validators = header_validators.1.iter().collect();
let (mut votes, mut headers) = prepare_votes( let votes = prepare_votes(
storage, storage.cached_finality_votes(&header.parent_hash, |hash| {
best_finalized_hash, *hash == header_validators.0.hash || *hash == best_finalized.hash
&header_validators.0, }),
best_finalized.number,
&validators, &validators,
hash, id,
header, header,
submitter, submitter.cloned(),
two_thirds_majority_transition,
)?; )?;
// now let's iterate in reverse order && find just finalized blocks // now let's iterate in reverse order && find just finalized blocks
let mut newly_finalized = Vec::new(); let mut finalized_headers = Vec::new();
while let Some((oldest_hash, oldest_number, submitter, signers)) = headers.pop_front() { let mut current_votes = votes.votes.clone();
if !is_finalized(&validators, &votes, oldest_number >= two_thirds_majority_transition) { for ancestor in &votes.ancestry {
if !is_finalized(
&validators,
&current_votes,
ancestor.id.number >= two_thirds_majority_transition,
) {
break; break;
} }
remove_signers_votes(&signers, &mut votes); remove_signers_votes(&ancestor.signers, &mut current_votes);
newly_finalized.push((oldest_number, oldest_hash, submitter)); finalized_headers.push((ancestor.id, ancestor.submitter.clone()));
} }
Ok(newly_finalized) Ok(FinalityEffects {
finalized_headers,
votes,
})
} }
/// Returns true if there are enough votes to treat this header as finalized. /// Returns true if there are enough votes to treat this header as finalized.
@@ -78,67 +131,66 @@ fn is_finalized(
} }
/// Prepare 'votes' of header and its ancestors' signers. /// Prepare 'votes' of header and its ancestors' signers.
fn prepare_votes<S: Storage>( fn prepare_votes<Submitter>(
storage: &S, mut cached_votes: CachedFinalityVotes<Submitter>,
best_finalized_hash: &H256, best_finalized_number: u64,
validators_begin: &H256,
validators: &BTreeSet<&Address>, validators: &BTreeSet<&Address>,
hash: &H256, id: HeaderId,
header: &Header, header: &Header,
submitter: Option<&S::Submitter>, submitter: Option<Submitter>,
two_thirds_majority_transition: u64, ) -> Result<FinalityVotes<Submitter>, Error> {
) -> Result<
(
BTreeMap<Address, u64>,
VecDeque<(H256, u64, Option<S::Submitter>, BTreeSet<Address>)>,
),
Error,
> {
// this fn can only work with single validators set // this fn can only work with single validators set
if !validators.contains(&header.author) { if !validators.contains(&header.author) {
return Err(Error::NotValidator); return Err(Error::NotValidator);
} }
// prepare iterator of signers of all ancestors of the header // now we have votes that were valid when some block B has been inserted
// we only take ancestors that are not yet pruned and those signed by // things may have changed a bit, but we do not need to read anything else
// the same set of validators // from the db, because we have ancestry
let mut parent_empty_step_signers = empty_steps_signers(header); // so the only thing we need to do is:
let ancestry = ancestry(storage, header.parent_hash) // 1) remove votes from blocks that have been finalized after B has been inserted;
.map(|(hash, header, submitter)| { // 2) add votes from B descendants
let mut signers = BTreeSet::new(); let mut votes = cached_votes.votes.unwrap_or_default();
sp_std::mem::swap(&mut signers, &mut parent_empty_step_signers);
signers.insert(header.author);
let empty_step_signers = empty_steps_signers(&header); // remove votes from finalized blocks
let res = (hash, header.number, submitter, signers); while let Some(old_ancestor) = votes.ancestry.pop_front() {
parent_empty_step_signers = empty_step_signers; if old_ancestor.id.number > best_finalized_number {
res votes.ancestry.push_front(old_ancestor);
})
.take_while(|&(hash, _, _, _)| hash != *validators_begin && hash != *best_finalized_hash);
// now let's iterate built iterator and compute number of validators
// 'voted' for each header
// we stop when finalized block is met (because we only interested in
// just finalized blocks)
let mut votes = BTreeMap::new();
let mut headers = VecDeque::new();
for (hash, number, submitter, signers) in ancestry {
add_signers_votes(validators, &signers, &mut votes)?;
if is_finalized(validators, &votes, number >= two_thirds_majority_transition) {
remove_signers_votes(&signers, &mut votes);
break; break;
} }
headers.push_front((hash, number, submitter, signers)); remove_signers_votes(&old_ancestor.signers, &mut votes.votes);
} }
// update votes with last header vote // add votes from new blocks
let mut parent_empty_step_signers = empty_steps_signers(header);
let mut unaccounted_ancestry = VecDeque::new();
while let Some((ancestor_id, ancestor_submitter, ancestor)) = cached_votes.unaccounted_ancestry.pop_front() {
let mut signers = empty_steps_signers(&ancestor);
sp_std::mem::swap(&mut signers, &mut parent_empty_step_signers);
signers.insert(ancestor.author);
add_signers_votes(validators, &signers, &mut votes.votes)?;
unaccounted_ancestry.push_front(FinalityAncestor {
id: ancestor_id,
submitter: ancestor_submitter,
signers,
});
}
votes.ancestry.extend(unaccounted_ancestry);
// add votes from block itself
let mut header_signers = BTreeSet::new(); let mut header_signers = BTreeSet::new();
header_signers.insert(header.author); header_signers.insert(header.author);
*votes.entry(header.author).or_insert(0) += 1; *votes.votes.entry(header.author).or_insert(0) += 1;
headers.push_back((*hash, header.number, submitter.cloned(), header_signers)); votes.ancestry.push_back(FinalityAncestor {
id,
submitter,
signers: header_signers,
});
Ok((votes, headers)) Ok(votes)
} }
/// Increase count of 'votes' for every passed signer. /// Increase count of 'votes' for every passed signer.
@@ -193,28 +245,21 @@ fn empty_step_signer(empty_step: &SealedEmptyStep, parent_hash: &H256) -> Option
.map(|public| public_to_address(&public)) .map(|public| public_to_address(&public))
} }
/// Return iterator of given header ancestors. impl<Submitter> Default for FinalityVotes<Submitter> {
pub(crate) fn ancestry<'a, S: Storage>( fn default() -> Self {
storage: &'a S, FinalityVotes {
mut parent_hash: H256, votes: BTreeMap::new(),
) -> impl Iterator<Item = (H256, Header, Option<S::Submitter>)> + 'a { ancestry: VecDeque::new(),
from_fn(move || {
let (header, submitter) = storage.header(&parent_hash)?;
if header.number == 0 {
return None;
} }
}
let hash = parent_hash.clone();
parent_hash = header.parent_hash.clone();
Some((hash, header, submitter))
})
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::mock::{custom_test_ext, genesis, validator, validators_addresses, TestRuntime}; use crate::mock::{custom_test_ext, genesis, insert_header, validator, validators_addresses, TestRuntime};
use crate::{BridgeStorage, HeaderToImport}; use crate::{BridgeStorage, FinalityCache, HeaderToImport};
use frame_support::StorageMap;
#[test] #[test]
fn verifies_header_author() { fn verifies_header_author() {
@@ -222,9 +267,9 @@ mod tests {
assert_eq!( assert_eq!(
finalize_blocks( finalize_blocks(
&BridgeStorage::<TestRuntime>::new(), &BridgeStorage::<TestRuntime>::new(),
&Default::default(), Default::default(),
(&Default::default(), &[]), (Default::default(), &[]),
&Default::default(), Default::default(),
None, None,
&Header::default(), &Header::default(),
0, 0,
@@ -235,7 +280,7 @@ mod tests {
} }
#[test] #[test]
fn prepares_votes() { fn finalize_blocks_works() {
custom_test_ext(genesis(), validators_addresses(5)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(5)).execute_with(|| {
// let's say we have 5 validators (we need 'votes' from 3 validators to achieve // let's say we have 5 validators (we need 'votes' from 3 validators to achieve
// finality) // finality)
@@ -244,30 +289,32 @@ mod tests {
// when header#1 is inserted, nothing is finalized (1 vote) // when header#1 is inserted, nothing is finalized (1 vote)
let header1 = Header { let header1 = Header {
author: validator(0).address().as_fixed_bytes().into(), author: validator(0).address().as_fixed_bytes().into(),
parent_hash: genesis().hash(), parent_hash: genesis().compute_hash(),
number: 1, number: 1,
..Default::default() ..Default::default()
}; };
let hash1 = header1.hash(); let id1 = header1.compute_id();
let mut header_to_import = HeaderToImport { let mut header_to_import = HeaderToImport {
context: storage.import_context(None, &genesis().hash()).unwrap(), context: storage.import_context(None, &genesis().compute_hash()).unwrap(),
is_best: true, is_best: true,
hash: hash1, id: id1,
header: header1, header: header1,
total_difficulty: 0.into(), total_difficulty: 0.into(),
enacted_change: None, enacted_change: None,
scheduled_change: None, scheduled_change: None,
finality_votes: Default::default(),
}; };
assert_eq!( assert_eq!(
finalize_blocks( finalize_blocks(
&storage, &storage,
&Default::default(), Default::default(),
(&Default::default(), &validators_addresses(5)), (Default::default(), &validators_addresses(5)),
&hash1, id1,
None, None,
&header_to_import.header, &header_to_import.header,
u64::max_value(), u64::max_value(),
), )
.map(|eff| eff.finalized_headers),
Ok(Vec::new()), Ok(Vec::new()),
); );
storage.insert_header(header_to_import.clone()); storage.insert_header(header_to_import.clone());
@@ -275,22 +322,23 @@ mod tests {
// when header#2 is inserted, nothing is finalized (2 votes) // when header#2 is inserted, nothing is finalized (2 votes)
header_to_import.header = Header { header_to_import.header = Header {
author: validator(1).address().as_fixed_bytes().into(), author: validator(1).address().as_fixed_bytes().into(),
parent_hash: hash1, parent_hash: id1.hash,
number: 2, number: 2,
..Default::default() ..Default::default()
}; };
header_to_import.hash = header_to_import.header.hash(); header_to_import.id = header_to_import.header.compute_id();
let hash2 = header_to_import.header.hash(); let id2 = header_to_import.header.compute_id();
assert_eq!( assert_eq!(
finalize_blocks( finalize_blocks(
&storage, &storage,
&Default::default(), Default::default(),
(&Default::default(), &validators_addresses(5)), (Default::default(), &validators_addresses(5)),
&hash2, id2,
None, None,
&header_to_import.header, &header_to_import.header,
u64::max_value(), u64::max_value(),
), )
.map(|eff| eff.finalized_headers),
Ok(Vec::new()), Ok(Vec::new()),
); );
storage.insert_header(header_to_import.clone()); storage.insert_header(header_to_import.clone());
@@ -298,25 +346,192 @@ mod tests {
// when header#3 is inserted, header#1 is finalized (3 votes) // when header#3 is inserted, header#1 is finalized (3 votes)
header_to_import.header = Header { header_to_import.header = Header {
author: validator(2).address().as_fixed_bytes().into(), author: validator(2).address().as_fixed_bytes().into(),
parent_hash: hash2, parent_hash: id2.hash,
number: 3, number: 3,
..Default::default() ..Default::default()
}; };
header_to_import.hash = header_to_import.header.hash(); header_to_import.id = header_to_import.header.compute_id();
let hash3 = header_to_import.header.hash(); let id3 = header_to_import.header.compute_id();
assert_eq!( assert_eq!(
finalize_blocks( finalize_blocks(
&storage, &storage,
&Default::default(), Default::default(),
(&Default::default(), &validators_addresses(5)), (Default::default(), &validators_addresses(5)),
&hash3, id3,
None, None,
&header_to_import.header, &header_to_import.header,
u64::max_value(), u64::max_value(),
), )
Ok(vec![(1, hash1, None)]), .map(|eff| eff.finalized_headers),
Ok(vec![(id1, None)]),
); );
storage.insert_header(header_to_import); storage.insert_header(header_to_import);
}); });
} }
#[test]
fn cached_votes_are_updated_with_ancestry() {
// we're inserting header#5
// cached votes are from header#3
// header#4 has finalized header#1 and header#2
// => when inserting header#5, we need to:
// 1) remove votes from header#1 and header#2
// 2) add votes from header#4 and header#5
let validators = validators_addresses(5);
let headers = (1..6)
.map(|number| Header {
number: number,
author: validators[number as usize - 1],
..Default::default()
})
.collect::<Vec<_>>();
let ancestry = headers
.iter()
.map(|header| FinalityAncestor {
id: header.compute_id(),
signers: vec![header.author].into_iter().collect(),
..Default::default()
})
.collect::<Vec<_>>();
let header5 = headers[4].clone();
assert_eq!(
prepare_votes::<()>(
CachedFinalityVotes {
unaccounted_ancestry: vec![(headers[3].compute_id(), None, headers[3].clone()),]
.into_iter()
.collect(),
votes: Some(FinalityVotes {
votes: vec![(validators[0], 1), (validators[1], 1), (validators[2], 1),]
.into_iter()
.collect(),
ancestry: ancestry[..3].iter().cloned().collect(),
}),
},
2,
&validators.iter().collect(),
header5.compute_id(),
&header5,
None,
)
.unwrap(),
FinalityVotes {
votes: vec![(validators[2], 1), (validators[3], 1), (validators[4], 1),]
.into_iter()
.collect(),
ancestry: ancestry[2..].iter().cloned().collect(),
},
);
}
#[test]
fn prepare_votes_respects_finality_cache() {
let validators_addresses = validators_addresses(5);
custom_test_ext(genesis(), validators_addresses.clone()).execute_with(move || {
// we need signatures of 3 validators to finalize block
let mut storage = BridgeStorage::<TestRuntime>::new();
// headers 1..3 are signed by validator#0
// headers 4..6 are signed by validator#1
// headers 7..9 are signed by validator#2
let mut hashes = Vec::new();
let mut headers = Vec::new();
let mut ancestry = Vec::new();
let mut parent_hash = genesis().compute_hash();
for i in 1..10 {
let header = Header {
author: validator((i - 1) / 3).address().as_fixed_bytes().into(),
parent_hash,
number: i as _,
..Default::default()
};
let id = header.compute_id();
insert_header(&mut storage, header.clone());
hashes.push(id.hash);
ancestry.push(FinalityAncestor {
id: header.compute_id(),
submitter: None,
signers: vec![header.author].into_iter().collect(),
});
headers.push(header);
parent_hash = id.hash;
}
// when we're inserting header#7 and last finalized header is 0:
// check that votes at #7 are computed correctly without cache
let expected_votes_at_7 = FinalityVotes {
votes: vec![
(validators_addresses[0].clone(), 3),
(validators_addresses[1].clone(), 3),
(validators_addresses[2].clone(), 1),
]
.into_iter()
.collect(),
ancestry: ancestry[..7].iter().cloned().collect(),
};
let id7 = headers[6].compute_id();
assert_eq!(
prepare_votes(
storage.cached_finality_votes(&hashes.get(5).unwrap(), |_| false,),
0,
&validators_addresses.iter().collect(),
id7,
headers.get(6).unwrap(),
None,
)
.unwrap(),
expected_votes_at_7,
);
// cached votes at #5
let expected_votes_at_5 = FinalityVotes {
votes: vec![
(validators_addresses[0].clone(), 3),
(validators_addresses[1].clone(), 2),
]
.into_iter()
.collect(),
ancestry: ancestry[..5].iter().cloned().collect(),
};
FinalityCache::<TestRuntime>::insert(hashes[4], expected_votes_at_5);
// when we're inserting header#7 and last finalized header is 0:
// check that votes at #7 are computed correctly with cache
assert_eq!(
prepare_votes(
storage.cached_finality_votes(&hashes.get(5).unwrap(), |_| false,),
0,
&validators_addresses.iter().collect(),
id7,
headers.get(6).unwrap(),
None,
)
.unwrap(),
expected_votes_at_7,
);
// when we're inserting header#7 and last finalized header is 3:
// check that votes at #7 are computed correctly with cache
let expected_votes_at_7 = FinalityVotes {
votes: vec![
(validators_addresses[1].clone(), 3),
(validators_addresses[2].clone(), 1),
]
.into_iter()
.collect(),
ancestry: ancestry[3..7].iter().cloned().collect(),
};
assert_eq!(
prepare_votes(
storage.cached_finality_votes(&hashes.get(5).unwrap(), |hash| *hash == hashes[2],),
3,
&validators_addresses.iter().collect(),
id7,
headers.get(6).unwrap(),
None,
)
.unwrap(),
expected_votes_at_7,
);
});
}
} }
+41 -31
View File
@@ -19,7 +19,7 @@ use crate::finality::finalize_blocks;
use crate::validators::{Validators, ValidatorsConfiguration}; use crate::validators::{Validators, ValidatorsConfiguration};
use crate::verification::{is_importable_header, verify_aura_header}; use crate::verification::{is_importable_header, verify_aura_header};
use crate::{AuraConfiguration, ChangeToEnact, Storage}; use crate::{AuraConfiguration, ChangeToEnact, Storage};
use primitives::{Header, Receipt, H256}; use primitives::{Header, HeaderId, Receipt};
use sp_std::{collections::btree_map::BTreeMap, prelude::*}; use sp_std::{collections::btree_map::BTreeMap, 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
@@ -64,7 +64,7 @@ pub fn import_headers<S: Storage>(
match import_result { match import_result {
Ok((_, finalized)) => { Ok((_, finalized)) => {
for (_, _, submitter) in finalized { for (_, submitter) in finalized {
if let Some(submitter) = submitter { if let Some(submitter) = submitter {
*finalized_headers.entry(submitter).or_default() += 1; *finalized_headers.entry(submitter).or_default() += 1;
} }
@@ -84,7 +84,7 @@ pub fn import_headers<S: Storage>(
/// Transactions receipts must be provided if `header_import_requires_receipts()` /// Transactions receipts must be provided if `header_import_requires_receipts()`
/// has returned true. /// has returned true.
/// ///
/// Returns imported block hash. /// Returns imported block id and list of all finalized headers.
pub fn import_header<S: Storage>( pub fn import_header<S: Storage>(
storage: &mut S, storage: &mut S,
aura_config: &AuraConfiguration, aura_config: &AuraConfiguration,
@@ -93,9 +93,9 @@ pub fn import_header<S: Storage>(
submitter: Option<S::Submitter>, submitter: Option<S::Submitter>,
header: Header, header: Header,
receipts: Option<Vec<Receipt>>, receipts: Option<Vec<Receipt>>,
) -> Result<(H256, Vec<(u64, H256, Option<S::Submitter>)>), Error> { ) -> Result<(HeaderId, Vec<(HeaderId, Option<S::Submitter>)>), Error> {
// first check that we are able to import this header at all // first check that we are able to import this header at all
let (hash, prev_finalized_hash) = is_importable_header(storage, &header)?; let (header_id, finalized_id) = is_importable_header(storage, &header)?;
// verify header // verify header
let import_context = verify_aura_header(storage, aura_config, submitter, &header)?; let import_context = verify_aura_header(storage, aura_config, submitter, &header)?;
@@ -108,9 +108,9 @@ pub fn import_header<S: Storage>(
let validators_set = import_context.validators_set(); let validators_set = import_context.validators_set();
let finalized_blocks = finalize_blocks( let finalized_blocks = finalize_blocks(
storage, storage,
&prev_finalized_hash, finalized_id,
(&validators_set.enact_block, &validators_set.validators), (validators_set.enact_block, &validators_set.validators),
&hash, header_id,
import_context.submitter(), import_context.submitter(),
&header, &header,
aura_config.two_thirds_majority_transition, aura_config.two_thirds_majority_transition,
@@ -120,35 +120,36 @@ pub fn import_header<S: Storage>(
signal_block: None, signal_block: None,
validators, validators,
}) })
.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks)); .or_else(|| validators.finalize_validators_change(storage, &finalized_blocks.finalized_headers));
// 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)
// and finally insert the block // and finally insert the block
let (_, _, best_total_difficulty) = storage.best_block(); let (_, best_total_difficulty) = storage.best_block();
let total_difficulty = import_context.total_difficulty() + header.difficulty; let total_difficulty = import_context.total_difficulty() + header.difficulty;
let is_best = total_difficulty > best_total_difficulty; let is_best = total_difficulty > best_total_difficulty;
let header_number = header.number; let header_number = header.number;
storage.insert_header(import_context.into_import_header( storage.insert_header(import_context.into_import_header(
is_best, is_best,
hash, header_id,
header, header,
total_difficulty, total_difficulty,
enacted_change, enacted_change,
scheduled_change, scheduled_change,
finalized_blocks.votes,
)); ));
// now mark finalized headers && prune old headers // now mark finalized headers && prune old headers
storage.finalize_headers( storage.finalize_headers(
finalized_blocks.last().map(|(number, hash, _)| (*number, *hash)), finalized_blocks.finalized_headers.last().map(|(id, _)| *id),
match is_best { match is_best {
true => header_number.checked_sub(prune_depth), true => header_number.checked_sub(prune_depth),
false => None, false => None,
}, },
); );
Ok((hash, finalized_blocks)) Ok((header_id, finalized_blocks.finalized_headers))
} }
/// Returns true if transactions receipts are required to import given header. /// Returns true if transactions receipts are required to import given header.
@@ -178,7 +179,13 @@ mod tests {
fn rejects_finalized_block_competitors() { fn rejects_finalized_block_competitors() {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let mut storage = BridgeStorage::<TestRuntime>::new(); let mut storage = BridgeStorage::<TestRuntime>::new();
storage.finalize_headers(Some((100, Default::default())), None); storage.finalize_headers(
Some(HeaderId {
number: 100,
..Default::default()
}),
None,
);
assert_eq!( assert_eq!(
import_header( import_header(
&mut storage, &mut storage,
@@ -239,7 +246,7 @@ mod tests {
let validators = validators(3); let validators = validators(3);
let mut storage = BridgeStorage::<TestRuntime>::new(); let mut storage = BridgeStorage::<TestRuntime>::new();
let header = block_i(1, &validators); let header = block_i(1, &validators);
let hash = header.hash(); let hash = header.compute_hash();
assert_eq!( assert_eq!(
import_header( import_header(
&mut storage, &mut storage,
@@ -273,10 +280,10 @@ mod tests {
// header [0..11] are finalizing blocks [0; 9] // header [0..11] are finalizing blocks [0; 9]
// => since we want to keep 10 finalized blocks, we aren't pruning anything // => since we want to keep 10 finalized blocks, we aren't pruning anything
let mut latest_block_hash = Default::default(); let mut latest_block_id = Default::default();
for i in 1..11 { for i in 1..11 {
let header = block_i(i, &validators); let header = block_i(i, &validators);
let (rolling_last_block_hash, finalized_blocks) = import_header( let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage, &mut storage,
&test_aura_config(), &test_aura_config(),
&validators_config, &validators_config,
@@ -289,15 +296,15 @@ mod tests {
match i { match i {
2..=10 => assert_eq!( 2..=10 => assert_eq!(
finalized_blocks, finalized_blocks,
vec![(i - 1, block_i(i - 1, &validators).hash(), Some(100))], vec![(block_i(i - 1, &validators).compute_id(), Some(100))],
"At {}", "At {}",
i, i,
), ),
_ => assert_eq!(finalized_blocks, vec![], "At {}", i), _ => assert_eq!(finalized_blocks, vec![], "At {}", i),
} }
latest_block_hash = rolling_last_block_hash; latest_block_id = rolling_last_block_id;
} }
assert!(storage.header(&genesis().hash()).is_some()); assert!(storage.header(&genesis().compute_hash()).is_some());
// header 11 finalizes headers [10] AND schedules change // header 11 finalizes headers [10] AND schedules change
// => we prune header#0 // => we prune header#0
@@ -307,7 +314,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
}); });
let (rolling_last_block_hash, finalized_blocks) = import_header( let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage, &mut storage,
&test_aura_config(), &test_aura_config(),
&validators_config, &validators_config,
@@ -315,23 +322,26 @@ mod tests {
Some(101), Some(101),
header11.clone(), header11.clone(),
Some(vec![crate::validators::tests::validators_change_recept( Some(vec![crate::validators::tests::validators_change_recept(
latest_block_hash, latest_block_id.hash,
)]), )]),
) )
.unwrap(); .unwrap();
assert_eq!(finalized_blocks, vec![(10, block_i(10, &validators).hash(), Some(100))],); assert_eq!(
assert!(storage.header(&genesis().hash()).is_none()); finalized_blocks,
latest_block_hash = rolling_last_block_hash; vec![(block_i(10, &validators).compute_id(), Some(100))],
);
assert!(storage.header(&genesis().compute_hash()).is_none());
latest_block_id = rolling_last_block_id;
// and now let's say validators 1 && 2 went offline // and now let's say validators 1 && 2 went offline
// => in the range 12-25 no blocks are finalized, but we still continue to prune old headers // => in the range 12-25 no blocks are finalized, but we still continue to prune old headers
// until header#11 is met. we can't prune #11, because it schedules change // until header#11 is met. we can't prune #11, because it schedules change
let mut step = 56; let mut step = 56;
let mut expected_blocks = vec![(11, header11.hash(), Some(101))]; let mut expected_blocks = vec![(header11.compute_id(), Some(101))];
for i in 12..25 { for i in 12..25 {
let header = Header { let header = Header {
number: i as _, number: i as _,
parent_hash: latest_block_hash, parent_hash: latest_block_id.hash,
gas_limit: 0x2000.into(), gas_limit: 0x2000.into(),
author: validator(2).address(), author: validator(2).address(),
seal: vec![vec![step].into(), vec![].into()], seal: vec![vec![step].into(), vec![].into()],
@@ -339,8 +349,8 @@ mod tests {
..Default::default() ..Default::default()
}; };
let header = signed_header(&validators, header, step as _); let header = signed_header(&validators, header, step as _);
expected_blocks.push((i, header.hash(), Some(102))); expected_blocks.push((header.compute_id(), Some(102)));
let (rolling_last_block_hash, finalized_blocks) = import_header( let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage, &mut storage,
&test_aura_config(), &test_aura_config(),
&validators_config, &validators_config,
@@ -351,7 +361,7 @@ mod tests {
) )
.unwrap(); .unwrap();
assert_eq!(finalized_blocks, vec![],); assert_eq!(finalized_blocks, vec![],);
latest_block_hash = rolling_last_block_hash; latest_block_id = rolling_last_block_id;
step += 3; step += 3;
} }
assert_eq!( assert_eq!(
@@ -367,7 +377,7 @@ mod tests {
step -= 2; step -= 2;
let header = Header { let header = Header {
number: 25, number: 25,
parent_hash: latest_block_hash, parent_hash: latest_block_id.hash,
gas_limit: 0x2000.into(), gas_limit: 0x2000.into(),
author: validator(0).address(), author: validator(0).address(),
seal: vec![vec![step].into(), vec![].into()], seal: vec![vec![step].into(), vec![].into()],
+263 -78
View File
@@ -16,9 +16,10 @@
#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(not(feature = "std"), no_std)]
use crate::finality::{CachedFinalityVotes, FinalityVotes};
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{decl_module, decl_storage, traits::Get}; use frame_support::{decl_module, decl_storage, traits::Get};
use primitives::{Address, Header, RawTransaction, Receipt, H256, U256}; use primitives::{Address, Header, HeaderId, RawTransaction, Receipt, H256, U256};
use sp_runtime::{ use sp_runtime::{
transaction_validity::{ transaction_validity::{
InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionSource, TransactionValidity, InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionSource, TransactionValidity,
@@ -93,7 +94,7 @@ pub struct StoredHeader<Submitter> {
/// Hash of the last block which has **SCHEDULED** validators set change. /// Hash of the last block which has **SCHEDULED** validators set change.
/// Note that signal doesn't mean that the set has been (or ever will be) enacted. /// Note that signal doesn't mean that the set has been (or ever will be) enacted.
/// Note that the header may already be pruned. /// Note that the header may already be pruned.
pub last_signal_block: Option<H256>, pub last_signal_block: Option<HeaderId>,
} }
/// Validators set as it is stored in the runtime storage. /// Validators set as it is stored in the runtime storage.
@@ -103,9 +104,9 @@ pub struct ValidatorsSet {
/// Validators of this set. /// Validators of this set.
pub validators: Vec<Address>, pub validators: Vec<Address>,
/// Hash of the block where this set has been signalled. None if this is the first set. /// Hash of the block where this set has been signalled. None if this is the first set.
pub signal_block: Option<H256>, pub signal_block: Option<HeaderId>,
/// Hash of the block where this set has been enacted. /// Hash of the block where this set has been enacted.
pub enact_block: H256, pub enact_block: HeaderId,
} }
/// Validators set change as it is stored in the runtime storage. /// Validators set change as it is stored in the runtime storage.
@@ -115,7 +116,7 @@ pub struct ScheduledChange {
/// Validators of this set. /// Validators of this set.
pub validators: Vec<Address>, pub validators: Vec<Address>,
/// Hash of the block which has emitted previous validators change signal. /// Hash of the block which has emitted previous validators change signal.
pub prev_signal_block: Option<H256>, pub prev_signal_block: Option<HeaderId>,
} }
/// Header that we're importing. /// Header that we're importing.
@@ -126,8 +127,8 @@ pub struct HeaderToImport<Submitter> {
pub context: ImportContext<Submitter>, pub context: ImportContext<Submitter>,
/// Should we consider this header as best? /// Should we consider this header as best?
pub is_best: bool, pub is_best: bool,
/// The hash of the header. /// The id of the header.
pub hash: H256, pub id: HeaderId,
/// The header itself. /// The header itself.
pub header: Header, pub header: Header,
/// Total chain difficulty at the header. /// Total chain difficulty at the header.
@@ -137,15 +138,17 @@ pub struct HeaderToImport<Submitter> {
pub enacted_change: Option<ChangeToEnact>, pub enacted_change: Option<ChangeToEnact>,
/// Validators set scheduled change, if happened at the header. /// Validators set scheduled change, if happened at the header.
pub scheduled_change: Option<Vec<Address>>, pub scheduled_change: Option<Vec<Address>>,
/// Finality votes at this header.
pub finality_votes: FinalityVotes<Submitter>,
} }
/// Header that we're importing. /// Header that we're importing.
#[derive(RuntimeDebug)] #[derive(RuntimeDebug)]
#[cfg_attr(test, derive(Clone, PartialEq))] #[cfg_attr(test, derive(Clone, PartialEq))]
pub struct ChangeToEnact { pub struct ChangeToEnact {
/// The hash of the header where change has been scheduled. /// The id of the header where change has been scheduled.
/// None if it is a first set within current `ValidatorsSource`. /// None if it is a first set within current `ValidatorsSource`.
pub signal_block: Option<H256>, pub signal_block: Option<HeaderId>,
/// Validators set that is enacted. /// Validators set that is enacted.
pub validators: Vec<Address>, pub validators: Vec<Address>,
} }
@@ -179,7 +182,7 @@ pub struct ImportContext<Submitter> {
parent_scheduled_change: Option<ScheduledChange>, parent_scheduled_change: Option<ScheduledChange>,
validators_set_id: u64, validators_set_id: u64,
validators_set: ValidatorsSet, validators_set: ValidatorsSet,
last_signal_block: Option<H256>, last_signal_block: Option<HeaderId>,
} }
impl<Submitter> ImportContext<Submitter> { impl<Submitter> ImportContext<Submitter> {
@@ -215,10 +218,13 @@ impl<Submitter> ImportContext<Submitter> {
/// Returns reference to the latest block which has signalled change of validators set. /// Returns reference to the latest block which has signalled change of validators set.
/// This may point to parent if parent has signalled change. /// This may point to parent if parent has signalled change.
pub fn last_signal_block(&self) -> Option<&H256> { pub fn last_signal_block(&self) -> Option<HeaderId> {
match self.parent_scheduled_change { match self.parent_scheduled_change {
Some(_) => Some(&self.parent_hash), Some(_) => Some(HeaderId {
None => self.last_signal_block.as_ref(), number: self.parent_header.number,
hash: self.parent_hash,
}),
None => self.last_signal_block,
} }
} }
@@ -226,20 +232,22 @@ impl<Submitter> ImportContext<Submitter> {
pub fn into_import_header( pub fn into_import_header(
self, self,
is_best: bool, is_best: bool,
hash: H256, id: HeaderId,
header: Header, header: Header,
total_difficulty: U256, total_difficulty: U256,
enacted_change: Option<ChangeToEnact>, enacted_change: Option<ChangeToEnact>,
scheduled_change: Option<Vec<Address>>, scheduled_change: Option<Vec<Address>>,
finality_votes: FinalityVotes<Submitter>,
) -> HeaderToImport<Submitter> { ) -> HeaderToImport<Submitter> {
HeaderToImport { HeaderToImport {
context: self, context: self,
is_best, is_best,
hash, id,
header, header,
total_difficulty, total_difficulty,
enacted_change, enacted_change,
scheduled_change, scheduled_change,
finality_votes,
} }
} }
} }
@@ -251,14 +259,22 @@ pub trait Storage {
/// Header submitter identifier. /// Header submitter identifier.
type Submitter: Clone + Ord; type Submitter: Clone + Ord;
/// Get best known block. /// Get best known block and total chain difficulty.
fn best_block(&self) -> (u64, H256, U256); fn best_block(&self) -> (HeaderId, U256);
/// Get last finalized block. /// Get last finalized block.
fn finalized_block(&self) -> (u64, H256); fn finalized_block(&self) -> HeaderId;
/// Get imported header by its hash. /// Get imported header by its hash.
/// ///
/// Returns header and its submitter (if known). /// Returns header and its submitter (if known).
fn header(&self, hash: &H256) -> Option<(Header, Option<Self::Submitter>)>; fn header(&self, hash: &H256) -> Option<(Header, Option<Self::Submitter>)>;
/// Returns latest cached finality votes (if any) for block ancestors, starting
/// from `parent_hash` block and stopping at genesis block, or block where `stop_at`
/// returns true.
fn cached_finality_votes(
&self,
parent_hash: &H256,
stop_at: impl Fn(&H256) -> bool,
) -> CachedFinalityVotes<Self::Submitter>;
/// Get header import context by parent header hash. /// Get header import context by parent header hash.
fn import_context( fn import_context(
&self, &self,
@@ -275,7 +291,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(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>); fn finalize_headers(&mut self, finalized: Option<HeaderId>, prune_end: Option<u64>);
} }
/// Decides whether the session should be ended. /// Decides whether the session should be ended.
@@ -302,10 +318,17 @@ impl<AccountId> OnHeadersSubmitted<AccountId> for () {
fn on_valid_headers_finalized(_submitter: AccountId, _finalized: u64) {} fn on_valid_headers_finalized(_submitter: AccountId, _finalized: u64) {}
} }
/// The module configuration trait /// The module configuration trait.
pub trait Trait: frame_system::Trait { pub trait Trait: frame_system::Trait {
/// Aura configuration. /// Aura configuration.
type AuraConfiguration: Get<AuraConfiguration>; type AuraConfiguration: Get<AuraConfiguration>;
/// Interval (in blocks) for for finality votes caching.
/// If None, cache is disabled.
///
/// Ideally, this should either be None (when we are sure that there won't
/// be any significant finalization delays), or something that is bit larger
/// than average finalization delay.
type FinalityVotesCachingInterval: Get<Option<u64>>;
/// Validators configuration. /// Validators configuration.
type ValidatorsConfiguration: Get<validators::ValidatorsConfiguration>; type ValidatorsConfiguration: Get<validators::ValidatorsConfiguration>;
/// Handler for headers submission result. /// Handler for headers submission result.
@@ -377,15 +400,17 @@ decl_module! {
decl_storage! { decl_storage! {
trait Store for Module<T: Trait> as Bridge { trait Store for Module<T: Trait> as Bridge {
/// Best known block. /// Best known block.
BestBlock: (u64, H256, U256); BestBlock: (HeaderId, U256);
/// Best finalized block. /// Best finalized block.
FinalizedBlock: (u64, H256); FinalizedBlock: HeaderId;
/// Range of blocks that we want to prune. /// Range of blocks that we want to prune.
BlocksToPrune: PruningRange; BlocksToPrune: PruningRange;
/// Map of imported headers by hash. /// Map of imported headers by hash.
Headers: map hasher(identity) H256 => Option<StoredHeader<T::AccountId>>; Headers: map hasher(identity) H256 => Option<StoredHeader<T::AccountId>>;
/// Map of imported header hashes by number. /// Map of imported header hashes by number.
HeadersByNumber: map hasher(blake2_128_concat) u64 => Option<Vec<H256>>; HeadersByNumber: map hasher(blake2_128_concat) u64 => Option<Vec<H256>>;
/// Map of cached finality data by header hash.
FinalityCache: map hasher(identity) H256 => Option<FinalityVotes<T::AccountId>>;
/// The ID of next validator set. /// The ID of next validator set.
NextValidatorsSetId: u64; NextValidatorsSetId: u64;
/// Map of validators sets by their id. /// Map of validators sets by their id.
@@ -412,9 +437,13 @@ decl_storage! {
"Initial validators set can't be empty", "Initial validators set can't be empty",
); );
let initial_hash = config.initial_header.hash(); let initial_hash = config.initial_header.compute_hash();
BestBlock::put((config.initial_header.number, initial_hash, config.initial_difficulty)); let initial_id = HeaderId {
FinalizedBlock::put((config.initial_header.number, initial_hash)); number: config.initial_header.number,
hash: initial_hash,
};
BestBlock::put((initial_id, config.initial_difficulty));
FinalizedBlock::put(initial_id);
BlocksToPrune::put(PruningRange { BlocksToPrune::put(PruningRange {
oldest_unpruned_block: config.initial_header.number, oldest_unpruned_block: config.initial_header.number,
oldest_block_to_keep: config.initial_header.number, oldest_block_to_keep: config.initial_header.number,
@@ -431,7 +460,7 @@ decl_storage! {
ValidatorsSets::insert(0, ValidatorsSet { ValidatorsSets::insert(0, ValidatorsSet {
validators: config.initial_validators.clone(), validators: config.initial_validators.clone(),
signal_block: None, signal_block: None,
enact_block: initial_hash, enact_block: initial_id,
}); });
ValidatorsSetsRc::insert(0, 1); ValidatorsSetsRc::insert(0, 1);
}) })
@@ -442,9 +471,8 @@ impl<T: Trait> Module<T> {
/// Returns number and hash of the best block known to the bridge module. /// Returns number and hash of the best block known to the bridge module.
/// The caller should only submit `import_header` transaction that makes /// The caller should only submit `import_header` transaction that makes
/// (or leads to making) other header the best one. /// (or leads to making) other header the best one.
pub fn best_block() -> (u64, H256) { pub fn best_block() -> HeaderId {
let (number, hash, _) = BridgeStorage::<T>::new().best_block(); BridgeStorage::<T>::new().best_block().0
(number, hash)
} }
/// Returns true if the import of given block requires transactions receipts. /// Returns true if the import of given block requires transactions receipts.
@@ -580,6 +608,7 @@ impl<T: Trait> BridgeStorage<T> {
while let Some(hash) = blocks_at_number.pop() { while let Some(hash) = blocks_at_number.pop() {
let header = Headers::<T>::take(&hash); let header = Headers::<T>::take(&hash);
ScheduledChanges::remove(hash); ScheduledChanges::remove(hash);
FinalityCache::<T>::remove(hash);
if let Some(header) = header { if let Some(header) = header {
ValidatorsSetsRc::mutate(header.next_validators_set_id, |rc| match *rc { ValidatorsSetsRc::mutate(header.next_validators_set_id, |rc| match *rc {
Some(rc) if rc > 1 => Some(rc - 1), Some(rc) if rc > 1 => Some(rc - 1),
@@ -599,11 +628,11 @@ impl<T: Trait> BridgeStorage<T> {
impl<T: Trait> Storage for BridgeStorage<T> { impl<T: Trait> Storage for BridgeStorage<T> {
type Submitter = T::AccountId; type Submitter = T::AccountId;
fn best_block(&self) -> (u64, H256, U256) { fn best_block(&self) -> (HeaderId, U256) {
BestBlock::get() BestBlock::get()
} }
fn finalized_block(&self) -> (u64, H256) { fn finalized_block(&self) -> HeaderId {
FinalizedBlock::get() FinalizedBlock::get()
} }
@@ -611,6 +640,41 @@ impl<T: Trait> Storage for BridgeStorage<T> {
Headers::<T>::get(hash).map(|header| (header.header, header.submitter)) Headers::<T>::get(hash).map(|header| (header.header, header.submitter))
} }
fn cached_finality_votes(
&self,
parent_hash: &H256,
stop_at: impl Fn(&H256) -> bool,
) -> CachedFinalityVotes<Self::Submitter> {
let mut votes = CachedFinalityVotes::default();
let mut current_hash = *parent_hash;
loop {
if stop_at(&current_hash) {
return votes;
}
let cached_votes = FinalityCache::<T>::get(&current_hash);
if let Some(cached_votes) = cached_votes {
votes.votes = Some(cached_votes);
return votes;
}
let header = match Headers::<T>::get(&current_hash) {
Some(header) if header.header.number != 0 => header,
_ => return votes,
};
let parent_hash = header.header.parent_hash;
let current_id = HeaderId {
number: header.header.number,
hash: current_hash,
};
votes
.unaccounted_ancestry
.push_back((current_id, header.submitter, header.header));
current_hash = parent_hash;
}
}
fn import_context( fn import_context(
&self, &self,
submitter: Option<Self::Submitter>, submitter: Option<Self::Submitter>,
@@ -639,11 +703,11 @@ impl<T: Trait> Storage for BridgeStorage<T> {
fn insert_header(&mut self, header: HeaderToImport<Self::Submitter>) { fn insert_header(&mut self, header: HeaderToImport<Self::Submitter>) {
if header.is_best { if header.is_best {
BestBlock::put((header.header.number, header.hash, header.total_difficulty)); BestBlock::put((header.id, header.total_difficulty));
} }
if let Some(scheduled_change) = header.scheduled_change { if let Some(scheduled_change) = header.scheduled_change {
ScheduledChanges::insert( ScheduledChanges::insert(
&header.hash, &header.id.hash,
ScheduledChange { ScheduledChange {
validators: scheduled_change, validators: scheduled_change,
prev_signal_block: header.context.last_signal_block, prev_signal_block: header.context.last_signal_block,
@@ -661,7 +725,7 @@ impl<T: Trait> Storage for BridgeStorage<T> {
next_validators_set_id, next_validators_set_id,
ValidatorsSet { ValidatorsSet {
validators: enacted_change.validators, validators: enacted_change.validators,
enact_block: header.hash, enact_block: header.id,
signal_block: enacted_change.signal_block, signal_block: enacted_change.signal_block,
}, },
); );
@@ -677,17 +741,25 @@ impl<T: Trait> Storage for BridgeStorage<T> {
} }
}; };
let finality_votes_caching_interval = T::FinalityVotesCachingInterval::get();
if let Some(finality_votes_caching_interval) = finality_votes_caching_interval {
let cache_entry_required = header.id.number != 0 && header.id.number % finality_votes_caching_interval == 0;
if cache_entry_required {
FinalityCache::<T>::insert(header.id.hash, header.finality_votes);
}
}
frame_support::debug::trace!( frame_support::debug::trace!(
target: "runtime", target: "runtime",
"Inserting PoA header: ({}, {})", "Inserting PoA header: ({}, {})",
header.header.number, header.header.number,
header.hash, header.id.hash,
); );
let last_signal_block = header.context.last_signal_block().cloned(); let last_signal_block = header.context.last_signal_block();
HeadersByNumber::append(header.header.number, header.hash); HeadersByNumber::append(header.id.number, header.id.hash);
Headers::<T>::insert( Headers::<T>::insert(
&header.hash, &header.id.hash,
StoredHeader { StoredHeader {
submitter: header.context.submitter, submitter: header.context.submitter,
header: header.header, header: header.header,
@@ -698,18 +770,18 @@ impl<T: Trait> Storage for BridgeStorage<T> {
); );
} }
fn finalize_headers(&mut self, finalized: Option<(u64, H256)>, prune_end: Option<u64>) { fn finalize_headers(&mut self, finalized: Option<HeaderId>, prune_end: Option<u64>) {
// remember just finalized block // remember just finalized block
let finalized_number = finalized let finalized_number = finalized
.as_ref() .as_ref()
.map(|f| f.0) .map(|f| f.number)
.unwrap_or_else(|| FinalizedBlock::get().0); .unwrap_or_else(|| FinalizedBlock::get().number);
if let Some(finalized) = finalized { if let Some(finalized) = finalized {
frame_support::debug::trace!( frame_support::debug::trace!(
target: "runtime", target: "runtime",
"Finalizing PoA header: ({}, {})", "Finalizing PoA header: ({}, {})",
finalized.0, finalized.number,
finalized.1, finalized.hash,
); );
FinalizedBlock::put(finalized); FinalizedBlock::put(finalized);
@@ -735,21 +807,21 @@ pub fn verify_transaction_finalized<S: Storage>(
Some((header, _)) => header, Some((header, _)) => header,
None => return false, None => return false,
}; };
let (finalized_number, finalized_hash) = storage.finalized_block(); let finalized = storage.finalized_block();
// if header is not yet finalized => return // if header is not yet finalized => return
if header.number > finalized_number { if header.number > finalized.number {
return false; return false;
} }
// check if header is actually finalized // check if header is actually finalized
let is_finalized = match header.number < finalized_number { let is_finalized = match header.number < finalized.number {
true => finality::ancestry(storage, finalized_hash) true => ancestry(storage, finalized.hash)
.skip_while(|(_, ancestor, _)| ancestor.number > header.number) .skip_while(|(_, ancestor)| ancestor.number > header.number)
.filter(|&(ancestor_hash, _, _)| ancestor_hash == block) .filter(|&(ancestor_hash, _)| ancestor_hash == block)
.next() .next()
.is_some(), .is_some(),
false => block == finalized_hash, false => block == finalized.hash,
}; };
if !is_finalized { if !is_finalized {
return false; return false;
@@ -765,14 +837,49 @@ fn pool_configuration() -> PoolConfiguration {
} }
} }
/// Return iterator of given header ancestors.
fn ancestry<'a, S: Storage>(storage: &'a S, mut parent_hash: H256) -> impl Iterator<Item = (H256, Header)> + 'a {
sp_std::iter::from_fn(move || {
let (header, _) = storage.header(&parent_hash)?;
if header.number == 0 {
return None;
}
let hash = parent_hash;
parent_hash = header.parent_hash;
Some((hash, header))
})
}
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::finality::FinalityAncestor;
use crate::mock::{ use crate::mock::{
custom_block_i, custom_test_ext, genesis, insert_header, validators, validators_addresses, TestRuntime, block_i, custom_block_i, custom_test_ext, genesis, insert_header, validators, validators_addresses, TestRuntime,
}; };
use primitives::compute_merkle_root; use primitives::compute_merkle_root;
fn example_tx() -> Vec<u8> {
vec![42]
}
fn example_header() -> Header {
let mut header = Header::default();
header.number = 2;
header.transactions_root = compute_merkle_root(vec![example_tx()].into_iter());
header.parent_hash = example_header_parent().compute_hash();
header
}
fn example_header_parent() -> Header {
let mut header = Header::default();
header.number = 1;
header.transactions_root = compute_merkle_root(vec![example_tx()].into_iter());
header.parent_hash = genesis().compute_hash();
header
}
fn with_headers_to_prune<T>(f: impl Fn(BridgeStorage<TestRuntime>) -> T) -> T { fn with_headers_to_prune<T>(f: impl Fn(BridgeStorage<TestRuntime>) -> T) -> T {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let validators = validators(3); let validators = validators(3);
@@ -782,7 +889,7 @@ pub(crate) mod tests {
let header = custom_block_i(i, &validators, |header| { let header = custom_block_i(i, &validators, |header| {
header.gas_limit = header.gas_limit + U256::from(j); header.gas_limit = header.gas_limit + U256::from(j);
}); });
let hash = header.hash(); let hash = header.compute_hash();
headers_by_number.push(hash); headers_by_number.push(hash);
Headers::<TestRuntime>::insert( Headers::<TestRuntime>::insert(
hash, hash,
@@ -935,24 +1042,97 @@ pub(crate) mod tests {
}); });
} }
fn example_tx() -> Vec<u8> { #[test]
vec![42] fn finality_votes_are_cached() {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let mut storage = BridgeStorage::<TestRuntime>::new();
let interval = <TestRuntime as Trait>::FinalityVotesCachingInterval::get().unwrap();
// for all headers with number < interval, cache entry is not created
let validators = validators(3);
for i in 1..interval {
let header = block_i(i, &validators);
let id = header.compute_id();
insert_header(&mut storage, header);
assert_eq!(FinalityCache::<TestRuntime>::get(&id.hash), None);
}
// for header with number = interval, cache entry is created
let header_with_entry = block_i(interval, &validators);
let header_with_entry_hash = header_with_entry.compute_hash();
insert_header(&mut storage, header_with_entry);
assert_eq!(
FinalityCache::<TestRuntime>::get(&header_with_entry_hash),
Some(Default::default()),
);
// when we later prune this header, cache entry is removed
BlocksToPrune::put(PruningRange {
oldest_unpruned_block: interval - 1,
oldest_block_to_keep: interval - 1,
});
storage.finalize_headers(None, Some(interval + 1));
assert_eq!(FinalityCache::<TestRuntime>::get(&header_with_entry_hash), None);
});
} }
fn example_header() -> Header { #[test]
let mut header = Header::default(); fn cached_finality_votes_finds_entry() {
header.number = 2; custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
header.transactions_root = compute_merkle_root(vec![example_tx()].into_iter()); // insert 5 headers
header.parent_hash = example_header_parent().hash(); let validators = validators(3);
header let mut storage = BridgeStorage::<TestRuntime>::new();
} let mut headers = Vec::new();
for i in 1..5 {
let header = block_i(i, &validators);
headers.push(header.clone());
insert_header(&mut storage, header);
}
fn example_header_parent() -> Header { // when inserting header#6, entry isn't found
let mut header = Header::default(); let hash5 = headers.last().unwrap().compute_hash();
header.number = 1; assert_eq!(
header.transactions_root = compute_merkle_root(vec![example_tx()].into_iter()); storage.cached_finality_votes(&hash5, |_| false),
header.parent_hash = genesis().hash(); CachedFinalityVotes {
header unaccounted_ancestry: headers
.iter()
.map(|header| (header.compute_id(), None, header.clone(),))
.rev()
.collect(),
votes: None,
},
);
// let's now create entry at #3
let hash3 = headers[2].compute_hash();
let votes_at_3 = FinalityVotes {
votes: vec![([42; 20].into(), 21)].into_iter().collect(),
ancestry: vec![FinalityAncestor {
id: HeaderId {
number: 100,
hash: Default::default(),
},
..Default::default()
}]
.into_iter()
.collect(),
};
FinalityCache::<TestRuntime>::insert(hash3, votes_at_3.clone());
// searching at #6 again => entry is found
assert_eq!(
storage.cached_finality_votes(&hash5, |_| false),
CachedFinalityVotes {
unaccounted_ancestry: headers
.iter()
.skip(3)
.map(|header| (header.compute_id(), None, header.clone(),))
.rev()
.collect(),
votes: Some(votes_at_3),
},
);
});
} }
#[test] #[test]
@@ -960,7 +1140,7 @@ pub(crate) mod tests {
custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| { custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| {
let storage = BridgeStorage::<TestRuntime>::new(); let storage = BridgeStorage::<TestRuntime>::new();
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header().hash(), 0, &vec![example_tx()],), verify_transaction_finalized(&storage, example_header().compute_hash(), 0, &vec![example_tx()],),
true, true,
); );
}); });
@@ -972,9 +1152,9 @@ pub(crate) mod tests {
let mut storage = BridgeStorage::<TestRuntime>::new(); let mut storage = BridgeStorage::<TestRuntime>::new();
insert_header(&mut storage, example_header_parent()); insert_header(&mut storage, example_header_parent());
insert_header(&mut storage, example_header()); insert_header(&mut storage, example_header());
storage.finalize_headers(Some((example_header().number, example_header().hash())), None); storage.finalize_headers(Some(example_header().compute_id()), None);
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header_parent().hash(), 0, &vec![example_tx()],), verify_transaction_finalized(&storage, example_header_parent().compute_hash(), 0, &vec![example_tx()],),
true, true,
); );
}); });
@@ -985,7 +1165,7 @@ pub(crate) mod tests {
custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| { custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| {
let storage = BridgeStorage::<TestRuntime>::new(); let storage = BridgeStorage::<TestRuntime>::new();
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header().hash(), 1, &vec![],), verify_transaction_finalized(&storage, example_header().compute_hash(), 1, &vec![],),
false, false,
); );
}); });
@@ -996,7 +1176,7 @@ pub(crate) mod tests {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let storage = BridgeStorage::<TestRuntime>::new(); let storage = BridgeStorage::<TestRuntime>::new();
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header().hash(), 1, &vec![],), verify_transaction_finalized(&storage, example_header().compute_hash(), 1, &vec![],),
false, false,
); );
}); });
@@ -1009,7 +1189,7 @@ pub(crate) mod tests {
insert_header(&mut storage, example_header_parent()); insert_header(&mut storage, example_header_parent());
insert_header(&mut storage, example_header()); insert_header(&mut storage, example_header());
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header().hash(), 0, &vec![example_tx()],), verify_transaction_finalized(&storage, example_header().compute_hash(), 0, &vec![example_tx()],),
false, false,
); );
}); });
@@ -1020,13 +1200,13 @@ pub(crate) mod tests {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let mut finalized_header_sibling = example_header(); let mut finalized_header_sibling = example_header();
finalized_header_sibling.timestamp = 1; finalized_header_sibling.timestamp = 1;
let finalized_header_sibling_hash = finalized_header_sibling.hash(); let finalized_header_sibling_hash = finalized_header_sibling.compute_hash();
let mut storage = BridgeStorage::<TestRuntime>::new(); let mut storage = BridgeStorage::<TestRuntime>::new();
insert_header(&mut storage, example_header_parent()); insert_header(&mut storage, example_header_parent());
insert_header(&mut storage, example_header()); insert_header(&mut storage, example_header());
insert_header(&mut storage, finalized_header_sibling); insert_header(&mut storage, finalized_header_sibling);
storage.finalize_headers(Some((example_header().number, example_header().hash())), None); storage.finalize_headers(Some(example_header().compute_id()), None);
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, finalized_header_sibling_hash, 0, &vec![example_tx()],), verify_transaction_finalized(&storage, finalized_header_sibling_hash, 0, &vec![example_tx()],),
false, false,
@@ -1039,13 +1219,13 @@ pub(crate) mod tests {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| { custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let mut finalized_header_uncle = example_header_parent(); let mut finalized_header_uncle = example_header_parent();
finalized_header_uncle.timestamp = 1; finalized_header_uncle.timestamp = 1;
let finalized_header_uncle_hash = finalized_header_uncle.hash(); let finalized_header_uncle_hash = finalized_header_uncle.compute_hash();
let mut storage = BridgeStorage::<TestRuntime>::new(); let mut storage = BridgeStorage::<TestRuntime>::new();
insert_header(&mut storage, example_header_parent()); insert_header(&mut storage, example_header_parent());
insert_header(&mut storage, finalized_header_uncle); insert_header(&mut storage, finalized_header_uncle);
insert_header(&mut storage, example_header()); insert_header(&mut storage, example_header());
storage.finalize_headers(Some((example_header().number, example_header().hash())), None); storage.finalize_headers(Some(example_header().compute_id()), None);
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, finalized_header_uncle_hash, 0, &vec![example_tx()],), verify_transaction_finalized(&storage, finalized_header_uncle_hash, 0, &vec![example_tx()],),
false, false,
@@ -1058,7 +1238,12 @@ pub(crate) mod tests {
custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| { custom_test_ext(example_header(), validators_addresses(3)).execute_with(|| {
let storage = BridgeStorage::<TestRuntime>::new(); let storage = BridgeStorage::<TestRuntime>::new();
assert_eq!( assert_eq!(
verify_transaction_finalized(&storage, example_header().hash(), 0, &vec![example_tx(), example_tx(),],), verify_transaction_finalized(
&storage,
example_header().compute_hash(),
0,
&vec![example_tx(), example_tx(),],
),
false, false,
); );
}); });
+5 -1
View File
@@ -14,6 +14,7 @@
// 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 Bridges Common. If not, see <http://www.gnu.org/licenses/>. // along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::finality::FinalityVotes;
use crate::validators::{ValidatorsConfiguration, ValidatorsSource}; use crate::validators::{ValidatorsConfiguration, ValidatorsSource};
use crate::{AuraConfiguration, GenesisConfig, HeaderToImport, HeadersByNumber, Storage, Trait}; use crate::{AuraConfiguration, GenesisConfig, HeaderToImport, HeadersByNumber, Storage, Trait};
use frame_support::StorageMap; use frame_support::StorageMap;
@@ -70,12 +71,14 @@ impl frame_system::Trait for TestRuntime {
} }
parameter_types! { parameter_types! {
pub const TestFinalityVotesCachingInterval: Option<u64> = Some(16);
pub const TestAuraConfiguration: AuraConfiguration = test_aura_config(); pub const TestAuraConfiguration: AuraConfiguration = test_aura_config();
pub const TestValidatorsConfiguration: ValidatorsConfiguration = test_validators_config(); pub const TestValidatorsConfiguration: ValidatorsConfiguration = test_validators_config();
} }
impl Trait for TestRuntime { impl Trait for TestRuntime {
type AuraConfiguration = TestAuraConfiguration; type AuraConfiguration = TestAuraConfiguration;
type FinalityVotesCachingInterval = TestFinalityVotesCachingInterval;
type ValidatorsConfiguration = TestValidatorsConfiguration; type ValidatorsConfiguration = TestValidatorsConfiguration;
type OnHeadersSubmitted = (); type OnHeadersSubmitted = ();
} }
@@ -171,10 +174,11 @@ pub fn insert_header<S: Storage>(storage: &mut S, header: Header) {
storage.insert_header(HeaderToImport { storage.insert_header(HeaderToImport {
context: storage.import_context(None, &header.parent_hash).unwrap(), context: storage.import_context(None, &header.parent_hash).unwrap(),
is_best: true, is_best: true,
hash: header.hash(), id: header.compute_id(),
header, header,
total_difficulty: 0.into(), total_difficulty: 0.into(),
enacted_change: None, enacted_change: None,
scheduled_change: None, scheduled_change: None,
finality_votes: FinalityVotes::default(),
}); });
} }
+111 -13
View File
@@ -16,7 +16,7 @@
use crate::error::Error; use crate::error::Error;
use crate::{ChangeToEnact, Storage}; use crate::{ChangeToEnact, Storage};
use primitives::{Address, Header, LogEntry, Receipt, H256, U256}; use primitives::{Address, Header, HeaderId, LogEntry, Receipt, U256};
use sp_std::prelude::*; use sp_std::prelude::*;
/// The hash of InitiateChange event of the validators set contract. /// The hash of InitiateChange event of the validators set contract.
@@ -181,18 +181,45 @@ impl<'a> Validators<'a> {
/// Finalize changes when blocks are finalized. /// Finalize changes when blocks are finalized.
pub fn finalize_validators_change<S: Storage>( pub fn finalize_validators_change<S: Storage>(
&self, &self,
storage: &mut S, storage: &S,
finalized_blocks: &[(u64, H256, Option<S::Submitter>)], finalized_blocks: &[(HeaderId, Option<S::Submitter>)],
) -> Option<ChangeToEnact> { ) -> Option<ChangeToEnact> {
for (_, finalized_hash, _) in finalized_blocks.iter().rev() { // if we haven't finalized any blocks, no changes may be finalized
if let Some(changes) = storage.scheduled_change(finalized_hash) { let newest_finalized_id = match finalized_blocks.last().map(|(id, _)| id) {
return Some(ChangeToEnact { Some(last_finalized_id) => last_finalized_id,
signal_block: Some(*finalized_hash), None => return None,
validators: changes.validators, };
}); let oldest_finalized_id = finalized_blocks
} .first()
} .map(|(id, _)| id)
None .expect("finalized_blocks is not empty; qed");
// try to directly go to the header that has scheduled last change
//
// if we're unable to create import context for some block, it means
// that the header has already been pruned => it and its ancestors had
// no scheduled changes
//
// if we're unable to find scheduled changes for some block, it means
// that these changes have been finalized already
storage
.import_context(None, &newest_finalized_id.hash)
.and_then(|context| context.last_signal_block())
.and_then(|signal_block| {
if signal_block.number >= oldest_finalized_id.number {
Some(signal_block)
} else {
None
}
})
.and_then(|signal_block| {
storage
.scheduled_change(&signal_block.hash)
.map(|change| ChangeToEnact {
signal_block: Some(signal_block),
validators: change.validators,
})
})
} }
/// Returns source of validators that should author the header. /// Returns source of validators that should author the header.
@@ -254,7 +281,10 @@ pub fn step_validator(header_validators: &[Address], header_step: u64) -> Addres
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use primitives::TransactionOutcome; use crate::mock::{custom_test_ext, genesis, validators_addresses, TestRuntime};
use crate::{BridgeStorage, Headers, ScheduledChange, ScheduledChanges, StoredHeader};
use frame_support::StorageMap;
use primitives::{TransactionOutcome, H256};
pub(crate) fn validators_change_recept(parent_hash: H256) -> Receipt { pub(crate) fn validators_change_recept(parent_hash: H256) -> Receipt {
Receipt { Receipt {
@@ -393,4 +423,72 @@ pub(crate) mod tests {
Err(Error::TransactionsReceiptsMismatch), Err(Error::TransactionsReceiptsMismatch),
); );
} }
fn try_finalize_with_scheduled_change(scheduled_at: Option<HeaderId>) -> Option<ChangeToEnact> {
custom_test_ext(genesis(), validators_addresses(3)).execute_with(|| {
let config = ValidatorsConfiguration::Single(ValidatorsSource::Contract(Default::default(), Vec::new()));
let validators = Validators::new(&config);
let storage = BridgeStorage::<TestRuntime>::new();
// when we're finailizing blocks 10...100
let id10 = HeaderId {
number: 10,
hash: [10; 32].into(),
};
let id100 = HeaderId {
number: 100,
hash: [100; 32].into(),
};
let finalized_blocks = vec![(id10, None), (id100, None)];
let header100 = StoredHeader::<u64> {
submitter: None,
header: Header {
number: 100,
..Default::default()
},
total_difficulty: 0.into(),
next_validators_set_id: 0,
last_signal_block: scheduled_at,
};
let scheduled_change = ScheduledChange {
validators: validators_addresses(1),
prev_signal_block: None,
};
Headers::<TestRuntime>::insert(id100.hash, header100);
if let Some(scheduled_at) = scheduled_at {
ScheduledChanges::insert(scheduled_at.hash, scheduled_change);
}
validators.finalize_validators_change(&storage, &finalized_blocks)
})
}
#[test]
fn finalize_validators_change_finalizes_scheduled_change() {
let id50 = HeaderId {
number: 50,
..Default::default()
};
assert_eq!(
try_finalize_with_scheduled_change(Some(id50)),
Some(ChangeToEnact {
signal_block: Some(id50),
validators: validators_addresses(1),
}),
);
}
#[test]
fn finalize_validators_change_does_not_finalize_when_changes_are_not_scheduled() {
assert_eq!(try_finalize_with_scheduled_change(None), None,);
}
#[test]
fn finalize_validators_change_does_not_finalize_changes_when_they_are_outside_of_range() {
let id5 = HeaderId {
number: 5,
..Default::default()
};
assert_eq!(try_finalize_with_scheduled_change(Some(id5)), None,);
}
} }
+45 -35
View File
@@ -18,26 +18,26 @@ use crate::error::Error;
use crate::validators::{step_validator, Validators, ValidatorsConfiguration}; use crate::validators::{step_validator, Validators, ValidatorsConfiguration};
use crate::{AuraConfiguration, ImportContext, PoolConfiguration, ScheduledChange, Storage}; use crate::{AuraConfiguration, ImportContext, PoolConfiguration, ScheduledChange, Storage};
use codec::Encode; use codec::Encode;
use primitives::{public_to_address, Address, Header, Receipt, SealedEmptyStep, H256, H520, U128, U256}; use primitives::{public_to_address, Address, Header, HeaderId, Receipt, SealedEmptyStep, H256, H520, U128, U256};
use sp_io::crypto::secp256k1_ecdsa_recover; use sp_io::crypto::secp256k1_ecdsa_recover;
use sp_std::{vec, vec::Vec}; use sp_std::{vec, vec::Vec};
/// Pre-check to see if should try and import this header. /// Pre-check to see if should try and import this header.
/// Returns error if we should not try to import this block. /// Returns error if we should not try to import this block.
/// Returns hash of the header and number of the last finalized block otherwise. /// Returns ID of passed header and best finalized header.
pub fn is_importable_header<S: Storage>(storage: &S, header: &Header) -> Result<(H256, H256), Error> { pub fn is_importable_header<S: Storage>(storage: &S, header: &Header) -> Result<(HeaderId, HeaderId), Error> {
// we never import any header that competes with finalized header // we never import any header that competes with finalized header
let (finalized_block_number, finalized_block_hash) = storage.finalized_block(); let finalized_id = storage.finalized_block();
if header.number <= finalized_block_number { if header.number <= finalized_id.number {
return Err(Error::AncientHeader); return Err(Error::AncientHeader);
} }
// we never import any header with known hash // we never import any header with known hash
let hash = header.hash(); let id = header.compute_id();
if storage.header(&hash).is_some() { if storage.header(&id.hash).is_some() {
return Err(Error::KnownHeader); return Err(Error::KnownHeader);
} }
Ok((hash, finalized_block_hash)) Ok((id, finalized_id))
} }
/// Try accept unsigned aura header into transaction pool. /// Try accept unsigned aura header into transaction pool.
@@ -50,7 +50,7 @@ pub fn accept_aura_header_into_pool<S: Storage>(
receipts: Option<&Vec<Receipt>>, receipts: Option<&Vec<Receipt>>,
) -> Result<(Vec<Vec<u8>>, Vec<Vec<u8>>), Error> { ) -> Result<(Vec<Vec<u8>>, Vec<Vec<u8>>), Error> {
// check if we can verify further // check if we can verify further
let (hash, _) = is_importable_header(storage, header)?; let (header_id, _) = is_importable_header(storage, header)?;
// we can always do contextless checks // we can always do contextless checks
contextless_checks(config, header)?; contextless_checks(config, header)?;
@@ -69,8 +69,8 @@ pub fn accept_aura_header_into_pool<S: Storage>(
// => if we see header with number > maximal ever seen header number + LIMIT, // => if we see header with number > maximal ever seen header number + LIMIT,
// => we consider this transaction invalid, but only at this moment (we do not want to ban it) // => we consider this transaction invalid, but only at this moment (we do not want to ban it)
// => let's mark it as Unknown transaction // => let's mark it as Unknown transaction
let (best_number, best_hash, _) = storage.best_block(); let (best_id, _) = storage.best_block();
let difference = header.number.saturating_sub(best_number); let difference = header.number.saturating_sub(best_id.number);
if difference > pool_config.max_future_number_difference { if difference > pool_config.max_future_number_difference {
return Err(Error::UnsignedTooFarInTheFuture); return Err(Error::UnsignedTooFarInTheFuture);
} }
@@ -87,7 +87,7 @@ pub fn accept_aura_header_into_pool<S: Storage>(
// previous headers here // previous headers here
// => we can at least 'verify' that headers comprise a chain by providing and requiring // => we can at least 'verify' that headers comprise a chain by providing and requiring
// tag (header.number, header.hash) // tag (header.number, header.hash)
let provides_header_number_and_hash_tag = (header.number, hash).encode(); let provides_header_number_and_hash_tag = header_id.encode();
// depending on whether parent header is available, we either perform full or 'shortened' check // depending on whether parent header is available, we either perform full or 'shortened' check
let context = storage.import_context(None, &header.parent_hash); let context = storage.import_context(None, &header.parent_hash);
@@ -109,7 +109,7 @@ pub fn accept_aura_header_into_pool<S: Storage>(
// PoA chain AND that the header is produced either by previous, or next // PoA chain AND that the header is produced either by previous, or next
// scheduled validators set change // scheduled validators set change
let header_step = header.step().ok_or(Error::MissingStep)?; let header_step = header.step().ok_or(Error::MissingStep)?;
let best_context = storage.import_context(None, &best_hash).expect( let best_context = storage.import_context(None, &best_id.hash).expect(
"import context is None only when header is missing from the storage;\ "import context is None only when header is missing from the storage;\
best header is always in the storage; qed", best header is always in the storage; qed",
); );
@@ -124,7 +124,11 @@ pub fn accept_aura_header_into_pool<S: Storage>(
// since our parent is missing from the storage, we **DO** require it // since our parent is missing from the storage, we **DO** require it
// to be in the transaction pool // to be in the transaction pool
// (- 1 can't underflow because there's always best block in the header) // (- 1 can't underflow because there's always best block in the header)
let requires_header_number_and_hash_tag = (header.number - 1, header.parent_hash).encode(); let requires_header_number_and_hash_tag = HeaderId {
number: header.number - 1,
hash: header.parent_hash,
}
.encode();
( (
vec![requires_header_number_and_hash_tag], vec![requires_header_number_and_hash_tag],
vec![provides_number_and_authority_tag, provides_header_number_and_hash_tag], vec![provides_number_and_authority_tag, provides_header_number_and_hash_tag],
@@ -313,7 +317,7 @@ fn find_next_validators_signal<S: Storage>(storage: &S, context: &ImportContext<
// if parent schedules validators set change, then it may be our set // if parent schedules validators set change, then it may be our set
// else we'll start with last known change // else we'll start with last known change
let mut current_set_signal_block = context.last_signal_block().cloned(); let mut current_set_signal_block = context.last_signal_block();
let mut next_scheduled_set: Option<ScheduledChange> = None; let mut next_scheduled_set: Option<ScheduledChange> = None;
loop { loop {
@@ -325,7 +329,7 @@ fn find_next_validators_signal<S: Storage>(storage: &S, context: &ImportContext<
return next_scheduled_set.map(|scheduled_set| scheduled_set.validators) return next_scheduled_set.map(|scheduled_set| scheduled_set.validators)
} }
None => return next_scheduled_set.map(|scheduled_set| scheduled_set.validators), None => return next_scheduled_set.map(|scheduled_set| scheduled_set.validators),
Some(current_set_signal_block) => storage.scheduled_change(&current_set_signal_block).expect( Some(current_set_signal_block) => storage.scheduled_change(&current_set_signal_block.hash).expect(
"header that is associated with this change is not pruned;\ "header that is associated with this change is not pruned;\
scheduled changes are only removed when header is pruned; qed", scheduled changes are only removed when header is pruned; qed",
), ),
@@ -386,12 +390,12 @@ mod tests {
let block1 = block_i(1, &validators); let block1 = block_i(1, &validators);
insert_header(&mut storage, block1); insert_header(&mut storage, block1);
let block2 = block_i(2, &validators); let block2 = block_i(2, &validators);
let block2_hash = block2.hash(); let block2_id = block2.compute_id();
insert_header(&mut storage, block2); insert_header(&mut storage, block2);
let block3 = block_i(3, &validators); let block3 = block_i(3, &validators);
insert_header(&mut storage, block3); insert_header(&mut storage, block3);
FinalizedBlock::put((2, block2_hash)); FinalizedBlock::put(block2_id);
let validators_config = let validators_config =
ValidatorsConfiguration::Single(ValidatorsSource::Contract(Default::default(), Vec::new())); ValidatorsConfiguration::Single(ValidatorsSource::Contract(Default::default(), Vec::new()));
@@ -415,7 +419,10 @@ mod tests {
ValidatorsSet { ValidatorsSet {
validators: finalized_set, validators: finalized_set,
signal_block: None, signal_block: None,
enact_block: HeadersByNumber::get(&0).unwrap()[0].clone(), enact_block: HeaderId {
number: 0,
hash: HeadersByNumber::get(&0).unwrap()[0],
},
}, },
); );
@@ -423,7 +430,10 @@ mod tests {
let mut header = Headers::<TestRuntime>::get(&header_hash).unwrap(); let mut header = Headers::<TestRuntime>::get(&header_hash).unwrap();
header.next_validators_set_id = set_id; header.next_validators_set_id = set_id;
if let Some(signalled_set) = signalled_set { if let Some(signalled_set) = signalled_set {
header.last_signal_block = Some(header.header.parent_hash); header.last_signal_block = Some(HeaderId {
number: header.header.number - 1,
hash: header.header.parent_hash,
});
ScheduledChanges::insert( ScheduledChanges::insert(
header.header.parent_hash, header.header.parent_hash,
ScheduledChange { ScheduledChange {
@@ -554,7 +564,7 @@ mod tests {
assert_eq!(default_verify(&header), Err(Error::MissingParentBlock)); assert_eq!(default_verify(&header), Err(Error::MissingParentBlock));
// when parent is in the storage // when parent is in the storage
header.parent_hash = genesis().hash(); header.parent_hash = genesis().compute_hash();
assert_ne!(default_verify(&header), Err(Error::MissingParentBlock)); assert_ne!(default_verify(&header), Err(Error::MissingParentBlock));
} }
@@ -564,7 +574,7 @@ mod tests {
let mut header = Header { let mut header = Header {
seal: vec![vec![].into(), vec![].into()], seal: vec![vec![].into(), vec![].into()],
gas_limit: test_aura_config().min_gas_limit, gas_limit: test_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().compute_hash(),
..Default::default() ..Default::default()
}; };
assert_eq!(default_verify(&header), Err(Error::MissingStep)); assert_eq!(default_verify(&header), Err(Error::MissingStep));
@@ -601,16 +611,16 @@ mod tests {
seal: vec![ seal: vec![
vec![45].into(), vec![45].into(),
vec![142].into(), vec![142].into(),
SealedEmptyStep::rlp_of(&[sealed_empty_step(&validators, &genesis().hash(), 42)]), SealedEmptyStep::rlp_of(&[sealed_empty_step(&validators, &genesis().compute_hash(), 42)]),
], ],
gas_limit: test_aura_config().min_gas_limit, gas_limit: test_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().compute_hash(),
..Default::default() ..Default::default()
}; };
assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof)); assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof));
// when empty step signature check fails // when empty step signature check fails
let mut wrong_sealed_empty_step = sealed_empty_step(&validators, &genesis().hash(), 43); let mut wrong_sealed_empty_step = sealed_empty_step(&validators, &genesis().compute_hash(), 43);
wrong_sealed_empty_step.signature = Default::default(); wrong_sealed_empty_step.signature = Default::default();
header.seal[2] = SealedEmptyStep::rlp_of(&[wrong_sealed_empty_step]); header.seal[2] = SealedEmptyStep::rlp_of(&[wrong_sealed_empty_step]);
assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof)); assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof));
@@ -618,15 +628,15 @@ mod tests {
// when we are accepting strict empty steps and they come not in order // when we are accepting strict empty steps and they come not in order
config.strict_empty_steps_transition = 0; config.strict_empty_steps_transition = 0;
header.seal[2] = SealedEmptyStep::rlp_of(&[ header.seal[2] = SealedEmptyStep::rlp_of(&[
sealed_empty_step(&validators, &genesis().hash(), 44), sealed_empty_step(&validators, &genesis().compute_hash(), 44),
sealed_empty_step(&validators, &genesis().hash(), 43), sealed_empty_step(&validators, &genesis().compute_hash(), 43),
]); ]);
assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof)); assert_eq!(verify_with_config(&config, &header), Err(Error::InsufficientProof));
// when empty steps are OK // when empty steps are OK
header.seal[2] = SealedEmptyStep::rlp_of(&[ header.seal[2] = SealedEmptyStep::rlp_of(&[
sealed_empty_step(&validators, &genesis().hash(), 43), sealed_empty_step(&validators, &genesis().compute_hash(), 43),
sealed_empty_step(&validators, &genesis().hash(), 44), sealed_empty_step(&validators, &genesis().compute_hash(), 44),
]); ]);
assert_ne!(verify_with_config(&config, &header), Err(Error::InsufficientProof)); assert_ne!(verify_with_config(&config, &header), Err(Error::InsufficientProof));
} }
@@ -640,7 +650,7 @@ mod tests {
let mut header = Header { let mut header = Header {
seal: vec![vec![43].into(), vec![].into()], seal: vec![vec![43].into(), vec![].into()],
gas_limit: test_aura_config().min_gas_limit, gas_limit: test_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().compute_hash(),
..Default::default() ..Default::default()
}; };
assert_eq!(verify_with_config(&config, &header), Err(Error::InvalidDifficulty)); assert_eq!(verify_with_config(&config, &header), Err(Error::InvalidDifficulty));
@@ -659,7 +669,7 @@ mod tests {
author: validators[1].address().as_fixed_bytes().into(), author: validators[1].address().as_fixed_bytes().into(),
seal: vec![vec![43].into(), vec![].into()], seal: vec![vec![43].into(), vec![].into()],
gas_limit: test_aura_config().min_gas_limit, gas_limit: test_aura_config().min_gas_limit,
parent_hash: genesis().hash(), parent_hash: genesis().compute_hash(),
..Default::default() ..Default::default()
}, },
43, 43,
@@ -787,7 +797,7 @@ mod tests {
assert_eq!( assert_eq!(
default_accept_into_pool(|validators| { default_accept_into_pool(|validators| {
let header = block_i(4, &validators); let header = block_i(4, &validators);
hash = Some(header.hash()); hash = Some(header.compute_hash());
(header, None) (header, None)
}), }),
Ok(( Ok((
@@ -819,7 +829,7 @@ mod tests {
}, },
47, 47,
); );
hash = Some(header.hash()); hash = Some(header.compute_hash());
(header, None) (header, None)
}), }),
Ok(( Ok((
@@ -879,7 +889,7 @@ mod tests {
}, },
47, 47,
); );
hash = Some(header.hash()); hash = Some(header.compute_hash());
(header, None) (header, None)
}), }),
@@ -919,7 +929,7 @@ mod tests {
.parse() .parse()
.unwrap(); .unwrap();
}); });
hash = Some(header.hash()); hash = Some(header.compute_hash());
(header, Some(vec![validators_change_recept(Default::default())])) (header, Some(vec![validators_change_recept(Default::default())]))
}), }),
Ok(( Ok((
+20 -3
View File
@@ -49,6 +49,15 @@ pub type RawTransaction = Vec<u8>;
/// An ethereum address. /// An ethereum address.
pub type Address = H160; pub type Address = H160;
/// Complete header id.
#[derive(Encode, Decode, Default, RuntimeDebug, PartialEq, Clone, Copy)]
pub struct HeaderId {
/// Header number.
pub number: u64,
/// Header hash.
pub hash: H256,
}
/// An Aura header. /// An Aura header.
#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)] #[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Default, Serialize, Deserialize))] #[cfg_attr(feature = "std", derive(Default, Serialize, Deserialize))]
@@ -156,8 +165,16 @@ pub struct SealedEmptyStep {
} }
impl Header { impl Header {
/// Get the hash of this header (keccak of the RLP with seal). /// Compute id of this header.
pub fn hash(&self) -> H256 { pub fn compute_id(&self) -> HeaderId {
HeaderId {
number: self.number,
hash: self.compute_hash(),
}
}
/// Compute hash of this header (keccak of the RLP with seal).
pub fn compute_hash(&self) -> H256 {
keccak_256(&self.rlp(true)).into() keccak_256(&self.rlp(true)).into()
} }
@@ -175,7 +192,7 @@ impl Header {
pub fn seal_hash(&self, include_empty_steps: bool) -> Option<H256> { pub fn seal_hash(&self, include_empty_steps: bool) -> Option<H256> {
Some(match include_empty_steps { Some(match include_empty_steps {
true => { true => {
let mut message = self.hash().as_bytes().to_vec(); let mut message = self.compute_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()
} }