diff --git a/substrate/srml/contract/COMPLEXITY.md b/substrate/srml/contract/COMPLEXITY.md index 3cd7fee448..c2d75efaba 100644 --- a/substrate/srml/contract/COMPLEXITY.md +++ b/substrate/srml/contract/COMPLEXITY.md @@ -298,11 +298,13 @@ This function serializes the address of the caller into the scratch buffer. **complexity**: Assuming that the address is of constant size, this function has constant complexity. -## ext_random_seed +## ext_random -This function serializes the current block's random seed into the scratch buffer. +This function serializes a random number generated by the given subject into the scratch buffer. +The complexity of this function highly depends on the complexity of `System::random`. `max_subject_len` +limits the size of the subject buffer. -**complexity**: Assuming that the random seed is of constant size, this function has constant complexity. +**complexity**: The complexity of this function depends on the implementation of `System::random`. ## ext_now diff --git a/substrate/srml/contract/src/exec.rs b/substrate/srml/contract/src/exec.rs index 9d63c037c0..1ab59fa0c2 100644 --- a/substrate/srml/contract/src/exec.rs +++ b/substrate/srml/contract/src/exec.rs @@ -106,8 +106,8 @@ pub trait Ext { /// Returns a reference to the timestamp of the current block fn now(&self) -> &MomentOf; - /// Returns a reference to the random seed for the current block - fn random_seed(&self) -> &SeedOf; + /// Returns a random number for the current block with the given subject. + fn random(&self, subject: &[u8]) -> SeedOf; /// Deposit an event with the given topics. /// @@ -353,7 +353,6 @@ where caller: self.self_account.clone(), value_transferred: value, timestamp: timestamp::Module::::now(), - random_seed: system::Module::::random_seed(), }, input_data, empty_output_buf, @@ -423,7 +422,6 @@ where caller: self.self_account.clone(), value_transferred: endowment, timestamp: timestamp::Module::::now(), - random_seed: system::Module::::random_seed(), }, input_data, EmptyOutputBuf::new(), @@ -576,7 +574,6 @@ struct CallContext<'a, 'b: 'a, T: Trait + 'b, V: Vm + 'b, L: Loader> { caller: T::AccountId, value_transferred: BalanceOf, timestamp: T::Moment, - random_seed: T::Hash, } impl<'a, 'b: 'a, T, E, V, L> Ext for CallContext<'a, 'b, T, V, L> @@ -642,8 +639,8 @@ where self.value_transferred } - fn random_seed(&self) -> &T::Hash { - &self.random_seed + fn random(&self, subject: &[u8]) -> SeedOf { + system::Module::::random(subject) } fn now(&self) -> &T::Moment { diff --git a/substrate/srml/contract/src/lib.rs b/substrate/srml/contract/src/lib.rs index b9412b3619..a255663bfb 100644 --- a/substrate/srml/contract/src/lib.rs +++ b/substrate/srml/contract/src/lib.rs @@ -690,6 +690,9 @@ pub struct Schedule { /// Whether the `ext_println` function is allowed to be used contracts. /// MUST only be enabled for `dev` chains, NOT for production chains pub enable_println: bool, + + /// The maximum length of a subject used for PRNG generation. + pub max_subject_len: u32, } impl> Default for Schedule { @@ -709,6 +712,7 @@ impl> Default for Schedule { max_stack_height: 64 * 1024, max_memory_pages: 16, enable_println: false, + max_subject_len: 32, } } } diff --git a/substrate/srml/contract/src/wasm/mod.rs b/substrate/srml/contract/src/wasm/mod.rs index 2776f4041b..d86678d383 100644 --- a/substrate/srml/contract/src/wasm/mod.rs +++ b/substrate/srml/contract/src/wasm/mod.rs @@ -177,9 +177,10 @@ mod tests { use crate::exec::{CallReceipt, Ext, InstantiateReceipt, EmptyOutputBuf, StorageKey}; use crate::gas::GasMeter; use crate::tests::{Test, Call}; - use wabt; use crate::wasm::prepare::prepare_contract; use crate::CodeHash; + use wabt; + use hex_literal::hex; #[derive(Debug, PartialEq, Eq)] struct DispatchEntry(Call); @@ -207,7 +208,6 @@ mod tests { // (topics, data) events: Vec<(Vec, Vec)>, next_account_id: u64, - random_seed: H256, } impl Ext for MockExt { type T = Test; @@ -276,8 +276,8 @@ mod tests { &1111 } - fn random_seed(&self) -> &H256{ - &self.random_seed + fn random(&self, subject: &[u8]) -> H256 { + H256::from_slice(subject) } fn deposit_event(&mut self, topics: Vec, data: Vec) { @@ -1115,11 +1115,12 @@ mod tests { .unwrap(); } - const CODE_RANDOM_SEED: &str = r#" + const CODE_RANDOM: &str = r#" (module - (import "env" "ext_random_seed" (func $ext_random_seed)) + (import "env" "ext_random" (func $ext_random (param i32 i32))) (import "env" "ext_scratch_size" (func $ext_scratch_size (result i32))) (import "env" "ext_scratch_copy" (func $ext_scratch_copy (param i32 i32 i32))) + (import "env" "ext_return" (func $ext_return (param i32 i32))) (import "env" "memory" (memory 1 1)) (func $assert (param i32) @@ -1133,7 +1134,10 @@ mod tests { (func (export "call") ;; This stores the block random seed in the scratch buffer - (call $ext_random_seed) + (call $ext_random + (i32.const 40) ;; Pointer in memory to the start of the subject buffer + (i32.const 32) ;; The subject buffer's length + ) ;; assert $ext_scratch_size == 32 (call $assert @@ -1150,35 +1154,44 @@ mod tests { (i32.const 32) ;; Count of bytes to copy. ) - ;; assert the contents of the buffer in 4 x i64 parts matches 1,2,3,4. - (call $assert (i64.eq (i64.load (i32.const 8)) (i64.const 1))) - (call $assert (i64.eq (i64.load (i32.const 16)) (i64.const 2))) - (call $assert (i64.eq (i64.load (i32.const 24)) (i64.const 3))) - (call $assert (i64.eq (i64.load (i32.const 32)) (i64.const 4))) + ;; return the data from the contract + (call $ext_return + (i32.const 8) + (i32.const 32) + ) ) (func (export "deploy")) + + ;; [8,40) is reserved for the result of PRNG. + + ;; the subject used for the PRNG. [40,72) + (data (i32.const 40) + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + "\00\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F" + ) ) "#; #[test] - fn random_seed() { + fn random() { let mut mock_ext = MockExt::default(); - let seed: [u8; 32] = [ - 1,0,0,0,0,0,0,0, - 2,0,0,0,0,0,0,0, - 3,0,0,0,0,0,0,0, - 4,0,0,0,0,0,0,0, - ]; - mock_ext.random_seed = H256::from_slice(&seed); let mut gas_meter = GasMeter::with_limit(50_000, 1); + + let mut return_buf = Vec::new(); execute( - CODE_RANDOM_SEED, + CODE_RANDOM, &[], - &mut Vec::new(), + &mut return_buf, &mut mock_ext, &mut gas_meter, ) .unwrap(); + + // The mock ext just returns the same data that was passed as the subject. + assert_eq!( + &return_buf, + &hex!("000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F") + ); } const CODE_DEPOSIT_EVENT: &str = r#" diff --git a/substrate/srml/contract/src/wasm/runtime.rs b/substrate/srml/contract/src/wasm/runtime.rs index b6f857269e..873464c5be 100644 --- a/substrate/srml/contract/src/wasm/runtime.rs +++ b/substrate/srml/contract/src/wasm/runtime.rs @@ -527,9 +527,19 @@ define_env!(Env, , Ok(()) }, - // Load the latest block RNG seed into the scratch buffer - ext_random_seed(ctx) => { - ctx.scratch_buf = ctx.ext.random_seed().encode(); + // Stores the random number for the current block for the given subject into the scratch + // buffer. + // + // The data is encoded as T::Hash. The current contents of the scratch buffer are + // overwritten. + ext_random(ctx, subject_ptr: u32, subject_len: u32) => { + // The length of a subject can't exceed `max_subject_len`. + if subject_len > ctx.schedule.max_subject_len { + return Err(sandbox::HostError); + } + + let subject_buf = read_sandbox_memory(ctx, subject_ptr, subject_len)?; + ctx.scratch_buf = ctx.ext.random(&subject_buf).encode(); Ok(()) },