Unverified Commit 513cb761 authored by Alexander Theißen's avatar Alexander Theißen Committed by GitHub
Browse files

Add support for new `seal_random` API (#734)

* [env] [lang] Add support for new `seal_random` API

* Don't support old seal_random
parent a9ab40db
Pipeline #129538 canceled with stages
in 18 minutes and 55 seconds
......@@ -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
///
......@@ -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,
{
......
......@@ -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;
}
......@@ -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>()?))
}
}
......@@ -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(
......@@ -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) {
......
......@@ -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,
{
......
......@@ -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")
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment