1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#![cfg_attr(not(feature = "std"), no_std)]

extern crate core;

pub use t3rn_sdk_primitives as primitives;

use crate::executor::{Executor, StateHandler, Submitter};
use codec::{Decode, Encode};
use error::Error;
use t3rn_sdk_primitives::{
    signal::{ExecutionSignal, KillReason, SignalKind, Signaller},
    state::{ExecutionState, GetSteps, SideEffects},
    storage::BoundedVec,
    xc::Chain,
    Box, Debug, DEFAULT_MAX_STEPS_IN_EXECUTION, MAX_PARAMETERS_IN_FUNCTION,
};

pub mod error;
pub mod executor;

/// Information for the next step to be sent to the virtual machine.
#[derive(Clone)]
pub struct Step<AccountId, Balance, Hash>
where
    AccountId: Encode + Decode + Debug + Clone,
    Balance: Encode + Decode + Debug + Clone,
    Hash: Encode + Decode + Debug + Clone,
{
    pub side_effects: BoundedVec<Chain<AccountId, Balance, Hash>, MAX_PARAMETERS_IN_FUNCTION>,
}

impl<AccountId, Balance, Hash> FromIterator<Chain<AccountId, Balance, Hash>>
    for Step<AccountId, Balance, Hash>
where
    AccountId: Encode + Decode + Debug + Clone,
    Balance: Encode + Decode + Debug + Clone,
    Hash: Encode + Decode + Debug + Clone,
{
    fn from_iter<T: IntoIterator<Item = Chain<AccountId, Balance, Hash>>>(iter: T) -> Self {
        Step {
            side_effects: BoundedVec::from_iter(iter),
        }
    }
}

impl<AccountId, Balance, Hash> Step<AccountId, Balance, Hash>
where
    AccountId: Encode + Decode + Debug + Clone,
    Balance: Encode + Decode + Debug + Clone,
    Hash: Encode + Decode + Debug + Clone,
{
    /// Try to push another side effect to the step, this would fail if the underlying data structure
    /// met max_capacity
    pub fn try_push(
        &mut self,
        side_effect: Chain<AccountId, Balance, Hash>,
    ) -> Result<&mut Self, Error> {
        log_msg!(
            "Checking that {} is not more than {}",
            self.side_effects.0.len() + 1,
            self.side_effects.0.capacity()
        );
        if self.side_effects.0.len() + 1 >= self.side_effects.0.capacity() {
            return Err(Error::TooManyFunctionParams)
        }
        self.side_effects.0.push(side_effect);
        Ok(self)
    }

    /// Same functionality as `Iter::pop`
    pub fn try_pop(&mut self) -> Option<Chain<AccountId, Balance, Hash>> {
        self.side_effects.0.pop()
    }

    /// Take a look at the last element of the Vector, if it exists, otherwise none.
    pub fn peek(&self) -> Option<&Chain<AccountId, Balance, Hash>> {
        self.side_effects.0.last()
    }
}

impl<AccountId, Balance, Hash> Default for Step<AccountId, Balance, Hash>
where
    AccountId: Encode + Decode + Debug + Clone,
    Balance: Encode + Decode + Debug + Clone,
    Hash: Encode + Decode + Debug + Clone,
{
    fn default() -> Self {
        Self {
            side_effects: BoundedVec::default(),
        }
    }
}

/// A user provided function that takes a hash and returns some steps to be appended to local state on-chain.
pub type UserStepHandler<Hash, AccountId, Balance> = Box<
    dyn Fn(
        &ExecutionState<Hash, AccountId, u64, Balance>,
    ) -> Result<Step<AccountId, Balance, Hash>, Error>,
>;

/// Execute all steps, providing the user function to apply to the steps.
///
/// This function signals on continuation for each step.
pub fn execute<Hash, AccountId, Balance>(
    execution_id: Option<Hash>,
    f: UserStepHandler<Hash, AccountId, Balance>,
) -> Result<(), Error>
where
    Hash: Encode + Decode + Clone + Debug + PartialEq + Eq + Default + Copy,
    AccountId: Encode + Decode + Clone + Debug,
    Balance: Encode + Decode + Clone + Debug,
{
    match run_steps(execution_id, f) {
        Ok(signal) => {
            Executor::signal(&signal)?;
            Ok(())
        },
        Err(e) => Err(e),
    }
}

/// Run the steps, applying the user function.
///
/// On each step, we retrieve execution state, either new(if execution id is None) or existing and run some validation on it.
///
/// Then apply the user function:
///     If ok and steps are empty:
///         complete execution
///     If ok and new steps:
///         submit them and call again
///     If err:
///         signal the err
fn run_steps<Hash, AccountId, Balance>(
    execution_id: Option<Hash>,
    f: UserStepHandler<Hash, AccountId, Balance>,
) -> Result<ExecutionSignal<Hash>, Error>
where
    Hash: Encode + Decode + Clone + Debug + Default + Copy,
    AccountId: Encode + Decode + Clone + Debug,
    Balance: Encode + Decode + Clone + Debug,
{
    let prev_state = Executor::get_state(execution_id)?;

    let execution_id = match execution_id {
        None => prev_state.xtx_id,
        Some(hash) => hash,
    };

    // We get step count here, we can just break if the step is greater than the limit.
    if prev_state.get_index() >= DEFAULT_MAX_STEPS_IN_EXECUTION as u32 {
        log_msg!("[SDK] Execution limit reached");
        return Ok(ExecutionSignal::new(
            &execution_id,
            Some(prev_state.get_index()),
            SignalKind::Complete,
        ))
    }
    // apply user function
    match f(&prev_state) {
        Ok(state) => {
            log_msg!("[SDK] user function handled successfully");
            let new_step_state: Step<AccountId, Balance, Hash> = state;

            // if ok and no side effects, send complete and break
            // Guard for no new steps and reached limit
            if prev_state.reached_end() && new_step_state.side_effects.is_empty() {
                return Ok(ExecutionSignal::new(
                    &execution_id,
                    Some(prev_state.get_index()),
                    SignalKind::Complete,
                ))
            }

            Executor::submit(SideEffects {
                execution_id,
                side_effects: new_step_state.side_effects,
            })
            .map_err(|e| {
                let _: Result<(), executor::Error> = Executor::signal(&ExecutionSignal::new(
                    &execution_id,
                    Some(prev_state.get_index()),
                    SignalKind::Kill(KillReason::Unhandled),
                ));
                e
            })?;
            // call self ready for next step
            run_steps(Some(execution_id), f)
        },
        Err(err) => {
            log_msg!("[SDK] error calling user provided function {:?}", err);

            // if err and error handler is defined, call error handler and break TODO: user provided error handler
            // if err and error handler is not defined, send default signal and break TODO: user provided error handler
            Executor::signal(&ExecutionSignal::new(
                &execution_id,
                Some(prev_state.get_index()),
                SignalKind::Kill(KillReason::Unhandled),
            ))?;

            Err(err)
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::executor::Submitter;
    use codec::alloc::sync::Mutex;
    use lazy_static::lazy_static;
    use scale_info::prelude::{collections::HashMap, vec};
    use t3rn_sdk_primitives::{
        state::{GetExecutionId, GetSteps, Getters},
        xc::{Chain, Operation},
        Vec,
    };

    type Hash = [u8; 32];
    type AccountId = [u8; 32];

    const CALLER: AccountId = [1_u8; 32];
    const CONTRACT: AccountId = [2_u8; 32];
    const EXECUTION_ID: Hash = [50_u8; 32];

    #[derive(Encode, Decode, Default)]
    struct State(Vec<u8>);

    struct TestProvider;

    lazy_static! {
        static ref STATE: Mutex<HashMap<Hash, Vec<u8>>> = Mutex::new(HashMap::new());
        static ref SIGNALS: Mutex<HashMap<Hash, Vec<u8>>> = Mutex::new(HashMap::new());
    }

    impl StateHandler<Hash, State> for TestProvider {
        fn get_state(execution_id: Option<Hash>) -> Result<State, executor::Error> {
            match STATE.lock().unwrap().get(&execution_id.unwrap()) {
                Some(bytes) => Ok(State(bytes.clone())),
                None => Ok(State(Vec::new())),
            }
        }
    }

    impl Submitter<SideEffects<AccountId, u128, Hash>> for TestProvider {
        fn submit(state: SideEffects<AccountId, u128, Hash>) -> Result<(), executor::Error> {
            STATE
                .lock()
                .unwrap()
                .insert(state.execution_id, state.encode());
            Ok(())
        }
    }

    impl GetExecutionId<Hash> for State {
        fn get_execution_id(&self) -> &Hash {
            &EXECUTION_ID
        }
    }

    impl GetSteps for State {
        fn get_index(&self) -> u32 {
            let guard = STATE.lock().unwrap();
            let state = guard.get(&EXECUTION_ID);
            if state.is_some() {
                1
            } else {
                0
            }
        }

        fn get_len(&self) -> u32 {
            1
        }

        fn reached_end(&self) -> bool {
            self.get_index() == self.get_len()
        }
    }

    impl Getters<Hash> for State {}

    impl Signaller<Hash> for TestProvider {
        type Result = Result<(), executor::Error>;

        fn signal(signal: &ExecutionSignal<Hash>) -> Result<(), executor::Error> {
            SIGNALS
                .lock()
                .unwrap()
                .insert(signal.execution_id, signal.encode());
            Ok(())
        }
    }

    // Ignored since we disallow executors, need to have a test executor
    #[ignore]
    #[test]
    fn test_run_step_provides_state_in_a_deterministic_way() {
        let mut state = Step::default();
        state
            .try_push(Chain::<_, _, _>::Polkadot(Operation::Transfer {
                caller: CALLER,
                to: CONTRACT,
                amount: 500,
                insurance: None,
            }))
            .unwrap();
        let f = Box::new(move |_state: &ExecutionState<Hash, AccountId, _, u64>| Ok(state.clone()));

        execute(Some(EXECUTION_ID), f).unwrap();
        let guard = STATE.lock().unwrap();
        let state = guard.get(&EXECUTION_ID).unwrap();
        assert_eq!(
            state,
            &vec![
                // 1, // option some TODO: check why this is removed
                50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
                50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, // exec id
                112, 111, 108, 107, 16, // polk
                116, 114, 97, 110, // tran
                128, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                1, 1, 1, 1, 1, 1, // caller
                128, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                2, 2, 2, 2, 2, 2, //contract
                16, 244, 1, // amt
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, // uber padding
            ]
        );
    }
}