Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Stabilize `call_runtime` ‒ [#1749](https://github.com/paritytech/ink/pull/1749)
- Make E2E testcases generic over `E2EBackend` trait - [#1867](https://github.com/paritytech/ink/pull/1867)
- Modify static buffer size via features - [#1872](https://github.com/paritytech/ink/pull/1872)

### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)
Expand Down
10 changes: 10 additions & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,13 @@ ink-debug = []

# Disable the ink! provided global memory allocator.
no-allocator = ["ink_allocator/no-allocator"]

# Configurable sizes of the static buffer
2GB-buffer = []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says that memory is to huge:
image

1GB-buffer = []
512MB-buffer = []
128MB-buffer = []
16MB-buffer = []
1MB-buffer = []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting 1MB it returns another error:
image

512kB-buffer = []
128kB-buffer = []
25 changes: 25 additions & 0 deletions crates/env/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ use ink_primitives::{
LangError,
};

/// Configurable size of the static buffer.
/// We have preset list of sizes
/// that developer can leverage in the smart contracts
pub const STATIC_BUFFER_SIZE: usize = {
if cfg!(feature = "2GB-buffer") {
1 << 31 // 2 GB
} else if cfg!(feature = "1GB-buffer") {
1 << 30 // 1 GB
} else if cfg!(feature = "512MB-buffer") {
1 << 29 // 512 MB
} else if cfg!(feature = "128MB-buffer") {
1 << 27 // 128 MB
} else if cfg!(feature = "16MB-buffer") {
1 << 24 // 16 MB
} else if cfg!(feature = "1MB-buffer") {
1 << 20 // 1 MB
} else if cfg!(feature = "512kB-buffer") {
1 << 19 // 512 kB
} else if cfg!(feature = "128kB-buffer") {
1 << 17 // 128 kB
} else {
1 << 14 // 16 kB
}
};

pub trait OnInstance: EnvBackend + TypedEnvBackend {
fn on_instance<F, R>(f: F) -> R
where
Expand Down
7 changes: 5 additions & 2 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::EnvInstance;
use super::{
EnvInstance,
STATIC_BUFFER_SIZE,
};
use crate::{
call::{
Call,
Expand Down Expand Up @@ -51,7 +54,7 @@ use ink_storage_traits::Storable;
/// The capacity of the static buffer.
/// This is the same size as the ink! on-chain environment. We chose to use the same size
/// to be as close to the on-chain behavior as possible.
const BUFFER_SIZE: usize = 1 << 14; // 16 kB
const BUFFER_SIZE: usize = STATIC_BUFFER_SIZE;

impl CryptoHash for Blake2x128 {
fn hash(input: &[u8], output: &mut <Self as HashOutput>::Type) {
Expand Down
5 changes: 4 additions & 1 deletion crates/env/src/engine/off_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ mod tests;

pub use call_data::CallData;

use super::OnInstance;
use super::{
OnInstance,
STATIC_BUFFER_SIZE,
};
use crate::Error;

use derive_more::From;
Expand Down
8 changes: 5 additions & 3 deletions crates/env/src/engine/on_chain/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// A static buffer with 16 kB of capacity.
use super::STATIC_BUFFER_SIZE;

/// A static buffer with a configurable capacity.
pub struct StaticBuffer {
/// The static buffer with a total capacity of 16 kB.
/// The static buffer with configurable capacity.
buffer: [u8; Self::CAPACITY],
}

impl StaticBuffer {
/// The capacity of the static buffer.
const CAPACITY: usize = 1 << 14; // 16 kB
const CAPACITY: usize = STATIC_BUFFER_SIZE;

/// Creates a new static buffer.
pub const fn new() -> Self {
Expand Down
5 changes: 4 additions & 1 deletion crates/env/src/engine/on_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use self::{
},
ext::Error,
};
use super::OnInstance;
use super::{
OnInstance,
STATIC_BUFFER_SIZE,
};

/// The on-chain environment.
pub struct EnvInstance {
Expand Down
1 change: 1 addition & 0 deletions crates/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub use self::{
NoChainExtension,
},
};
pub use engine::STATIC_BUFFER_SIZE;
use ink_primitives::Clear;

cfg_if::cfg_if! {
Expand Down
10 changes: 10 additions & 0 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ show-codegen-docs = []

# Disable the ink! provided global memory allocator.
no-allocator = ["ink_env/no-allocator"]

# Configurable sizes of the static buffer
2GB-buffer = ["ink_env/2GB-buffer"]
1GB-buffer = ["ink_env/1GB-buffer"]
512MB-buffer = ["ink_env/512MB-buffer"]
128MB-buffer = ["ink_env/128MB-buffer"]
16MB-buffer = ["ink_env/16MB-buffer"]
1MB-buffer = ["ink_env/1MB-buffer"]
512kB-buffer = ["ink_env/512kB-buffer"]
128kB-buffer = ["ink_env/128kB-buffer"]
9 changes: 9 additions & 0 deletions integration-tests/static-buffer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
28 changes: 28 additions & 0 deletions integration-tests/static-buffer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "static_buffer"
version = "4.2.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../crates/ink", default-features = false, features = ["512kB-buffer"]}

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []
24 changes: 24 additions & 0 deletions integration-tests/static-buffer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Static buffer configuration demo

This is a dummy contract illustrating how the [static buffer](/ARCHITECTURE.md#communication-with-the-pallet)
can be be configured using the crate features.

Right now we offer these set of sizes and their corresponding features:
```toml
# Configurable sizes of the static buffer
2GB-buffer = []
1GB-buffer = []
512MB-buffer = []
128MB-buffer = []
16MB-buffer = []
1MB-buffer = []
512kB-buffer = []
128kB-buffer = []
```

You can configure the buffer size by using one of the features:
```toml
ink = { path = "../../crates/ink", default-features = false, features = ["512kB-buffer"]}
```

Otherwise, the default size of 16 kB is used.
70 changes: 70 additions & 0 deletions integration-tests/static-buffer/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
pub mod static_buffer {
#[ink(storage)]
pub struct StaticBuffer {
value: bool,
}

impl StaticBuffer {
/// Creates a new flipper smart contract initialized with the given value.
#[ink(constructor)]
pub fn new(init_value: bool) -> Self {
Self { value: init_value }
}

/// Creates a new flipper smart contract initialized to `false`.
#[ink(constructor)]
pub fn new_default() -> Self {
Self::new(Default::default())
}

/// Returns the configured size of the static buffer.
#[ink(message)]
pub fn get_size(&self) -> u128 {
ink::env::STATIC_BUFFER_SIZE as u128
}
}

#[cfg(test)]
mod tests {
use super::*;

#[ink::test]
fn off_chain_buffer_size() {
let instance = StaticBuffer::new_default();
let expected_size: u128 = 1 << 19; // 512 kB
assert_eq!(instance.get_size(), expected_size);
}
}

#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
use ink_e2e::ContractsBackend;

type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn on_chain_buffer_size<Client: E2EBackend>(
mut client: Client,
) -> E2EResult<()> {
// given
let constructor = StaticBufferRef::new(false);
let contract = client
.instantiate("static_buffer", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed");
let call = contract.call::<StaticBuffer>();

// then
let get = call.get_size();
let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
let expected_size: u128 = 1 << 19; // 512 kB
assert_eq!(get_res.return_value(), expected_size);

Ok(())
}
}
}