From 807dc5dc41c279e8a20a72d2275d071bca2b4beb Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 9 Aug 2023 12:31:06 +0100 Subject: [PATCH 1/3] modify the buffer via features --- crates/env/Cargo.toml | 9 +++ crates/env/src/engine/mod.rs | 23 +++++++ crates/env/src/engine/off_chain/impls.rs | 7 ++- crates/env/src/engine/off_chain/mod.rs | 5 +- crates/env/src/engine/on_chain/buffer.rs | 8 ++- crates/env/src/engine/on_chain/mod.rs | 5 +- crates/env/src/lib.rs | 1 + crates/ink/Cargo.toml | 9 +++ integration-tests/static-buffer/.gitignore | 9 +++ integration-tests/static-buffer/Cargo.toml | 28 +++++++++ integration-tests/static-buffer/README.md | 23 +++++++ integration-tests/static-buffer/lib.rs | 70 ++++++++++++++++++++++ 12 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 integration-tests/static-buffer/.gitignore create mode 100644 integration-tests/static-buffer/Cargo.toml create mode 100644 integration-tests/static-buffer/README.md create mode 100644 integration-tests/static-buffer/lib.rs diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index 240a0d5b867..cd9a97001c2 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -79,3 +79,12 @@ ink-debug = [] # Disable the ink! provided global memory allocator. no-allocator = ["ink_allocator/no-allocator"] + +# Configurable sizes of the static buffer +2GB-buffer = [] +1GB-buffer = [] +512MB-buffer = [] +128MB-buffer = [] +1MB-buffer = [] +512kB-buffer = [] +128kB-buffer = [] diff --git a/crates/env/src/engine/mod.rs b/crates/env/src/engine/mod.rs index daefd111dac..cd495308fb4 100644 --- a/crates/env/src/engine/mod.rs +++ b/crates/env/src/engine/mod.rs @@ -31,6 +31,29 @@ 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 = "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: F) -> R where diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 88a7ba8e9d7..37a9470557f 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -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, @@ -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 ::Type) { diff --git a/crates/env/src/engine/off_chain/mod.rs b/crates/env/src/engine/off_chain/mod.rs index 1f463b4427f..69731d252f9 100644 --- a/crates/env/src/engine/off_chain/mod.rs +++ b/crates/env/src/engine/off_chain/mod.rs @@ -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; diff --git a/crates/env/src/engine/on_chain/buffer.rs b/crates/env/src/engine/on_chain/buffer.rs index 8cd59043a1a..73cd2b30545 100644 --- a/crates/env/src/engine/on_chain/buffer.rs +++ b/crates/env/src/engine/on_chain/buffer.rs @@ -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 { diff --git a/crates/env/src/engine/on_chain/mod.rs b/crates/env/src/engine/on_chain/mod.rs index 8a42b90445d..0e26c0c0547 100644 --- a/crates/env/src/engine/on_chain/mod.rs +++ b/crates/env/src/engine/on_chain/mod.rs @@ -24,7 +24,10 @@ use self::{ }, ext::Error, }; -use super::OnInstance; +use super::{ + OnInstance, + STATIC_BUFFER_SIZE, +}; /// The on-chain environment. pub struct EnvInstance { diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 6aa0e44e3ae..3faca223483 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -125,6 +125,7 @@ pub use self::{ NoChainExtension, }, }; +pub use engine::STATIC_BUFFER_SIZE; use ink_primitives::Clear; cfg_if::cfg_if! { diff --git a/crates/ink/Cargo.toml b/crates/ink/Cargo.toml index 10ff807003d..bfd7cc50680 100644 --- a/crates/ink/Cargo.toml +++ b/crates/ink/Cargo.toml @@ -54,3 +54,12 @@ 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"] +1MB-buffer = ["ink_env/1MB-buffer"] +512kB-buffer = ["ink_env/512kB-buffer"] +128kB-buffer = ["ink_env/128kB-buffer"] diff --git a/integration-tests/static-buffer/.gitignore b/integration-tests/static-buffer/.gitignore new file mode 100644 index 00000000000..bf910de10af --- /dev/null +++ b/integration-tests/static-buffer/.gitignore @@ -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 \ No newline at end of file diff --git a/integration-tests/static-buffer/Cargo.toml b/integration-tests/static-buffer/Cargo.toml new file mode 100644 index 00000000000..6a61163663d --- /dev/null +++ b/integration-tests/static-buffer/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "static_buffer" +version = "4.2.0" +authors = ["Parity Technologies "] +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 = [] diff --git a/integration-tests/static-buffer/README.md b/integration-tests/static-buffer/README.md new file mode 100644 index 00000000000..1a273796579 --- /dev/null +++ b/integration-tests/static-buffer/README.md @@ -0,0 +1,23 @@ +# 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 = [] +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. diff --git a/integration-tests/static-buffer/lib.rs b/integration-tests/static-buffer/lib.rs new file mode 100644 index 00000000000..7036896c636 --- /dev/null +++ b/integration-tests/static-buffer/lib.rs @@ -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 = std::result::Result>; + + #[ink_e2e::test] + async fn on_chain_buffer_size( + 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::(); + + // 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(()) + } + } +} From 217356d83e73a33d6e43cafa708c9864d69a3b26 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 9 Aug 2023 12:37:25 +0100 Subject: [PATCH 2/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b1ea7a9962..3e1dceb433f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) From 93273006fa67a35b78c92b6f3254af07d3503240 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 9 Aug 2023 12:43:59 +0100 Subject: [PATCH 3/3] add 16MB option --- crates/env/Cargo.toml | 1 + crates/env/src/engine/mod.rs | 2 ++ crates/ink/Cargo.toml | 1 + integration-tests/static-buffer/README.md | 1 + 4 files changed, 5 insertions(+) diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index cd9a97001c2..6bd8af71631 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -85,6 +85,7 @@ no-allocator = ["ink_allocator/no-allocator"] 1GB-buffer = [] 512MB-buffer = [] 128MB-buffer = [] +16MB-buffer = [] 1MB-buffer = [] 512kB-buffer = [] 128kB-buffer = [] diff --git a/crates/env/src/engine/mod.rs b/crates/env/src/engine/mod.rs index cd495308fb4..edb77d924a0 100644 --- a/crates/env/src/engine/mod.rs +++ b/crates/env/src/engine/mod.rs @@ -43,6 +43,8 @@ pub const STATIC_BUFFER_SIZE: usize = { 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") { diff --git a/crates/ink/Cargo.toml b/crates/ink/Cargo.toml index bfd7cc50680..3e751b04151 100644 --- a/crates/ink/Cargo.toml +++ b/crates/ink/Cargo.toml @@ -60,6 +60,7 @@ no-allocator = ["ink_env/no-allocator"] 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"] diff --git a/integration-tests/static-buffer/README.md b/integration-tests/static-buffer/README.md index 1a273796579..3b34354c9fc 100644 --- a/integration-tests/static-buffer/README.md +++ b/integration-tests/static-buffer/README.md @@ -10,6 +10,7 @@ Right now we offer these set of sizes and their corresponding features: 1GB-buffer = [] 512MB-buffer = [] 128MB-buffer = [] +16MB-buffer = [] 1MB-buffer = [] 512kB-buffer = [] 128kB-buffer = []