Commit 3feae15d authored by satellitex's avatar satellitex Committed by Hero Bird
Browse files

Add ExtCurrentBlockNumber and BlockNumber EnvTypes. (#132)



* add core gets ext_current_block and BlockNumber Types

* add lang gets ext_current_block and BlockNumber Types

* add model ext_current_block

* Update lang/src/gen/build.rs
Co-Authored-By: Andrew Jones's avatarAndrew Jones <ascjones@gmail.com>

* add test_env current_block clear;
parent 9589899b
...@@ -89,6 +89,7 @@ where ...@@ -89,6 +89,7 @@ where
type Balance = <T as EnvTypes>::Balance; type Balance = <T as EnvTypes>::Balance;
type Hash = <T as EnvTypes>::Hash; type Hash = <T as EnvTypes>::Hash;
type Moment = <T as EnvTypes>::Moment; type Moment = <T as EnvTypes>::Moment;
type BlockNumber = <T as EnvTypes>::BlockNumber;
} }
macro_rules! impl_getters_for_srml_env { macro_rules! impl_getters_for_srml_env {
...@@ -122,6 +123,7 @@ where ...@@ -122,6 +123,7 @@ where
(caller, ext_caller, <Self as EnvTypes>::AccountId), (caller, ext_caller, <Self as EnvTypes>::AccountId),
(random_seed, ext_random_seed, <Self as EnvTypes>::Hash), (random_seed, ext_random_seed, <Self as EnvTypes>::Hash),
(now, ext_now, <Self as EnvTypes>::Moment), (now, ext_now, <Self as EnvTypes>::Moment),
(current_block, ext_current_block, <Self as EnvTypes>::BlockNumber),
(gas_price, ext_gas_price, <Self as EnvTypes>::Balance), (gas_price, ext_gas_price, <Self as EnvTypes>::Balance),
(gas_left, ext_gas_left, <Self as EnvTypes>::Balance), (gas_left, ext_gas_left, <Self as EnvTypes>::Balance),
(value_transferred, ext_value_transferred, <Self as EnvTypes>::Balance) (value_transferred, ext_value_transferred, <Self as EnvTypes>::Balance)
......
...@@ -114,4 +114,7 @@ extern "C" { ...@@ -114,4 +114,7 @@ extern "C" {
/// Load the latest block timestamp into the scratch buffer. /// Load the latest block timestamp into the scratch buffer.
pub fn ext_now(); pub fn ext_now();
/// Load the latest block number into the scratch buffer.
pub fn ext_current_block();
} }
...@@ -34,6 +34,7 @@ impl EnvTypes for DefaultSrmlTypes { ...@@ -34,6 +34,7 @@ impl EnvTypes for DefaultSrmlTypes {
type Balance = Balance; type Balance = Balance;
type Hash = Hash; type Hash = Hash;
type Moment = Moment; type Moment = Moment;
type BlockNumber = BlockNumber;
} }
/// The default SRML address type. /// The default SRML address type.
...@@ -79,3 +80,6 @@ impl<'a> TryFrom<&'a [u8]> for Hash { ...@@ -79,3 +80,6 @@ impl<'a> TryFrom<&'a [u8]> for Hash {
/// The default SRML moment type. /// The default SRML moment type.
pub type Moment = u64; pub type Moment = u64;
/// The default SRML blocknumber type.
pub type BlockNumber = u64;
...@@ -150,6 +150,12 @@ pub struct TestEnvData { ...@@ -150,6 +150,12 @@ pub struct TestEnvData {
/// ///
/// The current timestamp can be adjusted by `TestEnvData::set_now`. /// The current timestamp can be adjusted by `TestEnvData::set_now`.
now: Vec<u8>, now: Vec<u8>,
/// The current block number for the next contract invocation.
///
/// # Note
///
/// The current current block number can be adjusted by `TestEnvData::set_current_block`.
current_block: Vec<u8>,
/// The expected return data of the next contract invocation. /// The expected return data of the next contract invocation.
/// ///
/// # Note /// # Note
...@@ -180,6 +186,7 @@ impl Default for TestEnvData { ...@@ -180,6 +186,7 @@ impl Default for TestEnvData {
input: Vec::new(), input: Vec::new(),
random_seed: Vec::new(), random_seed: Vec::new(),
now: Vec::new(), now: Vec::new(),
current_block: Vec::new(),
expected_return: Vec::new(), expected_return: Vec::new(),
total_reads: Cell::new(0), total_reads: Cell::new(0),
total_writes: 0, total_writes: 0,
...@@ -201,6 +208,7 @@ impl TestEnvData { ...@@ -201,6 +208,7 @@ impl TestEnvData {
self.input.clear(); self.input.clear();
self.random_seed.clear(); self.random_seed.clear();
self.now.clear(); self.now.clear();
self.current_block.clear();
self.expected_return.clear(); self.expected_return.clear();
self.total_reads.set(0); self.total_reads.set(0);
self.total_writes = 0; self.total_writes = 0;
...@@ -281,6 +289,11 @@ impl TestEnvData { ...@@ -281,6 +289,11 @@ impl TestEnvData {
self.now = timestamp; self.now = timestamp;
} }
/// Sets the current block number for the next contract invocation.
pub fn set_current_block(&mut self, current_block: Vec<u8>) {
self.current_block = current_block;
}
/// Returns an iterator over all emitted events. /// Returns an iterator over all emitted events.
pub fn emitted_events(&self) -> impl Iterator<Item = &[u8]> { pub fn emitted_events(&self) -> impl Iterator<Item = &[u8]> {
self.events self.events
...@@ -350,6 +363,10 @@ impl TestEnvData { ...@@ -350,6 +363,10 @@ impl TestEnvData {
self.now.clone() self.now.clone()
} }
pub fn current_block(&self) -> Vec<u8> {
self.current_block.clone()
}
pub fn gas_price(&self) -> Vec<u8> { pub fn gas_price(&self) -> Vec<u8> {
self.gas_price.clone() self.gas_price.clone()
} }
...@@ -438,7 +455,8 @@ impl<T> TestEnv<T> where T: EnvTypes { ...@@ -438,7 +455,8 @@ impl<T> TestEnv<T> where T: EnvTypes {
(set_balance, balance, T::Balance), (set_balance, balance, T::Balance),
(set_caller, caller, T::AccountId), (set_caller, caller, T::AccountId),
(set_random_seed, random_seed, T::Hash), (set_random_seed, random_seed, T::Hash),
(set_now, now, T::Moment) (set_now, now, T::Moment),
(set_current_block, current_block, T::BlockNumber)
); );
/// Returns an iterator over all emitted events. /// Returns an iterator over all emitted events.
...@@ -473,6 +491,7 @@ where ...@@ -473,6 +491,7 @@ where
type Balance = <T as EnvTypes>::Balance; type Balance = <T as EnvTypes>::Balance;
type Hash = <T as EnvTypes>::Hash; type Hash = <T as EnvTypes>::Hash;
type Moment = <T as EnvTypes>::Moment; type Moment = <T as EnvTypes>::Moment;
type BlockNumber = <T as EnvTypes>::BlockNumber;
} }
impl<T> Env for TestEnv<T> where T: EnvTypes impl<T> Env for TestEnv<T> where T: EnvTypes
...@@ -484,6 +503,7 @@ impl<T> Env for TestEnv<T> where T: EnvTypes ...@@ -484,6 +503,7 @@ impl<T> Env for TestEnv<T> where T: EnvTypes
(input, Vec<u8>), (input, Vec<u8>),
(random_seed, T::Hash), (random_seed, T::Hash),
(now, T::Moment), (now, T::Moment),
(current_block, T::BlockNumber),
(gas_price, T::Balance), (gas_price, T::Balance),
(gas_left, T::Balance), (gas_left, T::Balance),
(value_transferred, T::Balance) (value_transferred, T::Balance)
......
...@@ -31,6 +31,8 @@ pub trait EnvTypes { ...@@ -31,6 +31,8 @@ pub trait EnvTypes {
type Hash: Codec + Clone + PartialEq + Eq; type Hash: Codec + Clone + PartialEq + Eq;
/// The type of timestamps. /// The type of timestamps.
type Moment: Codec + Clone + PartialEq + Eq; type Moment: Codec + Clone + PartialEq + Eq;
/// The type of block number.
type BlockNumber: Codec + Clone + PartialEq + Eq;
} }
#[cfg(feature = "test-env")] #[cfg(feature = "test-env")]
...@@ -44,6 +46,8 @@ pub trait EnvTypes { ...@@ -44,6 +46,8 @@ pub trait EnvTypes {
type Hash: Codec + Clone + PartialEq + Eq + core::fmt::Debug; type Hash: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
/// The type of timestamps. /// The type of timestamps.
type Moment: Codec + Clone + PartialEq + Eq + core::fmt::Debug; type Moment: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
/// The type of block number.
type BlockNumber: Codec + Clone + PartialEq + Eq + core::fmt::Debug;
} }
/// Types implementing this can act as contract storage. /// Types implementing this can act as contract storage.
...@@ -93,6 +97,9 @@ pub trait Env: EnvTypes { ...@@ -93,6 +97,9 @@ pub trait Env: EnvTypes {
/// Get the timestamp of the latest block. /// Get the timestamp of the latest block.
fn now() -> <Self as EnvTypes>::Moment; fn now() -> <Self as EnvTypes>::Moment;
/// Get the block number of the latest block.
fn current_block() -> <Self as EnvTypes>::BlockNumber;
/// Returns the current gas price. /// Returns the current gas price.
fn gas_price() -> <Self as EnvTypes>::Balance; fn gas_price() -> <Self as EnvTypes>::Balance;
......
...@@ -267,6 +267,8 @@ pub enum PrimitiveTypeDescription { ...@@ -267,6 +267,8 @@ pub enum PrimitiveTypeDescription {
Hash, Hash,
/// The SRML moment type. /// The SRML moment type.
Moment, Moment,
/// The SRML block number type.
BlockNumber,
} }
impl TryFrom<&syn::TypePath> for PrimitiveTypeDescription { impl TryFrom<&syn::TypePath> for PrimitiveTypeDescription {
...@@ -291,6 +293,7 @@ impl TryFrom<&syn::TypePath> for PrimitiveTypeDescription { ...@@ -291,6 +293,7 @@ impl TryFrom<&syn::TypePath> for PrimitiveTypeDescription {
"Balance" => Ok(PrimitiveTypeDescription::Balance), "Balance" => Ok(PrimitiveTypeDescription::Balance),
"Hash" => Ok(PrimitiveTypeDescription::Hash), "Hash" => Ok(PrimitiveTypeDescription::Hash),
"Moment" => Ok(PrimitiveTypeDescription::Moment), "Moment" => Ok(PrimitiveTypeDescription::Moment),
"BlockNumber" => Ok(PrimitiveTypeDescription::BlockNumber),
unsupported => { unsupported => {
bail!( bail!(
ty, ty,
...@@ -583,6 +586,10 @@ mod tests { ...@@ -583,6 +586,10 @@ mod tests {
parse_quote!(Moment), parse_quote!(Moment),
Primitive(PrimitiveTypeDescription::Moment), Primitive(PrimitiveTypeDescription::Moment),
); );
assert_eq_type_description(
parse_quote!(BlockNumber),
Primitive(PrimitiveTypeDescription::BlockNumber),
);
} }
#[test] #[test]
......
...@@ -60,6 +60,7 @@ fn codegen_for_contract_env(tokens: &mut TokenStream2, contract: &hir::Contract) ...@@ -60,6 +60,7 @@ fn codegen_for_contract_env(tokens: &mut TokenStream2, contract: &hir::Contract)
pub type Balance = <ContractEnv<#env_types> as EnvTypes>::Balance; pub type Balance = <ContractEnv<#env_types> as EnvTypes>::Balance;
pub type Hash = <ContractEnv<#env_types> as EnvTypes>::Hash; pub type Hash = <ContractEnv<#env_types> as EnvTypes>::Hash;
pub type Moment = <ContractEnv<#env_types> as EnvTypes>::Moment; pub type Moment = <ContractEnv<#env_types> as EnvTypes>::Moment;
pub type BlockNumber = <ContractEnv<#env_types> as EnvTypes>::BlockNumber;
} }
use types::{ use types::{
...@@ -67,6 +68,7 @@ fn codegen_for_contract_env(tokens: &mut TokenStream2, contract: &hir::Contract) ...@@ -67,6 +68,7 @@ fn codegen_for_contract_env(tokens: &mut TokenStream2, contract: &hir::Contract)
Balance, Balance,
Hash, Hash,
Moment, Moment,
BlockNumber,
}; };
}) })
} }
......
...@@ -76,6 +76,7 @@ fn contract_compiles() { ...@@ -76,6 +76,7 @@ fn contract_compiles() {
pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance; pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance;
pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash; pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash;
pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment; pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment;
pub type BlockNumber = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::BlockNumber;
} }
use types::{ use types::{
...@@ -83,6 +84,7 @@ fn contract_compiles() { ...@@ -83,6 +84,7 @@ fn contract_compiles() {
Balance, Balance,
Hash, Hash,
Moment, Moment,
BlockNumber,
}; };
ink_model::state! { ink_model::state! {
......
...@@ -56,6 +56,7 @@ fn contract_compiles() { ...@@ -56,6 +56,7 @@ fn contract_compiles() {
pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance; pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance;
pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash; pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash;
pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment; pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment;
pub type BlockNumber = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::BlockNumber;
} }
use types::{ use types::{
...@@ -63,6 +64,7 @@ fn contract_compiles() { ...@@ -63,6 +64,7 @@ fn contract_compiles() {
Balance, Balance,
Hash, Hash,
Moment, Moment,
BlockNumber,
}; };
ink_model::state! { ink_model::state! {
......
...@@ -62,6 +62,7 @@ fn contract_compiles() { ...@@ -62,6 +62,7 @@ fn contract_compiles() {
pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance; pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance;
pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash; pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash;
pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment; pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment;
pub type BlockNumber = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::BlockNumber;
} }
use types::{ use types::{
...@@ -69,6 +70,7 @@ fn contract_compiles() { ...@@ -69,6 +70,7 @@ fn contract_compiles() {
Balance, Balance,
Hash, Hash,
Moment, Moment,
BlockNumber,
}; };
ink_model::state! { ink_model::state! {
......
...@@ -46,6 +46,7 @@ fn contract_compiles() { ...@@ -46,6 +46,7 @@ fn contract_compiles() {
pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance; pub type Balance = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Balance;
pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash; pub type Hash = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Hash;
pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment; pub type Moment = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::Moment;
pub type BlockNumber = <ContractEnv<DefaultSrmlTypes> as EnvTypes>::BlockNumber;
} }
use types::{ use types::{
...@@ -53,6 +54,7 @@ fn contract_compiles() { ...@@ -53,6 +54,7 @@ fn contract_compiles() {
Balance, Balance,
Hash, Hash,
Moment, Moment,
BlockNumber,
}; };
ink_model::state! { ink_model::state! {
......
...@@ -176,4 +176,8 @@ impl<T: Env> EnvHandler<T> { ...@@ -176,4 +176,8 @@ impl<T: Env> EnvHandler<T> {
pub fn now(&self) -> T::Moment { pub fn now(&self) -> T::Moment {
T::now() T::now()
} }
pub fn current_block(&self) -> T::BlockNumber {
T::current_block()
}
} }
Markdown is supported
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