This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fix WrapperOpaque max encded len and type info #9881
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4adbcd
fix wrapper opaque
gui1117 dafb386
fix compilation
gui1117 5f07186
improve more precise implementation
gui1117 5c2c140
spacing
gui1117 65669ac
fmt
gui1117 ad26241
Merge remote-tracking branch 'origin/master' into gui-fix-wrapper-opaque
gui1117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,11 @@ | |
|
||
//! Smaller traits used in FRAME which don't need their own file. | ||
|
||
use crate::{dispatch::Parameter, TypeInfo}; | ||
use crate::dispatch::Parameter; | ||
use codec::{Decode, Encode, EncodeLike, Input, MaxEncodedLen}; | ||
use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter}; | ||
use sp_runtime::{traits::Block as BlockT, DispatchError}; | ||
use sp_std::vec::Vec; | ||
use sp_std::prelude::*; | ||
|
||
/// Anything that can have a `::len()` method. | ||
pub trait Len { | ||
|
@@ -384,16 +385,15 @@ impl<Call, Balance: From<u32>, const T: u32> EstimateCallFee<Call, Balance> for | |
/// | ||
/// The encoding is the encoding of `T` prepended with the compact encoding of its size in bytes. | ||
/// Thus the encoded value can be decoded as a `Vec<u8>`. | ||
#[derive(Debug, Eq, PartialEq, Default, Clone, MaxEncodedLen, TypeInfo)] | ||
#[derive(Debug, Eq, PartialEq, Default, Clone)] | ||
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct WrapperOpaque<T>(pub T); | ||
|
||
impl<T: Encode> EncodeLike for WrapperOpaque<T> {} | ||
|
||
impl<T: Encode> Encode for WrapperOpaque<T> { | ||
fn size_hint(&self) -> usize { | ||
// Compact<u32> usually takes at most 4 bytes | ||
self.0.size_hint().saturating_add(4) | ||
self.0.size_hint().saturating_add(<codec::Compact<u32>>::max_encoded_len()) | ||
} | ||
|
||
fn encode_to<O: codec::Output + ?Sized>(&self, dest: &mut O) { | ||
|
@@ -425,6 +425,28 @@ impl<T> From<T> for WrapperOpaque<T> { | |
} | ||
} | ||
|
||
impl<T: MaxEncodedLen> MaxEncodedLen for WrapperOpaque<T> { | ||
fn max_encoded_len() -> usize { | ||
// Note: this can be improved if `T::max_encoded_len` is small. | ||
// E.g. if T max encoded len is 4, then the compact encoding of its encoded length is 1. | ||
<codec::Compact<u32>>::max_encoded_len().saturating_add(T::max_encoded_len()) | ||
} | ||
} | ||
|
||
impl<T: TypeInfo + 'static> TypeInfo for WrapperOpaque<T> { | ||
type Identity = Self; | ||
fn type_info() -> Type { | ||
Type::builder() | ||
.path(Path::new("WrapperOpaque", module_path!())) | ||
.type_params(vec![TypeParameter::new("T", Some(meta_type::<T>()))]) | ||
.composite( | ||
Fields::unnamed() | ||
.field(|f| f.compact::<u32>().type_name("EncodedLengthOfT")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I'll do another PR with this removed |
||
.field(|f| f.ty::<T>().type_name("T")), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
@@ -438,5 +460,6 @@ mod test { | |
assert_eq!(decoded_from_vec_u8, 3u32); | ||
let decoded = <WrapperOpaque<u32>>::decode(&mut &encoded[..]).unwrap(); | ||
assert_eq!(decoded.0, 3u32); | ||
assert_eq!(<WrapperOpaque<u32>>::max_encoded_len(), 5 + 4); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this comment here?
The point of this is to return the max encoded length?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but I think we can be more precise in certain situation, I finally did the more precise implementation 5f07186