From 7b3abf8a80d00314fb60879e4b95e276629db491 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 15 Mar 2023 15:45:43 +0800 Subject: [PATCH 1/5] clean deprecated --- authority/src/lib.rs | 32 ---- utilities/src/iterator.rs | 336 -------------------------------------- utilities/src/lib.rs | 8 - 3 files changed, 376 deletions(-) delete mode 100644 utilities/src/iterator.rs diff --git a/authority/src/lib.rs b/authority/src/lib.rs index ae9863c3a..5db74cda4 100644 --- a/authority/src/lib.rs +++ b/authority/src/lib.rs @@ -488,38 +488,6 @@ pub mod module { }) } - #[pallet::call_index(7)] - #[pallet::weight(( - T::WeightInfo::trigger_call().saturating_add((*call_weight_bound).into()), - DispatchClass::Operational, - ))] - #[allow(deprecated)] - #[deprecated(note = "1D weight is used in this extrinsic, please migrate to `trigger_call`")] - pub fn trigger_old_call( - origin: OriginFor, - hash: T::Hash, - #[pallet::compact] call_weight_bound: OldWeight, - ) -> DispatchResultWithPostInfo { - let call_weight_bound: Weight = call_weight_bound.into(); - let who = ensure_signed(origin)?; - SavedCalls::::try_mutate_exists(hash, |maybe_call| { - let (call, maybe_caller) = maybe_call.take().ok_or(Error::::CallNotAuthorized)?; - if let Some(caller) = maybe_caller { - ensure!(who == caller, Error::::TriggerCallNotPermitted); - } - ensure!( - call_weight_bound.ref_time() >= call.get_dispatch_info().weight.ref_time(), - Error::::WrongCallWeightBound - ); - let result = call.dispatch(OriginFor::::root()); - Self::deposit_event(Event::TriggeredCallBy { hash, caller: who }); - Self::deposit_event(Event::Dispatched { - result: result.map(|_| ()).map_err(|e| e.error), - }); - Ok(Pays::No.into()) - }) - } - #[pallet::call_index(8)] #[pallet::weight(( T::WeightInfo::trigger_call().saturating_add(*call_weight_bound), diff --git a/utilities/src/iterator.rs b/utilities/src/iterator.rs deleted file mode 100644 index c2e26c639..000000000 --- a/utilities/src/iterator.rs +++ /dev/null @@ -1,336 +0,0 @@ -use codec::{Decode, EncodeLike, FullCodec, FullEncode}; -use frame_support::{ - storage::{ - generator::{StorageDoubleMap as StorageDoubleMapT, StorageMap as StorageMapT}, - unhashed, StorageDoubleMap, StorageMap, - }, - ReversibleStorageHasher, -}; -use sp_std::prelude::*; - -/// Utility to iterate through items in a storage map. -/// Forks from substrate, expose previous_key field -pub struct StorageMapIterator { - prefix: Vec, - pub previous_key: Vec, - drain: bool, - _phantom: sp_std::marker::PhantomData<(K, V, Hasher)>, -} - -/// Forks from substrate -impl Iterator - for StorageMapIterator -{ - type Item = (K, V); - - fn next(&mut self) -> Option<(K, V)> { - loop { - let maybe_next = sp_io::storage::next_key(&self.previous_key).filter(|n| n.starts_with(&self.prefix)); - break match maybe_next { - Some(next) => { - self.previous_key = next; - match unhashed::get::(&self.previous_key) { - Some(value) => { - if self.drain { - unhashed::kill(&self.previous_key) - } - let mut key_material = Hasher::reverse(&self.previous_key[self.prefix.len()..]); - match K::decode(&mut key_material) { - Ok(key) => Some((key, value)), - Err(_) => continue, - } - } - None => continue, - } - } - None => None, - }; - } - } -} - -/// Shim for StorageMapIterator, add more features -pub struct StorageMapIteratorShim { - pub storage_map_iterator: StorageMapIterator, - pub remain_iterator_count: Option, - pub finished: bool, -} - -impl Iterator for StorageMapIteratorShim { - type Item = as Iterator>::Item; - - fn next(&mut self) -> Option { - // check accumulated iteration count - if let Some(remain_iterator_count) = self.remain_iterator_count { - if remain_iterator_count == 0 { - return None; - } else { - self.remain_iterator_count = Some(remain_iterator_count - 1); - } - } - - self.storage_map_iterator.next().or_else(|| { - // mark this map iterator has already finished - self.finished = true; - None - }) - } -} - -/// A strongly-typed map in storage whose keys and values can be iterated over. -pub trait IterableStorageMapExtended: StorageMap { - /// The type that iterates over all `(key, value)`. - type Iterator: Iterator; - - /// Enumerate all elements in the map in no particular order. If you alter - /// the map while doing this, you'll get undefined results. - fn iter(max_iterations: Option, start_key: Option>) -> Self::Iterator; - - /// Remove all elements from the map and iterate through them in no - /// particular order. If you add elements to the map while doing this, - /// you'll get undefined results. - fn drain(max_iterations: Option, start_key: Option>) -> Self::Iterator; -} - -impl> IterableStorageMapExtended for G -where - G::Hasher: ReversibleStorageHasher, -{ - type Iterator = StorageMapIteratorShim; - - /// Enumerate all elements in the map. - fn iter(max_iterations: Option, start_key: Option>) -> Self::Iterator { - let prefix = G::prefix_hash(); - let previous_key = start_key - .filter(|k| k.starts_with(&prefix)) - .unwrap_or_else(|| prefix.clone()); - let storage_map_iterator = StorageMapIterator { - prefix, - previous_key, - drain: false, - _phantom: Default::default(), - }; - - StorageMapIteratorShim { - storage_map_iterator, - remain_iterator_count: max_iterations, - finished: false, - } - } - - /// Enumerate all elements in the map. - fn drain(max_iterations: Option, start_key: Option>) -> Self::Iterator { - let prefix = G::prefix_hash(); - - let previous_key = start_key - .filter(|k| k.starts_with(&prefix)) - .unwrap_or_else(|| prefix.clone()); - let storage_map_iterator = StorageMapIterator { - prefix, - previous_key, - drain: true, - _phantom: Default::default(), - }; - - StorageMapIteratorShim { - storage_map_iterator, - remain_iterator_count: max_iterations, - finished: false, - } - } -} - -/// Iterate over a prefix and decode raw_key and raw_value into `T`. -/// Forks from substrate, expose previous_key field -pub struct MapIterator { - prefix: Vec, - pub previous_key: Vec, - /// If true then value are removed while iterating - drain: bool, - /// Function that take `(raw_key_without_prefix, raw_value)` and decode `T`. - /// `raw_key_without_prefix` is the raw storage key without the prefix - /// iterated on. - closure: fn(&[u8], &[u8]) -> Result, -} - -/// Forks from substrate -impl Iterator for MapIterator { - type Item = T; - - fn next(&mut self) -> Option { - loop { - let maybe_next = sp_io::storage::next_key(&self.previous_key).filter(|n| n.starts_with(&self.prefix)); - break match maybe_next { - Some(next) => { - self.previous_key = next; - let raw_value = match unhashed::get_raw(&self.previous_key) { - Some(raw_value) => raw_value, - None => { - frame_support::print("ERROR: next_key returned a key with no value in MapIterator"); - continue; - } - }; - if self.drain { - unhashed::kill(&self.previous_key) - } - let raw_key_without_prefix = &self.previous_key[self.prefix.len()..]; - let item = match (self.closure)(raw_key_without_prefix, &raw_value[..]) { - Ok(item) => item, - Err(_e) => { - frame_support::print("ERROR: (key, value) failed to decode in MapIterator"); - continue; - } - }; - - Some(item) - } - None => None, - }; - } - } -} - -/// Shim for MapIterator, add more features -pub struct MapIteratorShim { - pub map_iterator: MapIterator, - pub remain_iterator_count: Option, - pub finished: bool, -} - -impl Iterator for MapIteratorShim { - type Item = as Iterator>::Item; - - fn next(&mut self) -> Option { - // check accumulated iteration count - if let Some(remain_iterator_count) = self.remain_iterator_count { - if remain_iterator_count == 0 { - return None; - } else { - self.remain_iterator_count = Some(remain_iterator_count - 1); - } - } - - self.map_iterator.next().or_else(|| { - // mark this map iterator has already finished - self.finished = true; - None - }) - } -} - -/// A strongly-typed map in storage whose keys and values can be iterated over. -pub trait IterableStorageDoubleMapExtended: - StorageDoubleMap -{ - /// The type that iterates over all `(key2, value)`. - type PrefixIterator: Iterator; - - /// The type that iterates over all `(key1, key2, value)`. - type Iterator: Iterator; - - /// Enumerate all elements in the map with first key `k1` in no particular - /// order. If you add or remove values whose first key is `k1` to the map - /// while doing this, you'll get undefined results. - fn iter_prefix( - k1: impl EncodeLike, - max_iterations: Option, - start_key: Option>, - ) -> Self::PrefixIterator; - - /// Remove all elements from the map with first key `k1` and iterate through - /// them in no particular order. If you add elements with first key `k1` to - /// the map while doing this, you'll get undefined results. - fn drain_prefix( - k1: impl EncodeLike, - max_iterations: Option, - start_key: Option>, - ) -> Self::PrefixIterator; - - /// Enumerate all elements in the map in no particular order. If you add or - /// remove values to the map while doing this, you'll get undefined results. - fn iter(max_iterations: Option, start_key: Option>) -> Self::Iterator; - - /// Remove all elements from the map and iterate through them in no - /// particular order. If you add elements to the map while doing this, - /// you'll get undefined results. - fn drain(max_iterations: Option, start_key: Option>) -> Self::Iterator; -} - -impl> - IterableStorageDoubleMapExtended for G -where - G::Hasher1: ReversibleStorageHasher, - G::Hasher2: ReversibleStorageHasher, -{ - type PrefixIterator = MapIteratorShim<(K2, V)>; - type Iterator = MapIteratorShim<(K1, K2, V)>; - - fn iter_prefix( - k1: impl EncodeLike, - max_iterations: Option, - start_key: Option>, - ) -> Self::PrefixIterator { - let prefix = G::storage_double_map_final_key1(k1); - let previous_key = start_key - .filter(|k| k.starts_with(&prefix)) - .unwrap_or_else(|| prefix.clone()); - - let map_iterator = MapIterator { - prefix, - previous_key, - drain: false, - closure: |raw_key_without_prefix, mut raw_value| { - let mut key_material = G::Hasher2::reverse(raw_key_without_prefix); - Ok((K2::decode(&mut key_material)?, V::decode(&mut raw_value)?)) - }, - }; - - MapIteratorShim { - map_iterator, - remain_iterator_count: max_iterations, - finished: false, - } - } - - fn drain_prefix( - k1: impl EncodeLike, - max_iterations: Option, - start_key: Option>, - ) -> Self::PrefixIterator { - let mut shim = Self::iter_prefix(k1, max_iterations, start_key); - shim.map_iterator.drain = true; - shim - } - - fn iter(max_iterations: Option, start_key: Option>) -> Self::Iterator { - let prefix = G::prefix_hash(); - let previous_key = start_key - .filter(|k| k.starts_with(&prefix)) - .unwrap_or_else(|| prefix.clone()); - - let map_iterator = MapIterator { - prefix, - previous_key, - drain: false, - closure: |raw_key_without_prefix, mut raw_value| { - let mut k1_k2_material = G::Hasher1::reverse(raw_key_without_prefix); - let k1 = K1::decode(&mut k1_k2_material)?; - let mut k2_material = G::Hasher2::reverse(k1_k2_material); - let k2 = K2::decode(&mut k2_material)?; - Ok((k1, k2, V::decode(&mut raw_value)?)) - }, - }; - - MapIteratorShim { - map_iterator, - remain_iterator_count: max_iterations, - finished: false, - } - } - - fn drain(max_iterations: Option, start_key: Option>) -> Self::Iterator { - let mut shim = Self::iter(max_iterations, start_key); - shim.map_iterator.drain = true; - shim - } -} diff --git a/utilities/src/lib.rs b/utilities/src/lib.rs index a75ea1534..b63f2a2fd 100644 --- a/utilities/src/lib.rs +++ b/utilities/src/lib.rs @@ -4,17 +4,9 @@ use frame_support::storage::{with_transaction, TransactionOutcome}; use sp_runtime::DispatchError; use sp_std::result::Result; -#[deprecated( - since = "0.4.1", - note = "iterator module's functionality is now available in substrate's frame-support" -)] -pub mod iterator; pub mod offchain_worker; pub mod ordered_set; -#[allow(deprecated)] -pub use iterator::{IterableStorageDoubleMapExtended, IterableStorageMapExtended}; - pub use offchain_worker::OffchainErr; pub use ordered_set::OrderedSet; From dde9ca01aad21d2663968942bbcfc1445ca562df Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 15 Mar 2023 16:09:25 +0800 Subject: [PATCH 2/5] add dependencies on RPC --- oracle/rpc/runtime-api/src/lib.rs | 1 + tokens/rpc/runtime-api/src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/oracle/rpc/runtime-api/src/lib.rs b/oracle/rpc/runtime-api/src/lib.rs index fb4cca1ad..529743f38 100644 --- a/oracle/rpc/runtime-api/src/lib.rs +++ b/oracle/rpc/runtime-api/src/lib.rs @@ -10,6 +10,7 @@ use codec::Codec; use sp_std::prelude::Vec; sp_api::decl_runtime_apis! { + #[deprecated(note = "please use `state_call` instead of RPC")] pub trait OracleApi where ProviderId: Codec, Key: Codec, diff --git a/tokens/rpc/runtime-api/src/lib.rs b/tokens/rpc/runtime-api/src/lib.rs index fa7ceb0bd..c53504830 100644 --- a/tokens/rpc/runtime-api/src/lib.rs +++ b/tokens/rpc/runtime-api/src/lib.rs @@ -9,6 +9,7 @@ use codec::Codec; sp_api::decl_runtime_apis! { + #[deprecated(note = "please use `state_call` instead of RPC")] pub trait TokensApi where Balance: Codec, CurrencyId: Codec From 481164534910438fd399e7f1e5350fb86d2052ff Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 15 Mar 2023 17:23:51 +0800 Subject: [PATCH 3/5] add deprecated on RPC --- oracle/rpc/runtime-api/src/lib.rs | 1 - oracle/rpc/src/lib.rs | 1 + tokens/rpc/runtime-api/src/lib.rs | 1 - tokens/rpc/src/lib.rs | 1 + 4 files changed, 2 insertions(+), 2 deletions(-) diff --git a/oracle/rpc/runtime-api/src/lib.rs b/oracle/rpc/runtime-api/src/lib.rs index 529743f38..fb4cca1ad 100644 --- a/oracle/rpc/runtime-api/src/lib.rs +++ b/oracle/rpc/runtime-api/src/lib.rs @@ -10,7 +10,6 @@ use codec::Codec; use sp_std::prelude::Vec; sp_api::decl_runtime_apis! { - #[deprecated(note = "please use `state_call` instead of RPC")] pub trait OracleApi where ProviderId: Codec, Key: Codec, diff --git a/oracle/rpc/src/lib.rs b/oracle/rpc/src/lib.rs index acf65fb18..0fdeec3d6 100644 --- a/oracle/rpc/src/lib.rs +++ b/oracle/rpc/src/lib.rs @@ -21,6 +21,7 @@ pub trait OracleApi { } /// Provides RPC methods to query oracle value. +#[deprecated(note = "please use `state_call` instead of RPC")] pub struct Oracle { /// Shared reference to the client. client: Arc, diff --git a/tokens/rpc/runtime-api/src/lib.rs b/tokens/rpc/runtime-api/src/lib.rs index c53504830..fa7ceb0bd 100644 --- a/tokens/rpc/runtime-api/src/lib.rs +++ b/tokens/rpc/runtime-api/src/lib.rs @@ -9,7 +9,6 @@ use codec::Codec; sp_api::decl_runtime_apis! { - #[deprecated(note = "please use `state_call` instead of RPC")] pub trait TokensApi where Balance: Codec, CurrencyId: Codec diff --git a/tokens/rpc/src/lib.rs b/tokens/rpc/src/lib.rs index 81efda106..cd386ec2e 100644 --- a/tokens/rpc/src/lib.rs +++ b/tokens/rpc/src/lib.rs @@ -24,6 +24,7 @@ pub trait TokensApi { } /// Provides RPC methods to query existential deposit of currency. +#[deprecated(note = "please use `state_call` instead of RPC")] pub struct Tokens { /// Shared reference to the client. client: Arc, From 2a90ef5d3eb889589a823f49bdf89221dc7e60c6 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 15 Mar 2023 20:35:26 +0800 Subject: [PATCH 4/5] fix clippy --- authority/src/lib.rs | 1 - oracle/rpc/src/lib.rs | 2 ++ tokens/rpc/src/lib.rs | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/authority/src/lib.rs b/authority/src/lib.rs index 5db74cda4..f67310947 100644 --- a/authority/src/lib.rs +++ b/authority/src/lib.rs @@ -35,7 +35,6 @@ use frame_support::{ schedule::{v1::Named as ScheduleNamed, DispatchTime, Priority}, EitherOfDiverse, EnsureOrigin, Get, IsType, OriginTrait, }, - weights::OldWeight, }; use frame_system::{pallet_prelude::*, EnsureRoot, EnsureSigned}; use scale_info::TypeInfo; diff --git a/oracle/rpc/src/lib.rs b/oracle/rpc/src/lib.rs index 0fdeec3d6..2b21f673c 100644 --- a/oracle/rpc/src/lib.rs +++ b/oracle/rpc/src/lib.rs @@ -28,6 +28,7 @@ pub struct Oracle { _marker: std::marker::PhantomData, } +#[allow(deprecated)] impl Oracle { /// Creates a new instance of the `Oracle` helper. pub fn new(client: Arc) -> Self { @@ -51,6 +52,7 @@ impl From for i32 { } #[async_trait] +#[allow(deprecated)] impl OracleApiServer<::Hash, ProviderId, Key, Value> for Oracle where diff --git a/tokens/rpc/src/lib.rs b/tokens/rpc/src/lib.rs index cd386ec2e..e12999df5 100644 --- a/tokens/rpc/src/lib.rs +++ b/tokens/rpc/src/lib.rs @@ -31,6 +31,7 @@ pub struct Tokens { _marker: std::marker::PhantomData

, } +#[allow(deprecated)] impl Tokens { /// Creates a new instance of the `Tokens` helper. pub fn new(client: Arc) -> Self { @@ -56,6 +57,7 @@ impl From for i32 { } #[async_trait] +#[allow(deprecated)] impl TokensApiServer<::Hash, CurrencyId, Balance> for Tokens where Block: BlockT, From fcade91a5e6203421cb252a2681227664c907ff9 Mon Sep 17 00:00:00 2001 From: zjb0807 Date: Wed, 15 Mar 2023 20:54:25 +0800 Subject: [PATCH 5/5] remove tests --- authority/src/tests.rs | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/authority/src/tests.rs b/authority/src/tests.rs index 9aa301588..7d3eccb6c 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -652,48 +652,6 @@ fn trigger_call_should_be_free_and_operational() { }); } -#[test] -fn trigger_old_call_should_be_free_and_operational() { - ExtBuilder::default().build().execute_with(|| { - let call = RuntimeCall::RootTesting(pallet_root_testing::Call::fill_block { - ratio: Perbill::from_percent(50), - }); - let hash = ::Hashing::hash_of(&call); - let call_weight_bound: OldWeight = OldWeight(call.get_dispatch_info().weight.ref_time()); - let trigger_old_call = RuntimeCall::Authority(authority::Call::trigger_old_call { - hash, - call_weight_bound, - }); - - assert_ok!(Authority::authorize_call( - RuntimeOrigin::root(), - Box::new(call), - Some(1) - )); - - // bad caller pays fee - assert_eq!( - trigger_old_call.clone().dispatch(RuntimeOrigin::signed(2)), - Err(DispatchErrorWithPostInfo { - post_info: PostDispatchInfo { - actual_weight: None, - pays_fee: Pays::Yes - }, - error: Error::::TriggerCallNotPermitted.into() - }) - ); - - // successfull call doesn't pay fee - assert_eq!( - trigger_old_call.dispatch(RuntimeOrigin::signed(1)), - Ok(PostDispatchInfo { - actual_weight: None, - pays_fee: Pays::No - }) - ); - }); -} - #[test] fn origin_max_encoded_len_works() { assert_eq!(DelayedOrigin::::max_encoded_len(), 22);