Skip to content

Commit

Permalink
A0-3097: Degenerify block hashes (#1373)
Browse files Browse the repository at this point in the history
# Description

We were theoretically parametrized by the block hash type, but in
practice some parts of `finality-aleph` already fixed it based on
`primitives`. This PR succumbs to that reality, thus simplifying quite a
couple parametrizations here and there. The next step might be to use
`BlockId` even more directly deeper (e.g. in `sync`), thus eliminating
one more trait and lowering probably unnecessary parametrizations.

Also rename Metrics to BlockMetrics, since that was tiny in addition.

## Type of change

Refactor.

# Checklist:

Co-authored-by: timorl <[email protected]>
  • Loading branch information
timorl and timorleph authored Aug 29, 2023
1 parent d55764b commit 686ff59
Show file tree
Hide file tree
Showing 30 changed files with 506 additions and 689 deletions.
14 changes: 7 additions & 7 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::{

use aleph_runtime::{self, opaque::Block, RuntimeApi};
use finality_aleph::{
run_validator_node, AlephBlockImport, AlephConfig, BlockImporter, Justification,
JustificationTranslator, Metrics, MillisecsPerBlock, Protocol, ProtocolNaming,
RateLimiterConfig, SessionPeriod, SubstrateChainStatus, TracingBlockImport,
run_validator_node, AlephBlockImport, AlephConfig, BlockImporter, BlockMetrics, Justification,
JustificationTranslator, MillisecsPerBlock, Protocol, ProtocolNaming, RateLimiterConfig,
SessionPeriod, SubstrateChainStatus, TracingBlockImport,
};
use futures::channel::mpsc;
use log::{info, warn};
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn new_partial(
mpsc::UnboundedSender<Justification>,
mpsc::UnboundedReceiver<Justification>,
Option<Telemetry>,
Metrics<BlockHash>,
BlockMetrics,
),
>,
ServiceError,
Expand Down Expand Up @@ -137,16 +137,16 @@ pub fn new_partial(
);

let metrics = match config.prometheus_registry() {
Some(register) => match Metrics::new(register) {
Some(register) => match BlockMetrics::new(register) {
Ok(metrics) => metrics,
Err(e) => {
warn!("Failed to register Prometheus metrics: {:?}.", e);
Metrics::noop()
BlockMetrics::noop()
}
},
None => {
info!("Running with the metrics is not available.");
Metrics::noop()
BlockMetrics::noop()
}
};

Expand Down
14 changes: 7 additions & 7 deletions finality-aleph/src/abft/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block, Header};

use super::common::sanity_check_round_delays;
pub use crate::aleph_primitives::{BlockNumber, CURRENT_FINALITY_VERSION as VERSION};
pub use crate::aleph_primitives::{BlockHash, BlockNumber, CURRENT_FINALITY_VERSION as VERSION};
use crate::{
abft::{
common::{unit_creation_delay_fn, MAX_ROUNDS},
NetworkWrapper,
},
crypto::Signature,
data_io::{AlephData, OrderedDataInterpreter},
data_io::{AlephData, OrderedDataInterpreter, SubstrateChainInfoProvider},
network::data::Network,
oneshot,
party::{
Expand All @@ -27,18 +27,18 @@ pub fn run_member<B, C, ADN>(
multikeychain: Keychain,
config: Config,
network: NetworkWrapper<
current_aleph_bft::NetworkData<Hasher, AlephData<B>, Signature, SignatureSet<Signature>>,
current_aleph_bft::NetworkData<Hasher, AlephData, Signature, SignatureSet<Signature>>,
ADN,
>,
data_provider: impl current_aleph_bft::DataProvider<AlephData<B>> + Send + 'static,
ordered_data_interpreter: OrderedDataInterpreter<B, C>,
data_provider: impl current_aleph_bft::DataProvider<AlephData> + Send + 'static,
ordered_data_interpreter: OrderedDataInterpreter<SubstrateChainInfoProvider<B, C>>,
backup: ABFTBackup,
) -> Task
where
B: Block,
B: Block<Hash = BlockHash>,
B::Header: Header<Number = BlockNumber>,
C: HeaderBackend<B> + Send + 'static,
ADN: Network<CurrentNetworkData<B>> + 'static,
ADN: Network<CurrentNetworkData> + 'static,
{
// Remove this check once we implement one on the AlephBFT side (A0-2583).
// Checks that the total time of a session is at least 7 days.
Expand Down
14 changes: 7 additions & 7 deletions finality-aleph/src/abft/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block, Header};

use super::common::{sanity_check_round_delays, unit_creation_delay_fn, MAX_ROUNDS};
pub use crate::aleph_primitives::{BlockNumber, LEGACY_FINALITY_VERSION as VERSION};
pub use crate::aleph_primitives::{BlockHash, BlockNumber, LEGACY_FINALITY_VERSION as VERSION};
use crate::{
abft::NetworkWrapper,
data_io::{AlephData, OrderedDataInterpreter},
data_io::{AlephData, OrderedDataInterpreter, SubstrateChainInfoProvider},
network::data::Network,
oneshot,
party::{
Expand All @@ -22,16 +22,16 @@ pub fn run_member<B, C, ADN>(
subtask_common: SubtaskCommon,
multikeychain: Keychain,
config: Config,
network: NetworkWrapper<LegacyNetworkData<B>, ADN>,
data_provider: impl legacy_aleph_bft::DataProvider<AlephData<B>> + Send + 'static,
ordered_data_interpreter: OrderedDataInterpreter<B, C>,
network: NetworkWrapper<LegacyNetworkData, ADN>,
data_provider: impl legacy_aleph_bft::DataProvider<AlephData> + Send + 'static,
ordered_data_interpreter: OrderedDataInterpreter<SubstrateChainInfoProvider<B, C>>,
backup: ABFTBackup,
) -> Task
where
B: Block,
B: Block<Hash = BlockHash>,
B::Header: Header<Number = BlockNumber>,
C: HeaderBackend<B> + Send + 'static,
ADN: Network<LegacyNetworkData<B>> + 'static,
ADN: Network<LegacyNetworkData> + 'static,
{
// Remove this check once we implement one on the AlephBFT side (A0-2583).
// Checks that the total time of a session is at least 7 days.
Expand Down
21 changes: 10 additions & 11 deletions finality-aleph/src/abft/network.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::marker::PhantomData;

use log::warn;
use sp_runtime::traits::Block;

use crate::{
abft::SignatureSet,
Expand All @@ -11,24 +10,24 @@ use crate::{
Hasher, Recipient,
};

pub type LegacyNetworkData<B> =
legacy_aleph_bft::NetworkData<Hasher, AlephData<B>, Signature, SignatureSet<Signature>>;
pub type LegacyNetworkData =
legacy_aleph_bft::NetworkData<Hasher, AlephData, Signature, SignatureSet<Signature>>;

pub type CurrentNetworkData<B> =
current_aleph_bft::NetworkData<Hasher, AlephData<B>, Signature, SignatureSet<Signature>>;
pub type CurrentNetworkData =
current_aleph_bft::NetworkData<Hasher, AlephData, Signature, SignatureSet<Signature>>;

impl<B: Block> AlephNetworkMessage<B>
for legacy_aleph_bft::NetworkData<Hasher, AlephData<B>, Signature, SignatureSet<Signature>>
impl AlephNetworkMessage
for legacy_aleph_bft::NetworkData<Hasher, AlephData, Signature, SignatureSet<Signature>>
{
fn included_data(&self) -> Vec<AlephData<B>> {
fn included_data(&self) -> Vec<AlephData> {
self.included_data()
}
}

impl<B: Block> AlephNetworkMessage<B>
for current_aleph_bft::NetworkData<Hasher, AlephData<B>, Signature, SignatureSet<Signature>>
impl AlephNetworkMessage
for current_aleph_bft::NetworkData<Hasher, AlephData, Signature, SignatureSet<Signature>>
{
fn included_data(&self) -> Vec<AlephData<B>> {
fn included_data(&self) -> Vec<AlephData> {
self.included_data()
}
}
Expand Down
31 changes: 11 additions & 20 deletions finality-aleph/src/abft/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,43 @@ use futures::{channel::oneshot, Future, TryFutureExt};
use network_clique::SpawnHandleT;
use parity_scale_codec::{Codec, Decode, Encode};
use sc_service::SpawnTaskHandle;
use sp_api::{BlockT, HeaderT};
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Hash as SpHash;

use crate::{
aleph_primitives::BlockNumber,
data_io::{AlephData, DataProvider, OrderedDataInterpreter},
};
use crate::data_io::{AlephData, ChainInfoProvider, DataProvider, OrderedDataInterpreter};

/// A convenience trait for gathering all of the desired hash characteristics.
pub trait Hash: AsRef<[u8]> + StdHash + Eq + Clone + Codec + Debug + Send + Sync {}

impl<T: AsRef<[u8]> + StdHash + Eq + Clone + Codec + Debug + Send + Sync> Hash for T {}

#[async_trait::async_trait]
impl<B: BlockT> current_aleph_bft::DataProvider<AlephData<B>> for DataProvider<B> {
async fn get_data(&mut self) -> Option<AlephData<B>> {
impl current_aleph_bft::DataProvider<AlephData> for DataProvider {
async fn get_data(&mut self) -> Option<AlephData> {
DataProvider::get_data(self).await
}
}

#[async_trait::async_trait]
impl<B: BlockT> legacy_aleph_bft::DataProvider<AlephData<B>> for DataProvider<B> {
async fn get_data(&mut self) -> Option<AlephData<B>> {
impl legacy_aleph_bft::DataProvider<AlephData> for DataProvider {
async fn get_data(&mut self) -> Option<AlephData> {
DataProvider::get_data(self).await
}
}

impl<B, C> current_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
impl<CIP> current_aleph_bft::FinalizationHandler<AlephData> for OrderedDataInterpreter<CIP>
where
B: BlockT,
B::Header: HeaderT<Number = BlockNumber>,
C: HeaderBackend<B> + Send + 'static,
CIP: ChainInfoProvider,
{
fn data_finalized(&mut self, data: AlephData<B>) {
fn data_finalized(&mut self, data: AlephData) {
OrderedDataInterpreter::data_finalized(self, data)
}
}

impl<B, C> legacy_aleph_bft::FinalizationHandler<AlephData<B>> for OrderedDataInterpreter<B, C>
impl<CIP> legacy_aleph_bft::FinalizationHandler<AlephData> for OrderedDataInterpreter<CIP>
where
B: BlockT,
B::Header: HeaderT<Number = BlockNumber>,
C: HeaderBackend<B> + Send + 'static,
CIP: ChainInfoProvider,
{
fn data_finalized(&mut self, data: AlephData<B>) {
fn data_finalized(&mut self, data: AlephData) {
OrderedDataInterpreter::data_finalized(self, data)
}
}
Expand Down
Loading

0 comments on commit 686ff59

Please sign in to comment.