Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A0-1304: add state pruning compatibility #890

Merged
merged 7 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 54 additions & 15 deletions bin/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ use aleph_node::{new_authority, new_full, new_partial, Cli, Subcommand};
#[cfg(feature = "try-runtime")]
use aleph_runtime::Block;
use log::warn;
use sc_cli::{clap::Parser, SubstrateCli};
use sc_cli::{clap::Parser, CliConfiguration, PruningParams, SubstrateCli};
use sc_network::config::Role;
use sc_service::PartialComponents;

const STATE_PRUNING: &str = "archive";
const BLOCKS_PRUNING: &str = "archive-canonical";

fn pruning_changed(params: &PruningParams) -> bool {
let state_pruning_changed =
params.state_pruning != Some(STATE_PRUNING.into()) && params.state_pruning.is_some();

let blocks_pruning_changed =
params.blocks_pruning != Some(BLOCKS_PRUNING.into()) && params.blocks_pruning.is_some();

state_pruning_changed || blocks_pruning_changed
}

fn main() -> sc_cli::Result<()> {
let mut cli = Cli::parse();
let overwritten_pruning = pruning_changed(&cli.run.import_params.pruning_params);
if !cli.aleph.experimental_pruning() {
if cli
.run
.import_params
.pruning_params
.blocks_pruning
.is_some()
|| cli.run.import_params.pruning_params.state_pruning != Some("archive".into())
{
warn!("Pruning not supported. Switching to keeping all block bodies and states.");
cli.run.import_params.pruning_params.blocks_pruning = None;
cli.run.import_params.pruning_params.state_pruning = Some("archive".into());
}
} else {
warn!("Pruning not supported, but flag experimental_pruning was turned on. Usage of this flag can lead to misbehaviour, which can be punished.");
cli.run.import_params.pruning_params.state_pruning = Some(STATE_PRUNING.into());
cli.run.import_params.pruning_params.blocks_pruning = Some(BLOCKS_PRUNING.into());
}

match &cli.subcommand {
Expand Down Expand Up @@ -112,6 +114,15 @@ fn main() -> sc_cli::Result<()> {
.into()),
None => {
let runner = cli.create_runner(&cli.run)?;
if cli.aleph.experimental_pruning() {
warn!("Experimental_pruning was turned on. Usage of this flag can lead to misbehaviour, which can be punished. State pruning: {:?}; Blocks pruning: {:?};",
timorl marked this conversation as resolved.
Show resolved Hide resolved
cli.run.state_pruning()?.unwrap_or_default(),
cli.run.blocks_pruning()?,
);
} else if overwritten_pruning {
warn!("Pruning not supported. Switching to keeping all block bodies and states.");
}

let aleph_cli_config = cli.aleph;
runner.run_node_until_exit(|config| async move {
match config.role {
Expand All @@ -126,3 +137,31 @@ fn main() -> sc_cli::Result<()> {
}
}
}

#[cfg(test)]
mod tests {
use sc_service::{BlocksPruning, PruningMode};

use super::{PruningParams, BLOCKS_PRUNING, STATE_PRUNING};

#[test]
fn pruning_sanity_check() {
let state_pruning = Some(String::from(STATE_PRUNING));
let blocks_pruning = Some(String::from(BLOCKS_PRUNING));

let pruning_params = PruningParams {
state_pruning,
blocks_pruning,
};

assert_eq!(
pruning_params.blocks_pruning().unwrap(),
BlocksPruning::KeepFinalized
);

assert_eq!(
pruning_params.state_pruning().unwrap().unwrap(),
PruningMode::ArchiveAll
);
}
}
14 changes: 9 additions & 5 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_blockchain::Backend as _;
use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, Zero},
traits::{Block as BlockT, Header as HeaderT},
};

use crate::{aleph_cli::AlephCli, chain_spec::DEFAULT_BACKUP_FOLDER, executor::AlephExecutor};
Expand Down Expand Up @@ -311,17 +311,19 @@ pub fn new_authority(
.path(),
);

let finalized = client.info().finalized_number;

let session_period = SessionPeriod(
client
.runtime_api()
.session_period(&BlockId::Number(Zero::zero()))
.session_period(&BlockId::Number(finalized))
.unwrap(),
);

let millisecs_per_block = MillisecsPerBlock(
client
.runtime_api()
.millisecs_per_block(&BlockId::Number(Zero::zero()))
.millisecs_per_block(&BlockId::Number(finalized))
.unwrap(),
);

Expand Down Expand Up @@ -452,17 +454,19 @@ pub fn new_full(
justification_tx,
)?;

let finalized = client.info().finalized_number;

let session_period = SessionPeriod(
client
.runtime_api()
.session_period(&BlockId::Number(Zero::zero()))
.session_period(&BlockId::Number(finalized))
.unwrap(),
);

let millisecs_per_block = MillisecsPerBlock(
client
.runtime_api()
.millisecs_per_block(&BlockId::Number(Zero::zero()))
.millisecs_per_block(&BlockId::Number(finalized))
.unwrap(),
);

Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/nodes/nonvalidator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ where
let map_updater = SessionMapUpdater::<_, _, B>::new(
AuthorityProviderImpl::new(client.clone()),
FinalityNotificatorImpl::new(client.clone()),
session_period,
);
let session_authorities = map_updater.readonly_session_map();
spawn_handle.spawn("aleph/updater", None, async move {
debug!(target: "aleph-party", "SessionMapUpdater has started.");
map_updater.run(session_period).await
map_updater.run().await
});
let (_, handler_task) = setup_justification_handler(JustificationParams {
justification_rx,
Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/nodes/validator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ where
let map_updater = SessionMapUpdater::<_, _, B>::new(
AuthorityProviderImpl::new(client.clone()),
FinalityNotificatorImpl::new(client.clone()),
session_period,
);
let session_authorities = map_updater.readonly_session_map();
spawn_handle.spawn("aleph/updater", None, async move {
debug!(target: "aleph-party", "SessionMapUpdater has started.");
map_updater.run(session_period).await
map_updater.run().await
});

let (authority_justification_tx, handler_task) =
Expand Down
Loading