mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 19:51:02 +00:00
Fix handling of justifications (#2086)
* util: fork-tree: check predicate first while traversing tree * core: sync: keep track of justifications sent to the import queue * core: grandpa: verify authority set changes dependencies * core: fork-tree: add more tests * core: grandpa: extend enacts_standard_change tests
This commit is contained in:
committed by
Arkadiy Paronyan
parent
7469713dea
commit
25ec793e35
@@ -351,14 +351,18 @@ where
|
||||
/// authority set change (without triggering it), ensuring that if there are
|
||||
/// multiple changes in the same branch, finalizing this block won't
|
||||
/// finalize past multiple transitions (i.e. transitions must be finalized
|
||||
/// in-order). The given function `is_descendent_of` should return `true` if
|
||||
/// the second hash (target) is a descendent of the first hash (base).
|
||||
/// in-order). Returns `Some(true)` if the block being finalized enacts a
|
||||
/// change that can be immediately applied, `Some(false)` if the block being
|
||||
/// finalized enacts a change but it cannot be applied yet since there are
|
||||
/// other dependent changes, and `None` if no change is enacted. The given
|
||||
/// function `is_descendent_of` should return `true` if the second hash
|
||||
/// (target) is a descendent of the first hash (base).
|
||||
pub fn enacts_standard_change<F, E>(
|
||||
&self,
|
||||
finalized_hash: H,
|
||||
finalized_number: N,
|
||||
is_descendent_of: &F,
|
||||
) -> Result<bool, fork_tree::Error<E>>
|
||||
) -> Result<Option<bool>, fork_tree::Error<E>>
|
||||
where F: Fn(&H, &H) -> Result<bool, E>,
|
||||
E: std::error::Error,
|
||||
{
|
||||
@@ -659,21 +663,51 @@ mod tests {
|
||||
delay_kind: DelayKind::Finalized,
|
||||
};
|
||||
|
||||
let change_b = PendingChange {
|
||||
next_authorities: set_a.clone(),
|
||||
delay: 10,
|
||||
canon_height: 20,
|
||||
canon_hash: "hash_b",
|
||||
delay_kind: DelayKind::Finalized,
|
||||
};
|
||||
|
||||
authorities.add_pending_change(change_a.clone(), &static_is_descendent_of(false)).unwrap();
|
||||
authorities.add_pending_change(change_b.clone(), &static_is_descendent_of(true)).unwrap();
|
||||
|
||||
let is_descendent_of = is_descendent_of(|base, hash| match (*base, *hash) {
|
||||
("hash_a", "hash_b") => true,
|
||||
("hash_a", "hash_d") => true,
|
||||
("hash_a", "hash_e") => true,
|
||||
("hash_b", "hash_d") => true,
|
||||
("hash_b", "hash_e") => true,
|
||||
("hash_a", "hash_c") => false,
|
||||
("hash_b", "hash_c") => false,
|
||||
_ => unreachable!(),
|
||||
});
|
||||
|
||||
// "hash_c" won't finalize the existing change since it isn't a descendent
|
||||
assert!(!authorities.enacts_standard_change("hash_c", 15, &is_descendent_of).unwrap());
|
||||
// "hash_b" at depth 14 won't work either
|
||||
assert!(!authorities.enacts_standard_change("hash_b", 14, &is_descendent_of).unwrap());
|
||||
assert_eq!(
|
||||
authorities.enacts_standard_change("hash_c", 15, &is_descendent_of).unwrap(),
|
||||
None,
|
||||
);
|
||||
|
||||
// "hash_d" at depth 14 won't work either
|
||||
assert_eq!(
|
||||
authorities.enacts_standard_change("hash_d", 14, &is_descendent_of).unwrap(),
|
||||
None,
|
||||
);
|
||||
|
||||
// but it should work at depth 15 (change height + depth)
|
||||
assert!(authorities.enacts_standard_change("hash_b", 15, &is_descendent_of).unwrap());
|
||||
assert_eq!(
|
||||
authorities.enacts_standard_change("hash_d", 15, &is_descendent_of).unwrap(),
|
||||
Some(true),
|
||||
);
|
||||
|
||||
// finalizing "hash_e" at depth 20 will trigger change at "hash_b", but
|
||||
// it can't be applied yet since "hash_a" must be applied first
|
||||
assert_eq!(
|
||||
authorities.enacts_standard_change("hash_e", 30, &is_descendent_of).unwrap(),
|
||||
Some(false),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -709,7 +743,10 @@ mod tests {
|
||||
|
||||
// there's an effective change triggered at block 15 but not a standard one.
|
||||
// so this should do nothing.
|
||||
assert!(!authorities.enacts_standard_change("hash_c", 15, &static_is_descendent_of(true)).unwrap());
|
||||
assert_eq!(
|
||||
authorities.enacts_standard_change("hash_c", 15, &static_is_descendent_of(true)).unwrap(),
|
||||
None,
|
||||
);
|
||||
|
||||
// throw a standard change into the mix to prove that it's discarded
|
||||
// for being on the same fork.
|
||||
|
||||
@@ -116,7 +116,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> JustificationImport<Block>
|
||||
}
|
||||
|
||||
enum AppliedChanges<H, N> {
|
||||
Standard,
|
||||
Standard(bool), // true if the change is ready to be applied (i.e. it's a root)
|
||||
Forced(NewAuthoritySet<H, N>),
|
||||
None,
|
||||
}
|
||||
@@ -124,7 +124,7 @@ enum AppliedChanges<H, N> {
|
||||
impl<H, N> AppliedChanges<H, N> {
|
||||
fn needs_justification(&self) -> bool {
|
||||
match *self {
|
||||
AppliedChanges::Standard => true,
|
||||
AppliedChanges::Standard(_) => true,
|
||||
AppliedChanges::Forced(_) | AppliedChanges::None => false,
|
||||
}
|
||||
}
|
||||
@@ -345,8 +345,8 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> GrandpaBlockImport<B, E, Block, RA
|
||||
.map_err(|e| ConsensusErrorKind::ClientImport(e.to_string()))
|
||||
.map_err(ConsensusError::from)?;
|
||||
|
||||
if did_standard {
|
||||
AppliedChanges::Standard
|
||||
if let Some(root) = did_standard {
|
||||
AppliedChanges::Standard(root)
|
||||
} else {
|
||||
AppliedChanges::None
|
||||
}
|
||||
@@ -358,7 +358,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> GrandpaBlockImport<B, E, Block, RA
|
||||
if let Some((_, ref authorities)) = just_in_case {
|
||||
let authorities_change = match applied_changes {
|
||||
AppliedChanges::Forced(ref new) => Some(new),
|
||||
AppliedChanges::Standard => None, // the change isn't actually applied yet.
|
||||
AppliedChanges::Standard(_) => None, // the change isn't actually applied yet.
|
||||
AppliedChanges::None => None,
|
||||
};
|
||||
|
||||
@@ -405,7 +405,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> BlockImport<Block>
|
||||
let pending_changes = self.make_authorities_changes(&mut block, hash)?;
|
||||
|
||||
// we don't want to finalize on `inner.import_block`
|
||||
let justification = block.justification.take();
|
||||
let mut justification = block.justification.take();
|
||||
let enacts_consensus_change = new_authorities.is_some();
|
||||
let import_result = self.inner.import_block(block, new_authorities);
|
||||
|
||||
@@ -435,23 +435,34 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> BlockImport<Block>
|
||||
}
|
||||
|
||||
let needs_justification = applied_changes.needs_justification();
|
||||
if let AppliedChanges::Forced(new) = applied_changes {
|
||||
// NOTE: when we do a force change we are "discrediting" the old set so we
|
||||
// ignore any justifications from them. this block may contain a justification
|
||||
// which should be checked and imported below against the new authority
|
||||
// triggered by this forced change. the new grandpa voter will start at the
|
||||
// last median finalized block (which is before the block that enacts the
|
||||
// change), full nodes syncing the chain will not be able to successfully
|
||||
// import justifications for those blocks since their local authority set view
|
||||
// is still of the set before the forced change was enacted, still after #1867
|
||||
// they should import the block and discard the justification, and they will
|
||||
// then request a justification from sync if it's necessary (which they should
|
||||
// then be able to successfully validate).
|
||||
let _ = self.send_voter_commands.unbounded_send(VoterCommand::ChangeAuthorities(new));
|
||||
|
||||
// we must clear all pending justifications requests, presumably they won't be
|
||||
// finalized hence why this forced changes was triggered
|
||||
imported_aux.clear_justification_requests = true;
|
||||
match applied_changes {
|
||||
AppliedChanges::Forced(new) => {
|
||||
// NOTE: when we do a force change we are "discrediting" the old set so we
|
||||
// ignore any justifications from them. this block may contain a justification
|
||||
// which should be checked and imported below against the new authority
|
||||
// triggered by this forced change. the new grandpa voter will start at the
|
||||
// last median finalized block (which is before the block that enacts the
|
||||
// change), full nodes syncing the chain will not be able to successfully
|
||||
// import justifications for those blocks since their local authority set view
|
||||
// is still of the set before the forced change was enacted, still after #1867
|
||||
// they should import the block and discard the justification, and they will
|
||||
// then request a justification from sync if it's necessary (which they should
|
||||
// then be able to successfully validate).
|
||||
let _ = self.send_voter_commands.unbounded_send(VoterCommand::ChangeAuthorities(new));
|
||||
|
||||
// we must clear all pending justifications requests, presumably they won't be
|
||||
// finalized hence why this forced changes was triggered
|
||||
imported_aux.clear_justification_requests = true;
|
||||
},
|
||||
AppliedChanges::Standard(false) => {
|
||||
// we can't apply this change yet since there are other dependent changes that we
|
||||
// need to apply first, drop any justification that might have been provided with
|
||||
// the block to make sure we request them from `sync` which will ensure they'll be
|
||||
// applied in-order.
|
||||
justification.take();
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
if !needs_justification && !enacts_consensus_change {
|
||||
@@ -481,6 +492,7 @@ impl<B, E, Block: BlockT<Hash=H256>, RA, PRA> BlockImport<Block>
|
||||
if enacts_consensus_change {
|
||||
self.consensus_changes.lock().note_change((number, hash));
|
||||
}
|
||||
|
||||
imported_aux.needs_justification = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user