Skip to content

Commit

Permalink
Add support for new seal_random API (#734)
Browse files Browse the repository at this point in the history
* [env] [lang] Add support for new `seal_random` API

* Don't support old seal_random
  • Loading branch information
athei authored Mar 19, 2021
1 parent a9ab40d commit 513cb76
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
13 changes: 11 additions & 2 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ where
})
}

/// Returns a random hash seed.
/// Returns a random hash seed and the block number since which it was determinable
/// by chain observers.
///
/// # Note
///
Expand All @@ -493,7 +494,15 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn random<T>(subject: &[u8]) -> Result<T::Hash>
///
/// # 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<T>(subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
where
T: Environment,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`random`][`crate::random`]
fn random<T>(&mut self, subject: &[u8]) -> Result<T::Hash>
fn random<T>(&mut self, subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
where
T: Environment;
}
8 changes: 3 additions & 5 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,11 @@ impl TypedEnvBackend for EnvInstance {
self.transfer_impl::<T>(&destination, value)
}

fn random<T>(&mut self, subject: &[u8]) -> Result<T::Hash>
fn random<T>(&mut self, subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
where
T: Environment,
{
self.current_block()
.expect(UNITIALIZED_EXEC_CONTEXT)
.random::<T>(subject)
.map_err(Into::into)
let block = self.current_block().expect(UNITIALIZED_EXEC_CONTEXT);
Ok((block.random::<T>(subject)?, block.number::<T>()?))
}
}
16 changes: 10 additions & 6 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,6 @@ mod sys {

pub fn seal_set_rent_allowance(value_ptr: Ptr32<[u8]>, value_len: u32);

pub fn seal_random(
subject_ptr: Ptr32<[u8]>,
subject_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
pub fn seal_println(str_ptr: Ptr32<[u8]>, str_len: u32);

pub fn seal_hash_keccak_256(
Expand All @@ -337,6 +331,16 @@ mod sys {
output_ptr: Ptr32Mut<[u8]>,
);
}

#[link(wasm_import_module = "seal1")]
extern "C" {
pub fn seal_random(
subject_ptr: Ptr32<[u8]>,
subject_len: u32,
output_ptr: Ptr32Mut<[u8]>,
output_len_ptr: Ptr32Mut<u32>,
);
}
}

fn extract_from_slice(output: &mut &mut [u8], new_len: usize) {
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ impl TypedEnvBackend for EnvInstance {
scale::Decode::decode(&mut &output[..]).map_err(Into::into)
}

fn random<T>(&mut self, subject: &[u8]) -> Result<T::Hash>
fn random<T>(&mut self, subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
where
T: Environment,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
/// # Note
///
/// For more details visit: [`ink_env::random`]
pub fn random(self, subject: &[u8]) -> T::Hash {
pub fn random(self, subject: &[u8]) -> (T::Hash, T::BlockNumber) {
ink_env::random::<T>(subject).expect("couldn't decode randomized hash")
}

Expand Down

0 comments on commit 513cb76

Please sign in to comment.