This repository was archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Add testing for is_contract and is_caller_origin
#22
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
82d833f
Add testing for `is_contract` and `is_caller_origin`
cmichi e64f304
Add clarifying comments
cmichi aeca10d
Update README.md
agryaznov 51de45b
Merge branch 'master' into cmichi-add-local-ink-waterfall-examples
cmichi d48ce2e
Fix `cp`
cmichi d8de13a
Fix path
cmichi 0db47b1
Fix path
cmichi 4c5e4e1
Print debug info
cmichi edb4f10
Fix path
cmichi 2336fde
Fix path
cmichi 5b90b91
Fix path
cmichi 5067926
Fix path
cmichi 62ef36d
Fix path
cmichi 0e7e98a
Fix path
cmichi d8d677c
Remove debug info
cmichi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,9 @@ requirement. By default the published versions of those projects are used | |
| ## Run it locally | ||
|
|
||
| ```bash | ||
| # Create a link to ink! in the local examples of the `ink-waterfall`. | ||
| ln -s /path/to/ink/ ./examples/ink | ||
|
|
||
| export INK_EXAMPLES_PATH=/path/to/ink/examples/ | ||
| substrate-contracts-node --tmp --dev > /tmp/substrate-contracts-node.log 2>&1 & | ||
|
|
||
|
|
@@ -100,8 +103,8 @@ supply `--features polkadot-js-ui` to `cargo test`. | |
|
|
||
| * `INK_EXAMPLES_PATH` ‒ Path to the ink! examples folder. Must be set. | ||
| * `UI_URL` ‒ URL of the UI to use. Defaults to the live interface for the chosen UI. | ||
| * `WATERFALL_CLOSE_BROWSER` ‒ Do not close browser window at the end of a test run. | ||
| Defaults to `true`. Set it to `false` to prevent closing . | ||
| * `WATERFALL_CLOSE_BROWSER` ‒ Close browser window at the end of a test run. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lil typo fix here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! No biggie, but going forward please avoid pushing to someone else's PR without talking to them first. |
||
| Defaults to `true`. Set it to `false` to prevent closing. | ||
| * `WATERFALL_SKIP_CONTRACT_BUILD` ‒ Do not build the contracts, re-use existing artifacts | ||
| from their `target` folder. Defaults to `false`. Set it to `true` to skip building. | ||
| * `NODE_PORT` ‒ Port under which the `substrate-contracts-node` is running. Defaults to `9944`. | ||
|
|
@@ -117,4 +120,4 @@ before the test is finished processing them. | |
|
|
||
| The test will then fail with a `NoSucheElement` error, indicating that | ||
| the DOM element is no longer available. The easiest fix for this is to | ||
| limit the number of concurrent test threads via e.g. `cargo test --jobs 4`. | ||
| limit the number of concurrent test threads via e.g. `cargo test --jobs 4`. | ||
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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it asks a newline ^
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that's a GitHub thing, we can safely ignore it. |
||
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,35 @@ | ||
| [package] | ||
| name = "contract_introspection" | ||
| version = "3.0.0-rc9" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| ink_primitives = { path = "../ink/crates/primitives", default-features = false } | ||
| ink_metadata = { path = "../ink/crates/metadata", default-features = false, features = ["derive"], optional = true } | ||
| ink_env = { path = "../ink/crates/env", default-features = false } | ||
| ink_storage = { path = "../ink/crates/storage", default-features = false } | ||
| ink_lang = { path = "../ink/crates/lang", default-features = false } | ||
|
|
||
| scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
| scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } | ||
|
|
||
| [lib] | ||
| name = "contract_introspection" | ||
| path = "lib.rs" | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ink_primitives/std", | ||
| "ink_metadata/std", | ||
| "ink_env/std", | ||
| "ink_storage/std", | ||
| "ink_lang/std", | ||
| "scale/std", | ||
| "scale-info/std", | ||
| ] | ||
| ink-as-dependency = [] | ||
|
|
||
| [workspace] |
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,65 @@ | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use ink_lang as ink; | ||
|
|
||
| #[ink::contract] | ||
| mod contract_introspection { | ||
| use ink_env::{ | ||
| call::{ | ||
| build_call, | ||
| utils::ReturnType, | ||
| ExecutionInput, | ||
| Selector, | ||
| }, | ||
| CallFlags, | ||
| DefaultEnvironment, | ||
| }; | ||
|
|
||
| #[ink(storage)] | ||
| pub struct ContractInspection {} | ||
|
|
||
| impl ContractInspection { | ||
| #[ink(constructor)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
|
|
||
| /// Returns `true` if the caller is a contract. | ||
| #[ink(message, selector = 1)] | ||
| pub fn is_caller_contract(&self) -> bool { | ||
| self.env().is_contract(&self.env().caller()) | ||
| } | ||
|
|
||
| /// Returns `true` if the caller is the origin of this call. | ||
| #[ink(message, selector = 2)] | ||
| pub fn is_caller_origin(&self) -> bool { | ||
| self.env().caller_is_origin() | ||
| } | ||
|
|
||
| /// Calls into this contract to the [`is_caller_contract`] function | ||
| /// and returns the return value of that function. | ||
| #[ink(message)] | ||
| pub fn calls_is_caller_contract(&self) -> bool { | ||
| build_call::<DefaultEnvironment>() | ||
| .callee(self.env().account_id()) | ||
| .exec_input(ExecutionInput::new(Selector::new([0x00, 0x00, 0x00, 0x01]))) | ||
| .returns::<ReturnType<bool>>() | ||
| .call_flags(CallFlags::default().set_allow_reentry(true)) | ||
| .fire() | ||
| .expect("failed executing call") | ||
| } | ||
|
|
||
| /// Calls into this contract to the [`is_caller_origin`] function | ||
| /// and returns the return value of that function. | ||
| #[ink(message)] | ||
| pub fn calls_is_caller_origin(&self) -> bool { | ||
| build_call::<DefaultEnvironment>() | ||
| .callee(self.env().account_id()) | ||
| .exec_input(ExecutionInput::new(Selector::new([0x00, 0x00, 0x00, 0x02]))) | ||
| .returns::<ReturnType<bool>>() | ||
| .call_flags(CallFlags::default().set_allow_reentry(true)) | ||
| .fire() | ||
| .expect("failed executing call") | ||
| } | ||
| } | ||
| } |
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,68 @@ | ||
| // Copyright 2018-2021 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! Tests for the `contract_introspection `example. | ||
|
|
||
| use crate::{ | ||
| uis::{ | ||
| Call, | ||
| Result, | ||
| Ui, | ||
| Upload, | ||
| }, | ||
| utils::{ | ||
| self, | ||
| cargo_contract, | ||
| }, | ||
| }; | ||
| use lang_macro::waterfall_test; | ||
|
|
||
| #[waterfall_test(example = "contract_introspection")] | ||
| async fn delegator_works(mut ui: Ui) -> Result<()> { | ||
| // given | ||
| let contract_path = | ||
| cargo_contract::build(&utils::example_path("contract-introspection/Cargo.toml")) | ||
| .expect("contract build failed"); | ||
|
|
||
| // when | ||
| let addr = ui.execute_upload(Upload::new(contract_path)).await?; | ||
|
|
||
| // then | ||
| // the method is called directly via the ui. | ||
| assert_eq!( | ||
| ui.execute_rpc(Call::new(&addr, "is_caller_contract")) | ||
| .await?, | ||
| "false" | ||
| ); | ||
| // the `is_caller_contract` method is called indirectly from this contract method. | ||
| assert_eq!( | ||
| ui.execute_rpc(Call::new(&addr, "calls_is_caller_contract")) | ||
| .await?, | ||
| "true" | ||
| ); | ||
|
|
||
| // the method is called directly via the ui. | ||
| assert_eq!( | ||
| ui.execute_rpc(Call::new(&addr, "is_caller_origin")).await?, | ||
| "true" | ||
| ); | ||
| // the `is_caller_origin` method is called indirectly from this contract method. | ||
| assert_eq!( | ||
| ui.execute_rpc(Call::new(&addr, "calls_is_caller_origin")) | ||
| .await?, | ||
| "false" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use a released version of ink? The CI will replace it with current master anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, how so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line:
find ./examples/ -name 'Cargo.toml' -exec sed -i "s|../ink|../$INK_EXAMPLES_PATH|g" {} \;But I see why now. We want to run it against a custom version locally, too.