Unverified Commit 5898cafc authored by André Silva's avatar André Silva Committed by GitHub
Browse files

node: migrate grandpa voting rule to async api (#2422)



* node: migrate grandpa voting rule to async api

* node: fix grandpa voting rule tests

* "Update Substrate"

* update Substrate

Co-authored-by: parity-processbot <>
Co-authored-by: asynchronous rob's avatarRobert Habermeier <rphmeier@gmail.com>
parent d376fe66
Pipeline #124217 passed with stages
in 33 minutes and 52 seconds
This diff is collapsed.
......@@ -16,6 +16,8 @@
//! Polkadot-specific GRANDPA integration utilities.
use std::sync::Arc;
#[cfg(feature = "full-node")]
use polkadot_primitives::v1::Hash;
use sp_runtime::traits::{Block as BlockT, NumberFor};
......@@ -24,6 +26,7 @@ use sp_runtime::traits::{Block as BlockT, NumberFor};
/// same last finalized block) after a given block at height `N` has been
/// finalized and for a delay of `M` blocks, i.e. until the best block reaches
/// `N` + `M`, the voter will keep voting for block `N`.
#[derive(Clone)]
pub(crate) struct PauseAfterBlockFor<N>(pub(crate) N, pub(crate) N);
impl<Block, B> grandpa::VotingRule<Block, B> for PauseAfterBlockFor<NumberFor<Block>>
......@@ -33,11 +36,12 @@ where
{
fn restrict_vote(
&self,
backend: &B,
backend: Arc<B>,
base: &Block::Header,
best_target: &Block::Header,
current_target: &Block::Header,
) -> Option<(Block::Hash, NumberFor<Block>)> {
) -> grandpa::VotingRuleResult<Block> {
let aux = || {
use sp_runtime::generic::BlockId;
use sp_runtime::traits::Header as _;
......@@ -60,8 +64,9 @@ where
}
target_hash = *target_header.parent_hash();
target_header = backend.header(BlockId::Hash(target_hash)).ok()?
.expect("Header known to exist due to the existence of one of its descendents; qed");
target_header = backend.header(BlockId::Hash(target_hash)).ok()?.expect(
"Header known to exist due to the existence of one of its descendents; qed",
);
}
};
......@@ -86,6 +91,11 @@ where
}
None
};
let target = aux();
Box::pin(async move { target })
}
}
......@@ -276,7 +286,12 @@ mod tests {
// we have not reached the pause block
// therefore nothing should be restricted
assert_eq!(
voting_rule.restrict_vote(&*client, &get_header(0), &get_header(10), &get_header(10)),
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&get_header(0),
&get_header(10),
&get_header(10)
)),
None,
);
......@@ -287,7 +302,12 @@ mod tests {
// we are targeting the pause block,
// the vote should not be restricted
assert_eq!(
voting_rule.restrict_vote(&*client, &get_header(10), &get_header(20), &get_header(20)),
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&get_header(10),
&get_header(20),
&get_header(20)
)),
None,
);
......@@ -295,19 +315,24 @@ mod tests {
// be limited to the pause block.
let pause_block = get_header(20);
assert_eq!(
voting_rule.restrict_vote(&*client, &get_header(10), &get_header(21), &get_header(21)),
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&get_header(10),
&get_header(21),
&get_header(21)
)),
Some((pause_block.hash(), *pause_block.number())),
);
// we've finalized the pause block, so we'll keep
// restricting our votes to it.
assert_eq!(
voting_rule.restrict_vote(
&*client,
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&pause_block, // #20
&get_header(21),
&get_header(21),
),
)),
Some((pause_block.hash(), *pause_block.number())),
);
......@@ -318,23 +343,23 @@ mod tests {
// we're at the last block of the pause, this block
// should still be considered in the pause period
assert_eq!(
voting_rule.restrict_vote(
&*client,
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&pause_block, // #20
&get_header(50),
&get_header(50),
),
)),
Some((pause_block.hash(), *pause_block.number())),
);
// we're past the pause period, no votes should be filtered
assert_eq!(
voting_rule.restrict_vote(
&*client,
futures::executor::block_on(voting_rule.restrict_vote(
client.clone(),
&pause_block, // #20
&get_header(51),
&get_header(51),
),
)),
None,
);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment