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 all 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
29 changes: 29 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,35 @@ where
})
}

/// Retrieves the code hash of the contract at the specified account id.
///
/// # Errors
///
/// - If no code hash was found for the specified account id.
/// - If the returned value cannot be properly decoded.
pub fn code_hash<E>(account: &E::AccountId) -> Result<E::Hash>
where
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::code_hash::<E>(instance, 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?

///
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn own_code_hash<E>() -> Result<E::Hash>
where
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::own_code_hash::<E>(instance)
})
}

/// 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
18 changes: 18 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,22 @@ pub trait TypedEnvBackend: EnvBackend {
fn caller_is_origin<E>(&mut self) -> bool
where
E: Environment;

/// Retrieves the code hash of the contract at the given `account` id.
///
/// # 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 `code_hash`")
}

fn own_code_hash<E>(&mut self) -> Result<E::Hash>
where
E: Environment,
{
unimplemented!("off-chain environment does not support `own_code_hash`")
}
}
33 changes: 33 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>,
);
}
}

Expand Down Expand Up @@ -676,3 +687,25 @@ 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]) {
let mut output_len = output.len() as u32;
unsafe {
sys::seal_own_code_hash(
Ptr32Mut::from_slice(output),
Ptr32Mut::from_ref(&mut output_len),
)
}
}
24 changes: 24 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,28 @@ 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 output = scope.take(32);
scope.append_encoded(account_id);
let enc_account_id = scope.take_appended();

ext::code_hash(enc_account_id, output)?;
let hash = scale::Decode::decode(&mut &output[..])?;
Ok(hash)
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
}

fn own_code_hash<E>(&mut self) -> Result<E::Hash>
where
E: Environment,
{
let output = &mut self.scoped_buffer().take(32);
ext::own_code_hash(output);
let hash = scale::Decode::decode(&mut &output[..])?;
Ok(hash)
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
}
}
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 at the given `account` id.
///
/// # 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 at the given `account` id.
///
/// # 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>()
}
}