From 5d058baf27fb02a99960d9c1ef6dcc1de6ca23a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Wed, 19 Oct 2022 17:45:25 +0200 Subject: [PATCH 1/2] Remove random function --- CHANGELOG.md | 2 ++ crates/engine/Cargo.toml | 2 -- crates/engine/src/exec_context.rs | 11 +----- crates/engine/src/ext.rs | 32 ----------------- crates/engine/src/types.rs | 3 -- crates/env/Cargo.toml | 3 -- crates/env/src/api.rs | 28 --------------- crates/env/src/backend.rs | 9 ----- crates/env/src/engine/off_chain/impls.rs | 9 ----- crates/env/src/engine/off_chain/test_api.rs | 12 ------- crates/env/src/engine/on_chain/ext.rs | 23 ------------- crates/env/src/engine/on_chain/impls.rs | 11 ------ crates/ink/src/env_access.rs | 34 ------------------- .../ink/tests/ui/contract/pass/env-access.rs | 2 -- 14 files changed, 3 insertions(+), 178 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40065bc5d07..0cd5d174a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Remove random function from ink!. + ## Version 4.0.0-alpha.3 ### Breaking Changes diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 7faacd1e56c..262ba058cfd 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -23,8 +23,6 @@ sha2 = { version = "0.10" } sha3 = { version = "0.10" } blake2 = { version = "0.10" } -rand = { version = "0.8" } - # ECDSA for the off-chain environment. secp256k1 = { version = "0.24", features = ["recovery", "global-context"], optional = true } diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index 271ce26fd0f..ef5a5eed8f6 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -17,9 +17,7 @@ use super::types::{ Balance, BlockNumber, BlockTimestamp, - Hash, }; -use rand::Rng; /// The context of a contract execution. #[cfg_attr(test, derive(Debug, PartialEq, Eq))] @@ -44,21 +42,16 @@ pub struct ExecContext { pub block_number: BlockNumber, /// The current block timestamp. pub block_timestamp: BlockTimestamp, - /// The randomization entropy for a block. - pub entropy: Hash, } impl Default for ExecContext { fn default() -> Self { - let mut entropy: [u8; 32] = Default::default(); - rand::thread_rng().fill(entropy.as_mut()); Self { caller: None, callee: None, value_transferred: 0, block_number: 0, block_timestamp: 0, - entropy, } } } @@ -101,10 +94,8 @@ mod tests { assert_eq!(exec_cont.callee(), vec![13]); exec_cont.reset(); - exec_cont.entropy = Default::default(); - let mut new_exec_cont = ExecContext::new(); - new_exec_cont.entropy = Default::default(); + let new_exec_cont = ExecContext::new(); assert_eq!(exec_cont, new_exec_cont); } } diff --git a/crates/engine/src/ext.rs b/crates/engine/src/ext.rs index 94ff7840d26..458538a2613 100644 --- a/crates/engine/src/ext.rs +++ b/crates/engine/src/ext.rs @@ -31,10 +31,6 @@ use crate::{ BlockTimestamp, }, }; -use rand::{ - Rng, - SeedableRng, -}; use scale::Encode; use std::panic::panic_any; @@ -431,34 +427,6 @@ impl Engine { set_output(output, &fee[..]) } - /// Returns a randomized hash. - /// - /// # Note - /// - /// - This is the off-chain environment implementation of `random`. - /// It provides the same behavior in that it will likely yield the - /// same hash for the same subjects within the same block (or - /// execution context). - /// - /// # Example - /// - /// ```rust - /// let engine = ink_engine::ext::Engine::default(); - /// let subject = [0u8; 32]; - /// let mut output = [0u8; 32]; - /// engine.random(&subject, &mut output.as_mut_slice()); - /// ``` - pub fn random(&self, subject: &[u8], output: &mut &mut [u8]) { - let seed = (self.exec_context.entropy, subject).encode(); - let mut digest = [0u8; 32]; - Engine::hash_blake2_256(&seed, &mut digest); - - let mut rng = rand::rngs::StdRng::from_seed(digest); - let mut rng_bytes: [u8; 32] = Default::default(); - rng.fill(&mut rng_bytes); - set_output(output, &rng_bytes[..]) - } - /// Calls the chain extension method registered at `func_id` with `input`. pub fn call_chain_extension( &mut self, diff --git a/crates/engine/src/types.rs b/crates/engine/src/types.rs index ae1abe3b7a4..73307264ac6 100644 --- a/crates/engine/src/types.rs +++ b/crates/engine/src/types.rs @@ -17,9 +17,6 @@ use derive_more::From; -/// Same type as the `DefaultEnvironment::Hash` type. -pub type Hash = [u8; 32]; - /// Same type as the `DefaultEnvironment::BlockNumber` type. pub type BlockNumber = u32; diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index b6b556ae94d..81d5e0965e9 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -47,7 +47,6 @@ secp256k1 = { version = "0.24", features = ["recovery", "global-context"], optio # # Sadly couldn't be marked as dev-dependency. # Never use this crate outside the off-chain environment! -rand = { version = "0.8", default-features = false, features = ["alloc"], optional = true } scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } [features] @@ -62,8 +61,6 @@ std = [ "scale/std", "scale-info/std", "secp256k1", - "rand/std", - "rand/std_rng", "num-traits/std", # Enables hashing crates for off-chain environment. "sha2", diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index ad664b9945b..ed70b8d4998 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -405,34 +405,6 @@ where }) } -/// Returns a random hash seed and the block number since which it was determinable -/// by chain observers. -/// -/// # Note -/// -/// - The subject buffer can be used to further randomize the hash. -/// - Within the same execution returns the same random hash for the same subject. -/// -/// # Errors -/// -/// If the returned value cannot be properly decoded. -/// -/// # Important -/// -/// The returned seed should only be used to distinguish commitments made before -/// the returned block number. If the block number is too early (i.e. commitments were -/// made afterwards), then ensure no further commitments may be made and repeatedly -/// call this on later blocks until the block number returned is later than the latest -/// commitment. -pub fn random(subject: &[u8]) -> Result<(E::Hash, E::BlockNumber)> -where - E: Environment, -{ - ::on_instance(|instance| { - TypedEnvBackend::random::(instance, subject) - }) -} - /// Appends the given message to the debug message buffer. pub fn debug_message(message: &str) { ::on_instance(|instance| { diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 8b9972104d6..cdd55f2c085 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -450,15 +450,6 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; - /// Returns a random hash seed. - /// - /// # Note - /// - /// For more details visit: [`random`][`crate::random`] - fn random(&mut self, subject: &[u8]) -> Result<(E::Hash, E::BlockNumber)> - where - E: Environment; - /// Checks whether a specified account belongs to a contract. /// /// # Note diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 6d668ca72b8..07da3722b78 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -495,15 +495,6 @@ impl TypedEnvBackend for EnvInstance { }) } - fn random(&mut self, subject: &[u8]) -> Result<(E::Hash, E::BlockNumber)> - where - E: Environment, - { - let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; - self.engine.random(subject, &mut &mut output[..]); - scale::Decode::decode(&mut &output[..]).map_err(Into::into) - } - fn is_contract(&mut self, _account: &E::AccountId) -> bool where E: Environment, diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 01bef923811..6beaf79223f 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -98,18 +98,6 @@ where }) } -/// Set the entropy hash of the current block. -/// -/// # Note -/// -/// This allows to control what [`random`][`crate::random`] returns. -pub fn set_block_entropy(_entropy: T::Hash) -> Result<()> -where - T: Environment, -{ - unimplemented!("off-chain environment does not yet support `set_block_entropy`"); -} - /// Returns the contents of the past performed environmental debug messages in order. pub fn recorded_debug_messages() -> RecordedDebugMessages { ::on_instance(|instance| { diff --git a/crates/env/src/engine/on_chain/ext.rs b/crates/env/src/engine/on_chain/ext.rs index f38f301c6d2..a592ef88f39 100644 --- a/crates/env/src/engine/on_chain/ext.rs +++ b/crates/env/src/engine/on_chain/ext.rs @@ -347,13 +347,6 @@ mod sys { pub fn seal_terminate(beneficiary_ptr: Ptr32<[u8]>) -> !; - pub fn seal_random( - subject_ptr: Ptr32<[u8]>, - subject_len: u32, - output_ptr: Ptr32Mut<[u8]>, - output_len_ptr: Ptr32Mut, - ); - pub fn seal_call( flags: u32, callee_ptr: Ptr32<[u8]>, @@ -675,22 +668,6 @@ pub fn weight_to_fee(gas: u64, output: &mut &mut [u8]) { extract_from_slice(output, output_len as usize); } -#[inline(always)] -pub fn random(subject: &[u8], output: &mut &mut [u8]) { - let mut output_len = output.len() as u32; - { - unsafe { - sys::seal_random( - Ptr32::from_slice(subject), - subject.len() as u32, - Ptr32Mut::from_slice(output), - Ptr32Mut::from_ref(&mut output_len), - ) - }; - } - extract_from_slice(output, output_len as usize); -} - #[cfg(feature = "ink-debug")] /// Call `seal_debug_message` with the supplied UTF-8 encoded message. /// diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index 85d7c5f5475..c3693e14f14 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -523,17 +523,6 @@ impl TypedEnvBackend for EnvInstance { ::from_le_bytes(result) } - fn random(&mut self, subject: &[u8]) -> Result<(E::Hash, E::BlockNumber)> - where - E: Environment, - { - let mut scope = self.scoped_buffer(); - let enc_subject = scope.take_bytes(subject); - let output = &mut scope.take_rest(); - ext::random(enc_subject, output); - scale::Decode::decode(&mut &output[..]).map_err(Into::into) - } - fn is_contract(&mut self, account_id: &E::AccountId) -> bool where E: Environment, diff --git a/crates/ink/src/env_access.rs b/crates/ink/src/env_access.rs index 538bcbba7de..16247f40a81 100644 --- a/crates/ink/src/env_access.rs +++ b/crates/ink/src/env_access.rs @@ -666,40 +666,6 @@ where ink_env::transfer::(destination, value) } - /// Returns a random hash seed. - /// - /// # Example - /// - /// ``` - /// # #[ink::contract] - /// # pub mod my_contract { - /// # #[ink(storage)] - /// # pub struct MyContract { } - /// # - /// # impl MyContract { - /// # #[ink(constructor)] - /// # pub fn new() -> Self { - /// # Self {} - /// # } - /// # - /// #[ink(message)] - /// pub fn random_bool(&self) -> bool { - /// let additional_randomness = b"seed"; - /// let (hash, _block_number) = self.env().random(additional_randomness); - /// hash.as_ref()[0] != 0 - /// } - /// # - /// # } - /// # } - /// ``` - /// - /// # Note - /// - /// For more details visit: [`ink_env::random`] - pub fn random(self, subject: &[u8]) -> (E::Hash, E::BlockNumber) { - ink_env::random::(subject).expect("couldn't decode randomized hash") - } - /// Computes the hash of the given bytes using the cryptographic hash `H`. /// /// # Example diff --git a/crates/ink/tests/ui/contract/pass/env-access.rs b/crates/ink/tests/ui/contract/pass/env-access.rs index 817ab6c4c32..0b8df9e09b3 100644 --- a/crates/ink/tests/ui/contract/pass/env-access.rs +++ b/crates/ink/tests/ui/contract/pass/env-access.rs @@ -13,7 +13,6 @@ mod contract { let _ = Self::env().caller(); let _ = Self::env().gas_left(); let _ = Self::env().minimum_balance(); - let _ = Self::env().random(&[]); let _ = Self::env().transferred_value(); let _ = Self::env().weight_to_fee(0); Self {} @@ -28,7 +27,6 @@ mod contract { let _ = self.env().caller(); let _ = self.env().gas_left(); let _ = self.env().minimum_balance(); - let _ = self.env().random(&[]); let _ = self.env().transferred_value(); let _ = self.env().weight_to_fee(0); } From 0758443a8cba7bebe588dd02c4226ae2f20f5c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Thei=C3=9Fen?= Date: Fri, 21 Oct 2022 14:00:09 +0200 Subject: [PATCH 2/2] Fix clippy --- crates/e2e/macro/src/config.rs | 4 ++-- crates/engine/src/exec_context.rs | 13 +------------ crates/env/src/engine/on_chain/buffer.rs | 8 -------- 3 files changed, 3 insertions(+), 22 deletions(-) diff --git a/crates/e2e/macro/src/config.rs b/crates/e2e/macro/src/config.rs index 91ea8156886..0290ad5dcd2 100644 --- a/crates/e2e/macro/src/config.rs +++ b/crates/e2e/macro/src/config.rs @@ -95,8 +95,8 @@ impl TryFrom for E2EConfig { } } let additional_contracts = additional_contracts - .map(|(value, _)| value.value().split(" ").map(String::from).collect()) - .unwrap_or_else(|| Vec::new()); + .map(|(value, _)| value.value().split(' ').map(String::from).collect()) + .unwrap_or_else(Vec::new); Ok(E2EConfig { node_log: node_log.map(|(value, _)| value), ws_url: ws_url.map(|(value, _)| value), diff --git a/crates/engine/src/exec_context.rs b/crates/engine/src/exec_context.rs index ef5a5eed8f6..32d069e471e 100644 --- a/crates/engine/src/exec_context.rs +++ b/crates/engine/src/exec_context.rs @@ -21,6 +21,7 @@ use super::types::{ /// The context of a contract execution. #[cfg_attr(test, derive(Debug, PartialEq, Eq))] +#[derive(Default)] pub struct ExecContext { /// The caller of the contract execution. Might be user or another contract. /// @@ -44,18 +45,6 @@ pub struct ExecContext { pub block_timestamp: BlockTimestamp, } -impl Default for ExecContext { - fn default() -> Self { - Self { - caller: None, - callee: None, - value_transferred: 0, - block_number: 0, - block_timestamp: 0, - } - } -} - impl ExecContext { /// Creates a new execution context. pub fn new() -> Self { diff --git a/crates/env/src/engine/on_chain/buffer.rs b/crates/env/src/engine/on_chain/buffer.rs index 937c9e9894c..1d74f904a0a 100644 --- a/crates/env/src/engine/on_chain/buffer.rs +++ b/crates/env/src/engine/on_chain/buffer.rs @@ -146,14 +146,6 @@ impl<'a> ScopedBuffer<'a> { lhs } - /// Returns a buffer scope filled with `bytes` with the proper length. - pub fn take_bytes(&mut self, bytes: &[u8]) -> &'a mut [u8] { - debug_assert_eq!(self.offset, 0); - let buffer = self.take(bytes.len()); - buffer.copy_from_slice(bytes); - buffer - } - /// Encode the given value into the scoped buffer and return the sub slice /// containing all the encoded bytes. #[inline(always)]