Skip to content

Commit 09a305d

Browse files
A0-4216: Does not depend on actual runtime API in aleph-node (#1668)
tl;dr Goal of this PR is to have only chainspec generation as only `aleph-runtime` dependency in `aleph-node`. In aleph-node, we depend on aleph-runtimes RuntimeAPI type. This is not required, as per aleph-zero-foundation/polkadot-sdk@e53d15a : > These runtime api implementations are only used to make the compiler think that we have implemented all required runtime apis. They will notbe called as we switch the executor to WasmExecutor. In the nearfuture we will not require these fake implementations anymore afterSubstrate has shifted away from this compile time requirement.This brings us the advantage that the polkadot-service doesn't need todepend on the runtimes for getting the RuntimeApi  also removes around 1min of build time on my machine ;) 1. To achieve so we need to implement similar idea like https://github.com/Cardinal-Cryptography/polkadot-sdk/blob/aleph-v1.4.0/polkadot/node/service/src/fake_runtime_api.rs into our `aleph-node` binary 2. There’s an important remark on how this fake runtime must be implemented - it does not need to have all the same entries like [`impl_runtime_apis!`](https://github.com/Cardinal-Cryptography/aleph-node/blob/main/bin/runtime/src/lib.rs#L1001-L1259) has - in particular, it does not need an implementation for * `pallet_nomination_pools_runtime_api::NominationPoolsApi` * `pallet_staking_runtime_api::StakingApi` * `pallet_contracts::ContractsApi` ie, code compiles without them, even though real runtime has those. Why? Because this fake runtime API is only used only for sake of compilation: * for explicit calls in aleph-node to runtime API, e.g. [this requires primitives::AlephSessionApi to be implemented for fake_runtime_api](https://github.com/Cardinal-Cryptography/aleph-node/blob/main/bin/node/src/service.rs#L200) * [because of trait bounds when creating RPC client in node](https://github.com/Cardinal-Cryptography/aleph-node/blob/main/bin/node/src/rpc.rs#L49-L51) * [because of RPC modules to include in node](https://github.com/Cardinal-Cryptography/aleph-node/blob/main/bin/node/src/rpc.rs#L69-L83) 3. We use runtime’s SessionKeys, not opaque::SessionKeys when impl sp_session::SessionKeys<Block> for Runtime - this is because Polkadot and other parachains do the same, and code compiles after the change. 4. Unifications of imports of primitives in aleph-node. 5. Use fake_runtime in `finality-aleph`'s tests - no more aleph-runtime dependency! 6. Bumped `version` of `primitives` to be 0.14.0+dev - primitives needs to be versioned the same as runtime
1 parent 9ee4b97 commit 09a305d

File tree

15 files changed

+323
-74
lines changed

15 files changed

+323
-74
lines changed

Cargo.lock

Lines changed: 12 additions & 5 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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static_assertions = { workspace = true }
2828
thiserror = { workspace = true }
2929

3030
sc-basic-authorship = { workspace = true }
31-
sc-block-builder = { workspace = true }
3231
sc-chain-spec = { workspace = true }
3332
sc-cli = { workspace = true }
3433
sc-client-api = { workspace = true }
@@ -43,13 +42,12 @@ sc-service = { workspace = true }
4342
sc-telemetry = { workspace = true }
4443
sc-transaction-pool = { workspace = true }
4544
sc-transaction-pool-api = { workspace = true }
46-
4745
sp-application-crypto = { workspace = true }
4846
sp-arithmetic = { workspace = true }
47+
sp-block-builder = { workspace = true }
4948
sp-consensus = { workspace = true }
5049
sp-consensus-aura = { workspace = true }
5150
sp-core = { workspace = true }
52-
sp-inherents = { workspace = true }
5351
sp-io = { workspace = true }
5452
sp-keystore = { workspace = true }
5553
sp-runtime = { workspace = true }
@@ -62,7 +60,9 @@ try-runtime-cli = { workspace = true, optional = true }
6260
frame-benchmarking-cli = { workspace = true, optional = true }
6361
frame-benchmarking = { workspace = true, optional = true }
6462

63+
# this is only neeeded for chainspec generation
6564
aleph-runtime = { workspace = true }
65+
6666
aleph-runtime-interfaces = { workspace = true }
6767
finality-aleph = { workspace = true }
6868
primitives = { workspace = true }
@@ -73,7 +73,7 @@ sc-rpc = { workspace = true }
7373
sc-rpc-api = { workspace = true }
7474
sp-api = { workspace = true }
7575
sp-blockchain = { workspace = true }
76-
sp-block-builder = { workspace = true }
76+
7777
substrate-frame-rpc-system = { workspace = true }
7878
pallet-transaction-payment-rpc = { workspace = true }
7979

@@ -84,7 +84,7 @@ substrate-build-script-utils = { workspace = true }
8484
default = []
8585
short_session = [
8686
"aleph-runtime/short_session",
87-
"primitives/short_session"
87+
"primitives/short_session",
8888
]
8989
try-runtime = [
9090
"aleph-runtime/try-runtime",

bin/node/src/aleph_cli.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::path::PathBuf;
22

33
use finality_aleph::UnitCreationDelay;
44
use log::warn;
5+
use primitives::{DEFAULT_MAX_NON_FINALIZED_BLOCKS, DEFAULT_UNIT_CREATION_DELAY};
56
use sc_cli::clap::{self, ArgGroup, Parser};
67

7-
use crate::aleph_primitives::{DEFAULT_MAX_NON_FINALIZED_BLOCKS, DEFAULT_UNIT_CREATION_DELAY};
8-
98
#[derive(Debug, Parser, Clone)]
109
#[clap(group(ArgGroup::new("backup")))]
1110
pub struct AlephCli {

bin/node/src/chain_spec.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
use std::{collections::HashSet, str::FromStr, string::ToString};
22

33
use aleph_runtime::{
4-
AccountId, AlephConfig, AuraConfig, BalancesConfig, CommitteeManagementConfig, ElectionsConfig,
5-
Feature, FeatureControlConfig, Perbill, RuntimeGenesisConfig, SessionConfig, StakingConfig,
6-
SudoConfig, SystemConfig, VestingConfig, WASM_BINARY,
4+
AlephConfig, AuraConfig, BalancesConfig, CommitteeManagementConfig, ElectionsConfig, Feature,
5+
FeatureControlConfig, Perbill, RuntimeGenesisConfig, SessionConfig, StakingConfig, SudoConfig,
6+
SystemConfig, VestingConfig, WASM_BINARY,
77
};
88
use libp2p::PeerId;
99
use pallet_staking::{Forcing, StakerStatus};
10+
use primitives::{
11+
staking::{MIN_NOMINATOR_BOND, MIN_VALIDATOR_BOND},
12+
AccountId, AlephNodeSessionKeys as SessionKeys, AuraId, AuthorityId as AlephId,
13+
SessionValidators, Version as FinalityVersion, ADDRESSES_ENCODING, LEGACY_FINALITY_VERSION,
14+
TOKEN_DECIMALS,
15+
};
1016
use sc_cli::{
1117
clap::{self, Args},
1218
Error as CliError,
@@ -17,12 +23,6 @@ use serde_json::{Number, Value};
1723
use sp_application_crypto::Ss58Codec;
1824
use sp_core::{sr25519, Pair};
1925

20-
use crate::aleph_primitives::{
21-
staking::{MIN_NOMINATOR_BOND, MIN_VALIDATOR_BOND},
22-
AlephNodeSessionKeys as SessionKeys, AuraId, AuthorityId as AlephId, SessionValidators,
23-
Version as FinalityVersion, ADDRESSES_ENCODING, LEGACY_FINALITY_VERSION, TOKEN_DECIMALS,
24-
};
25-
2626
pub const CHAINTYPE_DEV: &str = "dev";
2727
pub const CHAINTYPE_LOCAL: &str = "local";
2828
pub const CHAINTYPE_LIVE: &str = "live";

bin/node/src/commands.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{
44
path::{Path, PathBuf},
55
};
66

7-
use aleph_runtime::AccountId;
87
use libp2p::identity::{ed25519 as libp2p_ed25519, PublicKey};
8+
use primitives::{AccountId, AuraId, AuthorityId as AlephId};
99
use sc_cli::{
1010
clap::{self, Args, Parser},
1111
Error, KeystoreParams,
@@ -15,12 +15,9 @@ use sc_service::config::{BasePath, KeystoreConfig};
1515
use sp_application_crypto::{key_types, Ss58Codec};
1616
use sp_keystore::Keystore;
1717

18-
use crate::{
19-
aleph_primitives::{AuraId, AuthorityId as AlephId},
20-
chain_spec::{
21-
self, account_id_from_string, AuthorityKeys, ChainParams, ChainSpec, SerializablePeerId,
22-
DEFAULT_BACKUP_FOLDER,
23-
},
18+
use crate::chain_spec::{
19+
self, account_id_from_string, AuthorityKeys, ChainParams, ChainSpec, SerializablePeerId,
20+
DEFAULT_BACKUP_FOLDER,
2421
};
2522

2623
#[derive(Debug, Args)]
@@ -67,10 +64,10 @@ fn aura_key(keystore: &impl Keystore) -> AuraId {
6764

6865
/// returns Aleph key, if absent a new key is generated
6966
fn aleph_key(keystore: &impl Keystore) -> AlephId {
70-
Keystore::ed25519_public_keys(keystore, crate::aleph_primitives::KEY_TYPE)
67+
Keystore::ed25519_public_keys(keystore, primitives::KEY_TYPE)
7168
.pop()
7269
.unwrap_or_else(|| {
73-
Keystore::ed25519_generate_new(keystore, crate::aleph_primitives::KEY_TYPE, None)
70+
Keystore::ed25519_generate_new(keystore, primitives::KEY_TYPE, None)
7471
.expect("Could not create Aleph key")
7572
})
7673
.into()

bin/node/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ mod service;
1010

1111
pub use cli::{Cli, Subcommand};
1212
pub use executor::ExecutorDispatch;
13-
use primitives as aleph_primitives;
1413
pub use service::{new_authority, new_partial};

bin/node/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use pruning_config::PruningConfigValidator;
77
use sc_cli::{clap::Parser, SubstrateCli};
88
use sc_network::config::Role;
99
use sc_service::{Configuration, PartialComponents};
10-
#[cfg(any(feature = "try-runtime", feature = "runtime-benchmarks"))]
11-
use {aleph_node::ExecutorDispatch, aleph_runtime::Block, sc_executor::NativeExecutionDispatch};
1210

1311
fn enforce_heap_pages(config: &mut Configuration) {
1412
config.default_heap_pages = Some(HEAP_PAGES);
@@ -88,9 +86,11 @@ fn main() -> sc_cli::Result<()> {
8886
}
8987
#[cfg(feature = "try-runtime")]
9088
Some(Subcommand::TryRuntime(cmd)) => {
91-
use primitives::MILLISECS_PER_BLOCK;
89+
use aleph_node::ExecutorDispatch;
90+
use primitives::{Block, MILLISECS_PER_BLOCK};
9291
use sc_executor::{sp_wasm_interface::ExtendedHostFunctions, NativeExecutionDispatch};
9392
use try_runtime_cli::block_building_info::timestamp_with_aura_info;
93+
9494
let runner = cli.create_runner(cmd)?;
9595
runner.async_run(|config| {
9696
let registry = config.prometheus_config.as_ref().map(|cfg| &cfg.registry);
@@ -99,6 +99,12 @@ fn main() -> sc_cli::Result<()> {
9999
.map_err(|e| sc_cli::Error::Service(sc_service::Error::Prometheus(e)))?;
100100

101101
Ok((
102+
// TODO
103+
// warning: use of deprecated method `try_runtime_cli::TryRuntimeCmd::run`:
104+
// Substrate's `try-runtime` subcommand has been migrated to a standalone CLI
105+
// (https://github.com/paritytech/try-runtime-cli). It is no longer being
106+
// maintained here and will be removed entirely some time after January 2024.
107+
// Please remove this subcommand from your runtime and use the standalone CLI.
102108
cmd.run::<Block, ExtendedHostFunctions<
103109
sp_io::SubstrateHostFunctions,
104110
<ExecutorDispatch as NativeExecutionDispatch>::ExtendHostFunctions,
@@ -115,6 +121,10 @@ fn main() -> sc_cli::Result<()> {
115121
.into()),
116122
#[cfg(feature = "runtime-benchmarks")]
117123
Some(Subcommand::Benchmark(cmd)) => {
124+
use aleph_node::ExecutorDispatch;
125+
use primitives::Block;
126+
use sc_executor::NativeExecutionDispatch;
127+
118128
let runner = cli.create_runner(cmd)?;
119129
runner.sync_run(|config| {
120130
if let frame_benchmarking_cli::BenchmarkCmd::Pallet(cmd) = cmd {

bin/node/src/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
use std::sync::Arc;
99

10-
use aleph_runtime::{opaque::Block, AccountId, Balance, Nonce};
1110
use finality_aleph::{Justification, JustificationTranslator, ValidatorAddressCache};
1211
use futures::channel::mpsc;
1312
use jsonrpsee::RpcModule;
13+
use primitives::{AccountId, Balance, Block, Nonce};
1414
use sc_client_api::StorageProvider;
1515
pub use sc_rpc_api::DenyUnsafe;
1616
use sc_transaction_pool_api::TransactionPool;

bin/node/src/service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use std::{
55
sync::Arc,
66
};
77

8-
use aleph_runtime::{self, opaque::Block, RuntimeApi};
98
use finality_aleph::{
109
run_validator_node, AlephBlockImport, AlephConfig, AllBlockMetrics, BlockImporter,
1110
ChannelProvider, Justification, JustificationTranslator, MillisecsPerBlock, Protocol,
1211
ProtocolNaming, RateLimiterConfig, RedirectingBlockImport, SessionPeriod, SubstrateChainStatus,
1312
SubstrateNetwork, SyncOracle, TracingBlockImport, ValidatorAddressCache,
1413
};
1514
use log::warn;
15+
use primitives::{
16+
fake_runtime_api::fake_runtime::RuntimeApi, AlephSessionApi, Block, MAX_BLOCK_SIZE,
17+
};
1618
use sc_basic_authorship::ProposerFactory;
1719
use sc_client_api::{BlockBackend, HeaderBackend};
1820
use sc_consensus::ImportQueue;
@@ -28,7 +30,6 @@ use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
2830

2931
use crate::{
3032
aleph_cli::AlephCli,
31-
aleph_primitives::{AlephSessionApi, MAX_BLOCK_SIZE},
3233
chain_spec::DEFAULT_BACKUP_FOLDER,
3334
executor::AlephExecutor,
3435
rpc::{create_full as create_full_rpc, FullDeps as RpcFullDeps},

bin/runtime/src/lib.rs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
4444
use primitives::{
4545
staking::MAX_NOMINATORS_REWARDED_PER_VALIDATOR, wrap_methods, Address,
4646
AlephNodeSessionKeys as SessionKeys, ApiError as AlephApiError, AuraId, AuthorityId as AlephId,
47-
Block as AlephBlock, BlockId as AlephBlockId, BlockNumber as AlephBlockNumber,
48-
Header as AlephHeader, SessionAuthorityData, SessionCommittee, SessionIndex,
49-
SessionInfoProvider, SessionValidatorError, Version as FinalityVersion, ADDRESSES_ENCODING,
50-
DEFAULT_BAN_REASON_LENGTH, DEFAULT_MAX_WINNERS, DEFAULT_SESSIONS_PER_ERA,
47+
BlockNumber as AlephBlockNumber, Header as AlephHeader, SessionAuthorityData, SessionCommittee,
48+
SessionIndex, SessionInfoProvider, SessionValidatorError, Version as FinalityVersion,
49+
ADDRESSES_ENCODING, DEFAULT_BAN_REASON_LENGTH, DEFAULT_MAX_WINNERS, DEFAULT_SESSIONS_PER_ERA,
5150
DEFAULT_SESSION_PERIOD, MAX_BLOCK_SIZE, MILLISECS_PER_BLOCK, TOKEN,
5251
};
5352
pub use primitives::{AccountId, AccountIndex, Balance, Hash, Nonce, Signature};
@@ -58,7 +57,7 @@ use sp_core::{crypto::KeyTypeId, ConstU128, OpaqueMetadata};
5857
#[cfg(any(feature = "std", test))]
5958
pub use sp_runtime::BuildStorage;
6059
use sp_runtime::{
61-
create_runtime_str, generic, impl_opaque_keys,
60+
create_runtime_str, generic,
6261
traits::{
6362
AccountIdLookup, BlakeTwo256, Block as BlockT, Bounded, Convert, ConvertInto,
6463
IdentityLookup, One, OpaqueKeys,
@@ -73,30 +72,6 @@ use sp_std::prelude::*;
7372
use sp_version::NativeVersion;
7473
use sp_version::RuntimeVersion;
7574

76-
/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
77-
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
78-
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
79-
/// to even the core data structures.
80-
pub mod opaque {
81-
pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
82-
83-
use super::*;
84-
85-
/// Opaque block header type.
86-
pub type Header = AlephHeader;
87-
/// Opaque block type.
88-
pub type Block = AlephBlock;
89-
/// Opaque block identifier type.
90-
pub type BlockId = AlephBlockId;
91-
92-
impl_opaque_keys! {
93-
pub struct SessionKeys {
94-
pub aura: Aura,
95-
pub aleph: Aleph,
96-
}
97-
}
98-
}
99-
10075
#[sp_version::runtime_version]
10176
pub const VERSION: RuntimeVersion = RuntimeVersion {
10277
spec_name: create_runtime_str!("aleph-node"),
@@ -1072,13 +1047,13 @@ impl_runtime_apis! {
10721047

10731048
impl sp_session::SessionKeys<Block> for Runtime {
10741049
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
1075-
opaque::SessionKeys::generate(seed)
1050+
SessionKeys::generate(seed)
10761051
}
10771052

10781053
fn decode_session_keys(
10791054
encoded: Vec<u8>,
10801055
) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {
1081-
opaque::SessionKeys::decode_into_raw_public_keys(&encoded)
1056+
SessionKeys::decode_into_raw_public_keys(&encoded)
10821057
}
10831058
}
10841059

0 commit comments

Comments
 (0)