mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 06:21:11 +00:00
Revert approval voting (#5438)
* Revert approval-voting subsystem * Approval voting revert encapsulated within 'ops' module * use 'get_stored_blocks' to get lower block height * Fix error message * Optionally shrink/delete stored blocks range * range end number is last block number plus 1 * Apply code review suggestions * Use tristate enum for block range in backend overlay * Add clarification comment * Add comments to private struct
This commit is contained in:
@@ -37,7 +37,9 @@ use {
|
||||
beefy_gadget::notification::{BeefyBestBlockSender, BeefySignedCommitmentSender},
|
||||
grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider},
|
||||
gum::info,
|
||||
polkadot_node_core_approval_voting::Config as ApprovalVotingConfig,
|
||||
polkadot_node_core_approval_voting::{
|
||||
self as approval_voting_subsystem, Config as ApprovalVotingConfig,
|
||||
},
|
||||
polkadot_node_core_av_store::Config as AvailabilityConfig,
|
||||
polkadot_node_core_av_store::Error as AvailabilityError,
|
||||
polkadot_node_core_candidate_validation::Config as CandidateValidationConfig,
|
||||
@@ -1429,6 +1431,78 @@ pub fn build_full(
|
||||
Err(Error::NoRuntime)
|
||||
}
|
||||
|
||||
/// Reverts the node state down to at most the last finalized block.
|
||||
///
|
||||
/// In particular this reverts:
|
||||
/// - `ApprovalVotingSubsystem` data in the parachains-db;
|
||||
/// - `ChainSelectionSubsystem` data in the parachains-db;
|
||||
/// - Low level Babe and Grandpa consensus data.
|
||||
#[cfg(feature = "full-node")]
|
||||
pub fn revert_backend(
|
||||
client: Arc<Client>,
|
||||
backend: Arc<FullBackend>,
|
||||
blocks: BlockNumber,
|
||||
config: Configuration,
|
||||
) -> Result<(), Error> {
|
||||
let best_number = client.info().best_number;
|
||||
let finalized = client.info().finalized_number;
|
||||
let revertible = blocks.min(best_number - finalized);
|
||||
|
||||
if revertible == 0 {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
let number = best_number - revertible;
|
||||
let hash = client.block_hash_from_id(&BlockId::Number(number))?.ok_or(
|
||||
sp_blockchain::Error::Backend(format!(
|
||||
"Unexpected hash lookup failure for block number: {}",
|
||||
number
|
||||
)),
|
||||
)?;
|
||||
|
||||
let parachains_db = open_database(&config.database)
|
||||
.map_err(|err| sp_blockchain::Error::Backend(err.to_string()))?;
|
||||
|
||||
revert_approval_voting(parachains_db.clone(), hash)?;
|
||||
revert_chain_selection(parachains_db, hash)?;
|
||||
// Revert Substrate consensus related components
|
||||
client.execute_with(RevertConsensus { blocks, backend })?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn revert_chain_selection(db: Arc<dyn Database>, hash: Hash) -> sp_blockchain::Result<()> {
|
||||
let config = chain_selection_subsystem::Config {
|
||||
col_data: parachains_db::REAL_COLUMNS.col_chain_selection_data,
|
||||
stagnant_check_interval: chain_selection_subsystem::StagnantCheckInterval::never(),
|
||||
};
|
||||
|
||||
let chain_selection = chain_selection_subsystem::ChainSelectionSubsystem::new(config, db);
|
||||
|
||||
chain_selection
|
||||
.revert_to(hash)
|
||||
.map_err(|err| sp_blockchain::Error::Backend(err.to_string()))
|
||||
}
|
||||
|
||||
fn revert_approval_voting(db: Arc<dyn Database>, hash: Hash) -> sp_blockchain::Result<()> {
|
||||
let config = approval_voting_subsystem::Config {
|
||||
col_data: parachains_db::REAL_COLUMNS.col_approval_data,
|
||||
slot_duration_millis: Default::default(),
|
||||
};
|
||||
|
||||
let approval_voting = approval_voting_subsystem::ApprovalVotingSubsystem::with_config(
|
||||
config,
|
||||
db,
|
||||
Arc::new(sc_keystore::LocalKeystore::in_memory()),
|
||||
Box::new(consensus_common::NoNetwork),
|
||||
approval_voting_subsystem::Metrics::default(),
|
||||
);
|
||||
|
||||
approval_voting
|
||||
.revert_to(hash)
|
||||
.map_err(|err| sp_blockchain::Error::Backend(err.to_string()))
|
||||
}
|
||||
|
||||
struct RevertConsensus {
|
||||
blocks: BlockNumber,
|
||||
backend: Arc<FullBackend>,
|
||||
@@ -1445,52 +1519,10 @@ impl ExecuteWithClient for RevertConsensus {
|
||||
Api: polkadot_client::RuntimeApiCollection<StateBackend = Backend::State>,
|
||||
Client: AbstractClient<Block, Backend, Api = Api> + 'static,
|
||||
{
|
||||
// Revert consensus-related components.
|
||||
// The operations are not correlated, thus call order is not relevant.
|
||||
babe::revert(client.clone(), self.backend, self.blocks)?;
|
||||
grandpa::revert(client, self.blocks)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Reverts the node state down to at most the last finalized block.
|
||||
///
|
||||
/// In particular this reverts:
|
||||
/// - `ChainSelectionSubsystem` data in the parachains-db.
|
||||
/// - Low level Babe and Grandpa consensus data.
|
||||
#[cfg(feature = "full-node")]
|
||||
pub fn revert_backend(
|
||||
client: Arc<Client>,
|
||||
backend: Arc<FullBackend>,
|
||||
blocks: BlockNumber,
|
||||
config: Configuration,
|
||||
) -> Result<(), Error> {
|
||||
let best_number = client.info().best_number;
|
||||
let finalized = client.info().finalized_number;
|
||||
let revertible = blocks.min(best_number - finalized);
|
||||
|
||||
let number = best_number - revertible;
|
||||
let hash = client.block_hash_from_id(&BlockId::Number(number))?.ok_or(
|
||||
sp_blockchain::Error::Backend(format!(
|
||||
"Unexpected hash lookup failure for block number: {}",
|
||||
number
|
||||
)),
|
||||
)?;
|
||||
|
||||
let parachains_db = open_database(&config.database)
|
||||
.map_err(|err| sp_blockchain::Error::Backend(err.to_string()))?;
|
||||
|
||||
let config = chain_selection_subsystem::Config {
|
||||
col_data: parachains_db::REAL_COLUMNS.col_chain_selection_data,
|
||||
stagnant_check_interval: chain_selection_subsystem::StagnantCheckInterval::never(),
|
||||
};
|
||||
|
||||
let chain_selection =
|
||||
chain_selection_subsystem::ChainSelectionSubsystem::new(config, parachains_db);
|
||||
|
||||
chain_selection
|
||||
.revert(hash)
|
||||
.map_err(|err| sp_blockchain::Error::Backend(err.to_string()))?;
|
||||
|
||||
client.execute_with(RevertConsensus { blocks, backend })?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user