Skip to content

Commit

Permalink
A0-4224: Extracted pallet alleph runtime API to a separate crate (#1734)
Browse files Browse the repository at this point in the history
# Description

This is a refactor which brings two new crates to `aleph-node`
workspace:
* `fake-runtime-api`, that is used both by `finality-aleph` and
`aleph-node` (was module in `primitives`)
* `pallet-aleph-runtime-api`, that is used by `aleph-runtime` and
`finality-aleph` (latter uses just one trait)
The motivation is that we want `primitives` to be just bags of costs and
types). Also, it follows an upstream approach, e.g.,
https://github.com/Cardinal-Cryptography/polkadot-sdk/blob/master/substrate/frame/nomination-pools/runtime-api/src/lib.rs
  • Loading branch information
Marcin-Radecki authored May 22, 2024
1 parent 066cee2 commit 5cd0359
Show file tree
Hide file tree
Showing 21 changed files with 385 additions and 60 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ members = [
"bin/node",
"bin/runtime",
"bin/chain-bootstrapper",
"bin/fake-runtime-api",
"clique",
"finality-aleph",
"pallets/aleph",
"pallets/aleph-runtime-api",
"pallets/elections",
"pallets/feature-control",
"pallets/committee-management",
Expand Down Expand Up @@ -177,13 +179,15 @@ halo2_proofs = { git = "https://github.com/Cardinal-Cryptography/pse-halo2", bra

# Local crates

fake-runtime-api = { path = "bin/fake-runtime-api", default-features = false }
aleph-runtime = { path = "bin/runtime" }
aleph-runtime-interfaces = { path = "runtime-interfaces", default-features = false }
baby-liminal-extension = { path = "baby-liminal-extension", default-features = false }
finality-aleph = { path = "finality-aleph" }
network-clique = { path = "clique" }
rate-limiter = { path = "rate-limiter" }
pallet-aleph = { path = "pallets/aleph", default-features = false }
pallet-aleph-runtime-api = { path = "pallets/aleph-runtime-api", default-features = false }
pallet-committee-management = { path = "pallets/committee-management", default-features = false }
pallet-elections = { path = "pallets/elections", default-features = false }
pallet-feature-control = { path = "pallets/feature-control", default-features = false }
Expand Down
39 changes: 39 additions & 0 deletions bin/fake-runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "fake-runtime-api"
version = "0.1.0"
license = "Apache 2.0"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
sp-api = { workspace = true }
sp-core = { workspace = true }
sp-std = { workspace = true }
sp-runtime = { workspace = true }
sp-consensus-aura = { workspace = true }

frame-support = { workspace = true }
frame-system-rpc-runtime-api = { workspace = true }

pallet-transaction-payment = { workspace = true }
pallet-transaction-payment-rpc-runtime-api = { workspace = true }

sp-version = { workspace = true }
sp-block-builder = { workspace = true }
sp-offchain = { workspace = true }
sp-transaction-pool = { workspace = true }
sp-session = { workspace = true }
sp-inherents = { workspace = true }

primitives = { workspace = true }
pallet-aleph-runtime-api = { workspace = true }

[features]
default = ["std"]
std = [
"primitives/std",
"pallet-aleph-runtime-api/std",
]
short_session = []
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
//! the native runtimes.

use frame_support::weights::Weight;
use pallet_aleph_runtime_api::*;
use pallet_transaction_payment::FeeDetails;
use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
use primitives::{
AccountId, ApiError as AlephApiError, AuraId, AuthorityId as AlephId, Balance, Block, Nonce,
SessionAuthorityData, SessionCommittee, SessionIndex, SessionValidatorError,
Version as FinalityVersion,
};
use sp_consensus_aura::SlotDuration;
use sp_core::OpaqueMetadata;
use sp_runtime::{
Expand All @@ -16,12 +22,6 @@ use sp_runtime::{
use sp_std::vec::Vec;
use sp_version::RuntimeVersion;

use crate::{
AccountId, ApiError as AlephApiError, AuraId, AuthorityId as AlephId, Balance, Block, Nonce,
SessionAuthorityData, SessionCommittee, SessionIndex, SessionValidatorError,
Version as FinalityVersion,
};

#[cfg(feature = "std")]
pub mod fake_runtime {

Expand Down
2 changes: 2 additions & 0 deletions bin/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ frame-benchmarking = { workspace = true, optional = true }
aleph-runtime-interfaces = { workspace = true }
finality-aleph = { workspace = true }
primitives = { workspace = true }
fake-runtime-api = { workspace = true, features = ["std"] }
pallet-aleph-runtime-api = { workspace = true, features = ["std"] }

# These dependencies are used for the node's RPCs
jsonrpsee = { workspace = true, features = ["server"] }
Expand Down
7 changes: 3 additions & 4 deletions bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ use std::{
sync::Arc,
};

use fake_runtime_api::fake_runtime::RuntimeApi;
use finality_aleph::{
build_network, run_validator_node, AlephBlockImport, AlephConfig, AllBlockMetrics,
BlockImporter, BuildNetworkOutput, ChannelProvider, Justification, JustificationTranslator,
MillisecsPerBlock, RateLimiterConfig, RedirectingBlockImport, SessionPeriod,
SubstrateChainStatus, SyncOracle, TracingBlockImport, ValidatorAddressCache,
};
use log::warn;
use primitives::{
fake_runtime_api::fake_runtime::RuntimeApi, AlephSessionApi, Block, DEFAULT_BACKUP_FOLDER,
MAX_BLOCK_SIZE,
};
use pallet_aleph_runtime_api::AlephSessionApi;
use primitives::{Block, DEFAULT_BACKUP_FOLDER, MAX_BLOCK_SIZE};
use sc_basic_authorship::ProposerFactory;
use sc_client_api::HeaderBackend;
use sc_consensus::{ImportQueue, Link};
Expand Down
2 changes: 2 additions & 0 deletions bin/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sp-genesis-builder = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }

pallet-aleph = { workspace = true }
pallet-aleph-runtime-api = { workspace = true }
pallet-committee-management = { workspace = true }
pallet-elections = { workspace = true }
pallet-feature-control = { workspace = true }
Expand All @@ -85,6 +86,7 @@ std = [
"frame-support/std",
"frame-try-runtime/std",
"pallet-aleph/std",
"pallet-aleph-runtime-api/std",
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
Expand Down
2 changes: 1 addition & 1 deletion bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ impl_runtime_apis! {
}
}

impl primitives::AlephSessionApi<Block> for Runtime {
impl pallet_aleph_runtime_api::AlephSessionApi<Block> for Runtime {
fn millisecs_per_block() -> u64 {
MILLISECS_PER_BLOCK
}
Expand Down
2 changes: 2 additions & 0 deletions finality-aleph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ primitives = { workspace = true }
legacy-aleph-aggregator = { package = "aggregator", git = "https://github.com/Cardinal-Cryptography/aleph-node.git", tag = "r-13.3" }
current-aleph-aggregator = { path = "../aggregator", package = "aggregator" }
rate-limiter = { package = "rate-limiter", path = "../rate-limiter" }
fake-runtime-api = { workspace = true, features = ["std"] }
pallet-aleph-runtime-api = { workspace = true, features = ["std"] }

async-trait = { workspace = true }
array-bytes = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions finality-aleph/src/idx_to_account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use primitives::{AccountId, AlephSessionApi, AuraId, BlockHash, BlockNumber};
use pallet_aleph_runtime_api::AlephSessionApi;
use primitives::{AccountId, AuraId, BlockHash, BlockNumber};
use sc_client_api::Backend;
use sp_consensus_aura::AuraApi;
use sp_runtime::traits::{Block, Header};
Expand All @@ -20,7 +21,7 @@ pub trait ValidatorIndexToAccountIdConverter {
pub struct ValidatorIndexToAccountIdConverterImpl<C, B, BE, RA>
where
C: ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: crate::aleph_primitives::AlephSessionApi<B> + AuraApi<B, AuraId>,
C::Api: AlephSessionApi<B> + AuraApi<B, AuraId>,
B: Block<Hash = BlockHash>,
BE: Backend<B> + 'static,
RA: RuntimeApi,
Expand All @@ -33,7 +34,7 @@ where
impl<C, B, BE, RA> ValidatorIndexToAccountIdConverterImpl<C, B, BE, RA>
where
C: ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: crate::aleph_primitives::AlephSessionApi<B> + AuraApi<B, AuraId>,
C::Api: AlephSessionApi<B> + AuraApi<B, AuraId>,
B: Block<Hash = BlockHash>,
B::Header: Header<Number = BlockNumber>,
BE: Backend<B> + 'static,
Expand All @@ -52,7 +53,7 @@ impl<C, B, BE, RA> ValidatorIndexToAccountIdConverter
for ValidatorIndexToAccountIdConverterImpl<C, B, BE, RA>
where
C: ClientForAleph<B, BE> + Send + Sync + 'static,
C::Api: crate::aleph_primitives::AlephSessionApi<B> + AuraApi<B, AuraId>,
C::Api: AlephSessionApi<B> + AuraApi<B, AuraId>,
B: Block<Hash = BlockHash>,
B::Header: Header<Number = BlockNumber>,
BE: Backend<B> + 'static,
Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bip39::{Language, Mnemonic, MnemonicType};
use futures::channel::oneshot;
use log::{debug, error};
use network_clique::{RateLimitingDialer, RateLimitingListener, Service, SpawnHandleT};
use pallet_aleph_runtime_api::AlephSessionApi;
use rate_limiter::SleepingRateLimiter;
use sc_client_api::Backend;
use sc_keystore::{Keystore, LocalKeystore};
Expand All @@ -12,7 +13,7 @@ use sp_consensus::SelectChain;
use sp_consensus_aura::AuraApi;

use crate::{
aleph_primitives::{AlephSessionApi, AuraId, Block},
aleph_primitives::{AuraId, Block},
block::{
substrate::{JustificationTranslator, SubstrateFinalizationInfo, VerifierCache},
BlockchainEvents, ChainStatus, FinalizationStatus, Justification,
Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/party/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use async_trait::async_trait;
use futures::channel::oneshot;
use log::{debug, info, trace, warn};
use network_clique::SpawnHandleExt;
use pallet_aleph_runtime_api::AlephSessionApi;
use sc_keystore::{Keystore, LocalKeystore};
use sp_application_crypto::RuntimeAppPublic;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
Expand All @@ -13,7 +14,7 @@ use crate::{
current_create_aleph_config, legacy_create_aleph_config, run_current_member,
run_legacy_member, SpawnHandle,
},
aleph_primitives::{AlephSessionApi, BlockHash, BlockNumber, KEY_TYPE},
aleph_primitives::{BlockHash, BlockNumber, KEY_TYPE},
block::{
substrate::{Justification, JustificationTranslator},
BestBlockSelector, Block, Header, HeaderVerifier, UnverifiedHeader,
Expand Down
3 changes: 2 additions & 1 deletion finality-aleph/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use std::{
};

use frame_support::StorageHasher;
use pallet_aleph_runtime_api::AlephSessionApi;
use parity_scale_codec::{Decode, DecodeAll, Encode, Error as DecodeError};
use sc_client_api::Backend;
use sp_application_crypto::key_types::AURA;
use sp_core::twox_128;
use sp_runtime::traits::{Block, OpaqueKeys};

use crate::{
aleph_primitives::{AccountId, AlephSessionApi, AuraId},
aleph_primitives::{AccountId, AuraId},
BlockHash, ClientForAleph,
};

Expand Down
Loading

0 comments on commit 5cd0359

Please sign in to comment.