16
16
17
17
use super :: * ;
18
18
use crate :: {
19
- env:: EnvTypes ,
19
+ env:: {
20
+ CallError ,
21
+ EnvTypes ,
22
+ } ,
20
23
memory:: collections:: hash_map:: {
21
24
Entry ,
22
25
HashMap ,
@@ -47,6 +50,25 @@ impl EventData {
47
50
}
48
51
}
49
52
53
+ /// Emulates the data given to remote smart contract call instructions.
54
+ pub struct RawCallData {
55
+ pub callee : Vec < u8 > ,
56
+ pub gas : u64 ,
57
+ pub value : Vec < u8 > ,
58
+ pub input_data : Vec < u8 > ,
59
+ }
60
+
61
+ /// Decoded call data of recorded external calls.
62
+ pub struct CallData < E >
63
+ where
64
+ E : crate :: env:: EnvTypes ,
65
+ {
66
+ pub callee : E :: AccountId ,
67
+ pub gas : u64 ,
68
+ pub value : E :: Balance ,
69
+ pub input_data : Vec < u8 > ,
70
+ }
71
+
50
72
/// An entry in the storage of the test environment.
51
73
///
52
74
/// # Note
@@ -173,6 +195,10 @@ pub struct TestEnvData {
173
195
gas_left : Vec < u8 > ,
174
196
/// The total transferred value.
175
197
value_transferred : Vec < u8 > ,
198
+ /// The recorded external calls.
199
+ calls : Vec < CallData > ,
200
+ /// The expected return data of the next external call.
201
+ call_return : Vec < u8 > ,
176
202
/// Returned data.
177
203
return_data : Vec < u8 > ,
178
204
}
@@ -195,6 +221,8 @@ impl Default for TestEnvData {
195
221
gas_left : Vec :: new ( ) ,
196
222
value_transferred : Vec :: new ( ) ,
197
223
dispatched_calls : Vec :: new ( ) ,
224
+ calls : Vec :: new ( ) ,
225
+ call_return : Vec :: new ( ) ,
198
226
return_data : Vec :: new ( ) ,
199
227
}
200
228
}
@@ -215,6 +243,8 @@ impl TestEnvData {
215
243
self . total_writes = 0 ;
216
244
self . events . clear ( ) ;
217
245
self . dispatched_calls . clear ( ) ;
246
+ self . calls . clear ( ) ;
247
+ self . call_return . clear ( ) ;
218
248
self . return_data . clear ( ) ;
219
249
}
220
250
@@ -309,6 +339,17 @@ impl TestEnvData {
309
339
self . dispatched_calls . iter ( ) . map ( Vec :: as_slice)
310
340
}
311
341
342
+ /// Records a new external call.
343
+ pub fn add_call ( & mut self , callee : & [ u8 ] , gas : u64 , value : & [ u8 ] , input_data : & [ u8 ] ) {
344
+ let new_call = CallData {
345
+ callee : callee. to_vec ( ) ,
346
+ gas,
347
+ value : value. to_vec ( ) ,
348
+ input_data : input_data. to_vec ( ) ,
349
+ } ;
350
+ self . calls . push ( new_call) ;
351
+ }
352
+
312
353
/// Returns the latest returned data.
313
354
pub fn returned_data ( & self ) -> & [ u8 ] {
314
355
& self . return_data
@@ -392,6 +433,17 @@ impl TestEnvData {
392
433
pub fn dispatch_call ( & mut self , call : & [ u8 ] ) {
393
434
self . add_dispatched_call ( call) ;
394
435
}
436
+
437
+ pub fn call (
438
+ & mut self ,
439
+ callee : & [ u8 ] ,
440
+ gas : u64 ,
441
+ value : & [ u8 ] ,
442
+ input_data : & [ u8 ] ,
443
+ ) -> Vec < u8 > {
444
+ self . add_call ( callee, gas, value, input_data) ;
445
+ self . call_return . clone ( )
446
+ }
395
447
}
396
448
397
449
thread_local ! {
@@ -541,6 +593,35 @@ where
541
593
fn dispatch_raw_call ( data : & [ u8 ] ) {
542
594
TEST_ENV_DATA . with ( |test_env| test_env. borrow_mut ( ) . dispatch_call ( data) )
543
595
}
596
+
597
+ fn call_invoke (
598
+ callee : T :: AccountId ,
599
+ gas : u64 ,
600
+ value : T :: Balance ,
601
+ input_data : & [ u8 ] ,
602
+ ) -> Result < ( ) , CallError > {
603
+ let callee = & ( callee. encode ( ) ) [ ..] ;
604
+ let value = & ( value. encode ( ) ) [ ..] ;
605
+ let _return_data = TEST_ENV_DATA
606
+ . with ( |test_env| test_env. borrow_mut ( ) . call ( callee, gas, value, input_data) ) ;
607
+ Ok ( ( ) )
608
+ }
609
+
610
+ fn call_evaluate < U : Decode > (
611
+ callee : T :: AccountId ,
612
+ gas : u64 ,
613
+ value : T :: Balance ,
614
+ input_data : & [ u8 ] ,
615
+ ) -> Result < U , CallError > {
616
+ let callee = & ( callee. encode ( ) ) [ ..] ;
617
+ let value = & ( value. encode ( ) ) [ ..] ;
618
+ TEST_ENV_DATA . with ( |test_env| {
619
+ U :: decode (
620
+ & mut & ( test_env. borrow_mut ( ) . call ( callee, gas, value, input_data) ) [ ..] ,
621
+ )
622
+ . map_err ( |_| CallError )
623
+ } )
624
+ }
544
625
}
545
626
546
627
pub enum TestEnvStorage { }
0 commit comments