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

Request justification from forks #764

Merged
merged 29 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

37 changes: 32 additions & 5 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use finality_aleph::{
};
use futures::channel::mpsc;
use log::warn;
use sc_client_api::{Backend, HeaderBackend};
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
use sc_network::NetworkService;
use sc_service::{
Expand All @@ -21,6 +22,7 @@ use sc_service::{
};
use sc_telemetry::{Telemetry, TelemetryWorker};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::Backend as _;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;
use sp_runtime::{
generic::BlockId,
Expand All @@ -33,7 +35,7 @@ type FullClient = sc_service::TFullClient<Block, RuntimeApi, AlephExecutor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;

fn get_backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
fn backup_path(aleph_config: &AlephCli, base_path: &Path) -> Option<PathBuf> {
if aleph_config.no_backup() {
return None;
}
Expand Down Expand Up @@ -253,7 +255,7 @@ pub fn new_authority(
other: (block_import, justification_tx, justification_rx, mut telemetry, metrics),
} = new_partial(&config)?;

let backup_path = get_backup_path(
let backup_path = backup_path(
&aleph_config,
config
.base_path
Expand Down Expand Up @@ -282,7 +284,7 @@ pub fn new_authority(

let (_rpc_handlers, network, network_starter) = setup(
config,
backend,
backend.clone(),
&keystore_container,
import_queue,
transaction_pool.clone(),
Expand Down Expand Up @@ -339,9 +341,11 @@ pub fn new_authority(
if aleph_config.external_addresses().is_empty() {
panic!("Cannot run a validator node without external addresses, stopping.");
}
let blockchain_backend = BlockchainBackendImpl { backend };
let aleph_config = AlephConfig {
network,
client,
blockchain_backend,
select_chain,
session_period,
millisecs_per_block,
Expand Down Expand Up @@ -379,7 +383,7 @@ pub fn new_full(
other: (_, justification_tx, justification_rx, mut telemetry, metrics),
} = new_partial(&config)?;

let backup_path = get_backup_path(
let backup_path = backup_path(
&aleph_config,
config
.base_path
Expand All @@ -390,7 +394,7 @@ pub fn new_full(

let (_rpc_handlers, network, network_starter) = setup(
config,
backend,
backend.clone(),
&keystore_container,
import_queue,
transaction_pool,
Expand All @@ -414,9 +418,11 @@ pub fn new_full(
.unwrap(),
);

let blockchain_backend = BlockchainBackendImpl { backend };
let aleph_config = AlephConfig {
network,
client,
blockchain_backend,
select_chain,
session_period,
millisecs_per_block,
Expand All @@ -439,3 +445,24 @@ pub fn new_full(
network_starter.start_network();
Ok(task_manager)
}

struct BlockchainBackendImpl {
backend: Arc<FullBackend>,
}
impl finality_aleph::BlockchainBackend<Block> for BlockchainBackendImpl {
fn children(&self, parent_hash: <Block as BlockT>::Hash) -> Vec<<Block as BlockT>::Hash> {
self.backend
.blockchain()
.children(parent_hash)
.unwrap_or_default()
}
fn info(&self) -> sp_blockchain::Info<Block> {
self.backend.blockchain().info()
}
fn header(
&self,
block_id: sp_api::BlockId<B>,
) -> sp_blockchain::Result<Option<<Block as BlockT>::Header>> {
self.backend.blockchain().header(block_id)
}
}
2 changes: 1 addition & 1 deletion finality-aleph/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "finality-aleph"
version = "0.5.2"
version = "0.5.3"
authors = ["Cardinal Cryptography"]
edition = "2021"
license = "Apache 2.0"
Expand Down
25 changes: 10 additions & 15 deletions finality-aleph/src/justification/handler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::{
sync::Arc,
time::{Duration, Instant},
};
use std::time::{Duration, Instant};

use futures::{channel::mpsc, Stream, StreamExt};
use futures_timer::Delay;
use log::{debug, error};
use sc_client_api::HeaderBackend;
use sp_api::BlockT;
use sp_runtime::traits::Header;
use tokio::time::timeout;
Expand All @@ -17,53 +13,52 @@ use crate::{
requester::BlockRequester, JustificationHandlerConfig, JustificationNotification,
JustificationRequestScheduler, SessionInfo, SessionInfoProvider, Verifier,
},
network, Metrics, STATUS_REPORT_INTERVAL,
network, BlockchainBackend, Metrics, STATUS_REPORT_INTERVAL,
};

pub struct JustificationHandler<B, V, RB, C, S, SI, F>
pub struct JustificationHandler<B, V, RB, S, SI, F, BB>
where
B: BlockT,
V: Verifier<B>,
RB: network::RequestBlocks<B> + 'static,
C: HeaderBackend<B> + Send + Sync + 'static,
S: JustificationRequestScheduler,
SI: SessionInfoProvider<B, V>,
F: BlockFinalizer<B>,
BB: BlockchainBackend<B> + 'static,
{
session_info_provider: SI,
block_requester: BlockRequester<B, RB, C, S, F, V>,
block_requester: BlockRequester<B, RB, S, F, V, BB>,
verifier_timeout: Duration,
notification_timeout: Duration,
}

impl<B, V, RB, C, S, SI, F> JustificationHandler<B, V, RB, C, S, SI, F>
impl<B, V, RB, S, SI, F, BB> JustificationHandler<B, V, RB, S, SI, F, BB>
where
B: BlockT,
V: Verifier<B>,
RB: network::RequestBlocks<B> + 'static,
C: HeaderBackend<B> + Send + Sync + 'static,
S: JustificationRequestScheduler,
SI: SessionInfoProvider<B, V>,
F: BlockFinalizer<B>,
BB: BlockchainBackend<B> + 'static,
{
pub fn new(
session_info_provider: SI,
block_requester: RB,
client: Arc<C>,
blockchain_backend: BB,
finalizer: F,
justification_request_scheduler: S,
metrics: Option<Metrics<<B::Header as Header>::Hash>>,
justification_handler_config: JustificationHandlerConfig<B>,
justification_handler_config: JustificationHandlerConfig,
) -> Self {
Self {
session_info_provider,
block_requester: BlockRequester::new(
block_requester,
client,
blockchain_backend,
finalizer,
justification_request_scheduler,
metrics,
justification_handler_config.min_allowed_delay,
),
verifier_timeout: justification_handler_config.verifier_timeout,
notification_timeout: justification_handler_config.notification_timeout,
Expand Down
19 changes: 6 additions & 13 deletions finality-aleph/src/justification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,29 @@ pub struct JustificationNotification<Block: BlockT> {
}

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

impl<B: BlockT> Default for JustificationHandlerConfig<B> {
impl Default for JustificationHandlerConfig {
fn default() -> Self {
Self {
verifier_timeout: Duration::from_millis(500),
notification_timeout: Duration::from_millis(1000),
min_allowed_delay: 3u32.into(),
// request justifications slightly more frequently than they're created
notification_timeout: Duration::from_millis(800),
timorl marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

#[cfg(test)]
impl<B: BlockT> JustificationHandlerConfig<B> {
pub fn new(
verifier_timeout: Duration,
notification_timeout: Duration,
min_allowed_delay: NumberFor<B>,
) -> Self {
impl JustificationHandlerConfig {
pub fn new(verifier_timeout: Duration, notification_timeout: Duration) -> Self {
Self {
verifier_timeout,
notification_timeout,
min_allowed_delay,
}
}
}
Loading