Skip to content

Commit c3a93ab

Browse files
authored
A0-1304: add state pruning compatibility (#890)
* make aleph node compatible with state pruning * add/fix session map tests * fix experimental pruning logs * remove unnecessary wrapping
1 parent 91d2eea commit c3a93ab

File tree

6 files changed

+308
-156
lines changed

6 files changed

+308
-156
lines changed

bin/node/src/main.rs

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ use aleph_node::{new_authority, new_full, new_partial, Cli, Subcommand};
44
#[cfg(feature = "try-runtime")]
55
use aleph_runtime::Block;
66
use log::warn;
7-
use sc_cli::{clap::Parser, SubstrateCli};
7+
use sc_cli::{clap::Parser, CliConfiguration, PruningParams, SubstrateCli};
88
use sc_network::config::Role;
99
use sc_service::PartialComponents;
1010

11+
const STATE_PRUNING: &str = "archive";
12+
const BLOCKS_PRUNING: &str = "archive-canonical";
13+
14+
fn pruning_changed(params: &PruningParams) -> bool {
15+
let state_pruning_changed =
16+
params.state_pruning != Some(STATE_PRUNING.into()) && params.state_pruning.is_some();
17+
18+
let blocks_pruning_changed =
19+
params.blocks_pruning != Some(BLOCKS_PRUNING.into()) && params.blocks_pruning.is_some();
20+
21+
state_pruning_changed || blocks_pruning_changed
22+
}
23+
1124
fn main() -> sc_cli::Result<()> {
1225
let mut cli = Cli::parse();
26+
let overwritten_pruning = pruning_changed(&cli.run.import_params.pruning_params);
1327
if !cli.aleph.experimental_pruning() {
14-
if cli
15-
.run
16-
.import_params
17-
.pruning_params
18-
.blocks_pruning
19-
.is_some()
20-
|| cli.run.import_params.pruning_params.state_pruning != Some("archive".into())
21-
{
22-
warn!("Pruning not supported. Switching to keeping all block bodies and states.");
23-
cli.run.import_params.pruning_params.blocks_pruning = None;
24-
cli.run.import_params.pruning_params.state_pruning = Some("archive".into());
25-
}
26-
} else {
27-
warn!("Pruning not supported, but flag experimental_pruning was turned on. Usage of this flag can lead to misbehaviour, which can be punished.");
28+
cli.run.import_params.pruning_params.state_pruning = Some(STATE_PRUNING.into());
29+
cli.run.import_params.pruning_params.blocks_pruning = Some(BLOCKS_PRUNING.into());
2830
}
2931

3032
match &cli.subcommand {
@@ -112,6 +114,15 @@ fn main() -> sc_cli::Result<()> {
112114
.into()),
113115
None => {
114116
let runner = cli.create_runner(&cli.run)?;
117+
if cli.aleph.experimental_pruning() {
118+
warn!("Experimental_pruning was turned on. Usage of this flag can lead to misbehaviour, which can be punished. State pruning: {:?}; Blocks pruning: {:?};",
119+
cli.run.state_pruning()?.unwrap_or_default(),
120+
cli.run.blocks_pruning()?,
121+
);
122+
} else if overwritten_pruning {
123+
warn!("Pruning not supported. Switching to keeping all block bodies and states.");
124+
}
125+
115126
let aleph_cli_config = cli.aleph;
116127
runner.run_node_until_exit(|config| async move {
117128
match config.role {
@@ -126,3 +137,31 @@ fn main() -> sc_cli::Result<()> {
126137
}
127138
}
128139
}
140+
141+
#[cfg(test)]
142+
mod tests {
143+
use sc_service::{BlocksPruning, PruningMode};
144+
145+
use super::{PruningParams, BLOCKS_PRUNING, STATE_PRUNING};
146+
147+
#[test]
148+
fn pruning_sanity_check() {
149+
let state_pruning = Some(String::from(STATE_PRUNING));
150+
let blocks_pruning = Some(String::from(BLOCKS_PRUNING));
151+
152+
let pruning_params = PruningParams {
153+
state_pruning,
154+
blocks_pruning,
155+
};
156+
157+
assert_eq!(
158+
pruning_params.blocks_pruning().unwrap(),
159+
BlocksPruning::KeepFinalized
160+
);
161+
162+
assert_eq!(
163+
pruning_params.state_pruning().unwrap().unwrap(),
164+
PruningMode::ArchiveAll
165+
);
166+
}
167+
}

bin/node/src/service.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use sp_blockchain::Backend as _;
2828
use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
2929
use sp_runtime::{
3030
generic::BlockId,
31-
traits::{Block as BlockT, Header as HeaderT, Zero},
31+
traits::{Block as BlockT, Header as HeaderT},
3232
};
3333

3434
use crate::{aleph_cli::AlephCli, chain_spec::DEFAULT_BACKUP_FOLDER, executor::AlephExecutor};
@@ -311,17 +311,19 @@ pub fn new_authority(
311311
.path(),
312312
);
313313

314+
let finalized = client.info().finalized_number;
315+
314316
let session_period = SessionPeriod(
315317
client
316318
.runtime_api()
317-
.session_period(&BlockId::Number(Zero::zero()))
319+
.session_period(&BlockId::Number(finalized))
318320
.unwrap(),
319321
);
320322

321323
let millisecs_per_block = MillisecsPerBlock(
322324
client
323325
.runtime_api()
324-
.millisecs_per_block(&BlockId::Number(Zero::zero()))
326+
.millisecs_per_block(&BlockId::Number(finalized))
325327
.unwrap(),
326328
);
327329

@@ -452,17 +454,19 @@ pub fn new_full(
452454
justification_tx,
453455
)?;
454456

457+
let finalized = client.info().finalized_number;
458+
455459
let session_period = SessionPeriod(
456460
client
457461
.runtime_api()
458-
.session_period(&BlockId::Number(Zero::zero()))
462+
.session_period(&BlockId::Number(finalized))
459463
.unwrap(),
460464
);
461465

462466
let millisecs_per_block = MillisecsPerBlock(
463467
client
464468
.runtime_api()
465-
.millisecs_per_block(&BlockId::Number(Zero::zero()))
469+
.millisecs_per_block(&BlockId::Number(finalized))
466470
.unwrap(),
467471
);
468472

finality-aleph/src/nodes/nonvalidator_node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ where
3434
let map_updater = SessionMapUpdater::<_, _, B>::new(
3535
AuthorityProviderImpl::new(client.clone()),
3636
FinalityNotificatorImpl::new(client.clone()),
37+
session_period,
3738
);
3839
let session_authorities = map_updater.readonly_session_map();
3940
spawn_handle.spawn("aleph/updater", None, async move {
4041
debug!(target: "aleph-party", "SessionMapUpdater has started.");
41-
map_updater.run(session_period).await
42+
map_updater.run().await
4243
});
4344
let (_, handler_task) = setup_justification_handler(JustificationParams {
4445
justification_rx,

finality-aleph/src/nodes/validator_node.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ where
103103
let map_updater = SessionMapUpdater::<_, _, B>::new(
104104
AuthorityProviderImpl::new(client.clone()),
105105
FinalityNotificatorImpl::new(client.clone()),
106+
session_period,
106107
);
107108
let session_authorities = map_updater.readonly_session_map();
108109
spawn_handle.spawn("aleph/updater", None, async move {
109110
debug!(target: "aleph-party", "SessionMapUpdater has started.");
110-
map_updater.run(session_period).await
111+
map_updater.run().await
111112
});
112113

113114
let (authority_justification_tx, handler_task) =

0 commit comments

Comments
 (0)