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
use crate::{
    circuit::CircuitStatus, xtx::LocalState, ExecutionSource, GatewayVendor, SpeedMode, TargetId,
};
use codec::{Decode, Encode};
use frame_support::{
    dispatch::{DispatchError, DispatchResult, DispatchResultWithPostInfo},
    weights::Weight,
};
use frame_system::{
    pallet_prelude::{BlockNumberFor, OriginFor},
    Config as ConfigSystem,
};
use sp_core::H256;
use sp_std::{fmt::Debug, vec::Vec};

use crate::{circuit::AdaptiveTimeout, light_client::InclusionReceipt};
use t3rn_sdk_primitives::signal::ExecutionSignal;
use t3rn_types::{
    fsx::FullSideEffect,
    sfx::{HardenedSideEffect, SecurityLvl, SideEffect, SideEffectId},
};

#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub struct LocalTrigger<T: ConfigSystem> {
    /// Id of the contract which generated the side effects
    pub contract: T::AccountId,
    /// Side effects generated from the contract call
    pub submitted_side_effects: Vec<Vec<u8>>,
    pub speed_mode: SpeedMode,
    pub maybe_xtx_id: Option<T::Hash>,
}

impl<T: ConfigSystem> LocalTrigger<T> {
    pub fn new(
        contract: T::AccountId,
        submitted_side_effects: Vec<Vec<u8>>,
        speed_mode: SpeedMode,
        maybe_xtx_id: Option<T::Hash>,
    ) -> Self {
        LocalTrigger {
            contract,
            submitted_side_effects,
            speed_mode,
            maybe_xtx_id,
        }
    }
}

#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
pub struct LocalStateExecutionView<T: ConfigSystem, BalanceOf> {
    pub local_state: LocalState,
    pub hardened_side_effects: Vec<
        Vec<
            HardenedSideEffect<
                T::AccountId,
                frame_system::pallet_prelude::BlockNumberFor<T>,
                BalanceOf,
            >,
        >,
    >,
    pub steps_cnt: (u32, u32),
    pub xtx_id: <T as ConfigSystem>::Hash,
}

impl<T: ConfigSystem, Balance> LocalStateExecutionView<T, Balance> {
    pub fn new(
        xtx_id: <T as ConfigSystem>::Hash,
        local_state: LocalState,
        hardened_side_effects: Vec<
            Vec<
                HardenedSideEffect<
                    T::AccountId,
                    frame_system::pallet_prelude::BlockNumberFor<T>,
                    Balance,
                >,
            >,
        >,
        steps_cnt: (u32, u32),
    ) -> Self {
        LocalStateExecutionView {
            xtx_id,
            local_state,
            hardened_side_effects,
            steps_cnt,
        }
    }
}

pub trait CircuitSubmitAPI<T: ConfigSystem, Balance> {
    fn on_extrinsic_trigger(
        origin: OriginFor<T>,
        side_effects: Vec<SideEffect<T::AccountId, Balance>>,
        speed_mode: SpeedMode,
        preferred_security_level: SecurityLvl,
    ) -> DispatchResultWithPostInfo;

    fn on_remote_origin_trigger(
        origin: OriginFor<T>,
        order_origin: T::AccountId,
        side_effects: Vec<SideEffect<T::AccountId, Balance>>,
        speed_mode: SpeedMode,
    ) -> DispatchResultWithPostInfo;

    fn bid(
        origin: OriginFor<T>,
        sfx_id: SideEffectId<T>,
        amount: Balance,
    ) -> DispatchResultWithPostInfo;

    fn store_gmp_payload(id: H256, payload: H256) -> bool;

    fn get_gmp_payload(id: H256) -> Option<H256>;

    fn verify_sfx_proof(
        target: TargetId,
        speed_mode: SpeedMode,
        source: Option<ExecutionSource>,
        encoded_proof: Vec<u8>,
    ) -> Result<InclusionReceipt<BlockNumberFor<T>>, DispatchError>;
}

pub trait CircuitDLQ<T: ConfigSystem> {
    fn process_dlq(n: frame_system::pallet_prelude::BlockNumberFor<T>) -> Weight;
    fn process_adaptive_xtx_timeout_queue(
        n: frame_system::pallet_prelude::BlockNumberFor<T>,
        verifier: &GatewayVendor,
    ) -> Weight;
}

pub trait OnLocalTrigger<T: ConfigSystem, Balance> {
    fn on_local_trigger(
        origin: &OriginFor<T>,
        trigger: LocalTrigger<T>,
    ) -> Result<LocalStateExecutionView<T, Balance>, sp_runtime::DispatchError>;

    fn load_local_state(
        origin: &OriginFor<T>,
        maybe_xtx_id: Option<T::Hash>,
    ) -> Result<LocalStateExecutionView<T, Balance>, sp_runtime::DispatchError>;

    fn on_signal(origin: &OriginFor<T>, signal: ExecutionSignal<T::Hash>) -> DispatchResult;
}

pub type XExecSignalId<T> = <T as ConfigSystem>::Hash;
pub type XExecStepSideEffectId<T> = <T as ConfigSystem>::Hash;

pub trait ReadSFX<Hash, Account, Balance, BlockNumber> {
    fn get_pending_xtx_ids() -> Vec<Hash>;

    fn get_pending_xtx_for(
        for_executor: Account,
    ) -> Vec<(
        Hash,                              // xtx_id
        Vec<SideEffect<Account, Balance>>, // side_effects
        Vec<Hash>,                         // sfx_ids
    )>;

    fn get_fsx_of_xtx(xtx_id: Hash) -> Result<Vec<Hash>, DispatchError>;

    fn get_fsx_status(fsx_id: Hash) -> Result<CircuitStatus, DispatchError>;

    fn get_fsx_executor(fsx_id: Hash) -> Result<Option<Account>, DispatchError>;

    fn recover_latest_submitted_xtx_id() -> Result<Hash, DispatchError>;

    fn get_fsx(
        fsx_id: Hash,
    ) -> Result<FullSideEffect<Account, BlockNumber, Balance>, DispatchError>;

    fn get_xtx_status(
        xtx_id: Hash,
    ) -> Result<(CircuitStatus, AdaptiveTimeout<BlockNumber, TargetId>), DispatchError>;

    fn get_fsx_requester(fsx_id: Hash) -> Result<Account, DispatchError>;
}