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

Commit db417ff

Browse files
gui1117bkchr
andcommitted
Use EncodeLike for storages traits (#3676)
* impl * patch * lock * some refactor * some avoided copy * new api without ref for doublemap * fix * version bump * fix * point to incoming release * use codec latest * bumpd impl version * fix unused * fix * Update srml/support/src/storage/mod.rs Co-Authored-By: Bastian Köcher <[email protected]>
1 parent dd1460d commit db417ff

File tree

36 files changed

+490
-466
lines changed

36 files changed

+490
-466
lines changed

Cargo.lock

Lines changed: 98 additions & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rustc-hex = { version = "2.0", default-features = false }
1111
serde = { version = "1.0", optional = true, features = ["derive"] }
1212
twox-hash = { version = "1.2.0", optional = true }
1313
byteorder = { version = "1.3.1", default-features = false }
14-
primitive-types = { version = "0.5.0", default-features = false, features = ["codec"] }
14+
primitive-types = { version = "0.5.1", default-features = false, features = ["codec"] }
1515
impl-serde = { version = "0.1", optional = true }
1616
log = { version = "0.4", optional = true }
1717
wasmi = { version = "0.5.0", optional = true }

core/sr-primitives/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl-trait-for-tuples = "0.1.1"
2020

2121
[dev-dependencies]
2222
serde_json = "1.0"
23-
primitive-types = "0.5.0"
23+
primitive-types = "0.5.1"
2424
rand = "0.7.2"
2525

2626
[features]

core/sr-primitives/src/generic/unchecked_extrinsic.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::fmt;
2121

2222
use rstd::prelude::*;
2323
use runtime_io::blake2_256;
24-
use codec::{Decode, Encode, Input, Error};
24+
use codec::{Decode, Encode, EncodeLike, Input, Error};
2525
use crate::{
2626
traits::{self, Member, MaybeDisplay, SignedExtension, Checkable, Extrinsic},
2727
generic::CheckedExtrinsic, transaction_validity::{TransactionValidityError, InvalidTransaction},
@@ -185,6 +185,12 @@ impl<Call, Extra> Encode for SignedPayload<Call, Extra> where
185185
}
186186
}
187187

188+
impl<Call, Extra> EncodeLike for SignedPayload<Call, Extra>
189+
where
190+
Call: Encode,
191+
Extra: SignedExtension,
192+
{}
193+
188194
impl<Address, Call, Signature, Extra> Decode
189195
for UncheckedExtrinsic<Address, Call, Signature, Extra>
190196
where
@@ -240,6 +246,15 @@ where
240246
}
241247
}
242248

249+
impl<Address, Call, Signature, Extra> EncodeLike
250+
for UncheckedExtrinsic<Address, Call, Signature, Extra>
251+
where
252+
Address: Encode,
253+
Signature: Encode,
254+
Call: Encode,
255+
Extra: SignedExtension,
256+
{}
257+
243258
#[cfg(feature = "std")]
244259
impl<Address: Encode, Signature: Encode, Call: Encode, Extra: SignedExtension> serde::Serialize
245260
for UncheckedExtrinsic<Address, Call, Signature, Extra>

node/runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
8585
// implementation changes and behavior does not, then leave spec_version as
8686
// is and increment impl_version.
8787
spec_version: 168,
88-
impl_version: 168,
88+
impl_version: 169,
8989
apis: RUNTIME_API_VERSIONS,
9090
};
9191

srml/assets/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ decl_module! {
159159
let id = Self::next_asset_id();
160160
<NextAssetId<T>>::mutate(|id| *id += One::one());
161161

162-
<Balances<T>>::insert((id, origin.clone()), total);
162+
<Balances<T>>::insert((id, &origin), total);
163163
<TotalSupply<T>>::insert(id, total);
164164

165165
Self::deposit_event(RawEvent::Issued(id, origin, total));
@@ -186,7 +186,7 @@ decl_module! {
186186
/// Destroy any assets of `id` owned by `origin`.
187187
fn destroy(origin, #[compact] id: T::AssetId) {
188188
let origin = ensure_signed(origin)?;
189-
let balance = <Balances<T>>::take((id, origin.clone()));
189+
let balance = <Balances<T>>::take((id, &origin));
190190
ensure!(!balance.is_zero(), "origin balance should be non-zero");
191191

192192
<TotalSupply<T>>::mutate(id, |total_supply| *total_supply -= balance);

srml/aura/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<T: Trait> Module<T> {
177177
fn initialize_authorities(authorities: &[T::AuthorityId]) {
178178
if !authorities.is_empty() {
179179
assert!(<Authorities<T>>::get().is_empty(), "Authorities are already initialized!");
180-
<Authorities<T>>::put_ref(authorities);
180+
<Authorities<T>>::put(authorities);
181181
}
182182
}
183183
}

srml/authority-discovery/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
#![cfg_attr(not(feature = "std"), no_std)]
3030

3131
use app_crypto::RuntimeAppPublic;
32-
use codec::{Decode, Encode};
32+
use codec::FullCodec;
3333
use rstd::prelude::*;
3434
use support::{decl_module, decl_storage};
3535

3636
/// The module's config trait.
3737
pub trait Trait: system::Trait + session::Trait {
38-
type AuthorityId: RuntimeAppPublic + Default + Decode + Encode + PartialEq;
38+
type AuthorityId: RuntimeAppPublic + Default + FullCodec + PartialEq;
3939
}
4040

4141
decl_storage! {
@@ -102,7 +102,7 @@ impl<T: Trait> Module<T> {
102102
fn initialize_keys(keys: &[T::AuthorityId]) {
103103
if !keys.is_empty() {
104104
assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
105-
Keys::<T>::put_ref(keys);
105+
Keys::<T>::put(keys);
106106
}
107107
}
108108
}

srml/babe/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<T: Trait> Module<T> {
358358
} else {
359359
// move onto the next segment and update the index.
360360
let segment_idx = segment_idx + 1;
361-
<UnderConstruction>::insert(&segment_idx, vec![*vrf_output].as_ref());
361+
<UnderConstruction>::insert(&segment_idx, &vec![*vrf_output]);
362362
<SegmentIndex>::put(&segment_idx);
363363
}
364364
}
@@ -438,7 +438,7 @@ impl<T: Trait> Module<T> {
438438
fn initialize_authorities(authorities: &[(AuthorityId, BabeAuthorityWeight)]) {
439439
if !authorities.is_empty() {
440440
assert!(Authorities::get().is_empty(), "Authorities are already initialized!");
441-
Authorities::put_ref(authorities);
441+
Authorities::put(authorities);
442442
}
443443
}
444444
}

srml/collective/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ impl<T: Trait<I>, I: Instance> ChangeMembers<T::AccountId> for Module<T, I> {
281281
}
282282
);
283283
}
284-
<Members<T, I>>::put_ref(new);
284+
<Members<T, I>>::put(new);
285285
}
286286
}
287287

288288
impl<T: Trait<I>, I: Instance> InitializeMembers<T::AccountId> for Module<T, I> {
289289
fn initialize_members(members: &[T::AccountId]) {
290290
if !members.is_empty() {
291291
assert!(<Members<T, I>>::get().is_empty(), "Members are already initialized!");
292-
<Members<T, I>>::put_ref(members);
292+
<Members<T, I>>::put(members);
293293
}
294294
}
295295
}

srml/contracts/src/wasm/code_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn load<T: Trait>(
9898
let original_code =
9999
<PristineCode<T>>::get(code_hash).ok_or_else(|| "pristine code is not found")?;
100100
prefab_module = prepare::prepare_contract::<Env>(&original_code, schedule)?;
101-
<CodeStorage<T>>::insert(code_hash, prefab_module.clone());
101+
<CodeStorage<T>>::insert(&code_hash, &prefab_module);
102102
}
103103
Ok(prefab_module)
104104
}

srml/democracy/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use sr_primitives::{
2424
traits::{Zero, Bounded, CheckedMul, CheckedDiv, EnsureOrigin, Hash, Dispatchable},
2525
weights::SimpleDispatchInfo,
2626
};
27-
use codec::{Encode, Decode, Input, Output, Error};
27+
use codec::{Ref, Encode, Decode, Input, Output, Error};
2828
use support::{
2929
decl_module, decl_storage, decl_event, ensure,
3030
Parameter,
@@ -377,10 +377,10 @@ decl_module! {
377377

378378
let index = Self::public_prop_count();
379379
PublicPropCount::put(index + 1);
380-
<DepositOf<T>>::insert(index, (value, vec![who.clone()]));
380+
<DepositOf<T>>::insert(index, (value, &[&who][..]));
381381

382-
let new_prop = (index, (*proposal).clone(), who);
383-
<PublicProps<T>>::append_or_put([new_prop].into_iter());
382+
let new_prop = (index, proposal, who);
383+
<PublicProps<T>>::append_or_put(&[Ref::from(&new_prop)][..]);
384384

385385
Self::deposit_event(RawEvent::Proposed(index, value));
386386
}
@@ -609,7 +609,7 @@ decl_module! {
609609
#[weight = SimpleDispatchInfo::FixedNormal(500_000)]
610610
pub fn delegate(origin, to: T::AccountId, conviction: Conviction) {
611611
let who = ensure_signed(origin)?;
612-
<Delegations<T>>::insert(who.clone(), (to.clone(), conviction));
612+
<Delegations<T>>::insert(&who, (&to, conviction));
613613
// Currency is locked indefinitely as long as it's delegated.
614614
T::Currency::extend_lock(
615615
DEMOCRACY_ID,
@@ -788,10 +788,10 @@ impl<T: Trait> Module<T> {
788788
/// Actually enact a vote, if legit.
789789
fn do_vote(who: T::AccountId, ref_index: ReferendumIndex, vote: Vote) -> Result {
790790
ensure!(Self::is_active_referendum(ref_index), "vote given for invalid referendum.");
791-
if !<VoteOf<T>>::exists(&(ref_index, who.clone())) {
792-
<VotersFor<T>>::append_or_insert(ref_index, [who.clone()].into_iter());
791+
if !<VoteOf<T>>::exists((ref_index, &who)) {
792+
<VotersFor<T>>::append_or_insert(ref_index, &[&who][..]);
793793
}
794-
<VoteOf<T>>::insert(&(ref_index, who), vote);
794+
<VoteOf<T>>::insert((ref_index, &who), vote);
795795
Ok(())
796796
}
797797

@@ -929,7 +929,7 @@ impl<T: Trait> Module<T> {
929929
} else {
930930
<DispatchQueue<T>>::append_or_insert(
931931
now + info.delay,
932-
[Some((info.proposal, index))].into_iter()
932+
&[Some((info.proposal, index))][..]
933933
);
934934
}
935935
} else {

srml/elections-phragmen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl<T: Trait> Module<T> {
539539

540540
// sort and save the members.
541541
new_members.sort();
542-
<Members<T>>::put(new_members.clone());
542+
<Members<T>>::put(&new_members);
543543

544544
// save the runners as-is
545545
<RunnersUp<T>>::put(runners_up);

srml/elections/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl<T: Trait> Module<T> {
816816
if set_len + 1 == VOTER_SET_SIZE {
817817
NextVoterSet::put(next + 1);
818818
}
819-
<Voters<T>>::append_or_insert(next, [Some(who.clone())].into_iter())
819+
<Voters<T>>::append_or_insert(next, &[Some(who.clone())][..])
820820
}
821821
}
822822

@@ -862,7 +862,7 @@ impl<T: Trait> Module<T> {
862862

863863
// initialize leaderboard.
864864
let leaderboard_size = empty_seats + T::CarryCount::get() as usize;
865-
<Leaderboard<T>>::put(vec![(Zero::zero(), T::AccountId::default()); leaderboard_size]);
865+
<Leaderboard<T>>::put(vec![(BalanceOf::<T>::zero(), T::AccountId::default()); leaderboard_size]);
866866

867867
Self::deposit_event(RawEvent::TallyStarted(empty_seats as u32));
868868
}
@@ -1027,7 +1027,7 @@ impl<T: Trait> Module<T> {
10271027
.chunks(APPROVAL_SET_SIZE)
10281028
.enumerate()
10291029
.for_each(|(index, slice)| <ApprovalsOf<T>>::insert(
1030-
(who.clone(), index as SetIndex), slice.to_vec())
1030+
(&who, index as SetIndex), slice)
10311031
);
10321032
}
10331033

srml/grandpa/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<T: Trait> Module<T> {
333333
fn initialize_authorities(authorities: &[(AuthorityId, AuthorityWeight)]) {
334334
if !authorities.is_empty() {
335335
assert!(Authorities::get().is_empty(), "Authorities are already initialized!");
336-
Authorities::put_ref(authorities);
336+
Authorities::put(authorities);
337337
}
338338
}
339339
}

srml/im-online/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ impl<T: Trait> Module<T> {
418418
fn initialize_keys(keys: &[T::AuthorityId]) {
419419
if !keys.is_empty() {
420420
assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
421-
Keys::<T>::put_ref(keys);
421+
Keys::<T>::put(keys);
422422
}
423423
}
424424
}

srml/scored-pool/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mod mock;
8888
#[cfg(test)]
8989
mod tests;
9090

91-
use codec::{Encode, Decode};
91+
use codec::FullCodec;
9292
use rstd::prelude::*;
9393
use support::{
9494
StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure,
@@ -117,7 +117,7 @@ pub trait Trait<I=DefaultInstance>: system::Trait {
117117
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
118118

119119
/// The score attributed to a member or candidate.
120-
type Score: SimpleArithmetic + Clone + Copy + Default + Encode + Decode + MaybeSerializeDebug;
120+
type Score: SimpleArithmetic + Clone + Copy + Default + FullCodec + MaybeSerializeDebug;
121121

122122
/// The overarching event type.
123123
type Event: From<Event<Self, I>> + Into<<Self as system::Trait>::Event>;

srml/session/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,15 @@ impl<T: Trait> Module<T> {
640640
}
641641

642642
fn key_owner(id: KeyTypeId, key_data: &[u8]) -> Option<T::ValidatorId> {
643-
<KeyOwner<T>>::get(DEDUP_KEY_PREFIX, &(id, key_data.to_vec()))
643+
<KeyOwner<T>>::get(DEDUP_KEY_PREFIX, (id, key_data))
644644
}
645645

646646
fn put_key_owner(id: KeyTypeId, key_data: &[u8], v: &T::ValidatorId) {
647-
<KeyOwner<T>>::insert(DEDUP_KEY_PREFIX, &(id, key_data.to_vec()), v)
647+
<KeyOwner<T>>::insert(DEDUP_KEY_PREFIX, (id, key_data), v)
648648
}
649649

650650
fn clear_key_owner(id: KeyTypeId, key_data: &[u8]) {
651-
<KeyOwner<T>>::remove(DEDUP_KEY_PREFIX, &(id, key_data.to_vec()));
651+
<KeyOwner<T>>::remove(DEDUP_KEY_PREFIX, (id, key_data));
652652
}
653653
}
654654

srml/staking/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ decl_module! {
722722

723723
// You're auto-bonded forever, here. We might improve this by only bonding when
724724
// you actually validate/nominate and remove once you unbond __everything__.
725-
<Bonded<T>>::insert(&stash, controller.clone());
725+
<Bonded<T>>::insert(&stash, &controller);
726726
<Payee<T>>::insert(&stash, payee);
727727

728728
let stash_balance = T::Currency::free_balance(&stash);
@@ -1339,7 +1339,7 @@ impl<T: Trait> Module<T> {
13391339
if exposure.total < slot_stake {
13401340
slot_stake = exposure.total;
13411341
}
1342-
<Stakers<T>>::insert(c.clone(), exposure.clone());
1342+
<Stakers<T>>::insert(&c, exposure.clone());
13431343
}
13441344

13451345
// Update slot stake.

srml/support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
serde = { version = "1.0", optional = true, features = ["derive"] }
9-
codec = { package = "parity-scale-codec", version = "1.0.5", default-features = false, features = ["derive"] }
9+
codec = { package = "parity-scale-codec", version = "1.0.6", default-features = false, features = ["derive"] }
1010
srml-metadata = { path = "../metadata", default-features = false }
1111
rstd = { package = "sr-std", path = "../../core/sr-std", default-features = false }
1212
runtime-io ={ package = "sr-io", path = "../../core/sr-io", default-features = false }

srml/support/procedural/src/storage/transformation.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn decl_store_extra_genesis(
338338
<
339339
#name<#struct_trait #instance> as
340340
#scrate::storage::StorageValue<#typ>
341-
>::put(&v);
341+
>::put::<#typ>(v);
342342
}}
343343
},
344344
DeclStorageTypeInfosKind::Map { key_type, is_linked, .. } => {
@@ -363,7 +363,7 @@ fn decl_store_extra_genesis(
363363
<
364364
#name<#struct_trait #instance> as
365365
#scrate::storage::#map<#key_type, #typ>
366-
>::insert(&k, &v);
366+
>::insert::<#key_type, #typ>(k, v);
367367
});
368368
}}
369369
},
@@ -384,7 +384,7 @@ fn decl_store_extra_genesis(
384384
<
385385
#name<#struct_trait #instance> as
386386
#scrate::storage::StorageDoubleMap<#key1_type, #key2_type, #typ>
387-
>::insert(&k1, &k2, &v);
387+
>::insert::<#key1_type, #key2_type, #typ>(k1, k2, v);
388388
});
389389
}}
390390
},
@@ -927,11 +927,11 @@ fn impl_store_fns(
927927

928928
quote!{
929929
#( #[ #attrs ] )*
930-
pub fn #get_fn<K: #scrate::rstd::borrow::Borrow<#key_type>>(key: K) -> #value_type {
930+
pub fn #get_fn<K: #scrate::codec::EncodeLike<#key_type>>(key: K) -> #value_type {
931931
<
932932
#name<#struct_trait #instance> as
933933
#scrate::storage::#map<#key_type, #typ>
934-
>::get(key.borrow())
934+
>::get(key)
935935
}
936936
}
937937
}
@@ -946,12 +946,10 @@ fn impl_store_fns(
946946
};
947947

948948
quote!{
949-
pub fn #get_fn<KArg1, KArg2>(k1: &KArg1, k2: &KArg2) -> #value_type
949+
pub fn #get_fn<KArg1, KArg2>(k1: KArg1, k2: KArg2) -> #value_type
950950
where
951-
#key1_type: #scrate::rstd::borrow::Borrow<KArg1>,
952-
#key2_type: #scrate::rstd::borrow::Borrow<KArg2>,
953-
KArg1: ?Sized + #scrate::codec::Encode,
954-
KArg2: ?Sized + #scrate::codec::Encode,
951+
KArg1: #scrate::codec::EncodeLike<#key1_type>,
952+
KArg2: #scrate::codec::EncodeLike<#key2_type>,
955953
{
956954
<
957955
#name<#struct_trait #instance> as

0 commit comments

Comments
 (0)