mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 22:37:57 +00:00
Per-transaction weight for srml (#2799)
* debug checkpoint. * new * Worked. * Worked and weight propagated to executive. * Works with some tests. * Cleanup debug prints. * More cleanup. * Undo more logs. * Undo a few more. * Fix build. * Allow len to be used in weight calculation. * Remove noop function from dispath. * Cleanup. * Unify traits. * Update docs and nits. * line width * Update core/sr-primitives/src/weights.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update core/sr-primitives/src/weights.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update core/sr-primitives/src/weights.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update core/sr-primitives/src/weights.rs Co-Authored-By: Amar Singh <asinghchrony@protonmail.com> * Update srml/example/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Final cleanup. * Fix build.
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
//! stage.
|
||||
|
||||
use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay};
|
||||
use crate::weights::{Weighable, Weight};
|
||||
|
||||
/// Definition of something that the external world might want to say; its
|
||||
/// existence implies that it has been checked and is good, particularly with
|
||||
@@ -32,8 +33,7 @@ pub struct CheckedExtrinsic<AccountId, Index, Call> {
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<AccountId, Index, Call> traits::Applyable
|
||||
for CheckedExtrinsic<AccountId, Index, Call>
|
||||
impl<AccountId, Index, Call> traits::Applyable for CheckedExtrinsic<AccountId, Index, Call>
|
||||
where
|
||||
AccountId: Member + MaybeDisplay,
|
||||
Index: Member + MaybeDisplay + SimpleArithmetic,
|
||||
@@ -55,3 +55,12 @@ where
|
||||
(self.function, self.signed.map(|x| x.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<AccountId, Index, Call> Weighable for CheckedExtrinsic<AccountId, Index, Call>
|
||||
where
|
||||
Call: Weighable,
|
||||
{
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
self.function.weight(len)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ use codec::{Encode, Decode};
|
||||
#[cfg(feature = "std")]
|
||||
pub mod testing;
|
||||
|
||||
pub mod weights;
|
||||
pub mod traits;
|
||||
use traits::{SaturatedConversion, UniqueSaturatedInto};
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializ
|
||||
use std::{fmt::Debug, ops::Deref, fmt};
|
||||
use crate::codec::{Codec, Encode, Decode};
|
||||
use crate::traits::{self, Checkable, Applyable, BlakeTwo256, Convert};
|
||||
use crate::weights::{Weighable, Weight};
|
||||
use crate::generic::DigestItem as GenDigestItem;
|
||||
pub use substrate_primitives::H256;
|
||||
use substrate_primitives::U256;
|
||||
@@ -239,3 +240,9 @@ impl<Call> Applyable for TestXt<Call> where
|
||||
(self.2, self.0)
|
||||
}
|
||||
}
|
||||
impl<Call> Weighable for TestXt<Call> {
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
// for testing: weight == size.
|
||||
len as Weight
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Primitives for transaction weighting.
|
||||
//!
|
||||
//! Each dispatch function within `decl_module!` can now have an optional
|
||||
//! `#[weight = $x]` attribute. $x can be any object that implements the
|
||||
//! `Weighable` trait. By default, All transactions are annotated by
|
||||
//! `#[weight = TransactionWeight::default()]`.
|
||||
//!
|
||||
//! Note that the decl_module macro _cannot_ enforce this and will simply fail
|
||||
//! if an invalid struct is passed in.
|
||||
|
||||
/// The final type that each `#[weight = $x:expr]`'s
|
||||
/// expression must evaluate to.
|
||||
pub type Weight = u32;
|
||||
|
||||
/// A `Call` enum (aka transaction) that can be weighted using the custom weight attribute of
|
||||
/// its dispatchable functions. Is implemented by default in the `decl_module!`.
|
||||
///
|
||||
/// Both the outer Call enum and the per-module individual ones will implement this.
|
||||
/// The outer enum simply calls the inner ones based on call type.
|
||||
pub trait Weighable {
|
||||
/// Return the weight of this call.
|
||||
/// The `len` argument is the encoded length of the transaction/call.
|
||||
fn weight(&self, len: usize) -> Weight;
|
||||
}
|
||||
|
||||
/// Default type used as the weight representative in a `#[weight = x]` attribute.
|
||||
///
|
||||
/// A user may pass in any other type that implements [`Weighable`]. If not, the `Default`
|
||||
/// implementation of [`TransactionWeight`] is used.
|
||||
pub enum TransactionWeight {
|
||||
/// Basic weight (base, byte).
|
||||
/// The values contained are the base weight and byte weight respectively.
|
||||
Basic(Weight, Weight),
|
||||
/// Maximum fee. This implies that this transaction _might_ get included but
|
||||
/// no more transaction can be added. This can be done by setting the
|
||||
/// implementation to _maximum block weight_.
|
||||
Max,
|
||||
/// Free. The transaction does not increase the total weight
|
||||
/// (i.e. is not included in weight calculation).
|
||||
Free,
|
||||
}
|
||||
|
||||
impl Weighable for TransactionWeight {
|
||||
fn weight(&self, len: usize) -> Weight {
|
||||
match self {
|
||||
TransactionWeight::Basic(base, byte) => base + byte * len as Weight,
|
||||
TransactionWeight::Max => 3 * 1024 * 1024,
|
||||
TransactionWeight::Free => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TransactionWeight {
|
||||
fn default() -> Self {
|
||||
// This implies that the weight is currently equal to tx-size, nothing more
|
||||
// for all substrate transactions that do NOT explicitly annotate weight.
|
||||
// TODO #2431 needs to be updated with proper max values.
|
||||
TransactionWeight::Basic(0, 1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user