// This file is part of Bizinikiwi. // 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. //! A minimal runtime that includes the template [`pallet`](`pezpallet_minimal_template`). #![cfg_attr(not(feature = "std"), no_std)] // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); extern crate alloc; use alloc::vec::Vec; use pezpallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; use pezkuwi_sdk::{ pezkuwi_sdk_frame::{ self as frame, deps::pezsp_genesis_builder, runtime::{apis, prelude::*}, }, *, }; /// Provides getters for genesis configuration presets. pub mod genesis_config_presets { use super::*; use crate::{ interface::{Balance, MinimumBalance}, pezsp_keyring::Sr25519Keyring, BalancesConfig, RuntimeGenesisConfig, SudoConfig, }; use alloc::{vec, vec::Vec}; use serde_json::Value; /// Returns a development genesis config preset. pub fn development_config_genesis() -> Value { let endowment = >::get().max(1) * 1000; pezframe_support::build_struct_json_patch!(RuntimeGenesisConfig { balances: BalancesConfig { balances: Sr25519Keyring::iter() .map(|a| (a.to_account_id(), endowment)) .collect::>(), }, sudo: SudoConfig { key: Some(Sr25519Keyring::Alice.to_account_id()) }, }) } /// Get the set of the available genesis config presets. pub fn get_preset(id: &PresetId) -> Option> { let patch = match id.as_ref() { pezsp_genesis_builder::DEV_RUNTIME_PRESET => development_config_genesis(), _ => return None, }; Some( serde_json::to_string(&patch) .expect("serialization to json is expected to work. qed.") .into_bytes(), ) } /// List of supported presets. pub fn preset_names() -> Vec { vec![PresetId::from(pezsp_genesis_builder::DEV_RUNTIME_PRESET)] } } /// The runtime version. #[runtime_version] pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: alloc::borrow::Cow::Borrowed("minimal-template-runtime"), impl_name: alloc::borrow::Cow::Borrowed("minimal-template-runtime"), authoring_version: 1, spec_version: 0, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, system_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() } } /// The transaction extensions that are added to the runtime. type TxExtension = ( // Authorize calls that validate themselves. pezframe_system::AuthorizeCall, // Checks that the sender is not the zero address. pezframe_system::CheckNonZeroSender, // Checks that the runtime version is correct. pezframe_system::CheckSpecVersion, // Checks that the transaction version is correct. pezframe_system::CheckTxVersion, // Checks that the genesis hash is correct. pezframe_system::CheckGenesis, // Checks that the era is valid. pezframe_system::CheckEra, // Checks that the nonce is valid. pezframe_system::CheckNonce, // Checks that the weight is valid. pezframe_system::CheckWeight, // Ensures that the sender has enough funds to pay for the transaction // and deducts the fee from the sender's account. pezpallet_transaction_payment::ChargeTransactionPayment, // Reclaim the unused weight from the block using post dispatch information. // It must be last in the pipeline in order to catch the refund in previous transaction // extensions pezframe_system::WeightReclaim, ); // Composes the runtime by adding all the used pallets and deriving necessary types. #[frame_construct_runtime] mod runtime { /// The main runtime type. #[runtime::runtime] #[runtime::derive( RuntimeCall, RuntimeEvent, RuntimeError, RuntimeOrigin, RuntimeFreezeReason, RuntimeHoldReason, RuntimeSlashReason, RuntimeLockId, RuntimeTask, RuntimeViewFunction )] pub struct Runtime; /// Mandatory system pallet that should always be included in a FRAME runtime. #[runtime::pezpallet_index(0)] pub type System = pezframe_system::Pallet; /// Provides a way for consensus systems to set and check the onchain time. #[runtime::pezpallet_index(1)] pub type Timestamp = pezpallet_timestamp::Pallet; /// Provides the ability to keep track of balances. #[runtime::pezpallet_index(2)] pub type Balances = pezpallet_balances::Pallet; /// Provides a way to execute privileged functions. #[runtime::pezpallet_index(3)] pub type Sudo = pezpallet_sudo::Pallet; /// Provides the ability to charge for extrinsic execution. #[runtime::pezpallet_index(4)] pub type TransactionPayment = pezpallet_transaction_payment::Pallet; /// A minimal pallet template. #[runtime::pezpallet_index(5)] pub type Template = pezpallet_minimal_template::Pallet; } parameter_types! { pub const Version: RuntimeVersion = VERSION; } /// Implements the types required for the system pallet. #[derive_impl(pezframe_system::config_preludes::SolochainDefaultConfig)] impl pezframe_system::Config for Runtime { type Block = Block; type Version = Version; // Use the account data from the balances pallet type AccountData = pezpallet_balances::AccountData<::Balance>; } // Implements the types required for the balances pallet. #[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)] impl pezpallet_balances::Config for Runtime { type AccountStore = System; } // Implements the types required for the sudo pallet. #[derive_impl(pezpallet_sudo::config_preludes::TestDefaultConfig)] impl pezpallet_sudo::Config for Runtime {} // Implements the types required for the sudo pallet. #[derive_impl(pezpallet_timestamp::config_preludes::TestDefaultConfig)] impl pezpallet_timestamp::Config for Runtime {} // Implements the types required for the transaction payment pallet. #[derive_impl(pezpallet_transaction_payment::config_preludes::TestDefaultConfig)] impl pezpallet_transaction_payment::Config for Runtime { type OnChargeTransaction = pezpallet_transaction_payment::FungibleAdapter; // Setting fee as independent of the weight of the extrinsic for demo purposes type WeightToFee = NoFee<::Balance>; // Setting fee as fixed for any length of the call data for demo purposes type LengthToFee = FixedFee<1, ::Balance>; } // Implements the types required for the template pallet. impl pezpallet_minimal_template::Config for Runtime {} type Block = frame::runtime::types_common::BlockOf; type Header = HeaderFor; type RuntimeExecutive = Executive, Runtime, AllPalletsWithSystem>; impl_runtime_apis! { impl apis::Core for Runtime { fn version() -> RuntimeVersion { VERSION } fn execute_block(block: ::LazyBlock) { RuntimeExecutive::execute_block(block) } fn initialize_block(header: &Header) -> ExtrinsicInclusionMode { RuntimeExecutive::initialize_block(header) } } impl apis::Metadata for Runtime { fn metadata() -> OpaqueMetadata { OpaqueMetadata::new(Runtime::metadata().into()) } fn metadata_at_version(version: u32) -> Option { Runtime::metadata_at_version(version) } fn metadata_versions() -> Vec { Runtime::metadata_versions() } } impl apis::BlockBuilder for Runtime { fn apply_extrinsic(extrinsic: ExtrinsicFor) -> ApplyExtrinsicResult { RuntimeExecutive::apply_extrinsic(extrinsic) } fn finalize_block() -> HeaderFor { RuntimeExecutive::finalize_block() } fn inherent_extrinsics(data: InherentData) -> Vec> { data.create_extrinsics() } fn check_inherents( block: ::LazyBlock, data: InherentData, ) -> CheckInherentsResult { data.check_extrinsics(&block) } } impl apis::TaggedTransactionQueue for Runtime { fn validate_transaction( source: TransactionSource, tx: ExtrinsicFor, block_hash: ::Hash, ) -> TransactionValidity { RuntimeExecutive::validate_transaction(source, tx, block_hash) } } impl apis::OffchainWorkerApi for Runtime { fn offchain_worker(header: &HeaderFor) { RuntimeExecutive::offchain_worker(header) } } impl apis::SessionKeys for Runtime { fn generate_session_keys(_seed: Option>) -> Vec { Default::default() } fn decode_session_keys( _encoded: Vec, ) -> Option, apis::KeyTypeId)>> { Default::default() } } impl apis::AccountNonceApi for Runtime { fn account_nonce(account: interface::AccountId) -> interface::Nonce { System::account_nonce(account) } } impl pezpallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi< Block, interface::Balance, > for Runtime { fn query_info(uxt: ExtrinsicFor, len: u32) -> RuntimeDispatchInfo { TransactionPayment::query_info(uxt, len) } fn query_fee_details(uxt: ExtrinsicFor, len: u32) -> FeeDetails { 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 apis::GenesisBuilder for Runtime { fn build_state(config: Vec) -> pezsp_genesis_builder::Result { build_state::(config) } fn get_preset(id: &Option) -> Option> { get_preset::(id, self::genesis_config_presets::get_preset) } fn preset_names() -> Vec { self::genesis_config_presets::preset_names() } } } /// 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/pezkuwichain/kurdistan-sdk/issues/3 pub mod interface { use super::Runtime; use pezkuwi_sdk::{pezkuwi_sdk_frame as frame, *}; pub type Block = super::Block; pub use frame::runtime::types_common::OpaqueBlock; pub type AccountId = ::AccountId; pub type Nonce = ::Nonce; pub type Hash = ::Hash; pub type Balance = ::Balance; pub type MinimumBalance = ::ExistentialDeposit; }