Fixed uncle pruning (#3491)

* Fixed uncle pruning

* Version bump
This commit is contained in:
Arkadiy Paronyan
2019-08-27 14:07:43 +02:00
committed by Bastian Köcher
parent 10b032bb0d
commit cab8fb5dc6
2 changed files with 29 additions and 26 deletions
+2 -2
View File
@@ -79,8 +79,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime // and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as // implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version. // is and increment impl_version.
spec_version: 152, spec_version: 153,
impl_version: 152, impl_version: 153,
apis: RUNTIME_API_VERSIONS, apis: RUNTIME_API_VERSIONS,
}; };
+27 -24
View File
@@ -27,7 +27,7 @@ use srml_support::traits::{FindAuthor, VerifySeal, Get};
use srml_support::dispatch::Result as DispatchResult; use srml_support::dispatch::Result as DispatchResult;
use codec::{Encode, Decode}; use codec::{Encode, Decode};
use system::ensure_none; use system::ensure_none;
use sr_primitives::traits::{SimpleArithmetic, Header as HeaderT, One, Zero}; use sr_primitives::traits::{Header as HeaderT, One, Zero};
use sr_primitives::weights::SimpleDispatchInfo; use sr_primitives::weights::SimpleDispatchInfo;
use inherents::{ use inherents::{
RuntimeString, InherentIdentifier, ProvideInherent, RuntimeString, InherentIdentifier, ProvideInherent,
@@ -236,29 +236,14 @@ decl_storage! {
} }
} }
fn prune_old_uncles<BlockNumber, Hash, Author>(
minimum_height: BlockNumber,
uncles: &mut Vec<UncleEntryItem<BlockNumber, Hash, Author>>
) where BlockNumber: SimpleArithmetic {
let prune_entries = uncles.iter().take_while(|item| match item {
UncleEntryItem::Uncle(_, _) => true,
UncleEntryItem::InclusionHeight(height) => height < &minimum_height,
});
let prune_index = prune_entries.count();
let _ = uncles.drain(..prune_index);
}
decl_module! { decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin { pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn on_initialize(now: T::BlockNumber) { fn on_initialize(now: T::BlockNumber) {
let uncle_generations = T::UncleGenerations::get(); let uncle_generations = T::UncleGenerations::get();
let mut uncles = <Self as Store>::Uncles::get();
// prune uncles that are older than the allowed number of generations. // prune uncles that are older than the allowed number of generations.
if uncle_generations <= now { if uncle_generations <= now {
let minimum_height = now - uncle_generations; let minimum_height = now - uncle_generations;
prune_old_uncles(minimum_height, &mut uncles) Self::prune_old_uncles(minimum_height)
} }
<Self as Store>::DidSetUncles::put(false); <Self as Store>::DidSetUncles::put(false);
@@ -387,6 +372,18 @@ impl<T: Trait> Module<T> {
// check uncle validity. // check uncle validity.
T::FilterUncle::filter_uncle(&uncle, accumulator) T::FilterUncle::filter_uncle(&uncle, accumulator)
} }
fn prune_old_uncles(minimum_height: T::BlockNumber) {
let mut uncles = <Self as Store>::Uncles::get();
let prune_entries = uncles.iter().take_while(|item| match item {
UncleEntryItem::Uncle(_, _) => true,
UncleEntryItem::InclusionHeight(height) => height < &minimum_height,
});
let prune_index = prune_entries.count();
let _ = uncles.drain(..prune_index);
<Self as Store>::Uncles::put(uncles);
}
} }
impl<T: Trait> ProvideInherent for Module<T> { impl<T: Trait> ProvideInherent for Module<T> {
@@ -569,15 +566,21 @@ mod tests {
#[test] #[test]
fn prune_old_uncles_works() { fn prune_old_uncles_works() {
use UncleEntryItem::*; use UncleEntryItem::*;
let mut uncles = vec![ with_externalities(&mut new_test_ext(), || {
InclusionHeight(1u32), Uncle((), Some(())), Uncle((), None), Uncle((), None), let hash = Default::default();
InclusionHeight(2u32), Uncle((), None), let author = Default::default();
InclusionHeight(3u32), Uncle((), None), let uncles = vec![
]; InclusionHeight(1u64), Uncle(hash, Some(author)), Uncle(hash, None), Uncle(hash, None),
InclusionHeight(2u64), Uncle(hash, None),
InclusionHeight(3u64), Uncle(hash, None),
];
prune_old_uncles(3, &mut uncles); <Authorship as Store>::Uncles::put(uncles);
Authorship::prune_old_uncles(3);
assert_eq!(uncles, vec![InclusionHeight(3), Uncle((), None)]); let uncles = <Authorship as Store>::Uncles::get();
assert_eq!(uncles, vec![InclusionHeight(3u64), Uncle(hash, None)]);
})
} }
#[test] #[test]