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

Weight annotation. #3157

Merged
merged 45 commits into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b042a93
Make extrinsics extensible.
gavofyork Jul 11, 2019
37f6ae0
Rest of mockup. Add tips.
gavofyork Jul 11, 2019
2e5b1f4
Fix some build issues
gavofyork Jul 11, 2019
b7646ec
Runtiem builds :)
gavofyork Jul 11, 2019
a871c9f
Substrate builds.
kianenigma Jul 12, 2019
8e7c803
Fix a doc test
gavofyork Jul 13, 2019
a824b56
Compact encoding
gavofyork Jul 13, 2019
5de080f
Extract out the era logic into an extension
gavofyork Jul 15, 2019
a8789b9
Weight Check signed extension. (#3115)
kianenigma Jul 16, 2019
7d96429
Merge remote-tracking branch 'origin/master' into gav-extensble-trans…
gavofyork Jul 16, 2019
30b4ba7
Don't use len for weight - use data.
gavofyork Jul 16, 2019
2a9c9df
Merge remote-tracking branch 'origin/master' into gav-extensble-trans…
gavofyork Jul 19, 2019
342efb5
Operational Transaction; second attempt (#3138)
kianenigma Jul 19, 2019
b8f564e
Bump transaction version
jacogr Jul 19, 2019
07fdfe2
Merge branch 'gav-extensble-transactions' of github.com:paritytech/su…
jacogr Jul 19, 2019
7f33006
Master.into()
kianenigma Jul 19, 2019
36063fe
Merge branch 'gav-extensble-transactions' of github.com:paritytech/su…
kianenigma Jul 19, 2019
7a0fbc9
Fix weight mult test.
kianenigma Jul 19, 2019
84fa279
Fix more tests and improve doc.
kianenigma Jul 19, 2019
f4d4579
Bump.
kianenigma Jul 19, 2019
def6425
Merge remote-tracking branch 'origin/master' into gav-extensble-trans…
gavofyork Jul 20, 2019
d12713a
Make some tests work again.
kianenigma Jul 20, 2019
3350f9c
Fix subkey.
kianenigma Jul 20, 2019
b788507
Remove todos + bump.
kianenigma Jul 20, 2019
b9b6b53
First draft of annotating weights.
kianenigma Jul 21, 2019
045681e
Refactor weight to u64.
kianenigma Jul 22, 2019
bfcfa36
More refactoring and tests.
kianenigma Jul 23, 2019
107801f
One hell of a merge conflict.
kianenigma Jul 23, 2019
c100df9
New convert for weight to fee
kianenigma Jul 23, 2019
f5d33c6
more tests.
kianenigma Jul 23, 2019
0c1c268
remove merge redundancy.
kianenigma Jul 23, 2019
6a0a1d4
Fix system test.
kianenigma Jul 24, 2019
b06ef82
Master.into()
kianenigma Jul 24, 2019
c3e0ee3
Bring back subkey stuff.
kianenigma Jul 24, 2019
5b0c715
a few stress tests.
kianenigma Jul 24, 2019
e683829
fix some of the grumbles.
kianenigma Jul 24, 2019
ce11fc5
Merge branch 'master' of github.com:paritytech/substrate into kiz-ann…
kianenigma Jul 24, 2019
0b592e2
Final nits.
kianenigma Jul 24, 2019
ee970d1
Merge branch 'master' into kiz-annotate-weights
Demi-Marie Jul 24, 2019
58191a3
Update srml/system/src/lib.rs
kianenigma Jul 24, 2019
67b268a
Master.into()
kianenigma Jul 25, 2019
0d9133f
Merge conflicts.
kianenigma Jul 25, 2019
ec6554f
Scale weights by 1000.
kianenigma Jul 25, 2019
a25e4be
Bump.
kianenigma Jul 25, 2019
5724771
Fix decl_storage test.
kianenigma Jul 25, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
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 {
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should safety be the default here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The priority was to be consistent with how the std operations work. This implementation might panic. There are saturating/checked ones that don't.

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As updated in the sheet, weight scale is now [10, 1_000_00] and the weight-to-fee factor 10^6.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1_000_000 seems small for a block. why not make it 1_000_000_000 per block to allow more fine-grained transaction weighting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this also came hand in hand with the fact that a fixed multiplier of 1000 was removed from weights. So the limit hasn't actually changed at all.

Could easily be reverted, yet, my arguments being:

  • We already have a redundant factor of 10 (smallest weight is 10, changes are within a few hundred) as backup and being more fine-grained.
  • A bit more readable to skim the weight annotations a and see weight = 10, weight = 50, weight = 100 rather than a stack of zeros attached to all of them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kianenigma what about using powers of 2 instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a redundant factor of 10

Not enough.

A bit more readable

Not a good enough reason, since this is actually important logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a redundant factor of 10

Not enough.

A bit more readable

Not a good enough reason, since this is actually important logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, Po2 not a good idea here. weights are meant to be vaguely human-comprehensible, not things that are stored in memory or otherwise benefit from Po2 sizes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted to a scale of 1_000_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