srml-contracts: Add ability to read substrate's storage (#4108)

* Add `ext_get_runtime_storage`

* Implement testing for ext_get_runtime_storage

* Add integration test.

* Update spec version

* Fix the doc

* Save memory allocation.
This commit is contained in:
Sergei Pepyakin
2019-11-18 13:37:15 +01:00
committed by GitHub
parent 0d658ab286
commit 33b3865858
4 changed files with 274 additions and 3 deletions
+127
View File
@@ -151,6 +151,7 @@ impl<'a, T: Trait> crate::exec::Vm<T> for WasmVm<'a> {
mod tests {
use super::*;
use std::collections::HashMap;
use std::cell::RefCell;
use primitives::H256;
use crate::exec::{Ext, StorageKey, ExecError, ExecReturnValue, STATUS_SUCCESS};
use crate::gas::{Gas, GasMeter};
@@ -199,6 +200,17 @@ mod tests {
// (topics, data)
events: Vec<(Vec<H256>, Vec<u8>)>,
next_account_id: u64,
/// Runtime storage keys works the following way.
///
/// - If the test code requests a value and it doesn't exist in this storage map then a
/// panic happens.
/// - If the value does exist it is returned and then removed from the map. So a panic
/// happens if the same value is requested for the second time.
///
/// This behavior is used to prevent mixing up an access to unexpected location and empty
/// cell.
runtime_storage_keys: RefCell<HashMap<Vec<u8>, Option<Vec<u8>>>>,
}
impl Ext for MockExt {
@@ -305,6 +317,19 @@ mod tests {
fn block_number(&self) -> u64 { 121 }
fn max_value_size(&self) -> u32 { 16_384 }
fn get_runtime_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
let opt_value = self.runtime_storage_keys
.borrow_mut()
.remove(key);
opt_value.unwrap_or_else(||
panic!(
"{:?} doesn't exist. values that do exist {:?}",
key,
self.runtime_storage_keys
)
)
}
}
impl Ext for &mut MockExt {
@@ -389,6 +414,9 @@ mod tests {
fn max_value_size(&self) -> u32 {
(**self).max_value_size()
}
fn get_runtime_storage(&self, key: &[u8]) -> Option<Vec<u8>> {
(**self).get_runtime_storage(key)
}
}
fn execute<E: Ext>(
@@ -1634,4 +1662,103 @@ mod tests {
assert_eq!(output, ExecReturnValue { status: 17, data: hex!("5566778899").to_vec() });
assert!(!output.is_success());
}
const CODE_GET_RUNTIME_STORAGE: &str = r#"
(module
(import "env" "ext_get_runtime_storage"
(func $ext_get_runtime_storage (param i32 i32) (result i32))
)
(import "env" "ext_scratch_size" (func $ext_scratch_size (result i32)))
(import "env" "ext_scratch_read" (func $ext_scratch_read (param i32 i32 i32)))
(import "env" "ext_scratch_write" (func $ext_scratch_write (param i32 i32)))
(import "env" "memory" (memory 1 1))
(func (export "deploy"))
(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)
(func $call (export "call")
;; Load runtime storage for the first key and assert that it exists.
(call $assert
(i32.eq
(call $ext_get_runtime_storage
(i32.const 16)
(i32.const 4)
)
(i32.const 0)
)
)
;; assert $ext_scratch_size == 4
(call $assert
(i32.eq
(call $ext_scratch_size)
(i32.const 4)
)
)
;; copy contents of the scratch buffer into the contract's memory.
(call $ext_scratch_read
(i32.const 4) ;; Pointer in memory to the place where to copy.
(i32.const 0) ;; Offset from the start of the scratch buffer.
(i32.const 4) ;; Count of bytes to copy.
)
;; assert that contents of the buffer is equal to the i32 value of 0x14144020.
(call $assert
(i32.eq
(i32.load
(i32.const 4)
)
(i32.const 0x14144020)
)
)
;; Load the second key and assert that it doesn't exist.
(call $assert
(i32.eq
(call $ext_get_runtime_storage
(i32.const 20)
(i32.const 4)
)
(i32.const 1)
)
)
)
;; The first key, 4 bytes long.
(data (i32.const 16) "\01\02\03\04")
;; The second key, 4 bytes long.
(data (i32.const 20) "\02\03\04\05")
)
"#;
#[test]
fn get_runtime_storage() {
let mut gas_meter = GasMeter::with_limit(50_000, 1);
let mock_ext = MockExt::default();
// "\01\02\03\04" - Some(0x14144020)
// "\02\03\04\05" - None
*mock_ext.runtime_storage_keys.borrow_mut() = [
([1, 2, 3, 4].to_vec(), Some(0x14144020u32.to_le_bytes().to_vec())),
([2, 3, 4, 5].to_vec().to_vec(), None),
]
.iter()
.cloned()
.collect();
let _ = execute(
CODE_GET_RUNTIME_STORAGE,
vec![],
mock_ext,
&mut gas_meter,
).unwrap();
}
}