diff --git a/crates/e2e/src/builders.rs b/crates/e2e/src/builders.rs index 164f1c07ca1..8d23ff7fd65 100644 --- a/crates/e2e/src/builders.rs +++ b/crates/e2e/src/builders.rs @@ -50,7 +50,7 @@ pub fn constructor_exec_input( // set all the other properties to default values, we only require the `exec_input`. builder .endowment(0u32.into()) - .code_hash(ink_primitives::Clear::clear()) + .code_hash(ink_primitives::Clear::CLEAR_HASH) .salt_bytes(Vec::new()) .params() .exec_input() diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index fe34065325c..9a45106ca7d 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -228,7 +228,7 @@ where /// # type AccountId = ::AccountId; /// let my_return_value: i32 = build_call::() /// .call_type(DelegateCall::new() -/// .code_hash(::Hash::clear())) +/// .code_hash(::Hash::CLEAR_HASH)) /// .exec_input( /// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF])) /// .push_arg(42u8) @@ -322,16 +322,16 @@ pub struct DelegateCall { impl DelegateCall { /// Returns a clean builder for [`DelegateCall`] - pub fn new() -> Self { - Default::default() + pub const fn new() -> Self { + DelegateCall { + code_hash: E::Hash::CLEAR_HASH, + } } } impl Default for DelegateCall { fn default() -> Self { - DelegateCall { - code_hash: E::Hash::clear(), - } + Self::new() } } diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index beb65d270c6..ce958c3b3a5 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -138,7 +138,7 @@ where { let encoded = topic_value.encode(); let len_encoded = encoded.len(); - let mut result = ::Hash::clear(); + let mut result = ::Hash::CLEAR_HASH; let len_result = result.as_ref().len(); if len_encoded <= len_result { result.as_mut()[..len_encoded].copy_from_slice(&encoded[..]); diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index c55660bf2ec..051a242762b 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -150,7 +150,7 @@ where { fn inner(encoded: &mut [u8]) -> ::Hash { let len_encoded = encoded.len(); - let mut result = ::Hash::clear(); + let mut result = ::Hash::CLEAR_HASH; let len_result = result.as_ref().len(); if len_encoded <= len_result { result.as_mut()[..len_encoded].copy_from_slice(encoded); diff --git a/crates/ink/src/env_access.rs b/crates/ink/src/env_access.rs index 3134839e744..66d4b15c043 100644 --- a/crates/ink/src/env_access.rs +++ b/crates/ink/src/env_access.rs @@ -584,7 +584,7 @@ where /// let call_params = build_call::() /// .call_type( /// DelegateCall::new() - /// .code_hash(::Hash::clear())) + /// .code_hash(::Hash::CLEAR_HASH)) /// .exec_input( /// ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE])) /// .push_arg(42u8) diff --git a/crates/primitives/src/types.rs b/crates/primitives/src/types.rs index e6645ac0f55..6b86068e6b5 100644 --- a/crates/primitives/src/types.rs +++ b/crates/primitives/src/types.rs @@ -129,29 +129,25 @@ impl AsMut<[u8]> for Hash { /// /// A hash that consists only of 0 bits is clear. pub trait Clear { + /// The clear hash. + const CLEAR_HASH: Self; + /// Returns `true` if the hash is clear. fn is_clear(&self) -> bool; - - /// Returns a clear hash. - fn clear() -> Self; } impl Clear for [u8; 32] { - fn is_clear(&self) -> bool { - self.as_ref().iter().all(|&byte| byte == 0x00) - } + const CLEAR_HASH: Self = [0x00; 32]; - fn clear() -> Self { - [0x00; 32] + fn is_clear(&self) -> bool { + self == &Self::CLEAR_HASH } } impl Clear for Hash { + const CLEAR_HASH: Self = Self(<[u8; 32] as Clear>::CLEAR_HASH); + fn is_clear(&self) -> bool { <[u8; 32] as Clear>::is_clear(&self.0) } - - fn clear() -> Self { - Self(<[u8; 32] as Clear>::clear()) - } } diff --git a/crates/storage/src/lazy/mapping.rs b/crates/storage/src/lazy/mapping.rs index dd75510ee8f..6515dec5cbc 100644 --- a/crates/storage/src/lazy/mapping.rs +++ b/crates/storage/src/lazy/mapping.rs @@ -93,9 +93,7 @@ where KeyType: StorageKey, { fn default() -> Self { - Self { - _marker: Default::default(), - } + Self::new() } } @@ -105,9 +103,9 @@ where KeyType: StorageKey, { /// Creates a new empty `Mapping`. - pub fn new() -> Self { + pub const fn new() -> Self { Self { - _marker: Default::default(), + _marker: PhantomData, } } } diff --git a/crates/storage/src/lazy/mod.rs b/crates/storage/src/lazy/mod.rs index 4909d8bda16..a9418eceba8 100644 --- a/crates/storage/src/lazy/mod.rs +++ b/crates/storage/src/lazy/mod.rs @@ -100,9 +100,7 @@ where KeyType: StorageKey, { fn default() -> Self { - Self { - _marker: Default::default(), - } + Self::new() } } @@ -111,9 +109,9 @@ where KeyType: StorageKey, { /// Creates a new empty `Lazy`. - pub fn new() -> Self { + pub const fn new() -> Self { Self { - _marker: Default::default(), + _marker: PhantomData, } } } diff --git a/examples/erc20/lib.rs b/examples/erc20/lib.rs index 86b947a4a73..4608550f0aa 100644 --- a/examples/erc20/lib.rs +++ b/examples/erc20/lib.rs @@ -217,7 +217,10 @@ mod erc20 { mod tests { use super::*; - use ink::primitives::Clear; + use ink::primitives::{ + Clear, + Hash, + }; type Event = ::Type; @@ -259,7 +262,7 @@ mod erc20 { for (n, (actual_topic, expected_topic)) in topics.iter().zip(expected_topics).enumerate() { - let mut topic_hash = Hash::clear(); + let mut topic_hash = Hash::CLEAR_HASH; let len = actual_topic.len(); topic_hash.as_mut()[0..len].copy_from_slice(&actual_topic[0..len]); @@ -513,7 +516,7 @@ mod erc20 { primitives::Clear, }; - let mut result = Hash::clear(); + let mut result = Hash::CLEAR_HASH; let len_result = result.as_ref().len(); let encoded = entity.encode(); let len_encoded = encoded.len(); diff --git a/examples/trait-erc20/lib.rs b/examples/trait-erc20/lib.rs index fb1bd8da5a2..2168d42f10c 100644 --- a/examples/trait-erc20/lib.rs +++ b/examples/trait-erc20/lib.rs @@ -290,7 +290,7 @@ mod erc20 { where T: scale::Encode, { - let mut result = Hash::clear(); + let mut result = Hash::CLEAR_HASH; let len_result = result.as_ref().len(); let encoded = entity.encode(); let len_encoded = encoded.len();