git.delta.rocks / unique-network / refs/commits / 96b20f2918fb

difftreelog

source

runtime/src/lib.rs16.9 KiBsourcehistory
1//! The Substrate Node Template runtime. This can be compiled with `#[no_std]`, ready for Wasm.23#![cfg_attr(not(feature = "std"), no_std)]4// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.5#![recursion_limit = "256"]67// Make the WASM binary available.8#[cfg(feature = "std")]9include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));1011use contracts_rpc_runtime_api::ContractExecResult;12use grandpa::fg_primitives;13use grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};14use sp_api::impl_runtime_apis;15use sp_consensus_aura::sr25519::AuthorityId as AuraId;16use sp_core::{crypto::KeyTypeId, OpaqueMetadata};17use sp_runtime::{18    create_runtime_str, generic, impl_opaque_keys,19    traits::{20        BlakeTwo256, Block as BlockT, IdentifyAccount, IdentityLookup, NumberFor, Saturating,21        Verify,22    },23    transaction_validity::{TransactionSource, TransactionValidity},24    ApplyExtrinsicResult, MultiSignature,25};26use sp_std::prelude::*;27#[cfg(feature = "std")]28use sp_version::NativeVersion;29use sp_version::RuntimeVersion;3031// A few exports that help ease life for downstream crates.32pub use balances::Call as BalancesCall;33pub use contracts::Schedule as ContractsSchedule;34pub use frame_support::{35    construct_runtime,36    dispatch::DispatchResult,37    parameter_types,38    traits::{39        Currency, ExistenceRequirement, Get, KeyOwnerProofSystem, OnUnbalanced, Randomness,40        WithdrawReason,41    },42    weights::{43        constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},44        DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,45        WeightToFeePolynomial,46    },47    StorageValue,48};49#[cfg(any(feature = "std", test))]50pub use sp_runtime::BuildStorage;51use sp_runtime::Perbill;52use system::{self as system};5354pub use timestamp::Call as TimestampCall;5556/// Importing a nft pallet57pub use nft;5859/// An index to a block.60pub type BlockNumber = u32;6162/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.63pub type Signature = MultiSignature;6465/// Some way of identifying an account on the chain. We intentionally make it equivalent66/// to the public key of our transaction signing scheme.67pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;6869/// The type for looking up accounts. We don't expect more than 4 billion of them, but you70/// never know...71pub type AccountIndex = u32;7273/// Balance of an account.74pub type Balance = u128;7576/// Index of a transaction in the chain.77pub type Index = u32;7879/// A hash of some data used by the chain.80pub type Hash = sp_core::H256;8182/// Digest item type.83pub type DigestItem = generic::DigestItem<Hash>;8485/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know86/// the specifics of the runtime. They can then be made to be agnostic over specific formats87/// of data like extrinsics, allowing for them to continue syncing the network through upgrades88/// to even the core data structures.89pub mod opaque {90    use super::*;9192    pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;9394    /// Opaque block header type.95    pub type Header = generic::Header<BlockNumber, BlakeTwo256>;96    /// Opaque block type.97    pub type Block = generic::Block<Header, UncheckedExtrinsic>;98    /// Opaque block identifier type.99    pub type BlockId = generic::BlockId<Block>;100101    impl_opaque_keys! {102        pub struct SessionKeys {103            pub aura: Aura,104            pub grandpa: Grandpa,105        }106    }107}108109/// This runtime version.110pub const VERSION: RuntimeVersion = RuntimeVersion {111    spec_name: create_runtime_str!("nft"),112    impl_name: create_runtime_str!("nft"),113    authoring_version: 1,114    spec_version: 1,115    impl_version: 1,116    apis: RUNTIME_API_VERSIONS,117    transaction_version: 1,118};119120pub const MILLISECS_PER_BLOCK: u64 = 6000;121122pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;123124// These time units are defined in number of blocks.125pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);126pub const HOURS: BlockNumber = MINUTES * 60;127pub const DAYS: BlockNumber = HOURS * 24;128129/// The version information used to identify this runtime when compiled natively.130#[cfg(feature = "std")]131pub fn native_version() -> NativeVersion {132    NativeVersion {133        runtime_version: VERSION,134        can_author_with: Default::default(),135    }136}137138parameter_types! {139    pub const BlockHashCount: BlockNumber = 2400;140    /// We allow for 2 seconds of compute with a 6 second average block time.141    pub const MaximumBlockWeight: Weight = 2 * WEIGHT_PER_SECOND;142    pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);143    /// Assume 10% of weight for average on_initialize calls.144    pub MaximumExtrinsicWeight: Weight = AvailableBlockRatio::get()145        .saturating_sub(Perbill::from_percent(10)) * MaximumBlockWeight::get();146    pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;147    pub const Version: RuntimeVersion = VERSION;148}149150impl system::Trait for Runtime {151    /// The basic call filter to use in dispatchable.152    type BaseCallFilter = ();153    /// The identifier used to distinguish between accounts.154    type AccountId = AccountId;155    /// The aggregated dispatch type that is available for extrinsics.156    type Call = Call;157    /// The lookup mechanism to get account ID from whatever is passed in dispatchers.158    type Lookup = IdentityLookup<AccountId>;159    /// The index type for storing how many extrinsics an account has signed.160    type Index = Index;161    /// The index type for blocks.162    type BlockNumber = BlockNumber;163    /// The type for hashing blocks and tries.164    type Hash = Hash;165    /// The hashing algorithm used.166    type Hashing = BlakeTwo256;167    /// The header type.168    type Header = generic::Header<BlockNumber, BlakeTwo256>;169    /// The ubiquitous event type.170    type Event = Event;171    /// The ubiquitous origin type.172    type Origin = Origin;173    /// Maximum number of block number to block hash mappings to keep (oldest pruned first).174    type BlockHashCount = BlockHashCount;175    /// Maximum weight of each block.176    type MaximumBlockWeight = MaximumBlockWeight;177    /// The weight of database operations that the runtime can invoke.178    type DbWeight = RocksDbWeight;179    /// The weight of the overhead invoked on the block import process, independent of the180    /// extrinsics included in that block.181    type BlockExecutionWeight = BlockExecutionWeight;182    /// The base weight of any extrinsic processed by the runtime, independent of the183    /// logic of that extrinsic. (Signature verification, nonce increment, fee, etc...)184    type ExtrinsicBaseWeight = ExtrinsicBaseWeight;185    /// The maximum weight that a single extrinsic of `Normal` dispatch class can have,186    /// idependent of the logic of that extrinsics. (Roughly max block weight - average on187    /// initialize cost).188    type MaximumExtrinsicWeight = MaximumExtrinsicWeight;189    /// Maximum size of all encoded transactions (in bytes) that are allowed in one block.190    type MaximumBlockLength = MaximumBlockLength;191    /// Portion of the block weight that is available to all normal transactions.192    type AvailableBlockRatio = AvailableBlockRatio;193    /// Version of the runtime.194    type Version = Version;195    /// Converts a module to the index of the module in `construct_runtime!`.196    /// This type is being generated by `construct_runtime!`.197    type ModuleToIndex = ModuleToIndex;198    /// What to do if a new account is created.199    type OnNewAccount = ();200    /// What to do if an account is fully reaped from the system.201    type OnKilledAccount = ();202    /// The data to be stored in an account.203    type AccountData = balances::AccountData<Balance>;204}205206impl aura::Trait for Runtime {207    type AuthorityId = AuraId;208}209210impl grandpa::Trait for Runtime {211    type Event = Event;212    type Call = Call;213214    type KeyOwnerProofSystem = ();215216    type KeyOwnerProof =217        <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(KeyTypeId, GrandpaId)>>::Proof;218219    type KeyOwnerIdentification = <Self::KeyOwnerProofSystem as KeyOwnerProofSystem<(220        KeyTypeId,221        GrandpaId,222    )>>::IdentificationTuple;223224    type HandleEquivocation = ();225}226227parameter_types! {228    pub const MinimumPeriod: u64 = SLOT_DURATION / 2;229}230231impl timestamp::Trait for Runtime {232    /// A timestamp: milliseconds since the unix epoch.233    type Moment = u64;234    type OnTimestampSet = Aura;235    type MinimumPeriod = MinimumPeriod;236}237238parameter_types! {239    // pub const ExistentialDeposit: u128 = 500;240    pub const ExistentialDeposit: u128 = 0;241}242243impl balances::Trait for Runtime {244    /// The type for recording an account's balance.245    type Balance = Balance;246    /// The ubiquitous event type.247    type Event = Event;248    type DustRemoval = ();249    type ExistentialDeposit = ExistentialDeposit;250    type AccountStore = System;251}252253pub const MILLICENTS: Balance = 1_000_000_000;254pub const CENTS: Balance = 1_000 * MILLICENTS;255pub const DOLLARS: Balance = 100 * CENTS;256257parameter_types! {258    pub const TombstoneDeposit: Balance = 16 * MILLICENTS;259    pub const RentByteFee: Balance = 4 * MILLICENTS;260    pub const RentDepositOffset: Balance = 1000 * MILLICENTS;261    pub const SurchargeReward: Balance = 150 * MILLICENTS;262}263264impl contracts::Trait for Runtime {265    type Call = Call;266    type Time = Timestamp;267    type Randomness = RandomnessCollectiveFlip;268    type Currency = Balances;269    type Event = Event;270    type DetermineContractAddress = contracts::SimpleAddressDeterminer<Runtime>;271    type TrieIdGenerator = contracts::TrieIdFromParentCounter<Runtime>;272    type RentPayment = ();273    type SignedClaimHandicap = contracts::DefaultSignedClaimHandicap;274    type TombstoneDeposit = TombstoneDeposit;275    type StorageSizeOffset = contracts::DefaultStorageSizeOffset;276    type RentByteFee = RentByteFee;277    type RentDepositOffset = RentDepositOffset;278    type SurchargeReward = SurchargeReward;279    type MaxDepth = contracts::DefaultMaxDepth;280    type MaxValueSize = contracts::DefaultMaxValueSize;281    type WeightPrice = transaction_payment::Module<Self>;282}283284parameter_types! {285    pub const TransactionByteFee: Balance = 1;286}287288impl transaction_payment::Trait for Runtime {289    type Currency = balances::Module<Runtime>;290    type OnTransactionPayment = ();291    type TransactionByteFee = TransactionByteFee;292    type WeightToFee = IdentityFee<Balance>;293    type FeeMultiplierUpdate = ();294}295296impl sudo::Trait for Runtime {297    type Event = Event;298    type Call = Call;299}300301/// Used for the module nft in `./nft.rs`302impl nft::Trait for Runtime {303    type Event = Event;304}305306construct_runtime!(307    pub enum Runtime where308        Block = Block,309        NodeBlock = opaque::Block,310        UncheckedExtrinsic = UncheckedExtrinsic311    {312        System: system::{Module, Call, Config, Storage, Event<T>},313        RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},314        Contracts: contracts::{Module, Call, Config, Storage, Event<T>},315        Timestamp: timestamp::{Module, Call, Storage, Inherent},316        Aura: aura::{Module, Config<T>, Inherent(Timestamp)},317        Grandpa: grandpa::{Module, Call, Storage, Config, Event},318        Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},319        TransactionPayment: transaction_payment::{Module, Storage},320        Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},321        Nft: nft::{Module, Call, Storage, Event<T>},322    }323);324325/// The address format for describing accounts.326pub type Address = AccountId;327/// Block header type as expected by this runtime.328pub type Header = generic::Header<BlockNumber, BlakeTwo256>;329/// Block type as expected by this runtime.330pub type Block = generic::Block<Header, UncheckedExtrinsic>;331/// A Block signed with a Justification332pub type SignedBlock = generic::SignedBlock<Block>;333/// BlockId type as expected by this runtime.334pub type BlockId = generic::BlockId<Block>;335/// The SignedExtension to the basic transaction logic.336pub type SignedExtra = (337    system::CheckSpecVersion<Runtime>,338    system::CheckTxVersion<Runtime>,339    system::CheckGenesis<Runtime>,340    system::CheckEra<Runtime>,341    system::CheckNonce<Runtime>,342    system::CheckWeight<Runtime>,343    nft::ChargeTransactionPayment<Runtime>,344);345/// Unchecked extrinsic type as expected by this runtime.346pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;347/// Extrinsic type that has already been checked.348pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;349/// Executive: handles dispatch to the various modules.350pub type Executive =351    frame_executive::Executive<Runtime, Block, system::ChainContext<Runtime>, Runtime, AllModules>;352353impl_runtime_apis! {354355    impl contracts_rpc_runtime_api::ContractsApi<Block, AccountId, Balance, BlockNumber>356    for Runtime357    {358        fn call(359            origin: AccountId,360            dest: AccountId,361            value: Balance,362            gas_limit: u64,363            input_data: Vec<u8>,364        ) -> ContractExecResult {365            let exec_result =366                Contracts::bare_call(origin, dest.into(), value, gas_limit, input_data);367            match exec_result {368                Ok(v) => ContractExecResult::Success {369                    status: v.status,370                    data: v.data,371                },372                Err(_) => ContractExecResult::Error,373            }374        }375376        fn get_storage(377            address: AccountId,378            key: [u8; 32],379        ) -> contracts_primitives::GetStorageResult {380            Contracts::get_storage(address, key)381        }382383        fn rent_projection(384            address: AccountId,385        ) -> contracts_primitives::RentProjectionResult<BlockNumber> {386            Contracts::rent_projection(address)387        }388    }389390    impl sp_api::Core<Block> for Runtime {391        fn version() -> RuntimeVersion {392            VERSION393        }394395        fn execute_block(block: Block) {396            Executive::execute_block(block)397        }398399        fn initialize_block(header: &<Block as BlockT>::Header) {400            Executive::initialize_block(header)401        }402    }403404    impl sp_api::Metadata<Block> for Runtime {405        fn metadata() -> OpaqueMetadata {406            Runtime::metadata().into()407        }408    }409410    impl sp_block_builder::BlockBuilder<Block> for Runtime {411        fn apply_extrinsic(extrinsic: <Block as BlockT>::Extrinsic) -> ApplyExtrinsicResult {412            Executive::apply_extrinsic(extrinsic)413        }414415        fn finalize_block() -> <Block as BlockT>::Header {416            Executive::finalize_block()417        }418419        fn inherent_extrinsics(data: sp_inherents::InherentData) -> Vec<<Block as BlockT>::Extrinsic> {420            data.create_extrinsics()421        }422423        fn check_inherents(424            block: Block,425            data: sp_inherents::InherentData,426        ) -> sp_inherents::CheckInherentsResult {427            data.check_extrinsics(&block)428        }429430        fn random_seed() -> <Block as BlockT>::Hash {431            RandomnessCollectiveFlip::random_seed()432        }433    }434435    impl sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block> for Runtime {436        fn validate_transaction(437            source: TransactionSource,438            tx: <Block as BlockT>::Extrinsic,439        ) -> TransactionValidity {440            Executive::validate_transaction(source, tx)441        }442    }443444    impl sp_offchain::OffchainWorkerApi<Block> for Runtime {445        fn offchain_worker(header: &<Block as BlockT>::Header) {446            Executive::offchain_worker(header)447        }448    }449450    impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {451        fn slot_duration() -> u64 {452            Aura::slot_duration()453        }454455        fn authorities() -> Vec<AuraId> {456            Aura::authorities()457        }458    }459460    impl sp_session::SessionKeys<Block> for Runtime {461        fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {462            opaque::SessionKeys::generate(seed)463        }464465        fn decode_session_keys(466            encoded: Vec<u8>,467        ) -> Option<Vec<(Vec<u8>, KeyTypeId)>> {468            opaque::SessionKeys::decode_into_raw_public_keys(&encoded)469        }470    }471472    impl fg_primitives::GrandpaApi<Block> for Runtime {473        fn grandpa_authorities() -> GrandpaAuthorityList {474            Grandpa::grandpa_authorities()475        }476477        fn submit_report_equivocation_extrinsic(478            _equivocation_proof: fg_primitives::EquivocationProof<479                <Block as BlockT>::Hash,480                NumberFor<Block>,481            >,482            _key_owner_proof: fg_primitives::OpaqueKeyOwnershipProof,483        ) -> Option<()> {484            None485        }486487        fn generate_key_ownership_proof(488            _set_id: fg_primitives::SetId,489            _authority_id: GrandpaId,490        ) -> Option<fg_primitives::OpaqueKeyOwnershipProof> {491            // NOTE: this is the only implementation possible since we've492            // defined our key owner proof type as a bottom type (i.e. a type493            // with no values).494            None495        }496    }497}