Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Limit nonfinalized block production
  • Loading branch information
timorl committed Nov 25, 2022
commit 6e98e8c15ed7e4756e9a9123f2beee5b39dfa16d
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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.30" }
sp-transaction-pool = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
sc-transaction-pool-api = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
sc-consensus-slots = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
sc-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
sp-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
sp-consensus = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.30" }
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
}
}
24 changes: 22 additions & 2 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.

use std::{
ops::Sub,
path::{Path, PathBuf},
sync::Arc,
};
Expand All @@ -14,14 +15,15 @@ use finality_aleph::{
use futures::channel::mpsc;
use log::warn;
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_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 @@ -33,6 +35,24 @@ 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: TryInto<u32> + Sub<Output = N>> 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 = (chain_head_number - finalized_number)
.try_into()
.unwrap_or(u32::MAX);
nonfinalized_blocks >= self.0
}
}

fn get_backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
if aleph_config.no_backup() {
return None;
Expand Down Expand Up @@ -277,7 +297,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