mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-01 10:07:56 +00:00
f910a15c1c
The runtime now can provide a number of predefined presets of `RuntimeGenesisConfig` struct. This presets are intended to be used in different deployments, e.g.: `local`, `staging`, etc, and should be included into the corresponding chain-specs. Having `GenesisConfig` presets in runtime allows to fully decouple node from runtime types (the problem is described in #1984). **Summary of changes:** - The `GenesisBuilder` API was adjusted to enable this functionality (and provide better naming - #150): ```rust fn preset_names() -> Vec<PresetId>; fn get_preset(id: Option<PresetId>) -> Option<serde_json::Value>; //`None` means default fn build_state(value: serde_json::Value); pub struct PresetId(Vec<u8>); ``` - **Breaking change**: Old `create_default_config` method was removed, `build_config` was renamed to `build_state`. As a consequence a node won't be able to interact with genesis config for older runtimes. The cleanup was made for sake of API simplicity. Also IMO maintaining compatibility with old API is not so crucial. - Reference implementation was provided for `substrate-test-runtime` and `rococo` runtimes. For rococo new [`genesis_configs_presets`](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/polkadot/runtime/rococo/src/genesis_config_presets.rs#L530) module was added and is used in `GenesisBuilder` [_presets-related_](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/polkadot/runtime/rococo/src/lib.rs#L2462-L2485) methods. - The `chain-spec-builder` util was also improved and allows to ([_doc_](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/substrate/bin/utils/chain-spec-builder/src/lib.rs#L19)): - list presets provided by given runtime (`list-presets`), - display preset or default config provided by the runtime (`display-preset`), - build chain-spec using named preset (`create ... named-preset`), - The `ChainSpecBuilder` is extended with [`with_genesis_config_preset_name`](https://github.com/paritytech/polkadot-sdk/blob/3b41d66b97c5ff0ec4a1989da5ffd8b9f3f588e3/substrate/client/chain-spec/src/chain_spec.rs#L447) method which allows to build chain-spec using named preset provided by the runtime. Sample usage on the node side [here](https://github.com/paritytech/polkadot-sdk/blob/2caffaae803e08a3d5b46c860e8016da023ff4ce/polkadot/node/service/src/chain_spec.rs#L404). Implementation of #1984. fixes: #150 part of: #25 --------- Co-authored-by: Sebastian Kunert <skunert49@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
255 lines
7.5 KiB
Rust
255 lines
7.5 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
// Make the WASM binary available.
|
|
#[cfg(feature = "std")]
|
|
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
|
|
|
use frame::{
|
|
deps::frame_support::{
|
|
genesis_builder_helper::{build_state, get_preset},
|
|
weights::{FixedFee, NoFee},
|
|
},
|
|
prelude::*,
|
|
runtime::{
|
|
apis::{
|
|
self, impl_runtime_apis, ApplyExtrinsicResult, CheckInherentsResult,
|
|
ExtrinsicInclusionMode, OpaqueMetadata,
|
|
},
|
|
prelude::*,
|
|
},
|
|
};
|
|
|
|
#[runtime_version]
|
|
pub const VERSION: RuntimeVersion = RuntimeVersion {
|
|
spec_name: create_runtime_str!("minimal-template-runtime"),
|
|
impl_name: create_runtime_str!("minimal-template-runtime"),
|
|
authoring_version: 1,
|
|
spec_version: 0,
|
|
impl_version: 1,
|
|
apis: RUNTIME_API_VERSIONS,
|
|
transaction_version: 1,
|
|
state_version: 1,
|
|
};
|
|
|
|
/// The version information used to identify this runtime when compiled natively.
|
|
#[cfg(feature = "std")]
|
|
pub fn native_version() -> NativeVersion {
|
|
NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
|
|
}
|
|
|
|
type SignedExtra = (
|
|
frame_system::CheckNonZeroSender<Runtime>,
|
|
frame_system::CheckSpecVersion<Runtime>,
|
|
frame_system::CheckTxVersion<Runtime>,
|
|
frame_system::CheckGenesis<Runtime>,
|
|
frame_system::CheckEra<Runtime>,
|
|
frame_system::CheckNonce<Runtime>,
|
|
frame_system::CheckWeight<Runtime>,
|
|
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
|
|
);
|
|
|
|
construct_runtime!(
|
|
pub enum Runtime {
|
|
System: frame_system,
|
|
Timestamp: pallet_timestamp,
|
|
|
|
Balances: pallet_balances,
|
|
Sudo: pallet_sudo,
|
|
TransactionPayment: pallet_transaction_payment,
|
|
|
|
// our local pallet
|
|
Template: pallet_minimal_template,
|
|
}
|
|
);
|
|
|
|
parameter_types! {
|
|
pub const Version: RuntimeVersion = VERSION;
|
|
}
|
|
|
|
#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig)]
|
|
impl frame_system::Config for Runtime {
|
|
type Block = Block;
|
|
type Version = Version;
|
|
type BlockHashCount = ConstU32<1024>;
|
|
type AccountData = pallet_balances::AccountData<<Runtime as pallet_balances::Config>::Balance>;
|
|
}
|
|
|
|
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
|
|
impl pallet_balances::Config for Runtime {
|
|
type AccountStore = System;
|
|
}
|
|
|
|
#[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig)]
|
|
impl pallet_sudo::Config for Runtime {}
|
|
|
|
#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
|
|
impl pallet_timestamp::Config for Runtime {}
|
|
|
|
#[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)]
|
|
impl pallet_transaction_payment::Config for Runtime {
|
|
type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter<Balances, ()>;
|
|
type WeightToFee = NoFee<<Self as pallet_balances::Config>::Balance>;
|
|
type LengthToFee = FixedFee<1, <Self as pallet_balances::Config>::Balance>;
|
|
}
|
|
|
|
impl pallet_minimal_template::Config for Runtime {}
|
|
|
|
type Block = frame::runtime::types_common::BlockOf<Runtime, SignedExtra>;
|
|
type Header = HeaderFor<Runtime>;
|
|
|
|
type RuntimeExecutive =
|
|
Executive<Runtime, Block, frame_system::ChainContext<Runtime>, Runtime, AllPalletsWithSystem>;
|
|
|
|
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
|
|
|
|
impl_runtime_apis! {
|
|
impl apis::Core<Block> for Runtime {
|
|
fn version() -> RuntimeVersion {
|
|
VERSION
|
|
}
|
|
|
|
fn execute_block(block: Block) {
|
|
RuntimeExecutive::execute_block(block)
|
|
}
|
|
|
|
fn initialize_block(header: &Header) -> ExtrinsicInclusionMode {
|
|
RuntimeExecutive::initialize_block(header)
|
|
}
|
|
}
|
|
impl apis::Metadata<Block> for Runtime {
|
|
fn metadata() -> OpaqueMetadata {
|
|
OpaqueMetadata::new(Runtime::metadata().into())
|
|
}
|
|
|
|
fn metadata_at_version(version: u32) -> Option<OpaqueMetadata> {
|
|
Runtime::metadata_at_version(version)
|
|
}
|
|
|
|
fn metadata_versions() -> Vec<u32> {
|
|
Runtime::metadata_versions()
|
|
}
|
|
}
|
|
|
|
impl apis::BlockBuilder<Block> for Runtime {
|
|
fn apply_extrinsic(extrinsic: ExtrinsicFor<Runtime>) -> ApplyExtrinsicResult {
|
|
RuntimeExecutive::apply_extrinsic(extrinsic)
|
|
}
|
|
|
|
fn finalize_block() -> HeaderFor<Runtime> {
|
|
RuntimeExecutive::finalize_block()
|
|
}
|
|
|
|
fn inherent_extrinsics(data: InherentData) -> Vec<ExtrinsicFor<Runtime>> {
|
|
data.create_extrinsics()
|
|
}
|
|
|
|
fn check_inherents(
|
|
block: Block,
|
|
data: InherentData,
|
|
) -> CheckInherentsResult {
|
|
data.check_extrinsics(&block)
|
|
}
|
|
}
|
|
|
|
impl apis::TaggedTransactionQueue<Block> for Runtime {
|
|
fn validate_transaction(
|
|
source: TransactionSource,
|
|
tx: ExtrinsicFor<Runtime>,
|
|
block_hash: <Runtime as frame_system::Config>::Hash,
|
|
) -> TransactionValidity {
|
|
RuntimeExecutive::validate_transaction(source, tx, block_hash)
|
|
}
|
|
}
|
|
|
|
impl apis::OffchainWorkerApi<Block> for Runtime {
|
|
fn offchain_worker(header: &HeaderFor<Runtime>) {
|
|
RuntimeExecutive::offchain_worker(header)
|
|
}
|
|
}
|
|
|
|
impl apis::SessionKeys<Block> for Runtime {
|
|
fn generate_session_keys(_seed: Option<Vec<u8>>) -> Vec<u8> {
|
|
Default::default()
|
|
}
|
|
|
|
fn decode_session_keys(
|
|
_encoded: Vec<u8>,
|
|
) -> Option<Vec<(Vec<u8>, apis::KeyTypeId)>> {
|
|
Default::default()
|
|
}
|
|
}
|
|
|
|
impl apis::AccountNonceApi<Block, interface::AccountId, interface::Nonce> for Runtime {
|
|
fn account_nonce(account: interface::AccountId) -> interface::Nonce {
|
|
System::account_nonce(account)
|
|
}
|
|
}
|
|
|
|
impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<
|
|
Block,
|
|
interface::Balance,
|
|
> for Runtime {
|
|
fn query_info(uxt: ExtrinsicFor<Runtime>, len: u32) -> RuntimeDispatchInfo<interface::Balance> {
|
|
TransactionPayment::query_info(uxt, len)
|
|
}
|
|
fn query_fee_details(uxt: ExtrinsicFor<Runtime>, len: u32) -> FeeDetails<interface::Balance> {
|
|
TransactionPayment::query_fee_details(uxt, len)
|
|
}
|
|
fn query_weight_to_fee(weight: Weight) -> interface::Balance {
|
|
TransactionPayment::weight_to_fee(weight)
|
|
}
|
|
fn query_length_to_fee(length: u32) -> interface::Balance {
|
|
TransactionPayment::length_to_fee(length)
|
|
}
|
|
}
|
|
|
|
impl sp_genesis_builder::GenesisBuilder<Block> for Runtime {
|
|
fn build_state(config: Vec<u8>) -> sp_genesis_builder::Result {
|
|
build_state::<RuntimeGenesisConfig>(config)
|
|
}
|
|
|
|
fn get_preset(id: &Option<sp_genesis_builder::PresetId>) -> Option<Vec<u8>> {
|
|
get_preset::<RuntimeGenesisConfig>(id, |_| None)
|
|
}
|
|
|
|
fn preset_names() -> Vec<sp_genesis_builder::PresetId> {
|
|
vec![]
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Some re-exports that the node side code needs to know. Some are useful in this context as well.
|
|
///
|
|
/// Other types should preferably be private.
|
|
// TODO: this should be standardized in some way, see:
|
|
// https://github.com/paritytech/substrate/issues/10579#issuecomment-1600537558
|
|
pub mod interface {
|
|
use super::Runtime;
|
|
use frame::deps::frame_system;
|
|
|
|
pub type Block = super::Block;
|
|
pub use frame::runtime::types_common::OpaqueBlock;
|
|
pub type AccountId = <Runtime as frame_system::Config>::AccountId;
|
|
pub type Nonce = <Runtime as frame_system::Config>::Nonce;
|
|
pub type Hash = <Runtime as frame_system::Config>::Hash;
|
|
pub type Balance = <Runtime as pallet_balances::Config>::Balance;
|
|
pub type MinimumBalance = <Runtime as pallet_balances::Config>::ExistentialDeposit;
|
|
}
|