Skip to content

Commit

Permalink
simplify backend dependecy
Browse files Browse the repository at this point in the history
  • Loading branch information
mike1729 committed Nov 24, 2022
1 parent 8e1249a commit abf2605
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
14 changes: 7 additions & 7 deletions finality-aleph/src/justification/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use futures::{channel::mpsc, Stream, StreamExt};
use futures_timer::Delay;
use log::{debug, error};
use sc_client_api::backend::Backend;
use sc_client_api::blockchain::Backend as BlockchainBackend;
use sp_api::BlockT;
use sp_runtime::traits::Header;
use tokio::time::timeout;
Expand All @@ -20,36 +20,36 @@ use crate::{
network, Metrics, STATUS_REPORT_INTERVAL,
};

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

impl<B, V, RB, S, SI, F, BE> JustificationHandler<B, V, RB, S, SI, F, BE>
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,
S: JustificationRequestScheduler,
SI: SessionInfoProvider<B, V>,
F: BlockFinalizer<B>,
BE: Backend<B> + Send + Sync + 'static,
BB: BlockchainBackend<B> + Send + Sync + 'static,
{
pub fn new(
session_info_provider: SI,
block_requester: RB,
backend: Arc<BE>,
backend: Arc<BB>,
finalizer: F,
justification_request_scheduler: S,
metrics: Option<Metrics<<B::Header as Header>::Hash>>,
Expand Down
27 changes: 12 additions & 15 deletions finality-aleph/src/justification/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, marker::PhantomData, sync::Arc, time::Instant};

use aleph_primitives::ALEPH_ENGINE_ID;
use log::{debug, error, info, warn};
use sc_client_api::{backend::Backend, blockchain::Backend as _, HeaderBackend};
use sc_client_api::{blockchain::Backend as BlockchainBackend, HeaderBackend};
use sp_api::{BlockId, BlockT, NumberFor};
use sp_runtime::traits::{Header, One};

Expand Down Expand Up @@ -72,36 +72,36 @@ impl<B: BlockT> fmt::Display for JustificationRequestStatus<B> {
}
}

pub struct BlockRequester<B, RB, S, F, V, BE>
pub struct BlockRequester<B, RB, S, F, V, BB>
where
B: BlockT,
RB: network::RequestBlocks<B> + 'static,
S: JustificationRequestScheduler,
F: BlockFinalizer<B>,
V: Verifier<B>,
BE: Backend<B>,
BB: BlockchainBackend<B>,
{
block_requester: RB,
backend: Arc<BE>,
backend: Arc<BB>,
finalizer: F,
justification_request_scheduler: S,
metrics: Option<Metrics<<B::Header as Header>::Hash>>,
request_status: JustificationRequestStatus<B>,
_phantom: PhantomData<V>,
}

impl<B, RB, S, F, V, BE> BlockRequester<B, RB, S, F, V, BE>
impl<B, RB, S, F, V, BB> BlockRequester<B, RB, S, F, V, BB>
where
B: BlockT,
RB: network::RequestBlocks<B> + 'static,
S: JustificationRequestScheduler,
F: BlockFinalizer<B>,
V: Verifier<B>,
BE: Backend<B>,
BB: BlockchainBackend<B>,
{
pub fn new(
block_requester: RB,
backend: Arc<BE>,
backend: Arc<BB>,
finalizer: F,
justification_request_scheduler: S,
metrics: Option<Metrics<<B::Header as Header>::Hash>>,
Expand Down Expand Up @@ -182,11 +182,11 @@ where
}

pub fn finalized_number(&self) -> NumberFor<B> {
self.backend.blockchain().info().finalized_number
self.backend.info().finalized_number
}

fn request(&mut self, hash: <B as BlockT>::Hash) {
if let Ok(Some(header)) = self.backend.blockchain().header(BlockId::Hash(hash)) {
if let Ok(Some(header)) = self.backend.header(BlockId::Hash(hash)) {
let number = *header.number();
debug!(target: "aleph-justification", "Trying to request block {:?}", number);
self.request_status.save_block_number(number);
Expand All @@ -207,19 +207,16 @@ where
// We don't remove the child that it's on the same branch as best since a fork may happen
// somewhere in between them.
fn request_targets(&self, mut top_wanted: NumberFor<B>) -> Vec<<B as BlockT>::Hash> {
let blockchain_backend = self.backend.blockchain();
let blockchain_info = blockchain_backend.info();
let blockchain_info = self.backend.info();
let finalized_hash = blockchain_info.finalized_hash;

let mut targets = blockchain_backend
.children(finalized_hash)
.unwrap_or_default();
let mut targets = self.backend.children(finalized_hash).unwrap_or_default();
let best_number = blockchain_info.best_number;
if best_number <= top_wanted {
// most probably block best_number is not yet finalized
top_wanted = best_number - NumberFor::<B>::one();
}
match blockchain_backend.hash(top_wanted) {
match self.backend.hash(top_wanted) {
Ok(Some(hash)) => {
if !targets.contains(&hash) {
targets.push(hash);
Expand Down
10 changes: 6 additions & 4 deletions finality-aleph/src/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use aleph_primitives::{AuthorityId, SessionAuthorityData};
use codec::Encode;
use log::warn;
pub use nonvalidator_node::run_nonvalidator_node;
use sc_client_api::blockchain::Backend as BlockchainBackend;
use sc_client_api::Backend;
use sc_network::{ExHashT, NetworkService};
use sp_runtime::{
Expand Down Expand Up @@ -83,10 +84,10 @@ impl<B: Block> Verifier<B> for JustificationVerifier {
}
}

struct JustificationParams<B: Block, H: ExHashT, C, BE> {
struct JustificationParams<B: Block, H: ExHashT, C, BB> {
pub network: Arc<NetworkService<B, H>>,
pub client: Arc<C>,
pub backend: Arc<BE>,
pub backend: Arc<BB>,
pub justification_rx: mpsc::UnboundedReceiver<JustificationNotification<B>>,
pub metrics: Option<Metrics<<B::Header as Header>::Hash>>,
pub session_period: SessionPeriod,
Expand Down Expand Up @@ -127,8 +128,8 @@ impl<B: Block> SessionInfoProvider<B, JustificationVerifier> for SessionInfoProv
}
}

fn setup_justification_handler<B, H, C, BE>(
just_params: JustificationParams<B, H, C, BE>,
fn setup_justification_handler<B, H, C, BB, BE>(
just_params: JustificationParams<B, H, C, BB>,
) -> (
UnboundedSender<JustificationNotification<B>>,
impl Future<Output = ()>,
Expand All @@ -138,6 +139,7 @@ where
H: ExHashT,
C: crate::ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: aleph_primitives::AlephSessionApi<B>,
BB: BlockchainBackend<B> + 'static,
BE: Backend<B> + 'static,
{
let JustificationParams {
Expand Down
4 changes: 3 additions & 1 deletion finality-aleph/src/nodes/nonvalidator_node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use log::{debug, error};
use sc_client_api::blockchain::Backend as BlockchainBackend;
use sc_client_api::Backend;
use sc_network::ExHashT;
use sp_consensus::SelectChain;
Expand All @@ -10,12 +11,13 @@ use crate::{
AlephConfig,
};

pub async fn run_nonvalidator_node<B, H, C, BE, SC>(aleph_config: AlephConfig<B, H, C, SC, BE>)
pub async fn run_nonvalidator_node<B, H, C, BB, BE, SC>(aleph_config: AlephConfig<B, H, C, SC, BB>)
where
B: Block,
H: ExHashT,
C: crate::ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: aleph_primitives::AlephSessionApi<B>,
BB: BlockchainBackend<B> + 'static,
BE: Backend<B> + 'static,
SC: SelectChain<B> + 'static,
{
Expand Down
4 changes: 3 additions & 1 deletion finality-aleph/src/nodes/validator_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{marker::PhantomData, sync::Arc};
use bip39::{Language, Mnemonic, MnemonicType};
use futures::channel::oneshot;
use log::{debug, error};
use sc_client_api::blockchain::Backend as BlockchainBackend;
use sc_client_api::Backend;
use sc_network::ExHashT;
use sp_consensus::SelectChain;
Expand Down Expand Up @@ -37,12 +38,13 @@ pub async fn new_pen(mnemonic: &str, keystore: Arc<dyn CryptoStore>) -> Authorit
.expect("we just generated this key so everything should work")
}

pub async fn run_validator_node<B, H, C, BE, SC>(aleph_config: AlephConfig<B, H, C, SC, BE>)
pub async fn run_validator_node<B, H, C, BB, BE, SC>(aleph_config: AlephConfig<B, H, C, SC, BB>)
where
B: Block,
H: ExHashT,
C: crate::ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: aleph_primitives::AlephSessionApi<B>,
BB: BlockchainBackend<B> + 'static,
BE: Backend<B> + 'static,
SC: SelectChain<B> + 'static,
{
Expand Down

0 comments on commit abf2605

Please sign in to comment.