-
Notifications
You must be signed in to change notification settings - Fork 480
Modify static buffer size via env var #1869
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cd25a65
modify buffer size via env var
0441a0e
fmt
c2aacd4
add docs and tests
4a954ae
fmt
0230c20
fix if-else statement in ci
338ac2a
do cargo clean before the running the test
c73ae61
clean binaries the test too
a698006
add changelog
c99ac67
remove unused dependency and add an error message
6c9f6ea
fmt
612cd3c
rename env var
6c4ee3b
use const_env instead of build script
5c86b18
clean up dependencies
672fddf
comment fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ version.workspace = true | |
| authors = ["Parity Technologies <[email protected]>", "Robin Freyler <[email protected]>"] | ||
| 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 = [ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 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("STATIC_BUFFER_SIZE") { | ||
| if let Ok(new_size) = size_str.parse::<usize>() { | ||
| size = new_size; | ||
| } | ||
| } | ||
| 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(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <[email protected]>"] | ||
| 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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
SkymanOne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| because the value is baked into the binaries. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #![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<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
|
||
| #[ink_e2e::test] | ||
| async fn e2e_run_out_of_buffer_memory<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>(); | ||
|
|
||
| // 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(()) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.