Skip to content

Commit f12d5a2

Browse files
Marcin-Radeckimike1729timorl
authored
Port aleph-node 8.2 testnet to mainnet branch (#776)
* Request justification from forks * Reverted changes to cargo lock introduced in 6ae50ca (#773) * A0-1635: Limit nonfinalized block production (#769) (#774) * 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 * Changed node version * Fixed leftover conflicted line in lock file * Reverted unwanted change to runtime/Cargo.toml * Updated Cargo.lock Co-authored-by: Michal Swietek <[email protected]> Co-authored-by: timorl <[email protected]>
1 parent 76f958b commit f12d5a2

File tree

17 files changed

+309
-171
lines changed

17 files changed

+309
-171
lines changed

.github/workflows/e2e-tests-main-devnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ jobs:
454454
run-e2e-rewards-stake-change,
455455
run-e2e-rewards-change-stake-force-new-era,
456456
run-e2e-rewards-points-basic,
457-
run-e2e-authorities-are-staking,
457+
# run-e2e-authorities-are-staking,
458458
run-e2e-ban-automatic,
459459
run-e2e-version-upgrade,
460460
]

Cargo.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aleph-node"
3-
version = "0.8.0+mainnet"
3+
version = "0.8.2+mainnet"
44
authors = ["Cardinal Cryptography"]
55
description = "Aleph node binary"
66
edition = "2021"
@@ -43,6 +43,8 @@ sc-network = { git = "https://github.com/Cardinal-Cryptography/substrate.git", b
4343
sc-transaction-pool = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
4444
sp-transaction-pool = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
4545
sc-transaction-pool-api = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
46+
sp-arithmetic = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
47+
sc-consensus-slots = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
4648
sc-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
4749
sp-consensus-aura = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }
4850
sp-consensus = { git = "https://github.com/Cardinal-Cryptography/substrate.git", branch = "aleph-v0.9.28" }

bin/node/src/aleph_cli.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::PathBuf;
33
use aleph_primitives::DEFAULT_UNIT_CREATION_DELAY;
44
use clap::{ArgGroup, Parser};
55
use finality_aleph::UnitCreationDelay;
6+
use log::warn;
67

78
#[derive(Debug, Parser, Clone)]
89
#[clap(group(ArgGroup::new("backup")))]
@@ -33,6 +34,12 @@ pub struct AlephCli {
3334
/// with `--no-backup`, but note that that limits crash recoverability.
3435
#[clap(long, value_name = "PATH", group = "backup")]
3536
backup_path: Option<PathBuf>,
37+
38+
/// The maximum number of nonfinalized blocks, after which block production should be locally
39+
/// stopped. DO NOT CHANGE THIS, PRODUCING MORE OR FEWER BLOCKS MIGHT BE CONSIDERED MALICIOUS
40+
/// BEHAVIOUR AND PUNISHED ACCORDINGLY!
41+
#[clap(long, default_value_t = 20)]
42+
max_nonfinalized_blocks: u32,
3643
}
3744

3845
impl AlephCli {
@@ -55,4 +62,11 @@ impl AlephCli {
5562
pub fn no_backup(&self) -> bool {
5663
self.no_backup
5764
}
65+
66+
pub fn max_nonfinalized_blocks(&self) -> u32 {
67+
if self.max_nonfinalized_blocks != 20 {
68+
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);
69+
}
70+
self.max_nonfinalized_blocks
71+
}
5872
}

bin/node/src/service.rs

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ use finality_aleph::{
1313
};
1414
use futures::channel::mpsc;
1515
use log::warn;
16-
use sc_client_api::ExecutorProvider;
16+
use sc_client_api::{Backend, ExecutorProvider, HeaderBackend};
1717
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
18+
use sc_consensus_slots::BackoffAuthoringBlocksStrategy;
1819
use sc_network::NetworkService;
1920
use sc_service::{
2021
error::Error as ServiceError, Configuration, KeystoreContainer, NetworkStarter, RpcHandlers,
2122
TFullClient, TaskManager,
2223
};
2324
use sc_telemetry::{Telemetry, TelemetryWorker};
2425
use sp_api::ProvideRuntimeApi;
25-
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
26+
use sp_arithmetic::traits::BaseArithmetic;
27+
use sp_blockchain::Backend as _;
28+
use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
2629
use sp_runtime::{
2730
generic::BlockId,
2831
traits::{Block as BlockT, Header as HeaderT, Zero},
@@ -34,7 +37,31 @@ type FullClient = sc_service::TFullClient<Block, RuntimeApi, AlephExecutor>;
3437
type FullBackend = sc_service::TFullBackend<Block>;
3538
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
3639

37-
fn get_backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
40+
struct LimitNonfinalized(u32);
41+
42+
impl<N: BaseArithmetic> BackoffAuthoringBlocksStrategy<N> for LimitNonfinalized {
43+
fn should_backoff(
44+
&self,
45+
chain_head_number: N,
46+
_chain_head_slot: Slot,
47+
finalized_number: N,
48+
_slow_now: Slot,
49+
_logging_target: &str,
50+
) -> bool {
51+
let nonfinalized_blocks: u32 = chain_head_number
52+
.saturating_sub(finalized_number)
53+
.unique_saturated_into();
54+
match nonfinalized_blocks >= self.0 {
55+
true => {
56+
warn!("We have {} nonfinalized blocks, with the limit being {}, delaying block production.", nonfinalized_blocks, self.0);
57+
true
58+
}
59+
false => false,
60+
}
61+
}
62+
}
63+
64+
fn backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
3865
if aleph_config.no_backup() {
3966
return None;
4067
}
@@ -264,7 +291,7 @@ pub fn new_authority(
264291
.extra_sets
265292
.push(finality_aleph::peers_set_config(Protocol::Validator));
266293

267-
let backup_path = get_backup_path(
294+
let backup_path = backup_path(
268295
&aleph_config,
269296
config
270297
.base_path
@@ -288,12 +315,12 @@ pub fn new_authority(
288315
);
289316

290317
let force_authoring = config.force_authoring;
291-
let backoff_authoring_blocks: Option<()> = None;
318+
let backoff_authoring_blocks = Some(LimitNonfinalized(aleph_config.max_nonfinalized_blocks()));
292319
let prometheus_registry = config.prometheus_registry().cloned();
293320

294321
let (_rpc_handlers, network, network_starter) = setup(
295322
config,
296-
backend,
323+
backend.clone(),
297324
&keystore_container,
298325
import_queue,
299326
transaction_pool.clone(),
@@ -353,9 +380,11 @@ pub fn new_authority(
353380
if aleph_config.external_addresses().is_empty() {
354381
panic!("Cannot run a validator node without external addresses, stopping.");
355382
}
383+
let blockchain_backend = BlockchainBackendImpl { backend };
356384
let aleph_config = AlephConfig {
357385
network,
358386
client,
387+
blockchain_backend,
359388
select_chain,
360389
session_period,
361390
millisecs_per_block,
@@ -393,7 +422,7 @@ pub fn new_full(
393422
other: (_, justification_tx, justification_rx, mut telemetry, metrics),
394423
} = new_partial(&config)?;
395424

396-
let backup_path = get_backup_path(
425+
let backup_path = backup_path(
397426
&aleph_config,
398427
config
399428
.base_path
@@ -404,7 +433,7 @@ pub fn new_full(
404433

405434
let (_rpc_handlers, network, network_starter) = setup(
406435
config,
407-
backend,
436+
backend.clone(),
408437
&keystore_container,
409438
import_queue,
410439
transaction_pool,
@@ -428,9 +457,11 @@ pub fn new_full(
428457
.unwrap(),
429458
);
430459

460+
let blockchain_backend = BlockchainBackendImpl { backend };
431461
let aleph_config = AlephConfig {
432462
network,
433463
client,
464+
blockchain_backend,
434465
select_chain,
435466
session_period,
436467
millisecs_per_block,
@@ -453,3 +484,24 @@ pub fn new_full(
453484
network_starter.start_network();
454485
Ok(task_manager)
455486
}
487+
488+
struct BlockchainBackendImpl {
489+
backend: Arc<FullBackend>,
490+
}
491+
impl finality_aleph::BlockchainBackend<Block> for BlockchainBackendImpl {
492+
fn children(&self, parent_hash: <Block as BlockT>::Hash) -> Vec<<Block as BlockT>::Hash> {
493+
self.backend
494+
.blockchain()
495+
.children(parent_hash)
496+
.unwrap_or_default()
497+
}
498+
fn info(&self) -> sp_blockchain::Info<Block> {
499+
self.backend.blockchain().info()
500+
}
501+
fn header(
502+
&self,
503+
block_id: sp_api::BlockId<Block>,
504+
) -> sp_blockchain::Result<Option<<Block as BlockT>::Header>> {
505+
self.backend.blockchain().header(block_id)
506+
}
507+
}

finality-aleph/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "finality-aleph"
3-
version = "0.5.0"
3+
version = "0.5.3"
44
authors = ["Cardinal Cryptography"]
55
edition = "2021"
66
license = "Apache 2.0"

finality-aleph/src/justification/handler.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use std::{
2-
sync::Arc,
3-
time::{Duration, Instant},
4-
};
1+
use std::time::{Duration, Instant};
52

63
use futures::{channel::mpsc, Stream, StreamExt};
74
use futures_timer::Delay;
85
use log::{debug, error};
9-
use sc_client_api::HeaderBackend;
106
use sp_api::BlockT;
117
use sp_runtime::traits::Header;
128
use tokio::time::timeout;
@@ -17,53 +13,52 @@ use crate::{
1713
requester::BlockRequester, JustificationHandlerConfig, JustificationNotification,
1814
JustificationRequestScheduler, SessionInfo, SessionInfoProvider, Verifier,
1915
},
20-
network, Metrics, STATUS_REPORT_INTERVAL,
16+
network, BlockchainBackend, Metrics, STATUS_REPORT_INTERVAL,
2117
};
2218

23-
pub struct JustificationHandler<B, V, RB, C, S, SI, F>
19+
pub struct JustificationHandler<B, V, RB, S, SI, F, BB>
2420
where
2521
B: BlockT,
2622
V: Verifier<B>,
2723
RB: network::RequestBlocks<B> + 'static,
28-
C: HeaderBackend<B> + Send + Sync + 'static,
2924
S: JustificationRequestScheduler,
3025
SI: SessionInfoProvider<B, V>,
3126
F: BlockFinalizer<B>,
27+
BB: BlockchainBackend<B> + 'static,
3228
{
3329
session_info_provider: SI,
34-
block_requester: BlockRequester<B, RB, C, S, F, V>,
30+
block_requester: BlockRequester<B, RB, S, F, V, BB>,
3531
verifier_timeout: Duration,
3632
notification_timeout: Duration,
3733
}
3834

39-
impl<B, V, RB, C, S, SI, F> JustificationHandler<B, V, RB, C, S, SI, F>
35+
impl<B, V, RB, S, SI, F, BB> JustificationHandler<B, V, RB, S, SI, F, BB>
4036
where
4137
B: BlockT,
4238
V: Verifier<B>,
4339
RB: network::RequestBlocks<B> + 'static,
44-
C: HeaderBackend<B> + Send + Sync + 'static,
4540
S: JustificationRequestScheduler,
4641
SI: SessionInfoProvider<B, V>,
4742
F: BlockFinalizer<B>,
43+
BB: BlockchainBackend<B> + 'static,
4844
{
4945
pub fn new(
5046
session_info_provider: SI,
5147
block_requester: RB,
52-
client: Arc<C>,
48+
blockchain_backend: BB,
5349
finalizer: F,
5450
justification_request_scheduler: S,
5551
metrics: Option<Metrics<<B::Header as Header>::Hash>>,
56-
justification_handler_config: JustificationHandlerConfig<B>,
52+
justification_handler_config: JustificationHandlerConfig,
5753
) -> Self {
5854
Self {
5955
session_info_provider,
6056
block_requester: BlockRequester::new(
6157
block_requester,
62-
client,
58+
blockchain_backend,
6359
finalizer,
6460
justification_request_scheduler,
6561
metrics,
66-
justification_handler_config.min_allowed_delay,
6762
),
6863
verifier_timeout: justification_handler_config.verifier_timeout,
6964
notification_timeout: justification_handler_config.notification_timeout,

finality-aleph/src/justification/mod.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,29 @@ pub struct JustificationNotification<Block: BlockT> {
5555
}
5656

5757
#[derive(Clone)]
58-
pub struct JustificationHandlerConfig<B: BlockT> {
58+
pub struct JustificationHandlerConfig {
5959
/// How long should we wait when the session verifier is not yet available.
6060
verifier_timeout: Duration,
6161
/// How long should we wait for any notification.
6262
notification_timeout: Duration,
63-
///Distance (in amount of blocks) between the best and the block we want to request justification
64-
min_allowed_delay: NumberFor<B>,
6563
}
6664

67-
impl<B: BlockT> Default for JustificationHandlerConfig<B> {
65+
impl Default for JustificationHandlerConfig {
6866
fn default() -> Self {
6967
Self {
7068
verifier_timeout: Duration::from_millis(500),
71-
notification_timeout: Duration::from_millis(1000),
72-
min_allowed_delay: 3u32.into(),
69+
// request justifications slightly more frequently than they're created
70+
notification_timeout: Duration::from_millis(800),
7371
}
7472
}
7573
}
7674

7775
#[cfg(test)]
78-
impl<B: BlockT> JustificationHandlerConfig<B> {
79-
pub fn new(
80-
verifier_timeout: Duration,
81-
notification_timeout: Duration,
82-
min_allowed_delay: NumberFor<B>,
83-
) -> Self {
76+
impl JustificationHandlerConfig {
77+
pub fn new(verifier_timeout: Duration, notification_timeout: Duration) -> Self {
8478
Self {
8579
verifier_timeout,
8680
notification_timeout,
87-
min_allowed_delay,
8881
}
8982
}
9083
}

0 commit comments

Comments
 (0)