Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core,model] Add ext_random_seed runtime call bindings #54

Merged
merged 14 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/env/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub type Address = <ContractEnv as EnvTypes>::Address;
/// The environmental balance type.
pub type Balance = <ContractEnv as EnvTypes>::Balance;

/// The environmental hash type.
pub type Hash = <ContractEnv as EnvTypes>::Hash;

/// Returns the address of the caller of the current smart contract execution.
pub fn caller() -> Address {
ContractEnv::caller()
Expand All @@ -42,7 +45,7 @@ pub fn input() -> Vec<u8> {
}

/// Returns the latest block RNG seed
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
pub fn random_seed() -> Vec<u8> {
pub fn random_seed() -> Hash {
ContractEnv::random_seed()
}

Expand Down
1 change: 1 addition & 0 deletions core/src/env/srml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod types;
pub use self::types::{
Address,
Balance,
Hash,
DefaultSrmlTypes,
};

Expand Down
4 changes: 3 additions & 1 deletion core/src/env/srml/srml_only/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ where
{
type Address = <T as EnvTypes>::Address;
type Balance = <T as EnvTypes>::Balance;
type Hash = <T as EnvTypes>::Hash;
}

impl<T> EnvStorage for SrmlEnv<T>
Expand Down Expand Up @@ -88,6 +89,7 @@ impl<T> Env for SrmlEnv<T>
where
T: EnvTypes,
<T as EnvTypes>::Address: for<'a> From<&'a [u8]>,
<T as EnvTypes>::Hash: for<'a> From<&'a [u8]>,
{
fn caller() -> <Self as EnvTypes>::Address {
unsafe { sys::ext_caller() };
Expand Down Expand Up @@ -116,7 +118,7 @@ where
}
}

fn random_seed() -> Vec<u8> {
fn random_seed() -> <Self as EnvTypes>::Hash {
unsafe { sys::ext_random_seed() };
let size = unsafe { sys::ext_scratch_size() };
let mut value = Vec::new();
Expand Down
15 changes: 15 additions & 0 deletions core/src/env/srml/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct DefaultSrmlTypes;
impl EnvTypes for DefaultSrmlTypes {
type Address = self::Address;
type Balance = self::Balance;
type Hash = self::Hash;
}

/// The default SRML address type.
Expand All @@ -44,3 +45,17 @@ impl<'a> From<&'a [u8]> for Address {

/// The default SRML balance type.
pub type Balance = u64;

/// The default SRML hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode)]
pub struct Hash([u8; 32]);
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved

impl<'a> From<&'a [u8]> for Hash {
Robbepop marked this conversation as resolved.
Show resolved Hide resolved
fn from(bytes: &'a [u8]) -> Self {
assert_eq!(bytes.len(), 32);
let mut array = [0; 32];
let bytes = &bytes[..array.len()]; // panics if not enough data
array.copy_from_slice(bytes);
Hash(array)
}
}
11 changes: 7 additions & 4 deletions core/src/env/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl Default for TestEnvData {
storage: HashMap::new(),
caller: vec![0x0; 32],
input: Vec::new(),
random_seed: Vec::new(),
random_seed: vec![0x0, 32],
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
expected_return: Vec::new(),
total_reads: Cell::new(0),
total_writes: 0,
Expand Down Expand Up @@ -256,8 +256,8 @@ impl TestEnvData {
}

/// Returns the random seed for the contract invocation.
pub fn random_seed(&self) -> Vec<u8> {
self.random_seed.clone()
pub fn random_seed(&self) -> srml::Hash {
srml::Hash::from(self.random_seed.as_slice())
}

/// Returns the data to the internal caller.
Expand Down Expand Up @@ -350,6 +350,7 @@ const TEST_ENV_LOG_TARGET: &str = "test-env";
impl EnvTypes for TestEnv {
type Address = srml::Address;
type Balance = srml::Balance;
type Hash = srml::Hash;
}

impl EnvStorage for TestEnv {
Expand Down Expand Up @@ -385,6 +386,8 @@ impl EnvStorage for TestEnv {
impl Env for TestEnv
where
<Self as EnvTypes>::Address: for<'a> From<&'a [u8]>,
<Self as EnvTypes>::Hash: for<'a> From<&'a [u8]>,

{
fn caller() -> <Self as EnvTypes>::Address {
log::debug!(target: TEST_ENV_LOG_TARGET, "TestEnv::caller()");
Expand All @@ -396,7 +399,7 @@ where
TEST_ENV_DATA.with(|test_env| test_env.borrow().input())
}

fn random_seed() -> Vec<u8> {
fn random_seed() -> <Self as EnvTypes>::Hash {
log::debug!(target: TEST_ENV_LOG_TARGET, "TestEnv::random_seed()",);
TEST_ENV_DATA.with(|test_env| test_env.borrow().random_seed())
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/env/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub trait EnvTypes {
type Address: Codec + PartialEq + Eq;
/// The type of balances.
type Balance: Codec;
/// The type of hash.
type Hash: Codec;
}

/// Types implementing this can act as contract storage.
Expand Down Expand Up @@ -64,7 +66,7 @@ pub trait Env: EnvTypes + EnvStorage {
fn input() -> Vec<u8>;

/// Get the latest block RNG seed
fn random_seed() -> Vec<u8>;
fn random_seed() -> <Self as EnvTypes>::Hash;

/// Returns from the contract execution with the given value.
///
Expand Down