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

Commit

Permalink
Weight annotation. (#3157)
Browse files Browse the repository at this point in the history
* Make extrinsics extensible.

Also Remove old extrinsic types.

* Rest of mockup. Add tips.

* Fix some build issues

* Runtiem builds :)

* Substrate builds.

* Fix a doc test

* Compact encoding

* Extract out the era logic into an extension

* Weight Check signed extension. (#3115)

* Weight signed extension.

* Revert a bit + test for check era.

* Update Cargo.toml

* Update node/cli/src/factory_impl.rs

* Update node/executor/src/lib.rs

* Update node/executor/src/lib.rs

* Don't use len for weight - use data.

* Operational Transaction; second attempt (#3138)

* working poc added.

* some fixes.

* Update doc.

* Fix all tests + final logic.

* more refactoring.

* nits.

* System block limit in bytes.

* Silent the storage macro warnings.

* More logic more tests.

* Fix import.

* Refactor names.

* Fix build.

* Update srml/balances/src/lib.rs

* Final refactor.

* Bump transaction version

* Fix weight mult test.

* Fix more tests and improve doc.

* Bump.

* Make some tests work again.

* Fix subkey.

* Remove todos + bump.

* First draft of annotating weights.

* Refactor weight to u64.

* More refactoring and tests.

* New convert for weight to fee

* more tests.

* remove merge redundancy.

* Fix system test.

* Bring back subkey stuff.

* a few stress tests.

* fix some of the grumbles.

* Final nits.

* Update srml/system/src/lib.rs

Co-Authored-By: DemiMarie-parity <[email protected]>

* Scale weights by 1000.

* Bump.

* Fix decl_storage test.
  • Loading branch information
kianenigma authored and bkchr committed Jul 25, 2019
1 parent 7702f1d commit f9e5a37
Show file tree
Hide file tree
Showing 44 changed files with 844 additions and 259 deletions.
31 changes: 21 additions & 10 deletions core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ impl Permill {
/// Everything.
pub fn one() -> Self { Self(1_000_000) }

/// create a new raw instance. This can be called at compile time.
pub const fn from_const_parts(parts: u32) -> Self {
Self([parts, 1_000_000][(parts > 1_000_000) as usize])
}

/// From an explicitly defined number of parts per maximum of the type.
pub fn from_parts(x: u32) -> Self { Self(x.min(1_000_000)) }
pub fn from_parts(parts: u32) -> Self { Self::from_const_parts(parts) }

/// Converts from a percent. Equal to `x / 100`.
pub fn from_percent(x: u32) -> Self { Self(x.min(100) * 10_000) }
pub const fn from_percent(x: u32) -> Self { Self([x, 100][(x > 100) as usize] * 10_000) }

/// Converts a fraction into `Permill`.
#[cfg(feature = "std")]
Expand Down Expand Up @@ -286,11 +291,16 @@ impl Perbill {
/// Everything.
pub fn one() -> Self { Self(1_000_000_000) }

/// create a new raw instance. This can be called at compile time.
pub const fn from_const_parts(parts: u32) -> Self {
Self([parts, 1_000_000_000][(parts > 1_000_000_000) as usize])
}

/// From an explicitly defined number of parts per maximum of the type.
pub fn from_parts(x: u32) -> Self { Self(x.min(1_000_000_000)) }
pub fn from_parts(parts: u32) -> Self { Self::from_const_parts(parts) }

/// Converts from a percent. Equal to `x / 100`.
pub fn from_percent(x: u32) -> Self { Self(x.min(100) * 10_000_000) }
pub const fn from_percent(x: u32) -> Self { Self([x, 100][(x > 100) as usize] * 10_000_000) }

/// Construct new instance where `x` is in millionths. Value equivalent to `x / 1,000,000`.
pub fn from_millionths(x: u32) -> Self { Self(x.min(1_000_000) * 1000) }
Expand Down Expand Up @@ -411,11 +421,12 @@ impl Fixed64 {

/// Performs a saturated multiply and accumulate.
///
/// Returns `n + (self * n)`.
/// Returns a saturated `n + (self * n)`.
/// TODO: generalize this to any weight type. #3189
pub fn saturated_multiply_accumulate(&self, int: u32) -> u32 {
let parts = self.0;

let positive = parts > 0;

// natural parts might overflow.
let natural_parts = self.clone().saturated_into::<u32>();
// fractional parts can always fit into u32.
Expand Down Expand Up @@ -459,8 +470,8 @@ impl Saturating for Fixed64 {
}
}

/// Note that this is a standard, _potentially-panicking_, implementation. Use `Saturating` trait for
/// safe addition.
/// Note that this is a standard, _potentially-panicking_, implementation. Use `Saturating` trait
/// for safe addition.
impl ops::Add for Fixed64 {
type Output = Self;

Expand All @@ -469,8 +480,8 @@ impl ops::Add for Fixed64 {
}
}

/// Note that this is a standard, _potentially-panicking_, implementation. Use `Saturating` trait for
/// safe subtraction.
/// Note that this is a standard, _potentially-panicking_, implementation. Use `Saturating` trait
/// for safe subtraction.
impl ops::Sub for Fixed64 {
type Output = Self;

Expand Down
2 changes: 1 addition & 1 deletion core/sr-primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl<Call: Encode, Extra: Encode> GetDispatchInfo for TestXt<Call, Extra> {
fn get_dispatch_info(&self) -> DispatchInfo {
// for testing: weight == size.
DispatchInfo {
weight: self.encode().len() as u32,
weight: self.encode().len() as _,
..Default::default()
}
}
Expand Down
15 changes: 9 additions & 6 deletions core/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ impl<T> Convert<T, T> for Identity {

/// A structure that performs standard conversion using the standard Rust conversion traits.
pub struct ConvertInto;

impl<A, B: From<A>> Convert<A, B> for ConvertInto {
fn convert(a: A) -> B { a.into() }
}
Expand Down Expand Up @@ -771,6 +770,9 @@ pub enum DispatchError {
/// General error to do with the inability to pay some fees (e.g. account balance too low).
Payment,

/// General error to do with the exhaustion of block resources.
Resource,

/// General error to do with the permissions of the sender.
NoPermission,

Expand All @@ -794,11 +796,12 @@ impl From<DispatchError> for i8 {
fn from(e: DispatchError) -> i8 {
match e {
DispatchError::Payment => -64,
DispatchError::NoPermission => -65,
DispatchError::BadState => -66,
DispatchError::Stale => -67,
DispatchError::Future => -68,
DispatchError::BadProof => -69,
DispatchError::Resource => -65,
DispatchError::NoPermission => -66,
DispatchError::BadState => -67,
DispatchError::Stale => -68,
DispatchError::Future => -69,
DispatchError::BadProof => -70,
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions core/sr-primitives/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ impl<T> ClassifyDispatch<T> for SimpleDispatchInfo {

impl Default for SimpleDispatchInfo {
fn default() -> Self {
// This implies that the weight is currently equal to 100, nothing more
// for all substrate transactions that do NOT explicitly annotate weight.
// TODO #2431 needs to be updated with proper max values.
SimpleDispatchInfo::FixedNormal(100)
// Default weight of all transactions.
SimpleDispatchInfo::FixedNormal(10_000)
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ use substrate_client::{
impl_runtime_apis,
};
use runtime_primitives::{
ApplyResult,
create_runtime_str,
ApplyResult, create_runtime_str, Perbill,
transaction_validity::{TransactionValidity, ValidTransaction},
traits::{
BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT,
Expand Down Expand Up @@ -330,6 +329,7 @@ parameter_types! {
pub const MinimumPeriod: u64 = 5;
pub const MaximumBlockWeight: u32 = 4 * 1024 * 1024;
pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
}

impl srml_system::Trait for Runtime {
Expand All @@ -346,6 +346,7 @@ impl srml_system::Trait for Runtime {
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
}

impl srml_timestamp::Trait for Runtime {
Expand Down
24 changes: 11 additions & 13 deletions node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));

#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use parity_codec::{Encode, Decode};
use rstd::prelude::*;
#[cfg(feature = "std")]
use primitives::bytes;
use primitives::{ed25519, sr25519, OpaqueMetadata};
use sr_primitives::{
ApplyResult, transaction_validity::TransactionValidity, generic, create_runtime_str,
traits::{self, NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify}, weights::Weight,
};
use sr_primitives::{ApplyResult, transaction_validity::TransactionValidity, generic, create_runtime_str};
use sr_primitives::traits::{NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify, ConvertInto};
use sr_primitives::weights::Weight;
use client::{
block_builder::api::{CheckInherentsResult, InherentData, self as block_builder_api},
runtime_api, impl_runtime_apis
Expand Down Expand Up @@ -102,8 +96,9 @@ pub fn native_version() -> NativeVersion {

parameter_types! {
pub const BlockHashCount: BlockNumber = 250;
pub const MaximumBlockWeight: Weight = 4 * 1024 * 1024;
pub const MaximumBlockLength: u32 = 4 * 1024 * 1024;
pub const MaximumBlockWeight: Weight = 1_000_000;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
}

impl system::Trait for Runtime {
Expand Down Expand Up @@ -133,6 +128,8 @@ impl system::Trait for Runtime {
type MaximumBlockWeight = MaximumBlockWeight;
/// Maximum size of all encoded transactions (in bytes) that are allowed in one block.
type MaximumBlockLength = MaximumBlockLength;
/// Portion of the block weight that is available to all normal transactions.
type AvailableBlockRatio = AvailableBlockRatio;
}

impl aura::Trait for Runtime {
Expand Down Expand Up @@ -166,8 +163,8 @@ parameter_types! {
pub const ExistentialDeposit: u128 = 500;
pub const TransferFee: u128 = 0;
pub const CreationFee: u128 = 0;
pub const TransactionBaseFee: u128 = 1;
pub const TransactionByteFee: u128 = 0;
pub const TransactionBaseFee: u128 = 0;
pub const TransactionByteFee: u128 = 1;
}

impl balances::Trait for Runtime {
Expand All @@ -188,6 +185,7 @@ impl balances::Trait for Runtime {
type CreationFee = CreationFee;
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
}

impl sudo::Trait for Runtime {
Expand Down
3 changes: 3 additions & 0 deletions node-template/runtime/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tests {
use support::{impl_outer_origin, assert_ok, parameter_types};
use sr_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};
use sr_primitives::weights::Weight;
use sr_primitives::Perbill;

impl_outer_origin! {
pub enum Origin for Test {}
Expand All @@ -88,6 +89,7 @@ mod tests {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
}
impl system::Trait for Test {
type Origin = Origin;
Expand All @@ -103,6 +105,7 @@ mod tests {
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
}
impl Trait for Test {
type Event = ();
Expand Down
3 changes: 2 additions & 1 deletion node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ use node_runtime::{
BabeConfig, BalancesConfig, ContractsConfig, CouncilConfig, DemocracyConfig,
ElectionsConfig, GrandpaConfig, ImOnlineConfig, IndicesConfig, Perbill,
SessionConfig, SessionKeys, StakerStatus, StakingConfig, SudoConfig, SystemConfig,
TechnicalCommitteeConfig, DAYS, DOLLARS, MILLICENTS, WASM_BINARY,
TechnicalCommitteeConfig, WASM_BINARY,
};
use node_runtime::constants::{time::*, currency::*};
pub use node_runtime::GenesisConfig;
use substrate_service;
use hex_literal::hex;
Expand Down
3 changes: 2 additions & 1 deletion node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ mod tests {
use babe::CompatibleDigestItem;
use consensus_common::{Environment, Proposer, BlockImportParams, BlockOrigin, ForkChoiceStrategy};
use node_primitives::DigestItem;
use node_runtime::{BalancesCall, Call, CENTS, SECS_PER_BLOCK, UncheckedExtrinsic};
use node_runtime::{BalancesCall, Call, UncheckedExtrinsic};
use node_runtime::constants::{currency::CENTS, time::SECS_PER_BLOCK};
use parity_codec::{Encode, Decode};
use primitives::{
crypto::Pair as CryptoPair, blake2_256,
Expand Down
1 change: 1 addition & 0 deletions node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ wabt = "~0.7.4"

[features]
benchmarks = []
stress-test = []
Loading

0 comments on commit f9e5a37

Please sign in to comment.