Skip to content

Commit 9066a03

Browse files
olahfemicmichi
andauthored
Adds support for 'caller_is_root' (#2332)
* Adds support for 'caller_is_root' * Adds PR to 'CHANGELOG.md' * Tested CI locally * Updates Cargo nightly * Revert "Updates Cargo nightly" This reverts commit c70c62d. * Revert "Tested CI locally" This reverts commit 0e37b8e. * Close delimiter * Apply `cargo fmt` * Move changelog entry to unreleased section * Implement `caller_is_root` on-chain * Account for `call_is_root() -> u32` return type * Panic when `u32 -> bool` not possible --------- Co-authored-by: Michael Mueller <[email protected]>
1 parent 92a62a7 commit 9066a03

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
## Changed
1010
- Restrict which `cfg` attributes can be used ‒ [#2313](https://github.com/use-ink/ink/pull/2313)
1111

12+
## Added
13+
- Support for `caller_is_root` - [#2332] (https://github.com/use-ink/ink/pull/2332)
14+
1215
## Fixed
1316
- [E2E] Have port parsing handle comma-separated list ‒ [#2336](https://github.com/use-ink/ink/pull/2336)
1417

crates/env/src/api.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,26 @@ where
729729
})
730730
}
731731

732+
/// Checks whether the caller of the current contract is root.
733+
///
734+
/// Note that only the origin of the call stack can be root. Hence this function returning
735+
/// `true` implies that the contract is being called by the origin.
736+
///
737+
/// A return value of `true` indicates that this contract is being called by a root
738+
/// origin, and `false` indicates that the caller is a signed origin.
739+
///
740+
/// # Errors
741+
///
742+
/// If the returned value cannot be properly decoded.
743+
pub fn caller_is_root<E>() -> bool
744+
where
745+
E: Environment,
746+
{
747+
<EnvInstance as OnInstance>::on_instance(|instance| {
748+
TypedEnvBackend::caller_is_root::<E>(instance)
749+
})
750+
}
751+
732752
/// Replace the contract code at the specified address with new code.
733753
///
734754
/// # Note

crates/env/src/backend.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ pub trait TypedEnvBackend: EnvBackend {
413413
where
414414
E: Environment;
415415

416+
/// Checks whether the caller of the current contract is root.
417+
///
418+
/// # Note
419+
///
420+
/// For more details visit: [`caller_is_root`][`crate::caller_is_root`]
421+
fn caller_is_root<E>(&mut self) -> bool
422+
where
423+
E: Environment;
424+
416425
/// Retrieves the code hash of the contract at the given `account` id.
417426
///
418427
/// # Note

crates/env/src/engine/off_chain/impls.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ impl TypedEnvBackend for EnvInstance {
567567
unimplemented!("off-chain environment does not support cross-contract calls")
568568
}
569569

570+
fn caller_is_root<E>(&mut self) -> bool
571+
where
572+
E: Environment,
573+
{
574+
unimplemented!("off-chain environment does not support `caller_is_root`")
575+
}
576+
570577
fn code_hash<E>(&mut self, _account: &E::AccountId) -> Result<E::Hash>
571578
where
572579
E: Environment,

crates/env/src/engine/on_chain/impls.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,20 @@ impl TypedEnvBackend for EnvInstance {
676676
ext::caller_is_origin()
677677
}
678678

679+
fn caller_is_root<E>(&mut self) -> bool
680+
where
681+
E: Environment,
682+
{
683+
// `ext::caller_is_root()` currently returns `u32`.
684+
// See https://github.com/paritytech/polkadot-sdk/issues/6767 for more details.
685+
let ret = ext::caller_is_root();
686+
match ret {
687+
0u32 => false,
688+
1u32 => true,
689+
_ => panic!("Invalid value for bool conversion: {}", ret),
690+
}
691+
}
692+
679693
fn code_hash<E>(&mut self, account_id: &E::AccountId) -> Result<E::Hash>
680694
where
681695
E: Environment,

crates/ink/src/env_access.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,37 @@ where
11781178
ink_env::caller_is_origin::<E>()
11791179
}
11801180

1181+
/// Checks whether the caller of the current contract is root.
1182+
///
1183+
/// # Example
1184+
///
1185+
/// ```
1186+
/// # #[ink::contract]
1187+
/// # pub mod my_contract {
1188+
/// # #[ink(storage)]
1189+
/// # pub struct MyContract { }
1190+
/// #
1191+
/// # impl MyContract {
1192+
/// # #[ink(constructor)]
1193+
/// # pub fn new() -> Self {
1194+
/// # Self {}
1195+
/// # }
1196+
/// #
1197+
/// #[ink(message)]
1198+
/// pub fn caller_is_root(&mut self) -> bool {
1199+
/// self.env().caller_is_root()
1200+
/// }
1201+
/// # }
1202+
/// # }
1203+
/// ```
1204+
///
1205+
/// # Note
1206+
///
1207+
/// For more details visit: [`ink_env::caller_is_root`]
1208+
pub fn caller_is_root(self) -> bool {
1209+
ink_env::caller_is_root::<E>()
1210+
}
1211+
11811212
/// Returns the code hash of the contract at the given `account` id.
11821213
///
11831214
/// # Example

0 commit comments

Comments
 (0)