Skip to content

Commit

Permalink
Merge fbd27ac into 1228905
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Aug 16, 2019
2 parents 1228905 + fbd27ac commit 0d06730
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
30 changes: 14 additions & 16 deletions core/src/env/srml/srml_only/impls.rs
Expand Up @@ -173,22 +173,21 @@ where
) -> Result<(), CallError> {
let callee = callee.encode();
let value = value.encode();
unsafe {
let success = sys::ext_call(
let result = unsafe {
sys::ext_call(
callee.as_ptr() as u32,
callee.len() as u32,
gas,
value.as_ptr() as u32,
value.len() as u32,
input_data.as_ptr() as u32,
input_data.len() as u32,
);
if success == 0 {
Ok(())
} else {
Err(CallError)
}
)
};
if result != 0 {
return Err(CallError)
}
Ok(())
}

fn call_evaluate<U: Decode>(
Expand All @@ -199,21 +198,20 @@ where
) -> Result<U, CallError> {
let callee = callee.encode();
let value = value.encode();
unsafe {
let success = sys::ext_call(
let result = unsafe {
sys::ext_call(
callee.as_ptr() as u32,
callee.len() as u32,
gas,
value.as_ptr() as u32,
value.len() as u32,
input_data.as_ptr() as u32,
input_data.len() as u32,
);
if success == 0 {
U::decode(&mut &read_scratch_buffer()[..]).map_err(|_| CallError)
} else {
Err(CallError)
}
)
};
if result != 0 {
return Err(CallError)
}
U::decode(&mut &read_scratch_buffer()[..]).map_err(|_| CallError)
}
}
2 changes: 2 additions & 0 deletions core/src/env/srml/srml_only/sys.rs
Expand Up @@ -32,6 +32,8 @@ extern "C" {
) -> u32;

/// Calls a remote smart contract.
///
/// Eventually returned data is put into the scratch buffer.
pub fn ext_call(
callee_ptr: u32,
callee_len: u32,
Expand Down
43 changes: 40 additions & 3 deletions core/src/env/test_env.rs
Expand Up @@ -61,7 +61,7 @@ pub struct RawCallData {
/// Decoded call data of recorded external calls.
pub struct CallData<E>
where
E: crate::env::EnvTypes,
E: EnvTypes,
{
pub callee: E::AccountId,
pub gas: u64,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub struct TestEnvData {
/// The total transferred value.
value_transferred: Vec<u8>,
/// The recorded external calls.
calls: Vec<CallData>,
calls: Vec<RawCallData>,
/// The expected return data of the next external call.
call_return: Vec<u8>,
/// Returned data.
Expand Down Expand Up @@ -341,7 +341,7 @@ impl TestEnvData {

/// Records a new external call.
pub fn add_call(&mut self, callee: &[u8], gas: u64, value: &[u8], input_data: &[u8]) {
let new_call = CallData {
let new_call = RawCallData {
callee: callee.to_vec(),
gas,
value: value.to_vec(),
Expand All @@ -350,6 +350,16 @@ impl TestEnvData {
self.calls.push(new_call);
}

/// Returns an iterator over all recorded external calls.
pub fn external_calls(&self) -> impl DoubleEndedIterator<Item = &RawCallData> {
self.calls.iter()
}

/// Set the expected return data of the next external call.
pub fn set_return_data(&mut self, return_data: &[u8]) {
self.return_data = return_data.to_vec();
}

/// Returns the latest returned data.
pub fn returned_data(&self) -> &[u8] {
&self.return_data
Expand Down Expand Up @@ -501,6 +511,12 @@ where
.with(|test_env| test_env.borrow_mut().set_input(input_bytes.to_vec()))
}

/// Sets the expected return data for the next external call.
pub fn set_return_data(expected_return_data: &[u8]) {
TEST_ENV_DATA
.with(|test_env| test_env.borrow_mut().set_return_data(expected_return_data))
}

impl_env_setters_for_test_env!(
(set_address, address, T::AccountId),
(set_balance, balance, T::Balance),
Expand All @@ -522,6 +538,27 @@ where
})
}

/// Returns an iterator over all emitted events.
pub fn external_calls() -> impl DoubleEndedIterator<Item = CallData<T>> {
TEST_ENV_DATA.with(|test_env| {
test_env
.borrow()
.external_calls()
.map(|raw_call_data| {
CallData {
callee: Decode::decode(&mut &raw_call_data.callee[..])
.expect("invalid encoded callee"),
gas: raw_call_data.gas,
value: Decode::decode(&mut &raw_call_data.value[..])
.expect("invalid encoded value"),
input_data: raw_call_data.input_data.clone(),
}
})
.collect::<Vec<_>>()
.into_iter()
})
}

/// Returns an iterator over all dispatched calls.
pub fn dispatched_calls() -> impl DoubleEndedIterator<Item = T::Call> {
TEST_ENV_DATA.with(|test_env| {
Expand Down

0 comments on commit 0d06730

Please sign in to comment.