Skip to content

Commit

Permalink
More robust grandpa revert procedure (paritytech#11719)
Browse files Browse the repository at this point in the history
* More robust revert procedure

Return an error if revert is called in a node that is not actively
running grandpa, i.e. grandpa genesis data has not been initialized.
Previous implementation was just firing an `unreachable!` code exception.

Furthermore we skip revert hassle if there is nothing to revert.

* Nit
  • Loading branch information
davxy authored and ark0f committed Feb 27, 2023
1 parent 6d7cd02 commit 41dbeae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,11 @@ where
{
let best_number = client.info().best_number;
let finalized = client.info().finalized_number;

let revertible = blocks.min(best_number - finalized);
if revertible == Zero::zero() {
return Ok(())
}

let revert_up_to_number = best_number - revertible;
let revert_up_to_hash = client.hash(revert_up_to_number)?.ok_or(ClientError::Backend(
Expand Down
12 changes: 10 additions & 2 deletions client/finality-grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,15 @@ fn local_authority_id(
pub fn revert<Block, Client>(client: Arc<Client>, blocks: NumberFor<Block>) -> ClientResult<()>
where
Block: BlockT,
Client: AuxStore + HeaderMetadata<Block, Error = sp_blockchain::Error> + HeaderBackend<Block>,
Client: AuxStore + HeaderMetadata<Block, Error = ClientError> + HeaderBackend<Block>,
{
let best_number = client.info().best_number;
let finalized = client.info().finalized_number;

let revertible = blocks.min(best_number - finalized);
if revertible == Zero::zero() {
return Ok(())
}

let number = best_number - revertible;
let hash = client
Expand All @@ -1185,8 +1189,12 @@ where
)))?;

let info = client.info();

let persistent_data: PersistentData<Block> =
aux_schema::load_persistent(&*client, info.genesis_hash, Zero::zero(), || unreachable!())?;
aux_schema::load_persistent(&*client, info.genesis_hash, Zero::zero(), || {
const MSG: &str = "Unexpected missing grandpa data during revert";
Err(ClientError::Application(Box::from(MSG)))
})?;

let shared_authority_set = persistent_data.authority_set;
let mut authority_set = shared_authority_set.inner();
Expand Down

0 comments on commit 41dbeae

Please sign in to comment.