From cd25a65ed0a778eca331d9f1fc0f77533c5a88be Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Mon, 7 Aug 2023 19:00:00 +0100 Subject: [PATCH 01/14] modify buffer size via env var --- crates/env/Cargo.toml | 5 +++++ crates/env/build.rs | 15 +++++++++++++++ crates/env/src/engine/on_chain/buffer.rs | 6 +++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 crates/env/build.rs diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index 240a0d5b86..19f2ff109f 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -4,6 +4,7 @@ version.workspace = true authors = ["Parity Technologies ", "Robin Freyler "] edition.workspace = true rust-version = "1.68" +build = "build.rs" license.workspace = true readme = "README.md" @@ -54,6 +55,10 @@ scale-info = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] ink = { workspace = true } +[build-dependencies] +const-gen = "1.4" +dotenvy = "0.15" + [features] default = ["std"] std = [ diff --git a/crates/env/build.rs b/crates/env/build.rs new file mode 100644 index 0000000000..e493080290 --- /dev/null +++ b/crates/env/build.rs @@ -0,0 +1,15 @@ +use const_gen::*; +use std::{env, fs, path::Path}; + +fn main() { + let mut size: usize = 1 << 14; + if let Ok(size_str) = std::env::var("STATIC_BUFFER_SIZE") { + if let Ok(new_size) = size_str.parse::() { + size = new_size; + } + } + let out_dir = env::var_os("OUT_DIR").unwrap(); + let dest_path = Path::new(&out_dir).join("const_gen.rs"); + let const_decl = const_declaration!(STATIC_BUFFER_SIZE = size); + fs::write(dest_path, const_decl).unwrap(); +} diff --git a/crates/env/src/engine/on_chain/buffer.rs b/crates/env/src/engine/on_chain/buffer.rs index 8cd59043a1..83fbd1afb2 100644 --- a/crates/env/src/engine/on_chain/buffer.rs +++ b/crates/env/src/engine/on_chain/buffer.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +include!(concat!(env!("OUT_DIR"), "/const_gen.rs")); + /// A static buffer with 16 kB of capacity. pub struct StaticBuffer { /// The static buffer with a total capacity of 16 kB. @@ -20,7 +22,9 @@ pub struct StaticBuffer { impl StaticBuffer { /// The capacity of the static buffer. - const CAPACITY: usize = 1 << 14; // 16 kB + /// Usually set to 16kB. + /// Can be modified by setting `STATIC_BUFFER_SIZE` environmental variable + const CAPACITY: usize = STATIC_BUFFER_SIZE; /// Creates a new static buffer. pub const fn new() -> Self { From 0441a0ef70d5b304a8f831e84cb586e70f569b55 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Mon, 7 Aug 2023 19:17:45 +0100 Subject: [PATCH 02/14] fmt --- crates/env/build.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/env/build.rs b/crates/env/build.rs index e493080290..cfca7ae3af 100644 --- a/crates/env/build.rs +++ b/crates/env/build.rs @@ -1,5 +1,9 @@ use const_gen::*; -use std::{env, fs, path::Path}; +use std::{ + env, + fs, + path::Path, +}; fn main() { let mut size: usize = 1 << 14; From c2aacd4f1fb3e2d1109b3a8d49368c3693d4f135 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 8 Aug 2023 20:29:32 +0100 Subject: [PATCH 03/14] add docs and tests --- .gitlab-ci.yml | 3 + crates/env/build.rs | 4 ++ crates/env/src/engine/off_chain/impls.rs | 6 +- crates/env/src/engine/on_chain/buffer.rs | 9 +-- integration-tests/static-buffer/.gitignore | 9 +++ integration-tests/static-buffer/Cargo.toml | 28 +++++++++ integration-tests/static-buffer/README.md | 18 ++++++ integration-tests/static-buffer/lib.rs | 69 ++++++++++++++++++++++ 8 files changed, 141 insertions(+), 5 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/.gitlab-ci.yml b/.gitlab-ci.yml index 57cdca8999..26677b74d2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -384,6 +384,9 @@ examples-test: cargo test --verbose --manifest-path ${example}/Cargo.toml --features "foo, bar"; fi; if grep -q "e2e-tests = \[\]" "${example}/Cargo.toml"; then + if [ "$example" = "integration-tests/static-buffer/" ]; then + STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; + else cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; else cargo test --verbose --manifest-path ${example}/Cargo.toml; diff --git a/crates/env/build.rs b/crates/env/build.rs index cfca7ae3af..871f33a038 100644 --- a/crates/env/build.rs +++ b/crates/env/build.rs @@ -6,7 +6,9 @@ use std::{ }; fn main() { + // Default size of the buffer: 16 kB. let mut size: usize = 1 << 14; + // if environmental variable is present we update the size. if let Ok(size_str) = std::env::var("STATIC_BUFFER_SIZE") { if let Ok(new_size) = size_str.parse::() { size = new_size; @@ -14,6 +16,8 @@ fn main() { } let out_dir = env::var_os("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("const_gen.rs"); + // create a constant with the specified value. let const_decl = const_declaration!(STATIC_BUFFER_SIZE = size); + // Appends it to a file with constants. fs::write(dest_path, const_decl).unwrap(); } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 88a7ba8e9d..e6703449f1 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -48,10 +48,14 @@ use ink_engine::{ }; use ink_storage_traits::Storable; +// Injected constants from the build script. +include!(concat!(env!("OUT_DIR"), "/const_gen.rs")); + /// 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 +/// The value is copied from the injected constant from the build script. +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/on_chain/buffer.rs b/crates/env/src/engine/on_chain/buffer.rs index 83fbd1afb2..2d782e3b3e 100644 --- a/crates/env/src/engine/on_chain/buffer.rs +++ b/crates/env/src/engine/on_chain/buffer.rs @@ -12,18 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Injected constants from the build script. include!(concat!(env!("OUT_DIR"), "/const_gen.rs")); -/// A static buffer with 16 kB of capacity. +/// A static buffer of variable capacity. pub struct StaticBuffer { - /// The static buffer with a total capacity of 16 kB. + /// A static buffer of variable capacity. buffer: [u8; Self::CAPACITY], } impl StaticBuffer { /// The capacity of the static buffer. - /// Usually set to 16kB. - /// Can be modified by setting `STATIC_BUFFER_SIZE` environmental variable + /// Usually set to 16 kB. + /// Can be modified by setting `STATIC_BUFFER_SIZE` environmental variable. const CAPACITY: usize = STATIC_BUFFER_SIZE; /// Creates a new static buffer. diff --git a/integration-tests/static-buffer/.gitignore b/integration-tests/static-buffer/.gitignore new file mode 100644 index 0000000000..bf910de10a --- /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 0000000000..0a2911165a --- /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 } + +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 0000000000..2b93517407 --- /dev/null +++ b/integration-tests/static-buffer/README.md @@ -0,0 +1,18 @@ +# 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 environmental variables. + +Simply, run: +```bash +cargo clean +STATIC_BUFFER_SIZE=30 cargo test -F e2e-tests +``` + +This will configure the buffer to have enough space to instantiate the contract, +but not enough space to retrieve the caller's address as it is of 32 bytes, +but we only allocated 30 bytes to the contract. + +## Note +You must run `cargo clean` every time you want to modify the buffer size +because the value is baked into the binaries. diff --git a/integration-tests/static-buffer/lib.rs b/integration-tests/static-buffer/lib.rs new file mode 100644 index 0000000000..899460efc9 --- /dev/null +++ b/integration-tests/static-buffer/lib.rs @@ -0,0 +1,69 @@ +#![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 dummy smart contract. + #[ink(constructor)] + pub fn new(init_value: bool) -> Self { + Self { value: init_value } + } + + /// Sets a default value. + #[ink(constructor)] + pub fn new_default() -> Self { + Self::new(Default::default()) + } + + /// Returns the caller of the contract. + /// Should panic if the buffer size is less than 32 bytes. + #[ink(message)] + pub fn get_caller(&self) -> AccountId { + self.env().caller() + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[ink::test] + #[should_panic(expected = "the output buffer is too small!")] + fn run_out_buffer_memory() { + let flipper = StaticBuffer::new(false); + flipper.get_caller() + } + } + + #[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 e2e_run_out_of_buffer_memory(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::(); + + //when + let get = call.get_caller(); + // then panics if `STATIC_BUFFER_SIZE` is less than 32 bytes. + let res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; + assert!(res.is_err()); + + Ok(()) + } + } +} From 4a954ae5296c3b0f5886ddb67d4626c9322e608a Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 8 Aug 2023 20:32:41 +0100 Subject: [PATCH 04/14] fmt --- integration-tests/static-buffer/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/integration-tests/static-buffer/lib.rs b/integration-tests/static-buffer/lib.rs index 899460efc9..a676add37b 100644 --- a/integration-tests/static-buffer/lib.rs +++ b/integration-tests/static-buffer/lib.rs @@ -48,7 +48,9 @@ pub mod static_buffer { type E2EResult = std::result::Result>; #[ink_e2e::test] - async fn e2e_run_out_of_buffer_memory(mut client: Client) -> E2EResult<()> { + async fn e2e_run_out_of_buffer_memory( + mut client: Client, + ) -> E2EResult<()> { // given let constructor = StaticBufferRef::new(false); let contract = client @@ -57,7 +59,7 @@ pub mod static_buffer { .expect("instantiate failed"); let call = contract.call::(); - //when + // when let get = call.get_caller(); // then panics if `STATIC_BUFFER_SIZE` is less than 32 bytes. let res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; From 0230c209eb66c5643507527a778745bd9ecc0733 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 8 Aug 2023 20:41:04 +0100 Subject: [PATCH 05/14] fix if-else statement in ci --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 26677b74d2..970fe0fd9f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -387,7 +387,8 @@ examples-test: if [ "$example" = "integration-tests/static-buffer/" ]; then STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; else - cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; + cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; + fi; else cargo test --verbose --manifest-path ${example}/Cargo.toml; fi; From 338ac2a491f137becee61465fe630109e40e396e Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 8 Aug 2023 22:57:28 +0100 Subject: [PATCH 06/14] do cargo clean before the running the test --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 970fe0fd9f..76fad4347d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -385,7 +385,7 @@ examples-test: fi; if grep -q "e2e-tests = \[\]" "${example}/Cargo.toml"; then if [ "$example" = "integration-tests/static-buffer/" ]; then - STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; + cargo clean && STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; else cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; fi; From c73ae6188b54defa9f144b30e46fed84c4f9db53 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 8 Aug 2023 23:24:31 +0100 Subject: [PATCH 07/14] clean binaries the test too --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 76fad4347d..ded1454c26 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -385,7 +385,7 @@ examples-test: fi; if grep -q "e2e-tests = \[\]" "${example}/Cargo.toml"; then if [ "$example" = "integration-tests/static-buffer/" ]; then - cargo clean && STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; + cargo clean && STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests && cargo clean; else cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; fi; From a69800650b2c6d075aee77f03539b6d513cbb47e Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 9 Aug 2023 12:39:02 +0100 Subject: [PATCH 08/14] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b1ea7a996..297b0ae5a4 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 environmental variables - [#1869](https://github.com/paritytech/ink/pull/1869) ### Added - Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765) From c99ac6744cb0bbdc57fa012010b5347b65f0c896 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 16 Aug 2023 13:09:20 +0100 Subject: [PATCH 09/14] remove unused dependency and add an error message --- crates/env/Cargo.toml | 1 - crates/env/build.rs | 10 ++++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index 19f2ff109f..3baf7d7ab2 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -57,7 +57,6 @@ ink = { workspace = true } [build-dependencies] const-gen = "1.4" -dotenvy = "0.15" [features] default = ["std"] diff --git a/crates/env/build.rs b/crates/env/build.rs index 871f33a038..d0e36a37b4 100644 --- a/crates/env/build.rs +++ b/crates/env/build.rs @@ -1,17 +1,15 @@ use const_gen::*; -use std::{ - env, - fs, - path::Path, -}; +use std::{env, fs, path::Path}; fn main() { // Default size of the buffer: 16 kB. let mut size: usize = 1 << 14; // if environmental variable is present we update the size. - if let Ok(size_str) = std::env::var("STATIC_BUFFER_SIZE") { + if let Ok(size_str) = std::env::var("INK_STATIC_BUFFER_SIZE") { if let Ok(new_size) = size_str.parse::() { size = new_size; + } else { + eprintln!("INK_STATIC_BUFFER_SIZE variable has been set an incorrect value!") } } let out_dir = env::var_os("OUT_DIR").unwrap(); From 6c9f6ead655459681b194d8e6c60d62da5213610 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 16 Aug 2023 13:25:11 +0100 Subject: [PATCH 10/14] fmt --- crates/env/build.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/env/build.rs b/crates/env/build.rs index d0e36a37b4..b0ad8a8153 100644 --- a/crates/env/build.rs +++ b/crates/env/build.rs @@ -1,5 +1,9 @@ use const_gen::*; -use std::{env, fs, path::Path}; +use std::{ + env, + fs, + path::Path, +}; fn main() { // Default size of the buffer: 16 kB. From 612cd3ce3805410b285a2d2ce1ece83ddae99fbd Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 16 Aug 2023 13:32:46 +0100 Subject: [PATCH 11/14] rename env var --- .gitlab-ci.yml | 2 +- integration-tests/static-buffer/README.md | 2 +- integration-tests/static-buffer/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ded1454c26..cda00fae49 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -385,7 +385,7 @@ examples-test: fi; if grep -q "e2e-tests = \[\]" "${example}/Cargo.toml"; then if [ "$example" = "integration-tests/static-buffer/" ]; then - cargo clean && STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests && cargo clean; + cargo clean && INK_STATIC_BUFFER_SIZE=30 cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests && cargo clean; else cargo test --verbose --manifest-path ${example}/Cargo.toml --features e2e-tests; fi; diff --git a/integration-tests/static-buffer/README.md b/integration-tests/static-buffer/README.md index 2b93517407..eb4f58723d 100644 --- a/integration-tests/static-buffer/README.md +++ b/integration-tests/static-buffer/README.md @@ -6,7 +6,7 @@ can be be configured using the environmental variables. Simply, run: ```bash cargo clean -STATIC_BUFFER_SIZE=30 cargo test -F e2e-tests +INK_STATIC_BUFFER_SIZE=30 cargo test -F e2e-tests ``` This will configure the buffer to have enough space to instantiate the contract, diff --git a/integration-tests/static-buffer/lib.rs b/integration-tests/static-buffer/lib.rs index a676add37b..b74b6fb5d6 100644 --- a/integration-tests/static-buffer/lib.rs +++ b/integration-tests/static-buffer/lib.rs @@ -61,7 +61,7 @@ pub mod static_buffer { // when let get = call.get_caller(); - // then panics if `STATIC_BUFFER_SIZE` is less than 32 bytes. + // then panics if `INK_STATIC_BUFFER_SIZE` is less than 32 bytes. let res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; assert!(res.is_err()); From 6c4ee3b1ce1c1a7ff5dd4bc3a345b49df420cf14 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Fri, 18 Aug 2023 13:53:44 +0100 Subject: [PATCH 12/14] use const_env instead of build script --- crates/env/Cargo.toml | 3 ++- crates/env/build.rs | 25 ------------------------ crates/env/src/engine/off_chain/impls.rs | 5 +---- crates/env/src/engine/on_chain/buffer.rs | 7 ++----- crates/env/src/lib.rs | 6 ++++++ integration-tests/static-buffer/lib.rs | 9 ++++++++- 6 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 crates/env/build.rs diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index 3baf7d7ab2..cac7aa6ba5 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -4,7 +4,7 @@ version.workspace = true authors = ["Parity Technologies ", "Robin Freyler "] edition.workspace = true rust-version = "1.68" -build = "build.rs" +# build = "build.rs" license.workspace = true readme = "README.md" @@ -29,6 +29,7 @@ cfg-if = { workspace = true } paste = { workspace = true } arrayref = { workspace = true } static_assertions = { workspace = true } +const_env = "0.1.2" [target.'cfg(target_arch = "wasm32")'.dependencies] rlibc = "1" diff --git a/crates/env/build.rs b/crates/env/build.rs deleted file mode 100644 index b0ad8a8153..0000000000 --- a/crates/env/build.rs +++ /dev/null @@ -1,25 +0,0 @@ -use const_gen::*; -use std::{ - env, - fs, - path::Path, -}; - -fn main() { - // Default size of the buffer: 16 kB. - let mut size: usize = 1 << 14; - // if environmental variable is present we update the size. - if let Ok(size_str) = std::env::var("INK_STATIC_BUFFER_SIZE") { - if let Ok(new_size) = size_str.parse::() { - size = new_size; - } else { - eprintln!("INK_STATIC_BUFFER_SIZE variable has been set an incorrect value!") - } - } - let out_dir = env::var_os("OUT_DIR").unwrap(); - let dest_path = Path::new(&out_dir).join("const_gen.rs"); - // create a constant with the specified value. - let const_decl = const_declaration!(STATIC_BUFFER_SIZE = size); - // Appends it to a file with constants. - fs::write(dest_path, const_decl).unwrap(); -} diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index e6703449f1..8517fbff11 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -48,14 +48,11 @@ use ink_engine::{ }; use ink_storage_traits::Storable; -// Injected constants from the build script. -include!(concat!(env!("OUT_DIR"), "/const_gen.rs")); - /// 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. /// The value is copied from the injected constant from the build script. -const BUFFER_SIZE: usize = STATIC_BUFFER_SIZE; +const BUFFER_SIZE: usize = crate::BUFFER_SIZE; impl CryptoHash for Blake2x128 { fn hash(input: &[u8], output: &mut ::Type) { diff --git a/crates/env/src/engine/on_chain/buffer.rs b/crates/env/src/engine/on_chain/buffer.rs index 2d782e3b3e..a6f0973437 100644 --- a/crates/env/src/engine/on_chain/buffer.rs +++ b/crates/env/src/engine/on_chain/buffer.rs @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Injected constants from the build script. -include!(concat!(env!("OUT_DIR"), "/const_gen.rs")); - /// A static buffer of variable capacity. pub struct StaticBuffer { /// A static buffer of variable capacity. @@ -24,8 +21,8 @@ pub struct StaticBuffer { impl StaticBuffer { /// The capacity of the static buffer. /// Usually set to 16 kB. - /// Can be modified by setting `STATIC_BUFFER_SIZE` environmental variable. - const CAPACITY: usize = STATIC_BUFFER_SIZE; + /// Can be modified by setting `INK_STATIC_BUFFER_SIZE` environmental variable. + const CAPACITY: usize = crate::BUFFER_SIZE; /// Creates a new static buffer. pub const fn new() -> Self { diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 6aa0e44e3a..881de49344 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -46,6 +46,12 @@ unused_extern_crates )] +/// The capacity of the static buffer. +/// Usually set to 16 kB. +/// Can be modified by setting `INK_STATIC_BUFFER_SIZE` environmental variable. +#[const_env::from_env("INK_STATIC_BUFFER_SIZE")] +pub const BUFFER_SIZE: usize = 16384; + #[cfg(all(not(feature = "std"), target_arch = "wasm32"))] #[allow(unused_extern_crates)] extern crate rlibc; diff --git a/integration-tests/static-buffer/lib.rs b/integration-tests/static-buffer/lib.rs index b74b6fb5d6..d84321436c 100644 --- a/integration-tests/static-buffer/lib.rs +++ b/integration-tests/static-buffer/lib.rs @@ -2,6 +2,8 @@ #[ink::contract] pub mod static_buffer { + #[allow(unused_imports)] + use ink::env::BUFFER_SIZE; #[ink(storage)] pub struct StaticBuffer { value: bool, @@ -63,7 +65,12 @@ pub mod static_buffer { let get = call.get_caller(); // then panics if `INK_STATIC_BUFFER_SIZE` is less than 32 bytes. let res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; - assert!(res.is_err()); + println!("{}", super::BUFFER_SIZE); + assert!( + res.is_err(), + "Buffer size was larger than expected: {}", + super::BUFFER_SIZE.to_string() + ); Ok(()) } From 5c86b18cf1a176acd10a7e2b128929f87280c0e5 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Fri, 18 Aug 2023 13:56:09 +0100 Subject: [PATCH 13/14] clean up dependencies --- Cargo.toml | 1 + crates/env/Cargo.toml | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 330f90c9d0..a595793679 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,6 +74,7 @@ tracing-subscriber = { version = "0.3.17" } trybuild = { version = "1.0.60" } which = { version = "4.4.0" } xxhash-rust = { version = "0.8" } +const_env = { version = "0.1"} # Substrate dependencies pallet-contracts-primitives = { version = "24.0.0", default-features = false } diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index cac7aa6ba5..28dec0bc14 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -4,7 +4,6 @@ version.workspace = true authors = ["Parity Technologies ", "Robin Freyler "] edition.workspace = true rust-version = "1.68" -# build = "build.rs" license.workspace = true readme = "README.md" @@ -29,7 +28,7 @@ cfg-if = { workspace = true } paste = { workspace = true } arrayref = { workspace = true } static_assertions = { workspace = true } -const_env = "0.1.2" +const_env = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] rlibc = "1" @@ -56,9 +55,6 @@ scale-info = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] ink = { workspace = true } -[build-dependencies] -const-gen = "1.4" - [features] default = ["std"] std = [ From 672fddf68d83e696f8ab516de890693c8bd85ee1 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Fri, 18 Aug 2023 13:58:20 +0100 Subject: [PATCH 14/14] comment fix --- crates/env/src/engine/off_chain/impls.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 8517fbff11..b7b1e539ee 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -51,7 +51,6 @@ 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. -/// The value is copied from the injected constant from the build script. const BUFFER_SIZE: usize = crate::BUFFER_SIZE; impl CryptoHash for Blake2x128 {