From 61f383a11678e554a879aa34b9be19e420914140 Mon Sep 17 00:00:00 2001 From: Alexander Popiak <alexander.popiak@parity.io> Date: Fri, 24 Apr 2020 12:00:07 +0200 Subject: [PATCH] Document weight for asset, system and timestamp pallets (#5593) Co-Authored-By: thiolliere <gui.thiolliere@gmail.com> --- substrate/frame/assets/src/lib.rs | 21 +++++++++++ substrate/frame/system/src/lib.rs | 55 ++++++++++++++++++++++++++++ substrate/frame/timestamp/src/lib.rs | 10 +++++ 3 files changed, 86 insertions(+) diff --git a/substrate/frame/assets/src/lib.rs b/substrate/frame/assets/src/lib.rs index 60a9ab87a75..9d31cceb7e2 100644 --- a/substrate/frame/assets/src/lib.rs +++ b/substrate/frame/assets/src/lib.rs @@ -158,6 +158,13 @@ decl_module! { /// Issue a new class of fungible assets. There are, and will only ever be, `total` /// such assets and they'll all belong to the `origin` initially. It will have an /// identifier `AssetId` instance: this will be specified in the `Issued` event. + /// + /// # <weight> + /// - `O(1)` + /// - 1 storage mutation (codec `O(1)`). + /// - 2 storage writes (condec `O(1)`). + /// - 1 event. + /// # </weight> #[weight = MINIMUM_WEIGHT] fn issue(origin, #[compact] total: T::Balance) { let origin = ensure_signed(origin)?; @@ -172,6 +179,13 @@ decl_module! { } /// Move some assets from one holder to another. + /// + /// # <weight> + /// - `O(1)` + /// - 1 static lookup + /// - 2 storage mutations (codec `O(1)`). + /// - 1 event. + /// # </weight> #[weight = MINIMUM_WEIGHT] fn transfer(origin, #[compact] id: T::AssetId, @@ -191,6 +205,13 @@ decl_module! { } /// Destroy any assets of `id` owned by `origin`. + /// + /// # <weight> + /// - `O(1)` + /// - 1 storage mutation (codec `O(1)`). + /// - 1 storage deletion (codec `O(1)`). + /// - 1 event. + /// # </weight> #[weight = MINIMUM_WEIGHT] fn destroy(origin, #[compact] id: T::AssetId) { let origin = ensure_signed(origin)?; diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 83733bb6c9c..b3ac7e8f959 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -494,12 +494,21 @@ decl_module! { } /// Make some on-chain remark. + /// + /// # <weight> + /// - `O(1)` + /// # </weight> #[weight = MINIMUM_WEIGHT] fn remark(origin, _remark: Vec<u8>) { ensure_signed(origin)?; } /// Set the number of pages in the WebAssembly environment's heap. + /// + /// # <weight> + /// - `O(1)` + /// - 1 storage write. + /// # </weight> #[weight = (MINIMUM_WEIGHT, DispatchClass::Operational)] fn set_heap_pages(origin, pages: u64) { ensure_root(origin)?; @@ -507,6 +516,13 @@ decl_module! { } /// Set the new runtime code. + /// + /// # <weight> + /// - `O(C + S)` where `C` length of `code` and `S` complexity of `can_set_code` + /// - 1 storage write (codec `O(C)`). + /// - 1 call to `can_set_code`: `O(S)` (calls `sp_io::misc::runtime_version` which is expensive). + /// - 1 event. + /// # </weight> #[weight = (200_000_000, DispatchClass::Operational)] pub fn set_code(origin, code: Vec<u8>) { Self::can_set_code(origin, &code)?; @@ -516,6 +532,12 @@ decl_module! { } /// Set the new runtime code without doing any checks of the given `code`. + /// + /// # <weight> + /// - `O(C)` where `C` length of `code` + /// - 1 storage write (codec `O(C)`). + /// - 1 event. + /// # </weight> #[weight = (200_000_000, DispatchClass::Operational)] pub fn set_code_without_checks(origin, code: Vec<u8>) { ensure_root(origin)?; @@ -524,6 +546,12 @@ decl_module! { } /// Set the new changes trie configuration. + /// + /// # <weight> + /// - `O(D)` where `D` length of `Digest` + /// - 1 storage write or delete (codec `O(1)`). + /// - 1 call to `deposit_log`: `O(D)` (which depends on the length of `Digest`) + /// # </weight> #[weight = (20_000_000, DispatchClass::Operational)] pub fn set_changes_trie_config(origin, changes_trie_config: Option<ChangesTrieConfiguration>) { ensure_root(origin)?; @@ -542,6 +570,11 @@ decl_module! { } /// Set some items of storage. + /// + /// # <weight> + /// - `O(I)` where `I` length of `items` + /// - `I` storage writes (`O(1)`). + /// # </weight> #[weight = (MINIMUM_WEIGHT, DispatchClass::Operational)] fn set_storage(origin, items: Vec<KeyValue>) { ensure_root(origin)?; @@ -551,6 +584,11 @@ decl_module! { } /// Kill some items from storage. + /// + /// # <weight> + /// - `O(VK)` where `V` length of `keys` and `K` length of one key + /// - `V` storage deletions. + /// # </weight> #[weight = (MINIMUM_WEIGHT, DispatchClass::Operational)] fn kill_storage(origin, keys: Vec<Key>) { ensure_root(origin)?; @@ -560,6 +598,11 @@ decl_module! { } /// Kill all storage items with a key that starts with the given prefix. + /// + /// # <weight> + /// - `O(P)` where `P` amount of keys with prefix `prefix` + /// - `P` storage deletions. + /// # </weight> #[weight = (MINIMUM_WEIGHT, DispatchClass::Operational)] fn kill_prefix(origin, prefix: Key) { ensure_root(origin)?; @@ -568,6 +611,13 @@ decl_module! { /// Kill the sending account, assuming there are no references outstanding and the composite /// data is equal to its default value. + /// + /// # <weight> + /// - `O(K)` with `K` being complexity of `on_killed_account` + /// - 1 storage read and deletion. + /// - 1 call to `on_killed_account` callback with unknown complexity `K` + /// - 1 event. + /// # </weight> #[weight = (25_000_000, DispatchClass::Operational)] fn suicide(origin) { let who = ensure_signed(origin)?; @@ -924,6 +974,11 @@ impl<T: Trait> Module<T> { } /// Deposits a log and ensures it matches the block's log data. + /// + /// # <weight> + /// - `O(D)` where `D` length of `Digest` + /// - 1 storage mutation (codec `O(D)`). + /// # </weight> pub fn deposit_log(item: DigestItemOf<T>) { let mut l = <Digest<T>>::get(); l.push(item); diff --git a/substrate/frame/timestamp/src/lib.rs b/substrate/frame/timestamp/src/lib.rs index cffe172c130..704343fd165 100644 --- a/substrate/frame/timestamp/src/lib.rs +++ b/substrate/frame/timestamp/src/lib.rs @@ -148,6 +148,12 @@ decl_module! { /// `MinimumPeriod`. /// /// The dispatch origin for this call must be `Inherent`. + /// + /// # <weight> + /// - `O(T)` where `T` complexity of `on_timestamp_set` + /// - 2 storage mutations (codec `O(1)`). + /// - 1 event handler `on_timestamp_set` `O(T)`. + /// # </weight> #[weight = (MINIMUM_WEIGHT, DispatchClass::Mandatory)] fn set(origin, #[compact] now: T::Moment) { ensure_none(origin)?; @@ -162,6 +168,10 @@ decl_module! { <T::OnTimestampSet as OnTimestampSet<_>>::on_timestamp_set(now); } + /// # <weight> + /// - `O(1)` + /// - 1 storage deletion (codec `O(1)`). + /// # </weight> fn on_finalize() { assert!(<Self as Store>::DidUpdate::take(), "Timestamp must be updated once in the block"); } -- GitLab