Skip to content

Commit

Permalink
A0-1635: Limit nonfinalized block production (#769) (#774)
Browse files Browse the repository at this point in the history
* A0-1635: Limit nonfinalized block production (#769)

* Limit nonfinalized block production

* Better saturation handling

* Add log when delaying block production

* Disable two tests that were operating under outdated assumptions

* Disable one more test

* Turn off the likely broken test
  • Loading branch information
timorl authored Nov 29, 2022
1 parent 2ac588d commit 285c13d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-tests-main-devnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ jobs:
run-e2e-rewards-stake-change,
run-e2e-rewards-change-stake-force-new-era,
run-e2e-rewards-points-basic,
run-e2e-authorities-are-staking,
# run-e2e-authorities-are-staking,
run-e2e-ban-automatic,
run-e2e-version-upgrade,
]
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion bin/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-node"
version = "0.8.1"
version = "0.8.2"
authors = ["Cardinal Cryptography"]
description = "Aleph node binary"
edition = "2021"
Expand Down Expand Up @@ -43,6 +43,8 @@ sc-network = { git = "https://github.com/Cardinal-Cryptography/substrate.git", b
sc-transaction-pool = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sp-transaction-pool = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sc-transaction-pool-api = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sp-arithmetic = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sc-consensus-slots = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sc-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sp-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
sp-consensus = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
Expand Down
14 changes: 14 additions & 0 deletions bin/node/src/aleph_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use aleph_primitives::DEFAULT_UNIT_CREATION_DELAY;
use clap::{ArgGroup, Parser};
use finality_aleph::UnitCreationDelay;
use log::warn;

#[derive(Debug, Parser, Clone)]
#[clap(group(ArgGroup::new("backup")))]
Expand Down Expand Up @@ -33,6 +34,12 @@ pub struct AlephCli {
/// with `--no-backup`, but note that that limits crash recoverability.
#[clap(long, value_name = "PATH", group = "backup")]
backup_path: Option<PathBuf>,

/// The maximum number of nonfinalized blocks, after which block production should be locally
/// stopped. DO NOT CHANGE THIS, PRODUCING MORE OR FEWER BLOCKS MIGHT BE CONSIDERED MALICIOUS
/// BEHAVIOUR AND PUNISHED ACCORDINGLY!
#[clap(long, default_value_t = 20)]
max_nonfinalized_blocks: u32,
}

impl AlephCli {
Expand All @@ -55,4 +62,11 @@ impl AlephCli {
pub fn no_backup(&self) -> bool {
self.no_backup
}

pub fn max_nonfinalized_blocks(&self) -> u32 {
if self.max_nonfinalized_blocks != 20 {
warn!("Running block production with a value of max-nonfinalized-blocks {}, which is not the default of 20. THIS MIGHT BE CONSIDERED MALICIOUS BEHAVIOUR AND RESULT IN PENALTIES!", self.max_nonfinalized_blocks);
}
self.max_nonfinalized_blocks
}
}
30 changes: 28 additions & 2 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ use futures::channel::mpsc;
use log::warn;
use sc_client_api::{Backend, ExecutorProvider, HeaderBackend};
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
use sc_consensus_slots::BackoffAuthoringBlocksStrategy;
use sc_network::NetworkService;
use sc_service::{
error::Error as ServiceError, Configuration, KeystoreContainer, NetworkStarter, RpcHandlers,
TFullClient, TaskManager,
};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_api::ProvideRuntimeApi;
use sp_arithmetic::traits::BaseArithmetic;
use sp_blockchain::Backend as _;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, Zero},
Expand All @@ -35,6 +37,30 @@ type FullClient = sc_service::TFullClient<Block, RuntimeApi, AlephExecutor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;

struct LimitNonfinalized(u32);

impl<N: BaseArithmetic> BackoffAuthoringBlocksStrategy<N> for LimitNonfinalized {
fn should_backoff(
&self,
chain_head_number: N,
_chain_head_slot: Slot,
finalized_number: N,
_slow_now: Slot,
_logging_target: &str,
) -> bool {
let nonfinalized_blocks: u32 = chain_head_number
.saturating_sub(finalized_number)
.unique_saturated_into();
match nonfinalized_blocks >= self.0 {
true => {
warn!("We have {} nonfinalized blocks, with the limit being {}, delaying block production.", nonfinalized_blocks, self.0);
true
}
false => false,
}
}
}

fn backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
if aleph_config.no_backup() {
return None;
Expand Down Expand Up @@ -289,7 +315,7 @@ pub fn new_authority(
);

let force_authoring = config.force_authoring;
let backoff_authoring_blocks: Option<()> = None;
let backoff_authoring_blocks = Some(LimitNonfinalized(aleph_config.max_nonfinalized_blocks()));
let prometheus_registry = config.prometheus_registry().cloned();

let (_rpc_handlers, network, network_starter) = setup(
Expand Down

0 comments on commit 285c13d

Please sign in to comment.