-
Notifications
You must be signed in to change notification settings - Fork 480
Allow mutable parameters in messages #2004
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b27beb
adjust macro in parsing tests
SkymanOne ffb5081
Add tests and filter out mut keyword
SkymanOne 6e75dca
add changelog entry
SkymanOne c5b606f
add idents to dictionary
SkymanOne 75bd4e5
remove integration test
SkymanOne e2e23e1
make clippy happy
SkymanOne 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
Add tests and filter out mut keyword
- Loading branch information
commit ffb5081954dc6385ffb2f69f77151d406b6802e5
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
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,23 @@ | ||
| [package] | ||
| name = "incrementer_mut" | ||
| version = "5.0.0-alpha" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| ink = { path = "../../crates/ink", default-features = false } | ||
|
|
||
| [dev-dependencies] | ||
| ink_e2e = { path = "../../crates/e2e" } | ||
|
|
||
| [lib] | ||
| path = "lib.rs" | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ink/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,136 @@ | ||
| #![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
|
|
||
| //! A simple incrementer contract | ||
| //! demonstrating the internal mutability of message parameters. | ||
|
|
||
| pub use self::incrementer_mut::{ | ||
| Incrementer, | ||
| IncrementerRef, | ||
| }; | ||
|
|
||
| #[ink::contract] | ||
| mod incrementer_mut { | ||
| #[ink(storage)] | ||
| pub struct Incrementer { | ||
| value: i32, | ||
| } | ||
|
|
||
| impl Incrementer { | ||
| /// Create a new contract with the specified counter value. | ||
| /// If it is below 0, it is set to 0 | ||
| #[ink(constructor)] | ||
| pub fn new(mut init_value: i32) -> Self { | ||
| if init_value < 0 { | ||
| init_value = 0; | ||
| } | ||
| Self { value: init_value } | ||
| } | ||
|
|
||
| #[ink(constructor)] | ||
| pub fn new_default() -> Self { | ||
| Self::new(Default::default()) | ||
| } | ||
|
|
||
| #[ink(message)] | ||
| pub fn inc(&mut self, by: i32) { | ||
| self.value = self.value.checked_add(by).unwrap(); | ||
| } | ||
|
|
||
| /// Update the counter with the specified value. | ||
| /// If it is above 100, we set it to 0. | ||
| #[ink(message)] | ||
| pub fn update(&mut self, mut value: i32) { | ||
| if value > 100 { | ||
| value = 0; | ||
| } | ||
| self.value = value; | ||
| } | ||
|
|
||
| #[ink(message)] | ||
| pub fn get(&self) -> i32 { | ||
| self.value | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[ink::test] | ||
| fn default_works() { | ||
| let contract = Incrementer::new_default(); | ||
| assert_eq!(contract.get(), 0); | ||
| } | ||
|
|
||
| #[ink::test] | ||
| fn it_works() { | ||
| let mut contract = Incrementer::new(42); | ||
| assert_eq!(contract.get(), 42); | ||
| contract.inc(5); | ||
| assert_eq!(contract.get(), 47); | ||
| contract.inc(-50); | ||
| assert_eq!(contract.get(), -3); | ||
| } | ||
| #[ink::test] | ||
| fn mutability_works() { | ||
| let mut contract = Incrementer::new(-5); | ||
| assert_eq!(contract.get(), 0); | ||
| contract.update(80); | ||
| assert_eq!(contract.get(), 80); | ||
| contract.update(120); | ||
| assert_eq!(contract.get(), 0); | ||
| } | ||
| } | ||
|
|
||
| #[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 it_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()> { | ||
| // given | ||
| let mut constructor = IncrementerRef::new(-2); | ||
| let contract = client | ||
| .instantiate("incrementer_mut", &ink_e2e::alice(), &mut constructor) | ||
| .submit() | ||
| .await | ||
| .expect("instantiate failed"); | ||
| let mut call = contract.call::<Incrementer>(); | ||
|
|
||
| let get = call.get(); | ||
| let get_res = client.call(&ink_e2e::bob(), &get).dry_run().await?; | ||
| assert!(matches!(get_res.return_value(), 0)); | ||
|
|
||
| // when | ||
| let flip = call.update(50); | ||
| let _flip_res = client | ||
| .call(&ink_e2e::bob(), &flip) | ||
| .submit() | ||
| .await | ||
| .expect("update failed"); | ||
|
|
||
| // then | ||
| let get = call.get(); | ||
| let get_res = client.call(&ink_e2e::bob(), &get).dry_run().await?; | ||
| assert!(matches!(get_res.return_value(), 50)); | ||
|
|
||
| // when | ||
| let flip = call.update(150); | ||
| let _flip_res = client | ||
| .call(&ink_e2e::bob(), &flip) | ||
| .submit() | ||
| .await | ||
| .expect("update failed"); | ||
|
|
||
| // then | ||
| let get = call.get(); | ||
| let get_res = client.call(&ink_e2e::bob(), &get).dry_run().await?; | ||
| assert!(matches!(get_res.return_value(), 0)); | ||
|
|
||
| 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.