Skip to content

Commit

Permalink
Merge c4b7917 into 860048d
Browse files Browse the repository at this point in the history
  • Loading branch information
lsaether committed May 14, 2019
2 parents 860048d + c4b7917 commit 4e17ed0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/env/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub type Balance = <ContractEnv as EnvTypes>::Balance;
/// The environmental hash type.
pub type Hash = <ContractEnv as EnvTypes>::Hash;

/// The environmental moment type.
pub type Moment = <ContractEnv as EnvTypes>::Moment;

/// Returns the address of the current smart contract.
pub fn address() -> AccountId {
ContractEnv::address()
Expand All @@ -59,6 +62,11 @@ pub fn random_seed() -> Hash {
ContractEnv::random_seed()
}

/// Returns the timestamp of the latest block.
pub fn now() -> Moment {
ContractEnv::now()
}

/// Returns the current smart contract exection back to the caller
/// and return the given encoded value.
///
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 @@ -24,6 +24,7 @@ pub use self::types::{
Balance,
DefaultSrmlTypes,
Hash,
Moment,
};

#[cfg(not(feature = "test-env"))]
Expand Down
8 changes: 8 additions & 0 deletions core/src/env/srml/srml_only/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ where
type AccountId = <T as EnvTypes>::AccountId;
type Balance = <T as EnvTypes>::Balance;
type Hash = <T as EnvTypes>::Hash;
type Moment = <T as EnvTypes>::Moment;
}

impl<T> EnvStorage for SrmlEnv<T>
Expand Down Expand Up @@ -161,6 +162,13 @@ where
.expect("we expect to always receive a correctly sized buffer here")
}

fn now() -> <Self as EnvTypes>::Moment {
unsafe { sys::ext_now() };
Decode::decode(&mut &Self::read_scratch_buffer()[..])
.ok_or("now received an incorrectly sized buffer from SRML")
.expect("we expect to receive a correctly sized buffer here")
}

unsafe fn r#return(data: &[u8]) -> ! {
sys::ext_return(data.as_ptr() as u32, data.len() as u32);
}
Expand Down
4 changes: 4 additions & 0 deletions core/src/env/srml/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ impl EnvTypes for DefaultSrmlTypes {
type AccountId = self::AccountId;
type Balance = self::Balance;
type Hash = self::Hash;
type Moment = self::Moment;
}

/// The default SRML address type.
Expand Down Expand Up @@ -74,3 +75,6 @@ impl<'a> TryFrom<&'a [u8]> for Hash {
Ok(Hash(hash))
}
}

/// The default SRML moment type.
pub type Moment = u64;
30 changes: 30 additions & 0 deletions core/src/env/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ pub struct TestEnvData {
///
/// The current random seed can be adjusted by `TestEnvData::set_random_seed`.
random_seed: srml::Hash,
/// The timestamp for the next contract invocation.
///
/// # Note
///
/// The current timestamp can be adjusted by `TestEnvData::set_now`.
now: srml::Moment,
/// The expected return data of the next contract invocation.
///
/// # Note
Expand All @@ -163,6 +169,7 @@ impl Default for TestEnvData {
caller: srml::AccountId::from([0x0; 32]),
input: Vec::new(),
random_seed: srml::Hash::from([0x0; 32]),
now: 0,
expected_return: Vec::new(),
total_reads: Cell::new(0),
total_writes: 0,
Expand All @@ -180,6 +187,7 @@ impl TestEnvData {
self.caller = srml::AccountId::from([0; 32]);
self.input.clear();
self.random_seed = srml::Hash::from([0; 32]);
self.now = 0;
self.expected_return.clear();
self.total_reads.set(0);
self.total_writes = 0;
Expand Down Expand Up @@ -252,6 +260,11 @@ impl TestEnvData {
self.random_seed = random_seed_hash;
}

/// Sets the timestamp for the next contract invocation.
pub fn set_now(&mut self, timestamp: srml::Moment) {
self.now = timestamp;
}

/// Returns an iterator over all emitted events.
pub fn emitted_events(&self) -> impl Iterator<Item = &[u8]> {
self.events.iter().map(|event_data| event_data.as_bytes())
Expand Down Expand Up @@ -323,6 +336,11 @@ impl TestEnvData {
self.random_seed
}

/// Returns the timestamp for the contract invocation.
pub fn now(&self) -> srml::Moment {
self.now
}

/// Returns the data to the internal caller.
///
/// # Note
Expand Down Expand Up @@ -422,6 +440,12 @@ impl TestEnv {
.with(|test_env| test_env.borrow_mut().set_random_seed(random_seed_bytes))
}

/// Sets the timestamp for the next contract invocation.
pub fn set_now(timestamp: srml::Moment) {
TEST_ENV_DATA
.with(|test_env| test_env.borrow_mut().set_now(timestamp))
}

/// Returns an iterator over all emitted events.
pub fn emitted_events() -> impl IntoIterator<Item = Vec<u8>> {
TEST_ENV_DATA.with(|test_env| {
Expand All @@ -440,6 +464,7 @@ impl EnvTypes for TestEnv {
type AccountId = srml::AccountId;
type Balance = srml::Balance;
type Hash = srml::Hash;
type Moment = srml::Moment;
}

impl EnvStorage for TestEnv {
Expand Down Expand Up @@ -502,6 +527,11 @@ where
TEST_ENV_DATA.with(|test_env| test_env.borrow().random_seed())
}

fn now() -> <Self as EnvTypes>::Moment {
log::debug!(target: TEST_ENV_LOG_TARGET, "TestEnv::now()",);
TEST_ENV_DATA.with(|test_env| test_env.borrow().now())
}

unsafe fn r#return(data: &[u8]) -> ! {
log::debug!(
target: TEST_ENV_LOG_TARGET,
Expand Down
5 changes: 5 additions & 0 deletions core/src/env/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub trait EnvTypes {
type Balance: Codec;
/// The type of hash.
type Hash: Codec;
/// The type of timestamps.
type Moment: Codec;
}

/// Types implementing this can act as contract storage.
Expand Down Expand Up @@ -74,6 +76,9 @@ pub trait Env: EnvTypes + EnvStorage {
/// Get the random seed from the latest block.
fn random_seed() -> <Self as EnvTypes>::Hash;

/// Get the timestamp of the latest block.
fn now() -> <Self as EnvTypes>::Moment;

/// Returns from the contract execution with the given value.
///
/// # Safety
Expand Down
5 changes: 5 additions & 0 deletions model/src/exec_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ impl EnvHandler {
pub fn random_seed(&self) -> env::Hash {
env::random_seed()
}

/// Returns the timestamp of the latest block.
pub fn now(&self) -> env::Moment {
env::now()
}
}

0 comments on commit 4e17ed0

Please sign in to comment.