Add ExtCurrentBlockNumber in SRML::Contract (#3047)

* add block number

* add ext_current_block

* add ext_current_block test

* Update srml/contracts/src/wasm/mod.rs

Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com>

* Update srml/contracts/src/wasm/mod.rs

Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com>

* change valid comment

* use BlockNumberOf<T>

* Update srml/contracts/src/exec.rs

Co-Authored-By: André Silva <andre.beat@gmail.com>

* return types copy, not reference

* version up spec_version and impl_version

* change method name current_block -> block_number

* Update srml/contracts/src/exec.rs

Co-Authored-By: Sergei Pepyakin <s.pepyakin@gmail.com>
This commit is contained in:
satellitex
2019-07-10 21:42:36 +09:00
committed by Sergei Pepyakin
parent 0366f7b35c
commit e9d41feb1c
3 changed files with 84 additions and 0 deletions
+68
View File
@@ -291,6 +291,8 @@ mod tests {
fn rent_allowance(&self) -> u64 {
self.rent_allowance
}
fn block_number(&self) -> u64 { 121 }
}
fn execute<E: Ext>(
@@ -1342,4 +1344,70 @@ mod tests {
Err("during execution"),
);
}
/// calls `ext_block_number`, loads the current block number from the scratch buffer and
/// compares it with the constant 121.
const CODE_BLOCK_NUMBER: &str = r#"
(module
(import "env" "ext_block_number" (func $ext_block_number))
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_copy" (func $ext_scratch_copy (param i32 i32 i32)))
(import "env" "memory" (memory 1 1))
(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)
(func (export "call")
;; This stores the block height in the scratch buffer
(call $ext_block_number)
;; assert $ext_scratch_size == 8
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.const 8)
)
)
;; copy contents of the scratch buffer into the contract's memory.
(call $ext_scratch_copy
(i32.const 8) ;; Pointer in memory to the place where to copy.
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 8) ;; Count of bytes to copy.
)
;; assert that contents of the buffer is equal to the i64 value of 121.
(call $assert
(i64.eq
(i64.load
(i32.const 8)
)
(i64.const 121)
)
)
)
(func (export "deploy"))
)
"#;
#[test]
fn block_number() {
let mut mock_ext = MockExt::default();
execute(
CODE_BLOCK_NUMBER,
&[],
&mut Vec::new(),
&mut mock_ext,
&mut GasMeter::with_limit(50_000, 1),
)
.unwrap();
}
}
@@ -701,6 +701,13 @@ define_env!(Env, <E: Ext>,
}
Ok(())
},
// Stores the current block number of the current contract into the scratch buffer.
ext_block_number(ctx) => {
ctx.scratch_buf.clear();
ctx.ext.block_number().encode_to(&mut ctx.scratch_buf);
Ok(())
},
);
/// Finds duplicates in a given vector.