From 5ae17cd01f8a44c3699db63c4804b0840188ab44 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 19:35:45 +0300 Subject: [PATCH 01/18] seal_code_hash() added --- crates/env/src/api.rs | 10 ++++++++++ crates/env/src/backend.rs | 14 ++++++++++++++ crates/env/src/engine/off_chain/impls.rs | 11 +++++++++++ crates/env/src/engine/on_chain/ext.rs | 7 +++++++ crates/env/src/engine/on_chain/impls.rs | 9 +++++++++ crates/lang/src/env_access.rs | 7 +++++++ 6 files changed, 58 insertions(+) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index fb96c44e87..516506b7ba 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -514,6 +514,16 @@ where }) } +/// +pub fn code_hash(account: &E::AccountId, output: &mut E::Hash) -> Result<()> +where + E: Environment, +{ + ::on_instance(|instance| { + instance.code_hash::(account, output) + }) +} + /// 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 diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index cb7b7723a9..2b30c3f554 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -447,4 +447,18 @@ pub trait TypedEnvBackend: EnvBackend { fn caller_is_origin(&mut self) -> bool where E: Environment; + + /// Reads the code hash of the contract for the given `account`, + /// and stores the result in `output`. + /// + /// # Note + /// + /// For more details visit: [`code_hash`][`crate::code_hash`] + fn code_hash( + &mut self, + account: &E::AccountId, + output: &mut E::Hash, + ) -> Result<()> + where + E: Environment; } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 7c302b16d2..26b33d161e 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -486,4 +486,15 @@ impl TypedEnvBackend for EnvInstance { { unimplemented!("off-chain environment does not support cross-contract calls") } + + fn code_hash( + &mut self, + _account: &E::AccountId, + _output: &mut E::Hash, + ) -> Result<()> + where + E: Environment, + { + unimplemented!("off-chain environment does not support contract instantiation") + } } diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index f35cae20da..e147ca78b3 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -676,3 +676,10 @@ 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 &mut [u8]) -> Result { + let ret_val = unsafe { + sys::seal_code_hash(Ptr32::from_slice(account_id), Ptr32Mut::from_slice(output)) + }; + ret_code.into() +} diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index da6a54bba8..a11bc8f986 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -512,4 +512,13 @@ impl TypedEnvBackend for EnvInstance { { ext::caller_is_origin() } + + fn code_hash(&mut self, account_id: &E::AccountId, output: E::Hash) -> Result<()> + where + E: Environment, + { + let mut scope = self.scoped_buffer(); + let enc_account_id = scope.take_encoded(account_id); + ext::code_hash(enc_account_id, output).map_err(Into::into) + } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index ede514f098..34715c4e77 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -25,6 +25,7 @@ use ink_env::{ CryptoHash, HashOutput, }, + Clear, Environment, Error, Result, @@ -901,4 +902,10 @@ where pub fn caller_is_origin(self) -> bool { ink_env::caller_is_origin::() } + + /// + pub fn code_hash(self, account_id: &E::AccountId) -> Result { + let mut output = ::Hash::clear(); + ink_env::code_hash::(account_id, &mut output).map(|_| output.into()) + } } From c977acc7ba2e9e204dcc39597ef8f154412cc0de Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 19:48:59 +0300 Subject: [PATCH 02/18] +docs --- crates/env/src/api.rs | 2 +- crates/env/src/backend.rs | 2 +- crates/lang/src/env_access.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 516506b7ba..74f1ef9877 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -514,7 +514,7 @@ where }) } -/// +/// Retrieves the code hash of a contract living at specified account pub fn code_hash(account: &E::AccountId, output: &mut E::Hash) -> Result<()> where E: Environment, diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 2b30c3f554..4bbbed7199 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -448,7 +448,7 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; - /// Reads the code hash of the contract for the given `account`, + /// Retrieves the code hash of the contract for the given `account`, /// and stores the result in `output`. /// /// # Note diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 34715c4e77..83e235d797 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -903,7 +903,34 @@ where ink_env::caller_is_origin::() } + /// Returns the code hash of the contract living at the given `account`. /// + /// # Example + /// + /// ``` + /// # 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) -> Result { + /// self.env().code_hash(&account_id) + /// } + /// # } + /// # } + /// ``` + /// + /// # Note + /// + /// For more details visit: [`ink_env::code_hash`] pub fn code_hash(self, account_id: &E::AccountId) -> Result { let mut output = ::Hash::clear(); ink_env::code_hash::(account_id, &mut output).map(|_| output.into()) From c0744f9df0d89529179580fe7f395e309f21af8e Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 20:13:53 +0300 Subject: [PATCH 03/18] seal_own_code_hash() added --- crates/env/src/api.rs | 10 +++++++ crates/env/src/backend.rs | 9 +++++++ crates/env/src/engine/off_chain/impls.rs | 7 +++++ crates/env/src/engine/on_chain/ext.rs | 5 ++++ crates/env/src/engine/on_chain/impls.rs | 7 +++++ crates/lang/src/env_access.rs | 33 ++++++++++++++++++++++++ 6 files changed, 71 insertions(+) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 74f1ef9877..de9427b42b 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -524,6 +524,16 @@ where }) } +/// Retrieves the code hash of the currently executing contract. +pub fn own_code_hash(output: &mut E::Hash) -> Result<()> +where + E: Environment, +{ + ::on_instance(|instance| { + instance.own_code_hash::(output) + }) +} + /// 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 diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 4bbbed7199..36df2f1077 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -461,4 +461,13 @@ pub trait TypedEnvBackend: EnvBackend { ) -> Result<()> 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(&mut self, output: &mut E::Hash) -> Result<()> + where + E: Environment; } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 26b33d161e..bb60ba3077 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -497,4 +497,11 @@ impl TypedEnvBackend for EnvInstance { { unimplemented!("off-chain environment does not support contract instantiation") } + + fn own_code_hash(&mut self, _output: &mut E::Hash) -> Result<()> + where + E: Environment, + { + unimplemented!("off-chain environment does not support contract instantiation") + } } diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index e147ca78b3..606fab6cb7 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -683,3 +683,8 @@ pub fn code_hash(account_id: &[u8], output: &mut &mut [u8]) -> Result { }; ret_code.into() } + +pub fn own_code_hash(output: &mut &mut [u8]) -> Result { + let ret_val = unsafe { sys::seal_own_code_hash(Ptr32Mut::from_slice(output)) }; + ret_code.into() +} diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index a11bc8f986..aacd59d8a4 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -521,4 +521,11 @@ impl TypedEnvBackend for EnvInstance { let enc_account_id = scope.take_encoded(account_id); ext::code_hash(enc_account_id, output).map_err(Into::into) } + + fn own_code_hash(&mut self, output: E::Hash) -> Result<()> + where + E: Environment, + { + ext::own_code_hash(output).map_err(Into::into) + } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 83e235d797..5a9a5985d8 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -935,4 +935,37 @@ where let mut output = ::Hash::clear(); ink_env::code_hash::(account_id, &mut output).map(|_| output.into()) } + + /// Returns the code hash of the contract living at the given `account`. + /// + /// # Example + /// + /// ``` + /// # 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) -> Result { + /// self.env().own_code_hash() + /// } + /// # } + /// # } + /// ``` + /// + /// # Note + /// + /// For more details visit: [`ink_env::own_code_hash`] + pub fn own_code_hash(self) -> Result { + let mut output = ::Hash::clear(); + ink_env::own_code_hash::(&mut output).map(|_| output.into()) + } } From 99ef061e647dfb7e5fcf1a44be9d86058827ac10 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 20:37:20 +0300 Subject: [PATCH 04/18] ci errors fixes --- crates/env/src/engine/on_chain/ext.rs | 11 +++++++++++ crates/lang/src/env_access.rs | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index 606fab6cb7..2900b0ce98 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -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, + ) -> ReturnCode; + + pub fn seal_own_code_hash( + output_ptr: Ptr32Mut<[u8]>, + output_len_ptr: Ptr32Mut, + ) -> ReturnCode; } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 5a9a5985d8..78dfd78082 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -966,6 +966,6 @@ where /// For more details visit: [`ink_env::own_code_hash`] pub fn own_code_hash(self) -> Result { let mut output = ::Hash::clear(); - ink_env::own_code_hash::(&mut output).map(|_| output.into()) + ink_env::own_code_hash::(&mut output).map(|_| output) } } From f9769dc8daaa4d3a90687b4afc5f6e8fb372123b Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 21:16:45 +0300 Subject: [PATCH 05/18] more ci fixes --- crates/env/src/engine/on_chain/ext.rs | 23 +++++++++++++++++------ crates/env/src/engine/on_chain/impls.rs | 8 ++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index 2900b0ce98..bd4cec323a 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -688,14 +688,25 @@ pub fn caller_is_origin() -> bool { ret_val.into_bool() } -pub fn code_hash(account_id: &[u8], output: &mut &mut [u8]) -> Result { +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)) + sys::seal_code_hash( + Ptr32::from_slice(account_id), + Ptr32Mut::from_slice(output), + Ptr32Mut::from_ref(&mut output_len), + ) }; - ret_code.into() + ret_val.into() } -pub fn own_code_hash(output: &mut &mut [u8]) -> Result { - let ret_val = unsafe { sys::seal_own_code_hash(Ptr32Mut::from_slice(output)) }; - ret_code.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() } diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index aacd59d8a4..2ad894ca48 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -513,7 +513,11 @@ impl TypedEnvBackend for EnvInstance { ext::caller_is_origin() } - fn code_hash(&mut self, account_id: &E::AccountId, output: E::Hash) -> Result<()> + fn code_hash( + &mut self, + account_id: &E::AccountId, + output: &mut E::Hash, + ) -> Result<()> where E: Environment, { @@ -522,7 +526,7 @@ impl TypedEnvBackend for EnvInstance { ext::code_hash(enc_account_id, output).map_err(Into::into) } - fn own_code_hash(&mut self, output: E::Hash) -> Result<()> + fn own_code_hash(&mut self, output: &mut E::Hash) -> Result<()> where E: Environment, { From 2e019bf4a4bac0288a211e14bc0133e6a16a91a4 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 21:28:27 +0300 Subject: [PATCH 06/18] more ci fixes --- crates/env/src/engine/on_chain/impls.rs | 4 ++-- crates/lang/src/env_access.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 2ad894ca48..1fdca85770 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -523,13 +523,13 @@ impl TypedEnvBackend for EnvInstance { { let mut scope = self.scoped_buffer(); let enc_account_id = scope.take_encoded(account_id); - ext::code_hash(enc_account_id, output).map_err(Into::into) + ext::code_hash(enc_account_id, output.as_mut()).map_err(Into::into) } fn own_code_hash(&mut self, output: &mut E::Hash) -> Result<()> where E: Environment, { - ext::own_code_hash(output).map_err(Into::into) + ext::own_code_hash(output.as_mut()).map_err(Into::into) } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 78dfd78082..6f275bc06c 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -933,7 +933,7 @@ where /// For more details visit: [`ink_env::code_hash`] pub fn code_hash(self, account_id: &E::AccountId) -> Result { let mut output = ::Hash::clear(); - ink_env::code_hash::(account_id, &mut output).map(|_| output.into()) + ink_env::code_hash::(account_id, &mut output).map(|_| output) } /// Returns the code hash of the contract living at the given `account`. From da094122f9902319aefbb9821fdc6cefea80bb8d Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 1 Apr 2022 14:46:05 +0300 Subject: [PATCH 07/18] refactored on what to return --- crates/env/src/api.rs | 12 ++++-------- crates/env/src/backend.rs | 8 ++------ crates/env/src/engine/off_chain/impls.rs | 8 ++------ crates/env/src/engine/on_chain/impls.rs | 18 ++++++++++-------- crates/lang/src/env_access.rs | 15 ++++++--------- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index de9427b42b..7c32657c74 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -515,23 +515,19 @@ where } /// Retrieves the code hash of a contract living at specified account -pub fn code_hash(account: &E::AccountId, output: &mut E::Hash) -> Result<()> +pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, { - ::on_instance(|instance| { - instance.code_hash::(account, output) - }) + ::on_instance(|instance| instance.code_hash::(account)) } /// Retrieves the code hash of the currently executing contract. -pub fn own_code_hash(output: &mut E::Hash) -> Result<()> +pub fn own_code_hash() -> Result where E: Environment, { - ::on_instance(|instance| { - instance.own_code_hash::(output) - }) + ::on_instance(|instance| instance.own_code_hash::()) } /// Checks whether the caller of the current contract is the origin of the whole call stack. diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 36df2f1077..fb9cd404d2 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -454,11 +454,7 @@ pub trait TypedEnvBackend: EnvBackend { /// # Note /// /// For more details visit: [`code_hash`][`crate::code_hash`] - fn code_hash( - &mut self, - account: &E::AccountId, - output: &mut E::Hash, - ) -> Result<()> + fn code_hash(&mut self, account: &E::AccountId) -> Result where E: Environment; @@ -467,7 +463,7 @@ pub trait TypedEnvBackend: EnvBackend { /// # Note /// /// For more details visit: [`own_code_hash`][`crate::own_code_hash`] - fn own_code_hash(&mut self, output: &mut E::Hash) -> Result<()> + fn own_code_hash(&mut self) -> Result where E: Environment; } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index bb60ba3077..f6be219f8e 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -487,18 +487,14 @@ impl TypedEnvBackend for EnvInstance { unimplemented!("off-chain environment does not support cross-contract calls") } - fn code_hash( - &mut self, - _account: &E::AccountId, - _output: &mut E::Hash, - ) -> Result<()> + fn code_hash(&mut self, _account: &E::AccountId) -> Result where E: Environment, { unimplemented!("off-chain environment does not support contract instantiation") } - fn own_code_hash(&mut self, _output: &mut E::Hash) -> Result<()> + fn own_code_hash(&mut self) -> Result where E: Environment, { diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 1fdca85770..978aab8543 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -513,23 +513,25 @@ impl TypedEnvBackend for EnvInstance { ext::caller_is_origin() } - fn code_hash( - &mut self, - account_id: &E::AccountId, - output: &mut E::Hash, - ) -> Result<()> + fn code_hash(&mut self, account_id: &E::AccountId) -> Result where E: Environment, { let mut scope = self.scoped_buffer(); let enc_account_id = scope.take_encoded(account_id); - ext::code_hash(enc_account_id, output.as_mut()).map_err(Into::into) + let mut output = ::Hash::clear(); + ext::code_hash(enc_account_id, output.as_mut()) + .map(|_| output) + .map_err(Into::into) } - fn own_code_hash(&mut self, output: &mut E::Hash) -> Result<()> + fn own_code_hash(&mut self) -> Result where E: Environment, { - ext::own_code_hash(output.as_mut()).map_err(Into::into) + let mut output = ::Hash::clear(); + ext::own_code_hash(output.as_mut()) + .map(|_| output) + .map_err(Into::into) } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 6f275bc06c..a2362f0518 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -25,7 +25,6 @@ use ink_env::{ CryptoHash, HashOutput, }, - Clear, Environment, Error, Result, @@ -921,8 +920,8 @@ where /// # } /// # /// #[ink(message)] - /// pub fn code_hash(&mut self, account_id: AccountId) -> Result { - /// self.env().code_hash(&account_id) + /// pub fn code_hash(&mut self, account_id: AccountId) -> Option { + /// self.env().code_hash(&account_id).ok() /// } /// # } /// # } @@ -932,8 +931,7 @@ where /// /// For more details visit: [`ink_env::code_hash`] pub fn code_hash(self, account_id: &E::AccountId) -> Result { - let mut output = ::Hash::clear(); - ink_env::code_hash::(account_id, &mut output).map(|_| output) + ink_env::code_hash::(account_id) } /// Returns the code hash of the contract living at the given `account`. @@ -954,8 +952,8 @@ where /// # } /// # /// #[ink(message)] - /// pub fn own_code_hash(&mut self) -> Result { - /// self.env().own_code_hash() + /// pub fn own_code_hash(&mut self) -> Hash { + /// self.env().own_code_hash().expect("contract should have a code hash") /// } /// # } /// # } @@ -965,7 +963,6 @@ where /// /// For more details visit: [`ink_env::own_code_hash`] pub fn own_code_hash(self) -> Result { - let mut output = ::Hash::clear(); - ink_env::own_code_hash::(&mut output).map(|_| output) + ink_env::own_code_hash::() } } From 87a0e24afa4adbaa7d19f604dff85bb9cdb20c0e Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 4 Apr 2022 19:01:20 +0300 Subject: [PATCH 08/18] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/api.rs | 2 +- crates/env/src/backend.rs | 2 +- crates/env/src/engine/off_chain/impls.rs | 4 ++-- crates/lang/src/env_access.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 7c32657c74..ef6a3bbd28 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -514,7 +514,7 @@ where }) } -/// Retrieves the code hash of a contract living at specified account +/// Retrieves the code hash of the contract at the specified account id. pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index fb9cd404d2..1c36f6ced6 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -448,7 +448,7 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; - /// Retrieves the code hash of the contract for the given `account`, + /// Retrieves the code hash of the contract at the given `account` id, /// and stores the result in `output`. /// /// # Note diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index f6be219f8e..969d761746 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -491,13 +491,13 @@ impl TypedEnvBackend for EnvInstance { where E: Environment, { - unimplemented!("off-chain environment does not support contract instantiation") + unimplemented!("off-chain environment does not support `code_hash`") } fn own_code_hash(&mut self) -> Result where E: Environment, { - unimplemented!("off-chain environment does not support contract instantiation") + unimplemented!("off-chain environment does not support `own_code_hash`") } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index a2362f0518..80a10a2ee5 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -902,7 +902,7 @@ where ink_env::caller_is_origin::() } - /// Returns the code hash of the contract living at the given `account`. + /// Returns the code hash of the contract at the given `account` id. /// /// # Example /// From b77254c609dc743223728984ed2fb00137a1f0d6 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Wed, 6 Apr 2022 14:46:14 +0300 Subject: [PATCH 09/18] fix: new funcs are in TypedEnvBackend trait --- crates/env/src/api.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index ef6a3bbd28..931b972588 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -519,7 +519,9 @@ pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, { - ::on_instance(|instance| instance.code_hash::(account)) + ::on_instance(|instance| { + TypedEnvBackend::code_hash::(instance, account) + }) } /// Retrieves the code hash of the currently executing contract. @@ -527,7 +529,9 @@ pub fn own_code_hash() -> Result where E: Environment, { - ::on_instance(|instance| instance.own_code_hash::()) + ::on_instance(|instance| { + TypedEnvBackend::own_code_hash::(instance) + }) } /// Checks whether the caller of the current contract is the origin of the whole call stack. From 92ca44ff6290f7385179c2dbfe552defcfb5bd35 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Wed, 6 Apr 2022 16:18:43 +0300 Subject: [PATCH 10/18] errors section to code_hash doc + own_code_hash to return non-wrapped val --- crates/env/src/api.rs | 6 +++++- crates/env/src/backend.rs | 2 +- crates/env/src/engine/off_chain/impls.rs | 2 +- crates/env/src/engine/on_chain/ext.rs | 5 ++--- crates/env/src/engine/on_chain/impls.rs | 7 +++---- crates/lang/src/env_access.rs | 4 ++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 931b972588..b6aa6f682b 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -515,6 +515,10 @@ where } /// Retrieves the code hash of the contract at the specified account id. +/// +/// # Errors +/// +/// If no code hash found for the specified account id. pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, @@ -525,7 +529,7 @@ where } /// Retrieves the code hash of the currently executing contract. -pub fn own_code_hash() -> Result +pub fn own_code_hash() -> E::Hash where E: Environment, { diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 1c36f6ced6..2fa46bb85c 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -463,7 +463,7 @@ pub trait TypedEnvBackend: EnvBackend { /// # Note /// /// For more details visit: [`own_code_hash`][`crate::own_code_hash`] - fn own_code_hash(&mut self) -> Result + fn own_code_hash(&mut self) -> E::Hash where E: Environment; } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 969d761746..08cb2d1937 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -494,7 +494,7 @@ impl TypedEnvBackend for EnvInstance { unimplemented!("off-chain environment does not support `code_hash`") } - fn own_code_hash(&mut self) -> Result + fn own_code_hash(&mut self) -> E::Hash where E: Environment, { diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index bd4cec323a..80c55d8ff8 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -700,13 +700,12 @@ pub fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { ret_val.into() } -pub fn own_code_hash(output: &mut [u8]) -> Result { +pub fn own_code_hash(output: &mut [u8]) { let mut output_len = output.len() as u32; - let ret_val = unsafe { + unsafe { sys::seal_own_code_hash( Ptr32Mut::from_slice(output), Ptr32Mut::from_ref(&mut output_len), ) }; - ret_val.into() } diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 978aab8543..ea198dd6a2 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -525,13 +525,12 @@ impl TypedEnvBackend for EnvInstance { .map_err(Into::into) } - fn own_code_hash(&mut self) -> Result + fn own_code_hash(&mut self) -> E::Hash where E: Environment, { let mut output = ::Hash::clear(); - ext::own_code_hash(output.as_mut()) - .map(|_| output) - .map_err(Into::into) + ext::own_code_hash(output.as_mut()); + output } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 80a10a2ee5..3c6c312438 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -953,7 +953,7 @@ where /// # /// #[ink(message)] /// pub fn own_code_hash(&mut self) -> Hash { - /// self.env().own_code_hash().expect("contract should have a code hash") + /// self.env().own_code_hash() /// } /// # } /// # } @@ -962,7 +962,7 @@ where /// # Note /// /// For more details visit: [`ink_env::own_code_hash`] - pub fn own_code_hash(self) -> Result { + pub fn own_code_hash(self) -> E::Hash { ink_env::own_code_hash::() } } From 74647effda813a3af2706d844ee567e20572c50d Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Wed, 6 Apr 2022 18:01:58 +0300 Subject: [PATCH 11/18] bug caught and fixed: wrong return val for seal_own_code_hash --- crates/env/src/engine/on_chain/ext.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index 80c55d8ff8..e36b782ec7 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -357,7 +357,7 @@ mod sys { pub fn seal_own_code_hash( output_ptr: Ptr32Mut<[u8]>, output_len_ptr: Ptr32Mut, - ) -> ReturnCode; + ); } } From 9a25772590b61cf0233b17898317016c2a348d66 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 14:09:51 +0300 Subject: [PATCH 12/18] Update crates/env/src/api.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/api.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index b6aa6f682b..73cb68866f 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -518,7 +518,7 @@ where /// /// # Errors /// -/// If no code hash found for the specified account id. +/// Returns `Error::KeyNotFound` if no code hash was found for the specified account id. pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, From 1114083d2aa4adb1875b21d132d964634bed23a1 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 14:16:40 +0300 Subject: [PATCH 13/18] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/backend.rs | 3 +-- crates/lang/src/env_access.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 2fa46bb85c..181884be6e 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -448,8 +448,7 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; - /// Retrieves the code hash of the contract at the given `account` id, - /// and stores the result in `output`. + /// Retrieves the code hash of the contract at the given `account` id. /// /// # Note /// diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 3c6c312438..1a78d249e0 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -934,7 +934,7 @@ where ink_env::code_hash::(account_id) } - /// Returns the code hash of the contract living at the given `account`. + /// Returns the code hash of the contract at the given `account` id. /// /// # Example /// From 3ec737414a9ba95b44f0f93f72e584bcd78eefdc Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 14:59:56 +0300 Subject: [PATCH 14/18] own_code_hash to return Result, as there could be decoding issues --- crates/env/src/api.rs | 9 +++++++-- crates/env/src/backend.rs | 2 +- crates/env/src/engine/off_chain/impls.rs | 2 +- crates/env/src/engine/on_chain/ext.rs | 5 +++-- crates/env/src/engine/on_chain/impls.rs | 21 +++++++++++++-------- crates/lang/src/env_access.rs | 4 ++-- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 73cb68866f..889d763d24 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -518,7 +518,8 @@ where /// /// # Errors /// -/// Returns `Error::KeyNotFound` if no code hash was found for the specified account id. +/// - If no code hash was found for the specified account id. +/// - If the returned value cannot be properly decoded. pub fn code_hash(account: &E::AccountId) -> Result where E: Environment, @@ -529,7 +530,11 @@ where } /// Retrieves the code hash of the currently executing contract. -pub fn own_code_hash() -> E::Hash +/// +/// # Errors +/// +/// If the returned value cannot be properly decoded. +pub fn own_code_hash() -> Result where E: Environment, { diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 181884be6e..1bbaa4c7e1 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -462,7 +462,7 @@ pub trait TypedEnvBackend: EnvBackend { /// # Note /// /// For more details visit: [`own_code_hash`][`crate::own_code_hash`] - fn own_code_hash(&mut self) -> E::Hash + fn own_code_hash(&mut self) -> Result where E: Environment; } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 08cb2d1937..969d761746 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -494,7 +494,7 @@ impl TypedEnvBackend for EnvInstance { unimplemented!("off-chain environment does not support `code_hash`") } - fn own_code_hash(&mut self) -> E::Hash + fn own_code_hash(&mut self) -> Result where E: Environment, { diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index e36b782ec7..fa8e0677ad 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -700,12 +700,13 @@ pub fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { ret_val.into() } -pub fn own_code_hash(output: &mut [u8]) { +pub fn own_code_hash(output: &mut [u8]) -> Result { let mut output_len = output.len() as u32; - unsafe { + let ret_val = unsafe { sys::seal_own_code_hash( Ptr32Mut::from_slice(output), Ptr32Mut::from_ref(&mut output_len), ) }; + ret_val.into() } diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index ea198dd6a2..a44f4b68a2 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -518,19 +518,24 @@ impl TypedEnvBackend for EnvInstance { E: Environment, { let mut scope = self.scoped_buffer(); - let enc_account_id = scope.take_encoded(account_id); - let mut output = ::Hash::clear(); - ext::code_hash(enc_account_id, output.as_mut()) - .map(|_| output) + let mut output = scope.take(32); + scope.append_encoded(account_id); + let enc_account_id = scope.take_appended(); + + ext::code_hash(enc_account_id, output.as_mut()); + scale::Decode::decode(&mut &output[..]) .map_err(Into::into) + .into() } - fn own_code_hash(&mut self) -> E::Hash + fn own_code_hash(&mut self) -> Result where E: Environment, { - let mut output = ::Hash::clear(); - ext::own_code_hash(output.as_mut()); - output + let output = &mut self.scoped_buffer().take(32); + ext::own_code_hash(output); + scale::Decode::decode(&mut &output[..]) + .map_err(Into::into) + .into() } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 1a78d249e0..fa843c6826 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -953,7 +953,7 @@ where /// # /// #[ink(message)] /// pub fn own_code_hash(&mut self) -> Hash { - /// self.env().own_code_hash() + /// self.env().own_code_hash().expect("contract should have a code hash") /// } /// # } /// # } @@ -962,7 +962,7 @@ where /// # Note /// /// For more details visit: [`ink_env::own_code_hash`] - pub fn own_code_hash(self) -> E::Hash { + pub fn own_code_hash(self) -> Result { ink_env::own_code_hash::() } } From 764a381570bbcfb1778c5adc51b0eb35dc5fb0c1 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 17:06:40 +0300 Subject: [PATCH 15/18] bugfix (e2e testing caught) --- crates/env/src/engine/on_chain/ext.rs | 7 +++---- crates/env/src/engine/on_chain/impls.rs | 14 ++++++-------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index fa8e0677ad..13d63677da 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -700,13 +700,12 @@ pub fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { ret_val.into() } -pub fn own_code_hash(output: &mut [u8]) -> Result { +pub fn own_code_hash(output: &mut [u8]) { let mut output_len = output.len() as u32; - let ret_val = unsafe { + unsafe { sys::seal_own_code_hash( Ptr32Mut::from_slice(output), Ptr32Mut::from_ref(&mut output_len), ) - }; - ret_val.into() + } } diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index a44f4b68a2..4923b864f4 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -518,14 +518,13 @@ impl TypedEnvBackend for EnvInstance { E: Environment, { let mut scope = self.scoped_buffer(); - let mut output = scope.take(32); + let output = scope.take(32); scope.append_encoded(account_id); let enc_account_id = scope.take_appended(); - ext::code_hash(enc_account_id, output.as_mut()); - scale::Decode::decode(&mut &output[..]) - .map_err(Into::into) - .into() + ext::code_hash(enc_account_id, output.as_mut())?; + let hash = scale::Decode::decode(&mut &output[..])?; + Ok(hash) } fn own_code_hash(&mut self) -> Result @@ -534,8 +533,7 @@ impl TypedEnvBackend for EnvInstance { { let output = &mut self.scoped_buffer().take(32); ext::own_code_hash(output); - scale::Decode::decode(&mut &output[..]) - .map_err(Into::into) - .into() + let hash = scale::Decode::decode(&mut &output[..])?; + Ok(hash) } } From d1ec5297b750015d44ddc34104c5d8113c4d4da3 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 17:13:17 +0300 Subject: [PATCH 16/18] ci fix --- crates/env/src/engine/on_chain/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 4923b864f4..e6611b2367 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -522,7 +522,7 @@ impl TypedEnvBackend for EnvInstance { scope.append_encoded(account_id); let enc_account_id = scope.take_appended(); - ext::code_hash(enc_account_id, output.as_mut())?; + ext::code_hash(enc_account_id, output)?; let hash = scale::Decode::decode(&mut &output[..])?; Ok(hash) } From 5591d955a5f5a04432415b1b60543db810ff349f Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 19:32:02 +0300 Subject: [PATCH 17/18] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Michael Müller --- crates/env/src/engine/on_chain/impls.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index e6611b2367..26f4386356 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -523,8 +523,7 @@ impl TypedEnvBackend for EnvInstance { let enc_account_id = scope.take_appended(); ext::code_hash(enc_account_id, output)?; - let hash = scale::Decode::decode(&mut &output[..])?; - Ok(hash) + scale::Decode::decode(&mut &output[..]) } fn own_code_hash(&mut self) -> Result @@ -533,7 +532,6 @@ impl TypedEnvBackend for EnvInstance { { let output = &mut self.scoped_buffer().take(32); ext::own_code_hash(output); - let hash = scale::Decode::decode(&mut &output[..])?; - Ok(hash) + scale::Decode::decode(&mut &output[..]) } } From 12901dc209adc60a588171b3db3caa8c70bc957d Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 19:35:06 +0300 Subject: [PATCH 18/18] Revert "Apply suggestions from code review" This reverts commit 5591d955a5f5a04432415b1b60543db810ff349f. --- crates/env/src/engine/on_chain/impls.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 26f4386356..e6611b2367 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -523,7 +523,8 @@ impl TypedEnvBackend for EnvInstance { let enc_account_id = scope.take_appended(); ext::code_hash(enc_account_id, output)?; - scale::Decode::decode(&mut &output[..]) + let hash = scale::Decode::decode(&mut &output[..])?; + Ok(hash) } fn own_code_hash(&mut self) -> Result @@ -532,6 +533,7 @@ impl TypedEnvBackend for EnvInstance { { let output = &mut self.scoped_buffer().take(32); ext::own_code_hash(output); - scale::Decode::decode(&mut &output[..]) + let hash = scale::Decode::decode(&mut &output[..])?; + Ok(hash) } }