Skip to content

Commit 975966d

Browse files
committed
implment call_invoke and call_evaluate
1 parent f3c3740 commit 975966d

File tree

6 files changed

+69
-61
lines changed

6 files changed

+69
-61
lines changed

core/src/env/srml/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub use self::types::DefaultSrmlTypes;
2424
#[cfg(not(feature = "test-env"))]
2525
pub use self::srml_only::{
2626
sys,
27-
SrmlReturnData,
2827
SrmlEnv,
2928
SrmlEnvStorage,
3029
};

core/src/env/srml/srml_only/impls.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::{
2020
Env,
2121
EnvStorage,
2222
EnvTypes,
23-
EnvReturnData,
2423
},
2524
memory::vec::Vec,
2625
storage::Key,
@@ -73,27 +72,12 @@ impl EnvStorage for SrmlEnvStorage {
7372
}
7473
}
7574

76-
/// Wrapped access to return data.
77-
/// get() or get_raw Must be used right after ReturnData is returned.
78-
pub struct SrmlReturnData;
79-
80-
impl EnvReturnData for SrmlReturnData
81-
{
82-
fn get<U : Decode>(self) -> U {
83-
U::decode(&mut &read_scratch_buffer()[..]).unwrap()
84-
}
85-
86-
fn get_raw(self) -> Vec<u8> {
87-
read_scratch_buffer()
88-
}
89-
}
90-
9175
/// The SRML contracts environment.
9276
pub struct SrmlEnv<T>
9377
where
9478
T: EnvTypes,
9579
{
96-
marker: PhantomData<fn () -> T>,
80+
marker: PhantomData<fn() -> T>,
9781
}
9882

9983
impl<T> EnvTypes for SrmlEnv<T>
@@ -129,8 +113,6 @@ where
129113
T: EnvTypes,
130114
{
131115

132-
type ReturnData = SrmlReturnData;
133-
134116
fn input() -> Vec<u8> {
135117
read_scratch_buffer()
136118
}
@@ -165,13 +147,33 @@ where
165147
)
166148
}
167149
}
150+
fn call_invoke(
151+
callee: <Self as EnvTypes>::AccountId,
152+
gas: u64,
153+
value: <Self as EnvTypes>::Balance,
154+
input_data: &[u8],
155+
) {
156+
let callee = callee.encode();
157+
let value = value.encode();
158+
unsafe {
159+
sys::ext_call(
160+
callee.as_ptr() as u32,
161+
callee.len() as u32,
162+
gas,
163+
value.as_ptr() as u32,
164+
value.len() as u32,
165+
input_data.as_ptr() as u32,
166+
input_data.len() as u32
167+
);
168+
}
169+
}
168170

169-
fn call(
171+
fn call_evaluate<U: Decode>(
170172
callee: <Self as EnvTypes>::AccountId,
171173
gas: u64,
172174
value: <Self as EnvTypes>::Balance,
173175
input_data: &[u8],
174-
) -> Option<Self::ReturnData> {
176+
) -> Option<U> {
175177
let callee = callee.encode();
176178
let value = value.encode();
177179
unsafe {
@@ -185,7 +187,7 @@ where
185187
input_data.len() as u32
186188
);
187189
if success == 0 {
188-
Some(Self::ReturnData{})
190+
Some(U::decode(&mut &read_scratch_buffer()[..]).unwrap())
189191
} else {
190192
None
191193
}

core/src/env/srml/srml_only/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ mod impls;
1818
pub mod sys;
1919

2020
pub use self::impls::{
21-
SrmlReturnData,
2221
SrmlEnv,
2322
SrmlEnvStorage,
2423
};

core/src/env/test_env.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
use super::*;
1818
use crate::{
19-
env::{
20-
EnvReturnData,
21-
EnvTypes,
22-
},
19+
env::EnvTypes,
2320
memory::collections::hash_map::{
2421
Entry,
2522
HashMap,
@@ -404,13 +401,13 @@ impl TestEnvData {
404401
}
405402

406403
#[allow(unused_variables)]
407-
pub fn call(
404+
pub fn call<T: Decode>(
408405
&mut self,
409406
callee: &[u8],
410407
gas: u64,
411408
value: &[u8],
412409
input_data: &[u8],
413-
) -> Option<TestReturnData> {
410+
) -> Option<T> {
414411
unimplemented!();
415412
}
416413
}
@@ -425,17 +422,6 @@ thread_local! {
425422
};
426423
}
427424

428-
pub struct TestReturnData;
429-
430-
impl EnvReturnData for TestReturnData {
431-
fn get<U: Decode>(self) -> U {
432-
unimplemented!();
433-
}
434-
fn get_raw(self) -> Vec<u8> {
435-
unimplemented!();
436-
}
437-
}
438-
439425
/// Test environment for testing SRML contract off-chain.
440426
pub struct TestEnv<T> {
441427
marker: PhantomData<fn() -> T>,
@@ -530,8 +516,6 @@ impl<T> Env for TestEnv<T>
530516
where
531517
T: EnvTypes,
532518
{
533-
type ReturnData = TestReturnData;
534-
535519
impl_env_getters_for_test_env!(
536520
(address, T::AccountId),
537521
(balance, T::Balance),
@@ -560,12 +544,24 @@ where
560544
})
561545
}
562546

563-
fn call(
547+
fn call_invoke(
548+
callee: T::AccountId,
549+
gas: u64,
550+
value: T::Balance,
551+
input_data: &[u8],
552+
) {
553+
let callee = &(callee.encode())[..];
554+
let value = &(value.encode())[..];
555+
TEST_ENV_DATA
556+
.with(|test_env| test_env.borrow_mut().call(callee, gas, value, input_data));
557+
}
558+
559+
fn call_evaluate<U : Decode>(
564560
callee: T::AccountId,
565561
gas: u64,
566562
value: T::Balance,
567563
input_data: &[u8],
568-
) -> Option<Self::ReturnData> {
564+
) -> Option<U> {
569565
let callee = &(callee.encode())[..];
570566
let value = &(value.encode())[..];
571567
TEST_ENV_DATA

core/src/env/traits.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,8 @@ pub trait EnvStorage {
8080
unsafe fn load(key: Key) -> Option<Vec<u8>>;
8181
}
8282

83-
pub trait EnvReturnData
84-
{
85-
fn get<U : Decode>(self) -> U;
86-
87-
fn get_raw(self) -> Vec<u8>;
88-
}
89-
9083
/// The environment API usable by contracts defined with pDSL.
9184
pub trait Env: EnvTypes {
92-
93-
type ReturnData : EnvReturnData;
94-
9585
/// Returns the chain address of the contract.
9686
fn address() -> <Self as EnvTypes>::AccountId;
9787

@@ -141,11 +131,21 @@ pub trait Env: EnvTypes {
141131
/// Deposits raw event data through Contracts module.
142132
fn deposit_raw_event(topics: &[<Self as EnvTypes>::Hash], data: &[u8]);
143133

144-
/// Calls a remote smart contract and return access to scratch buffer
145-
fn call(
134+
135+
/// Calls a remote smart contract and return access to scratch buffer without return data
136+
fn call_invoke(
137+
callee: <Self as EnvTypes>::AccountId,
138+
gas: u64,
139+
value: <Self as EnvTypes>::Balance,
140+
input_data: &[u8]
141+
);
142+
143+
/// Calls a remote smart contract and return access to scratch buffer with return data
144+
#[must_use]
145+
fn call_evaluate<T: Decode>(
146146
callee: <Self as EnvTypes>::AccountId,
147147
gas: u64,
148148
value: <Self as EnvTypes>::Balance,
149149
input_data: &[u8],
150-
) -> Option<Self::ReturnData>;
150+
) -> Option<T>;
151151
}

model/src/exec_env.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use ink_core::{
2828
Initialize,
2929
},
3030
};
31+
use parity_codec::Decode;
3132

3233
/// Provides a safe interface to an environment given a contract state.
3334
pub struct ExecutionEnv<State, Env> {
@@ -181,14 +182,25 @@ impl<T: Env> EnvHandler<T> {
181182
T::block_number()
182183
}
183184

185+
/// Calls a remote smart contract
186+
pub fn call_invoke(
187+
&self,
188+
callee: T::AccountId,
189+
gas: u64,
190+
value: T::Balance,
191+
input_data: &[u8],
192+
) {
193+
T::call_invoke(callee, gas, value, input_data);
194+
}
195+
184196
/// Calls a remote smart contract and return access to scratch buffer
185-
pub fn call(
197+
pub fn call_evaluate<U:Decode>(
186198
&self,
187199
callee: T::AccountId,
188200
gas: u64,
189201
value: T::Balance,
190202
input_data: &[u8],
191-
) -> Option<T::ReturnData> {
192-
T::call(callee, gas, value, input_data)
203+
) -> Option<U> {
204+
T::call_evaluate(callee, gas, value, input_data)
193205
}
194206
}

0 commit comments

Comments
 (0)