Skip to content
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

Implement seal_code_hash and seal_own_code_hash #1205

Merged
merged 20 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,22 @@ where
})
}

/// Retrieves the code hash of a contract living at specified account
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a section /// # Errors here?

pub fn code_hash<E>(account: &E::AccountId) -> Result<E::Hash>
where
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| instance.code_hash::<E>(account))
}

/// Retrieves the code hash of the currently executing contract.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a section /// # Errors here?

pub fn own_code_hash<E>() -> Result<E::Hash>
where
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| instance.own_code_hash::<E>())
}

/// Checks whether the caller of the current contract is the origin of the whole call stack.
///
/// Prefer this over [`is_contract`] when checking whether your contract is being called by
Expand Down
19 changes: 19 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,23 @@ pub trait TypedEnvBackend: EnvBackend {
fn caller_is_origin<E>(&mut self) -> bool
where
E: Environment;

/// Retrieves the code hash of the contract for the given `account`,
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
/// and stores the result in `output`.
///
/// # Note
///
/// For more details visit: [`code_hash`][`crate::code_hash`]
fn code_hash<E>(&mut self, account: &E::AccountId) -> Result<E::Hash>
where
E: Environment;

/// Retrieves the code hash of the currently executing contract.
///
/// # Note
///
/// For more details visit: [`own_code_hash`][`crate::own_code_hash`]
fn own_code_hash<E>(&mut self) -> Result<E::Hash>
where
E: Environment;
}
14 changes: 14 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,18 @@ impl TypedEnvBackend for EnvInstance {
{
unimplemented!("off-chain environment does not support cross-contract calls")
}

fn code_hash<E>(&mut self, _account: &E::AccountId) -> Result<E::Hash>
where
E: Environment,
{
unimplemented!("off-chain environment does not support contract instantiation")
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
}

fn own_code_hash<E>(&mut self) -> Result<E::Hash>
where
E: Environment,
{
unimplemented!("off-chain environment does not support contract instantiation")
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
}
}
34 changes: 34 additions & 0 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ mod sys {
message_hash_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
) -> ReturnCode;

pub fn seal_code_hash(
account_id_ptr: Ptr32<[u8]>,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;

pub fn seal_own_code_hash(
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
) -> ReturnCode;
}
}

Expand Down Expand Up @@ -676,3 +687,26 @@ pub fn caller_is_origin() -> bool {
let ret_val = unsafe { sys::seal_caller_is_origin() };
ret_val.into_bool()
}

pub fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_val = unsafe {
sys::seal_code_hash(
Ptr32::from_slice(account_id),
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
ret_val.into()
}

pub fn own_code_hash(output: &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_val = unsafe {
sys::seal_own_code_hash(
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
};
ret_val.into()
}
22 changes: 22 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,26 @@ impl TypedEnvBackend for EnvInstance {
{
ext::caller_is_origin()
}

fn code_hash<E>(&mut self, account_id: &E::AccountId) -> Result<E::Hash>
where
E: Environment,
{
let mut scope = self.scoped_buffer();
let enc_account_id = scope.take_encoded(account_id);
let mut output = <E as Environment>::Hash::clear();
ext::code_hash(enc_account_id, output.as_mut())
.map(|_| output)
.map_err(Into::into)
}

fn own_code_hash<E>(&mut self) -> Result<E::Hash>
where
E: Environment,
{
let mut output = <E as Environment>::Hash::clear();
ext::own_code_hash(output.as_mut())
.map(|_| output)
.map_err(Into::into)
}
}
64 changes: 64 additions & 0 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,4 +901,68 @@ where
pub fn caller_is_origin(self) -> bool {
ink_env::caller_is_origin::<E>()
}

/// Returns the code hash of the contract living at the given `account`.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Example
///
/// ```
cmichi marked this conversation as resolved.
Show resolved Hide resolved
/// # use ink_lang as ink;
/// # #[ink::contract]
/// # pub mod my_contract {
/// # #[ink(storage)]
/// # pub struct MyContract { }
/// #
/// # impl MyContract {
/// # #[ink(constructor)]
/// # pub fn new() -> Self {
/// # Self {}
/// # }
/// #
/// #[ink(message)]
/// pub fn code_hash(&mut self, account_id: AccountId) -> Option<Hash> {
/// self.env().code_hash(&account_id).ok()
/// }
/// # }
/// # }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::code_hash`]
pub fn code_hash(self, account_id: &E::AccountId) -> Result<E::Hash> {
ink_env::code_hash::<E>(account_id)
}

/// Returns the code hash of the contract living at the given `account`.
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Example
///
/// ```
cmichi marked this conversation as resolved.
Show resolved Hide resolved
/// # use ink_lang as ink;
/// # #[ink::contract]
/// # pub mod my_contract {
/// # #[ink(storage)]
/// # pub struct MyContract { }
/// #
/// # impl MyContract {
/// # #[ink(constructor)]
/// # pub fn new() -> Self {
/// # Self {}
/// # }
/// #
/// #[ink(message)]
/// pub fn own_code_hash(&mut self) -> Hash {
/// self.env().own_code_hash().expect("contract should have a code hash")
/// }
/// # }
/// # }
/// ```
///
/// # Note
///
/// For more details visit: [`ink_env::own_code_hash`]
pub fn own_code_hash(self) -> Result<E::Hash> {
ink_env::own_code_hash::<E>()
}
}