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

Commit

Permalink
refactor constants & primitives (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
borispovod committed Aug 5, 2021
1 parent e66caee commit 8382b58
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 54 deletions.
6 changes: 4 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use sp_core::{Pair, Public, sr25519};
use mv_node_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SudoConfig,
SystemConfig, VestingConfig, WASM_BINARY, Signature, PONT, DECIMALS, MvmConfig,
AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SudoConfig, SystemConfig,
VestingConfig, WASM_BINARY, MvmConfig,
primitives::{AccountId, Signature},
constants::currency::{PONT, DECIMALS},
};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_finality_grandpa::AuthorityId as GrandpaId;
Expand Down
5 changes: 4 additions & 1 deletion node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

use std::sync::Arc;

use mv_node_runtime::{opaque::Block, AccountId, Balance, Index};
use mv_node_runtime::{
opaque::Block,
primitives::{AccountId, Balance, Index},
};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Error as BlockChainError, HeaderMetadata, HeaderBackend};
use sp_block_builder::BlockBuilder;
Expand Down
38 changes: 38 additions & 0 deletions runtime/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Currency constants.
pub mod currency {
use crate::primitives::Balance;

// Currencies constants.
// Decimals.
pub const DECIMALS: u32 = 10;

// 1 PONT.
pub const PONT: Balance = u64::pow(10, DECIMALS);
}

// Time related constants.
pub mod time {
use crate::primitives::{BlockNumber};

/// This determines the average expected block time that we are targeting.
/// Blocks will be produced at a minimum duration defined by `SLOT_DURATION`.
/// `SLOT_DURATION` is picked up by `pallet_timestamp` which is in turn picked
/// up by `pallet_aura` to implement `fn slot_duration()`.
///
/// Change this to adjust the block time.
pub const MILLISECS_PER_BLOCK: u64 = 6000;

// Network slot duration allocated for block producing.
pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;

// Time is measured by number of blocks.

// 10 blocks per minute.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);

// 600 blocks per hour.
pub const HOURS: BlockNumber = MINUTES * 60;

// 14400 blocks per day.
pub const DAYS: BlockNumber = HOURS * 24;
}
59 changes: 8 additions & 51 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
use sp_std::prelude::*;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
ApplyExtrinsicResult, generic, create_runtime_str, impl_opaque_keys, MultiSignature,
ApplyExtrinsicResult, generic, create_runtime_str, impl_opaque_keys,
transaction_validity::{TransactionValidity, TransactionSource},
};
use sp_runtime::traits::{
BlakeTwo256, Block as BlockT, AccountIdLookup, Verify, IdentifyAccount, NumberFor,
ConvertInto,
};
use sp_runtime::traits::{BlakeTwo256, Block as BlockT, AccountIdLookup, NumberFor, ConvertInto};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use pallet_grandpa::{AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList};
Expand All @@ -24,6 +21,11 @@ use sp_version::RuntimeVersion;
#[cfg(feature = "std")]
use sp_version::NativeVersion;

pub mod constants;
use constants::{currency::*, time::*};
pub mod primitives;
use primitives::*;

// A few exports that help ease life for downstream crates.
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
Expand All @@ -47,32 +49,6 @@ use pallet_transaction_payment::CurrencyAdapter;
pub use sp_mvm::gas::{GasWeightMapping};
pub use sp_mvm_rpc_runtime::types::MVMApiEstimation;

/// An index to a block.
pub type BlockNumber = u32;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

/// The type for looking up accounts. We don't expect more than 4 billion of them, but you
/// never know...
pub type AccountIndex = u32;

/// Balance of an account.
pub type Balance = u64;

/// Index of a transaction in the chain.
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;

/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
Expand Down Expand Up @@ -114,25 +90,6 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
transaction_version: 1,
};

/// This determines the average expected block time that we are targeting.
/// Blocks will be produced at a minimum duration defined by `SLOT_DURATION`.
/// `SLOT_DURATION` is picked up by `pallet_timestamp` which is in turn picked
/// up by `pallet_aura` to implement `fn slot_duration()`.
///
/// Change this to adjust the block time.
pub const MILLISECS_PER_BLOCK: u64 = 6000;

pub const SLOT_DURATION: u64 = MILLISECS_PER_BLOCK;

// Time is measured by number of blocks.
pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;

// Currencies constants.
pub const DECIMALS: u32 = 10;
pub const PONT: Balance = u64::pow(10, DECIMALS);

/// The version information used to identify this runtime when compiled natively.
#[cfg(feature = "std")]
pub fn native_version() -> NativeVersion {
Expand Down Expand Up @@ -243,7 +200,7 @@ impl pallet_timestamp::Config for Runtime {

parameter_types! {
// 1 PONT.
pub const MinVestedTransfer: Balance = PONT;
pub const MinVestedTransfer: Balance = 1 * PONT;
}

impl pallet_vesting::Config for Runtime {
Expand Down
30 changes: 30 additions & 0 deletions runtime/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_runtime::{generic, MultiSignature};
use sp_runtime::traits::{Verify, IdentifyAccount};

/// An index to a block.
pub type BlockNumber = u32;

/// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
pub type Signature = MultiSignature;

/// Some way of identifying an account on the chain. We intentionally make it equivalent
/// to the public key of our transaction signing scheme.
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

/// The type for looking up accounts. We don't expect more than 4 billion of them, but you
/// never know...
pub type AccountIndex = u32;

/// Balance of an account.
pub type Balance = u64;

/// Index of a transaction in the chain.
pub type Index = u32;

/// A hash of some data used by the chain.
pub type Hash = sp_core::H256;

/// Digest item type.
pub type DigestItem = generic::DigestItem<Hash>;

0 comments on commit 8382b58

Please sign in to comment.