Files
pezkuwi-subxt/substrate/max-encoded-len/tests/max_encoded_len.rs
T
Shawn Tabrizi 444d4e204c Simple MaxBoundedLen Implementations (#8793)
* implement max_values + storages info

* some formatting + doc

* sudo sanity check

* timestamp

* assets (not working)

* fix assets

* impl for proxy

* update balances

* rename StoragesInfo -> PalletStorageInfo

* merge both StorageInfoTrait and PalletStorageInfo

I think it is more future proof. In the future some storage could make
use of multiple prefix. Like one to store how much value has been
inserted, etc...

* Update frame/support/procedural/src/storage/parse.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Update frame/support/procedural/src/storage/storage_struct.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Fix max_size using hasher information

hasher now expose `max_len` which allows to computes their maximum len.
For hasher without concatenation, it is the size of the hash part,
for hasher with concatenation, it is the size of the hash part + max
encoded len of the key.

* fix tests

* fix ui tests

* Move `MaxBoundedLen` into its own crate (#8814)

* move MaxEncodedLen into its own crate

* remove MaxEncodedLen impl from frame-support

* add to assets and balances

* try more fixes

* fix compile

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* nits

* fix compile

* line width

* fix max-values-macro merge

* Add some derive, needed for test and other purpose

* use weak bounded vec in some cases

* Update lib.rs

* move max-encoded-len crate

* fix

* remove app crypto for now

* width

* Revert "remove app crypto for now"

This reverts commit 73623e9933d50648e0e7fe90b6171a8e45d7f5a2.

* unused variable

* more unused variables

* more fixes

* Add #[max_encoded_len_crate(...)] helper attribute

The purpose of this attribute is to reduce the surface area of
max_encoded_len changes. Crates deriving `MaxEncodedLen` do not
need to add it to `Cargo.toml`; they can instead just do

```rust
\#[derive(Encode, MaxEncodedLen)]
\#[max_encoded_len_crate(frame_support::max_encoded_len)]
struct Example;
```

* fix a ui test

* use #[max_encoded_len_crate(...)] helper in app_crypto

* remove max_encoded_len import where not necessary

* update lockfile

* fix ui test

* ui

* newline

* fix merge

* try fix ui again

* Update max-encoded-len/derive/src/lib.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* extract generate_crate_access_2018

* Update lib.rs

* compiler isnt smart enough

Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
Co-authored-by: Peter Goodspeed-Niklaus <peter.r.goodspeedniklaus@gmail.com>
2021-05-28 18:35:15 +00:00

152 lines
3.3 KiB
Rust

// This file is part of Substrate.
// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Tests for MaxEncodedLen derive macro
#![cfg(feature = "derive")]
use max_encoded_len::MaxEncodedLen;
use codec::{Compact, Encode};
// These structs won't even compile if the macro isn't working right.
#[derive(Encode, MaxEncodedLen)]
struct Primitives {
bool: bool,
eight: u8,
}
#[test]
fn primitives_max_length() {
assert_eq!(Primitives::max_encoded_len(), 2);
}
#[derive(Encode, MaxEncodedLen)]
struct Composites {
fixed_size_array: [u8; 128],
tuple: (u128, u128),
}
#[test]
fn composites_max_length() {
assert_eq!(Composites::max_encoded_len(), 128 + 16 + 16);
}
#[derive(Encode, MaxEncodedLen)]
struct Generic<T> {
one: T,
two: T,
}
#[test]
fn generic_max_length() {
assert_eq!(Generic::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
assert_eq!(Generic::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
}
#[derive(Encode, MaxEncodedLen)]
struct TwoGenerics<T, U> {
t: T,
u: U,
}
#[test]
fn two_generics_max_length() {
assert_eq!(
TwoGenerics::<u8, u16>::max_encoded_len(),
u8::max_encoded_len() + u16::max_encoded_len()
);
assert_eq!(
TwoGenerics::<Compact<u64>, [u16; 8]>::max_encoded_len(),
Compact::<u64>::max_encoded_len() + <[u16; 8]>::max_encoded_len()
);
}
#[derive(Encode, MaxEncodedLen)]
struct UnitStruct;
#[test]
fn unit_struct_max_length() {
assert_eq!(UnitStruct::max_encoded_len(), 0);
}
#[derive(Encode, MaxEncodedLen)]
struct TupleStruct(u8, u32);
#[test]
fn tuple_struct_max_length() {
assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
}
#[derive(Encode, MaxEncodedLen)]
struct TupleGeneric<T>(T, T);
#[test]
fn tuple_generic_max_length() {
assert_eq!(TupleGeneric::<u8>::max_encoded_len(), u8::max_encoded_len() * 2);
assert_eq!(TupleGeneric::<u32>::max_encoded_len(), u32::max_encoded_len() * 2);
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum UnitEnum {
A,
B,
}
#[test]
fn unit_enum_max_length() {
assert_eq!(UnitEnum::max_encoded_len(), 1);
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum TupleEnum {
A(u32),
B,
}
#[test]
fn tuple_enum_max_length() {
assert_eq!(TupleEnum::max_encoded_len(), 1 + u32::max_encoded_len());
}
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum StructEnum {
A { sixty_four: u64, one_twenty_eight: u128 },
B,
}
#[test]
fn struct_enum_max_length() {
assert_eq!(StructEnum::max_encoded_len(), 1 + u64::max_encoded_len() + u128::max_encoded_len());
}
// ensure that enums take the max of variant length, not the sum
#[derive(Encode, MaxEncodedLen)]
#[allow(unused)]
enum EnumMaxNotSum {
A(u32),
B(u32),
}
#[test]
fn enum_max_not_sum_max_length() {
assert_eq!(EnumMaxNotSum::max_encoded_len(), 1 + u32::max_encoded_len());
}