Skip to content

Commit b98e0b3

Browse files
[stable2407] Backport #6526 (#6530)
Backport #6526 into `stable2407` from bkchr. See the [documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md) on how to use this bot. <!-- # To be used by other automation, do not modify: original-pr-number: #${pull_number} --> --------- Co-authored-by: Bastian Köcher <git@kchr.de> Co-authored-by: command-bot <> Co-authored-by: Bastian Köcher <info@kchr.de>
1 parent b5eb098 commit b98e0b3

File tree

8 files changed

+55
-49
lines changed

8 files changed

+55
-49
lines changed

cumulus/pallets/parachain-system/src/validate_block/trie_cache.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ impl<H: Hasher> CacheProvider<H> {
8585
}
8686

8787
impl<H: Hasher> TrieCacheProvider<H> for CacheProvider<H> {
88-
type Cache<'a> = TrieCache<'a, H> where H: 'a;
88+
type Cache<'a>
89+
= TrieCache<'a, H>
90+
where
91+
H: 'a;
8992

9093
fn as_trie_db_cache(&self, storage_root: <H as Hasher>::Out) -> Self::Cache<'_> {
9194
TrieCache {

cumulus/pallets/parachain-system/src/validate_block/trie_recorder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ impl<H: Hasher> SizeOnlyRecorderProvider<H> {
115115
}
116116

117117
impl<H: trie_db::Hasher> sp_trie::TrieRecorderProvider<H> for SizeOnlyRecorderProvider<H> {
118-
type Recorder<'a> = SizeOnlyRecorder<'a, H> where H: 'a;
118+
type Recorder<'a>
119+
= SizeOnlyRecorder<'a, H>
120+
where
121+
H: 'a;
119122

120123
fn drain_storage_proof(self) -> Option<StorageProof> {
121124
None

cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
542542
RuntimeCall::Utility { .. } |
543543
RuntimeCall::Multisig { .. } |
544544
RuntimeCall::NftFractionalization { .. } |
545-
RuntimeCall::Nfts { .. } |
546-
RuntimeCall::Uniques { .. }
545+
RuntimeCall::Nfts { .. } | RuntimeCall::Uniques { .. }
547546
)
548547
},
549548
ProxyType::AssetOwner => matches!(

cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
536536
RuntimeCall::Utility { .. } |
537537
RuntimeCall::Multisig { .. } |
538538
RuntimeCall::NftFractionalization { .. } |
539-
RuntimeCall::Nfts { .. } |
540-
RuntimeCall::Uniques { .. }
539+
RuntimeCall::Nfts { .. } | RuntimeCall::Uniques { .. }
541540
)
542541
},
543542
ProxyType::AssetOwner => matches!(

prdoc/pr_6526.prdoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: 'sp-runtime: Be a little bit more functional :D'
2+
doc:
3+
- audience: Runtime Dev
4+
description:
5+
Some internal refactorings in the `Digest` code.
6+
crates:
7+
- name: sp-runtime
8+
bump: patch

substrate/primitives/runtime/src/generic/digest.rs

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#[cfg(all(not(feature = "std"), feature = "serde"))]
2121
use alloc::format;
2222
use alloc::vec::Vec;
23+
use codec::DecodeAll;
2324
#[cfg(feature = "serde")]
2425
use serde::{Deserialize, Serialize};
2526

@@ -256,8 +257,7 @@ impl DigestItem {
256257
self.dref().try_as_raw(id)
257258
}
258259

259-
/// Returns the data contained in the item if `Some` if this entry has the id given, decoded
260-
/// to the type provided `T`.
260+
/// Returns the data decoded as `T`, if the `id` is matching.
261261
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
262262
self.dref().try_to::<T>(id)
263263
}
@@ -367,69 +367,48 @@ impl<'a> DigestItemRef<'a> {
367367
/// Try to match this digest item to the given opaque item identifier; if it matches, then
368368
/// try to cast to the given data type; if that works, return it.
369369
pub fn try_to<T: Decode>(&self, id: OpaqueDigestItemId) -> Option<T> {
370-
self.try_as_raw(id).and_then(|mut x| Decode::decode(&mut x).ok())
370+
self.try_as_raw(id).and_then(|mut x| DecodeAll::decode_all(&mut x).ok())
371371
}
372372

373373
/// Try to match this to a `Self::Seal`, check `id` matches and decode it.
374374
///
375375
/// Returns `None` if this isn't a seal item, the `id` doesn't match or when the decoding fails.
376376
pub fn seal_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
377-
match self {
378-
Self::Seal(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
379-
_ => None,
380-
}
377+
self.as_seal()
378+
.filter(|s| s.0 == *id)
379+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
381380
}
382381

383382
/// Try to match this to a `Self::Consensus`, check `id` matches and decode it.
384383
///
385384
/// Returns `None` if this isn't a consensus item, the `id` doesn't match or
386385
/// when the decoding fails.
387386
pub fn consensus_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
388-
match self {
389-
Self::Consensus(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
390-
_ => None,
391-
}
387+
self.as_consensus()
388+
.filter(|s| s.0 == *id)
389+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
392390
}
393391

394392
/// Try to match this to a `Self::PreRuntime`, check `id` matches and decode it.
395393
///
396394
/// Returns `None` if this isn't a pre-runtime item, the `id` doesn't match or
397395
/// when the decoding fails.
398396
pub fn pre_runtime_try_to<T: Decode>(&self, id: &ConsensusEngineId) -> Option<T> {
399-
match self {
400-
Self::PreRuntime(v, s) if *v == id => Decode::decode(&mut &s[..]).ok(),
401-
_ => None,
402-
}
397+
self.as_pre_runtime()
398+
.filter(|s| s.0 == *id)
399+
.and_then(|mut d| DecodeAll::decode_all(&mut d.1).ok())
403400
}
404401
}
405402

406403
impl<'a> Encode for DigestItemRef<'a> {
407404
fn encode(&self) -> Vec<u8> {
408-
let mut v = Vec::new();
409-
410405
match *self {
411-
Self::Consensus(val, data) => {
412-
DigestItemType::Consensus.encode_to(&mut v);
413-
(val, data).encode_to(&mut v);
414-
},
415-
Self::Seal(val, sig) => {
416-
DigestItemType::Seal.encode_to(&mut v);
417-
(val, sig).encode_to(&mut v);
418-
},
419-
Self::PreRuntime(val, data) => {
420-
DigestItemType::PreRuntime.encode_to(&mut v);
421-
(val, data).encode_to(&mut v);
422-
},
423-
Self::Other(val) => {
424-
DigestItemType::Other.encode_to(&mut v);
425-
val.encode_to(&mut v);
426-
},
427-
Self::RuntimeEnvironmentUpdated => {
428-
DigestItemType::RuntimeEnvironmentUpdated.encode_to(&mut v);
429-
},
406+
Self::Consensus(val, data) => (DigestItemType::Consensus, val, data).encode(),
407+
Self::Seal(val, sig) => (DigestItemType::Seal, val, sig).encode(),
408+
Self::PreRuntime(val, data) => (DigestItemType::PreRuntime, val, data).encode(),
409+
Self::Other(val) => (DigestItemType::Other, val).encode(),
410+
Self::RuntimeEnvironmentUpdated => DigestItemType::RuntimeEnvironmentUpdated.encode(),
430411
}
431-
432-
v
433412
}
434413
}
435414

substrate/primitives/state-machine/src/trie_backend.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ pub trait TrieCacheProvider<H: Hasher> {
7373

7474
#[cfg(feature = "std")]
7575
impl<H: Hasher> TrieCacheProvider<H> for LocalTrieCache<H> {
76-
type Cache<'a> = TrieCache<'a, H> where H: 'a;
76+
type Cache<'a>
77+
= TrieCache<'a, H>
78+
where
79+
H: 'a;
7780

7881
fn as_trie_db_cache(&self, storage_root: H::Out) -> Self::Cache<'_> {
7982
self.as_trie_db_cache(storage_root)
@@ -90,7 +93,10 @@ impl<H: Hasher> TrieCacheProvider<H> for LocalTrieCache<H> {
9093

9194
#[cfg(feature = "std")]
9295
impl<H: Hasher> TrieCacheProvider<H> for &LocalTrieCache<H> {
93-
type Cache<'a> = TrieCache<'a, H> where Self: 'a;
96+
type Cache<'a>
97+
= TrieCache<'a, H>
98+
where
99+
Self: 'a;
94100

95101
fn as_trie_db_cache(&self, storage_root: H::Out) -> Self::Cache<'_> {
96102
(*self).as_trie_db_cache(storage_root)
@@ -139,7 +145,10 @@ impl<H: Hasher> trie_db::TrieCache<NodeCodec<H>> for UnimplementedCacheProvider<
139145

140146
#[cfg(not(feature = "std"))]
141147
impl<H: Hasher> TrieCacheProvider<H> for UnimplementedCacheProvider<H> {
142-
type Cache<'a> = UnimplementedCacheProvider<H> where H: 'a;
148+
type Cache<'a>
149+
= UnimplementedCacheProvider<H>
150+
where
151+
H: 'a;
143152

144153
fn as_trie_db_cache(&self, _storage_root: <H as Hasher>::Out) -> Self::Cache<'_> {
145154
unimplemented!()
@@ -176,7 +185,10 @@ impl<H: Hasher> trie_db::TrieRecorder<H::Out> for UnimplementedRecorderProvider<
176185

177186
#[cfg(not(feature = "std"))]
178187
impl<H: Hasher> TrieRecorderProvider<H> for UnimplementedRecorderProvider<H> {
179-
type Recorder<'a> = UnimplementedRecorderProvider<H> where H: 'a;
188+
type Recorder<'a>
189+
= UnimplementedRecorderProvider<H>
190+
where
191+
H: 'a;
180192

181193
fn drain_storage_proof(self) -> Option<StorageProof> {
182194
unimplemented!()

substrate/primitives/trie/src/recorder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ pub struct TrieRecorder<'a, H: Hasher> {
252252
}
253253

254254
impl<H: Hasher> crate::TrieRecorderProvider<H> for Recorder<H> {
255-
type Recorder<'a> = TrieRecorder<'a, H> where H: 'a;
255+
type Recorder<'a>
256+
= TrieRecorder<'a, H>
257+
where
258+
H: 'a;
256259

257260
fn drain_storage_proof(self) -> Option<StorageProof> {
258261
Some(Recorder::drain_storage_proof(self))

0 commit comments

Comments
 (0)