Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MaxEncodedLen for output buffer size #2128

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ink_prelude = { workspace = true }
ink_primitives = { workspace = true }
pallet-contracts-uapi = { workspace = true }

scale = { workspace = true }
scale = { workspace = true, features = ["max-encoded-len"] }
derive_more = { workspace = true, features = ["from", "display"] }
num-traits = { workspace = true, features = ["i128"] }
cfg-if = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/env/src/engine/on_chain/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ impl<'a> ScopedBuffer<'a> {
lhs
}

/// Returns the first [`scale::MaxEncodedLen::max_encoded_len`] bytes of the buffer as
/// a mutable slice.
#[inline(always)]
pub fn take_max_encoded_len<T>(&mut self) -> &'a mut [u8]
where
T: scale::MaxEncodedLen,
{
let len = T::max_encoded_len();
self.take(len)
}

/// Encode the given value into the scoped buffer and return the sub slice
/// containing all the encoded bytes.
#[inline(always)]
Expand Down
14 changes: 4 additions & 10 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,7 @@ impl TypedEnvBackend for EnvInstance {
let enc_code_hash = scoped.take_encoded(params.code_hash());
let enc_endowment = scoped.take_encoded(params.endowment());
let enc_input = scoped.take_encoded(params.exec_input());
// We support `AccountId` types with an encoding that requires up to
// 1024 bytes. Beyond that limit ink! contracts will trap for now.
// In the default configuration encoded `AccountId` require 32 bytes.
let out_address = &mut scoped.take(1024);
let out_address = &mut scoped.take_max_encoded_len::<E::AccountId>();
let salt = params.salt_bytes().as_ref();
let out_return_value = &mut scoped.take_rest();

Expand Down Expand Up @@ -616,10 +613,7 @@ impl TypedEnvBackend for EnvInstance {
let enc_code_hash = scoped.take_encoded(params.code_hash());
let enc_endowment = scoped.take_encoded(params.endowment());
let enc_input = scoped.take_encoded(params.exec_input());
// We support `AccountId` types with an encoding that requires up to
// 1024 bytes. Beyond that limit ink! contracts will trap for now.
// In the default configuration encoded `AccountId` require 32 bytes.
let out_address = &mut scoped.take(1024);
let out_address = &mut scoped.take_max_encoded_len::<E::AccountId>();
let salt = params.salt_bytes().as_ref();
let out_return_value = &mut scoped.take_rest();

Expand Down Expand Up @@ -686,7 +680,7 @@ impl TypedEnvBackend for EnvInstance {
E: Environment,
{
let mut scope = self.scoped_buffer();
let output = scope.take(32);
let output = scope.take_max_encoded_len::<E::Hash>();
scope.append_encoded(account_id);
let enc_account_id = scope.take_appended();

Expand All @@ -699,7 +693,7 @@ impl TypedEnvBackend for EnvInstance {
where
E: Environment,
{
let output = &mut self.scoped_buffer().take(32);
let output = &mut self.scoped_buffer().take_max_encoded_len::<E::Hash>();
ext::own_code_hash(output);
let hash = scale::Decode::decode(&mut &output[..])?;
Ok(hash)
Expand Down
2 changes: 2 additions & 0 deletions crates/env/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub trait Environment: Clone {
/// The account id type.
type AccountId: 'static
+ scale::Codec
+ scale::MaxEncodedLen
+ CodecAsType
+ Clone
+ PartialEq
Expand All @@ -149,6 +150,7 @@ pub trait Environment: Clone {
/// The type of hash.
type Hash: 'static
+ scale::Codec
+ scale::MaxEncodedLen
+ CodecAsType
+ Copy
+ Clone
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include = ["/Cargo.toml", "src/**/*.rs", "/README.md", "/LICENSE"]
[dependencies]
derive_more = { workspace = true, features = ["from", "display"] }
ink_prelude = { workspace = true }
scale = { workspace = true }
scale = { workspace = true, features = ["max-encoded-len"] }
scale-decode = { workspace = true, features = ["derive"], optional = true }
scale-encode = { workspace = true, features = ["derive"], optional = true }
scale-info = { workspace = true, features = ["derive"], optional = true }
Expand Down
15 changes: 14 additions & 1 deletion crates/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use derive_more::From;
use scale::{
Decode,
Encode,
MaxEncodedLen,
};
#[cfg(feature = "std")]
use {
Expand All @@ -32,7 +33,18 @@ use {
/// This is a mirror of the `AccountId` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Decode, Encode, From,
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct AccountId([u8; 32]);
Expand Down Expand Up @@ -91,6 +103,7 @@ impl<'a> TryFrom<&'a [u8]> for AccountId {
Hash,
Decode,
Encode,
MaxEncodedLen,
From,
Default,
)]
Expand Down