-
Notifications
You must be signed in to change notification settings - Fork 477
Implement seal_is_contract and seal_caller_is_origin
#1129
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 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b817ff8
[env][lang] add support for new `is_contract` API
agryaznov 9c39937
added `is_contract()` to ink_lang::EnvAccess to use from within contr…
agryaznov 2a58c38
added `is-contract` example
agryaznov 5e1bc60
add support for `caller_is_origin()` API
agryaznov ff08823
updated is-contract example to use `caller_is_origin()` as well
agryaznov 623b044
Merge branch 'master' of github.com:agryaznov/ink into is_contract
agryaznov b1310fb
Corrections in response to @HCastrano review
agryaznov 81ec866
Merge branch 'master' of https://github.com/paritytech/ink into is_co…
agryaznov 444c497
removed `examples/is-contract`
agryaznov 5617aeb
turned back whitespaces in `env_access.rs` not to mess up with the ch…
agryaznov 0bfcd80
Merge branch 'master' of https://github.com/paritytech/ink into is_co…
agryaznov 4f76c30
added new functions to `experimental_offchain_engine`
agryaznov a40fab1
fixed `wrong_self_convention` clippy warning for `is_` functions
agryaznov 123474e
supressed `wrong_self_convention` clippy warning for `is_` functions
agryaznov 6ac81a7
Update crates/env/src/api.rs
agryaznov f5d9384
Merge branch 'master' of https://github.com/paritytech/ink into is_co…
agryaznov 68d7cc4
set `is_contract` and `caller_is_origin` as `unimplemented` in experi…
agryaznov b00e7e4
Apply suggestions from code review
agryaznov 2061940
message cant take ref as a param: revert suggested change
agryaznov 7d463d6
Merge branch 'master' of https://github.com/paritytech/ink into is_co…
agryaznov 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
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,11 @@ | ||
| # 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 | ||
|
|
||
| *~ | ||
|
agryaznov marked this conversation as resolved.
Outdated
|
||
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,37 @@ | ||
| [package] | ||
|
agryaznov marked this conversation as resolved.
Outdated
|
||
| name = "is_contract" | ||
| version = "3.0.0-rc8" | ||
| authors = ["Parity Technologies <admin@parity.io>"] | ||
| edition = "2021" | ||
|
|
||
| [dependencies] | ||
| ink_primitives = { version = "3.0.0-rc8", path = "../../crates/primitives", default-features = false } | ||
| ink_metadata = { version = "3.0.0-rc8", path = "../../crates/metadata", default-features = false, features = ["derive"], optional = true } | ||
| ink_env = { version = "3.0.0-rc8", path = "../../crates/env", default-features = false, features = [ "ink-debug" ] } | ||
| ink_storage = { version = "3.0.0-rc8", path = "../../crates/storage", default-features = false } | ||
| ink_lang = { version = "3.0.0-rc8", path = "../../crates/lang", default-features = false } | ||
| ink_prelude = { version = "3.0.0-rc8", path = "../../crates/prelude", default-features = false } | ||
|
|
||
| scale = { package = "parity-scale-codec", version = "2", default-features = false, features = ["derive"] } | ||
| scale-info = { version = "1", default-features = false, features = ["derive"], optional = true } | ||
|
|
||
| [lib] | ||
| name = "is_contract" | ||
| path = "lib.rs" | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ink_primitives/std", | ||
| "ink_metadata", | ||
| "ink_metadata/std", | ||
| "ink_env/std", | ||
| "ink_storage/std", | ||
| "ink_lang/std", | ||
| "scale/std", | ||
| "scale-info", | ||
| "scale-info/std", | ||
| ] | ||
| ink-as-dependency = [] | ||
| ink-experimental-engine = ["ink_env/ink-experimental-engine"] | ||
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,159 @@ | ||
| //! A smart contract which demonstrates behavior of the `self.env().is_contract()` | ||
| //! and `self.env().caller_is_origin()` functions. | ||
| //! It checks whether a specified account_id is a contract | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
| #![allow(clippy::new_without_default)] | ||
|
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. I would address this instead of suppressing the Clippy warning |
||
|
|
||
| use ink_lang as ink; | ||
|
|
||
| #[ink::contract] | ||
| pub mod is_contract { | ||
| use ink_env::call::{ | ||
| build_call, | ||
| utils::{ | ||
| EmptyArgumentList, | ||
| ReturnType, | ||
| }, | ||
| ExecutionInput, | ||
| Selector, | ||
| }; | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] | ||
| #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] | ||
| /// Error types | ||
| pub enum Error { | ||
| SomeError, | ||
| } | ||
|
|
||
| /// Event emitted when Winning block is detected. | ||
|
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. Left-over copy-pasta |
||
| #[ink(event)] | ||
| pub struct AccountIsContract { | ||
| account_id: AccountId, | ||
| is_contract: bool, | ||
| } | ||
|
|
||
| /// Event emitted when a winner is detected. | ||
|
agryaznov marked this conversation as resolved.
Outdated
|
||
| #[ink(event)] | ||
| pub struct CallerIsOrigin { | ||
| is_origin: bool, | ||
| } | ||
|
|
||
| /// No storage is needed for this simple contract. | ||
| #[ink(storage)] | ||
| pub struct IsContract {} | ||
|
|
||
| impl IsContract { | ||
| /// Creates a new instance of this contract. | ||
| #[ink(constructor)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
|
|
||
| /// Cross contract invocation method | ||
| fn invoke_contract( | ||
|
agryaznov marked this conversation as resolved.
Outdated
|
||
| &self, | ||
| contract: AccountId, | ||
| input: ExecutionInput<EmptyArgumentList>, | ||
| ) { | ||
| let params = build_call::<Environment>() | ||
| .callee(contract) | ||
| .exec_input(input) | ||
| .returns::<ReturnType<Result<(), Error>>>(); | ||
|
|
||
| match params.fire() { | ||
| Ok(_v) => {} | ||
| Err(e) => { | ||
| match e { | ||
| ink_env::Error::CodeNotFound | ink_env::Error::NotCallable => { | ||
| // Our recipient wasn't a smart contract, so there's nothing more for | ||
| // us to do | ||
| let msg = ink_prelude::format!( | ||
| "Recipient at {:#04X?} from is not a smart contract ({:?})", | ||
| contract, | ||
| e | ||
| ); | ||
| panic!("{}", msg) | ||
| } | ||
| _ => { | ||
| // We got some sort of error from the call to our recipient smart | ||
| // contract, and as such we must revert this call | ||
| let msg = ink_prelude::format!( | ||
| "Got error \"{:?}\" while trying to call {:?}", | ||
| e, | ||
| contract, | ||
| ); | ||
| panic!("{}", msg) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Checks if the specified account_id belogns to a contract | ||
|
agryaznov marked this conversation as resolved.
Outdated
|
||
| #[ink(message, selector = 0xBABEFEED)] | ||
| pub fn is_contract(&mut self, account_id: AccountId) -> bool { | ||
| ink_env::debug_println!( | ||
| "checking if account_id is contract: {:?}", | ||
| account_id | ||
| ); | ||
|
|
||
| let is_contract = self.env().is_contract(account_id); | ||
|
|
||
| self.env().emit_event(AccountIsContract { | ||
| account_id: account_id.clone(), | ||
| is_contract: is_contract.clone(), | ||
| }); | ||
|
|
||
| is_contract | ||
| } | ||
|
|
||
| /// Checks if the caller is the origin of the whole call stack. | ||
| #[ink(message)] | ||
| pub fn caller_is_origin(&mut self) -> bool { | ||
| ink_env::debug_println!("checking if caller is the origin",); | ||
|
|
||
| let is_origin = self.env().caller_is_origin(); | ||
|
|
||
| self.env().emit_event(CallerIsOrigin { | ||
| is_origin: is_origin.clone(), | ||
| }); | ||
|
|
||
| is_origin | ||
| } | ||
|
|
||
| /// Checks if the caller is the origin of the whole call stack. | ||
| #[ink(message)] | ||
| pub fn call_contract(&mut self, address: AccountId) { | ||
| let selector = Selector::new([0xBA, 0xBE, 0xFE, 0xED]); | ||
| let input = ExecutionInput::new(selector); | ||
| self.invoke_contract(address, input); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature = "ink-experimental-engine"))] | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| use ink_lang as ink; | ||
|
|
||
| fn accounts() -> ink_env::test::DefaultAccounts<Environment> { | ||
| ink_env::test::default_accounts::<Environment>().unwrap() | ||
| } | ||
|
|
||
| #[ink::test] | ||
| fn is_contract_works() { | ||
| // to be honest, this is just a boilerplate | ||
| // since off_chain env does not support contracts | ||
| assert!(!ink_env::is_contract::<Environment>(accounts().alice)); | ||
| } | ||
|
|
||
| #[ink::test] | ||
| fn caller_is_origin_works() { | ||
| // to be honest, this is just a boilerplate | ||
| // since off_chain env does not support contracts | ||
| assert!(ink_env::caller_is_origin::<Environment>()); | ||
| } | ||
| } | ||
| } | ||
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.