Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
make impl_runtime_api work without sp-api
Browse files Browse the repository at this point in the history
  • Loading branch information
kianenigma committed Jun 24, 2023
1 parent 6b7e50a commit 7d912d0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

11 changes: 4 additions & 7 deletions bin/minimal/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2021"

[dependencies]
parity-scale-codec = { version = "3.0.0", default-features = false }
scale-info = { version = "2.6.0", default-features = false }

# this is a frame-based runtime, thus importing `frame` with runtime feature enabled.
frame = { path = "../../../frame", default-features = false, features = ["runtime"] }

# pallets that we want to use
Expand All @@ -12,11 +16,6 @@ pallet-sudo = { path = "../../../frame/sudo", default-features = false }
pallet-timestamp = { path = "../../../frame/timestamp", default-features = false }
pallet-transaction-payment = { path = "../../../frame/transaction-payment", default-features = false }

# TODO: https://github.com/paritytech/substrate/issues/14145
sp-api = { path = "../../../primitives/api", default-features = false }

parity-scale-codec = { version = "3.0.0", default-features = false }
scale-info = { version = "2.6.0", default-features = false }

[build-dependencies]
substrate-wasm-builder = { path = "../../../utils/wasm-builder", optional = true }
Expand All @@ -31,8 +30,6 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment/std",

"sp-api/std",

"parity-scale-codec/std",
"scale-info/std",

Expand Down
5 changes: 1 addition & 4 deletions bin/minimal/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ use frame::{
weights::FixedFee,
},
};

// TODO: this is temp
use frame::deps::frame_support;

#[runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("minimal-runtime"),
Expand Down Expand Up @@ -89,6 +85,7 @@ impl pallet_balances::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type DustRemoval = ();
type AccountStore = System;
type ExistentialDeposit = ConstU128<1>;
}

#[derive_impl(pallet_sudo::config_preludes::SolochainDefaultConfig as pallet_sudo::DefaultConfig)]
Expand Down
3 changes: 1 addition & 2 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ pub mod pallet {
#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type Balance = u64;
type ExistentialDeposit = ();
type MaxLocks = ConstU32<{ u32::MAX }>;
type MaxReserves = ConstU32<{ u32::MAX }>;
type MaxFreezes = ConstU32<{ u32::MAX }>;
Expand All @@ -238,7 +237,6 @@ pub mod pallet {
#[frame_support::register_default_impl(SolochainDefaultConfig)]
impl DefaultConfig for SolochainDefaultConfig {
type Balance = u128;
type ExistentialDeposit = ();
type MaxLocks = ConstU32<{ 128 }>;
type MaxReserves = ConstU32<{ 128 }>;
type MaxFreezes = ConstU32<{ 128 }>;
Expand Down Expand Up @@ -286,6 +284,7 @@ pub mod pallet {
///
/// Bottom line: Do yourself a favour and make it at least one!
#[pallet::constant]
#[pallet::no_default]
type ExistentialDeposit: Get<Self::Balance>;

/// The means of storing the balances of an account.
Expand Down
9 changes: 1 addition & 8 deletions frame/examples/frame-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! An example pallet built purely with the `frame` crate.

#![cfg_attr(not(feature = "std"), no_std)]
use frame::prelude::*;

#[frame::pallet]
Expand All @@ -23,11 +24,6 @@ mod tests {
use super::pallet as pallet_example;
use frame::{prelude::*, testing_prelude::*};

// TODO: this is only for backwards compatibility with `tt_*` macros generated by other
// crates, which still force even a pallet/runtime made with `frame` need to have
// `frame_support` in scope.
use frame::deps::frame_support;

type UncheckedExtrinsic = MockUncheckedExtrinsic<Test>;
type Block = MockBlock<Test>;

Expand All @@ -45,9 +41,6 @@ mod tests {

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
impl frame_system::Config for Test {
// these items are defined by frame-system as `no_default`, so we must specify them here.
// Note that these are types that actually rely on the outer runtime, and can't sensibly
// have an _independent_ default.
type BaseCallFilter = frame::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
Expand Down
13 changes: 9 additions & 4 deletions frame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ pub mod prelude {
pub use sp_std::prelude::*;
}

pub use frame_support::log;

#[cfg(feature = "std")]
pub mod testing_prelude {
#[cfg(feature = "runtime")]
pub use super::runtime::testing_prelude::*;
pub use super::runtime::{prelude::*, testing_prelude::*};

pub use frame_support::{
assert_err, assert_err_ignore_postinfo, assert_error_encoded_size, assert_noop, assert_ok,
assert_storage_noop, derive_impl, ord_parameter_types, parameter_types,
parameter_types_impl_thread_local, storage_alias,
};

pub use frame_system::{self, mocking::*};
pub use sp_io::TestExternalities as TestState;
}
Expand Down Expand Up @@ -117,12 +119,15 @@ pub mod derive {

/// types that are often needed to amalgamate a real runtime. In principle, this is very similar to
/// [`test`], but it contains production-ready types, as opposed to mocks.
#[cfg(feature = "runtime")]
#[cfg(any(feature = "runtime", feature = "std"))]
pub mod runtime {
pub mod prelude {
pub use frame_executive::*;
pub use frame_support::{
construct_runtime, derive_impl, parameter_types,
self, // TODO: this is a temporary hack.
construct_runtime,
derive_impl,
parameter_types,
traits::{
ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128, ConstU16,
ConstU32, ConstU64, ConstU8,
Expand Down
5 changes: 5 additions & 0 deletions frame/support/procedural/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub fn generate_crate_access(unique_id: &str, def_crate: &str) -> TokenStream {
/// for `frame-support` output will for example be `frame_support`.
pub fn generate_crate_access_2018(def_crate: &str) -> Result<syn::Path, Error> {
if let Ok(FoundCrate::Name(name)) = crate_name(&"frame") {
// TODO: add tes to make sure `frame` always contains
// `frame::deps::frame_support/frame_system`.
let path = format!("{}::deps::{}", name, def_crate.to_string().replace("-", "_"));
let path = syn::parse_str::<syn::Path>(&path)?;
return Ok(path)
Expand All @@ -73,6 +75,9 @@ pub fn generate_hidden_includes(unique_id: &str, def_crate: &str) -> TokenStream
let mod_name = generate_hidden_includes_mod_name(unique_id);

if let Ok(FoundCrate::Name(name)) = crate_name(&"frame") {
// TODO: add tes to make sure `frame` always contains
// `frame::deps::frame_support/frame_system`.
// TODO: handle error.
let path = format!("{}::deps::{}", name, def_crate.to_string().replace("-", "_"));
let path = syn::parse_str::<syn::Path>(&path).unwrap();
return quote::quote!(
Expand Down
8 changes: 8 additions & 0 deletions primitives/api/proc-macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ use inflector::Inflector;

/// Generates the access to the `sc_client` crate.
pub fn generate_crate_access() -> TokenStream {
if let Ok(FoundCrate::Name(name)) = crate_name(&"frame") {
// TODO: add tes to make sure `frame` always contains `frame::deps::sp_api`.
// TODO: handle error.
let path = format!("{}::deps::{}", name, "sp_api");
let path = syn::parse_str::<syn::Path>(&path).unwrap();
return path.into_token_stream()
}

match crate_name("sp-api") {
Ok(FoundCrate::Itself) => quote!(sp_api),
Ok(FoundCrate::Name(renamed_name)) => {
Expand Down

0 comments on commit 7d912d0

Please sign in to comment.