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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#![feature(associated_type_defaults)]
#![feature(slice_take)]
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

use frame_support::{
    dispatch::DispatchResult, ensure, sp_runtime::DispatchError, traits::Currency,
};
use frame_system::pallet_prelude::BlockNumberFor;
use sp_runtime::traits::StaticLookup;
use sp_std::vec::Vec;
use t3rn_primitives::{
    account_manager::Outcome,
    circuit::{
        LocalStateExecutionView, OnLocalTrigger, VacuumEVM3DOrder, VacuumEVMOrder, VacuumEVMProof,
        VacuumEVMTeleportOrder,
    },
    contract_metadata::ContractType,
    contracts_registry::{AuthorInfo, ContractsRegistry, KindValidator, RegistryContract},
    threevm::{
        LocalStateAccess, ModuleOperations, Precompile, PrecompileArgs, PrecompileInvocation,
        Remunerated, Remuneration, SignalOpcode, ThreeVm, VacuumAccess,
    },
};
use t3rn_sdk_primitives::signal::{ExecutionSignal, Signaller};

const LOG_TARGET: &str = "3vm";

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

pub mod precompile;
pub mod remuneration;
pub mod signal;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

pub type CurrencyOf<T> = <T as pallet::Config>::Currency;
pub type BalanceOf<T> =
    <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[frame_support::pallet]
pub mod pallet {

    use crate::BalanceOf;
    use frame_support::{pallet_prelude::*, traits::Currency};
    use frame_system::pallet_prelude::BlockNumberFor;
    use sp_std::vec::Vec;
    use t3rn_primitives::{
        account_manager::AccountManager,
        circuit::OnLocalTrigger,
        contract_metadata::ContractType,
        contracts_registry::ContractsRegistry,
        portal::Portal,
        threevm::{AddressMapping, VacuumAccess},
        ChainId,
    };

    use t3rn_sdk_primitives::signal::SignalKind;

    #[pallet::config]
    pub trait Config: frame_system::Config {
        type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

        /// The ID of the circuit
        type CircuitTargetId: Get<ChainId>;

        /// Determines the tolerance of debouncing signal requests that have already been sent.
        #[pallet::constant]
        type SignalBounceThreshold: Get<u32>;

        /// The pallet that handles the contracts registry, used to fetch contracts
        type ContractsRegistry: ContractsRegistry<Self, Self::Currency>;

        type Currency: Currency<Self::AccountId>;

        /// The address of the escrow account
        #[pallet::constant]
        type EscrowAccount: Get<Self::AccountId>;

        /// Asset Id for the account manager
        type AssetId;

        /// The account manager that handles the escrow pool
        type AccountManager: AccountManager<
            Self::AccountId,
            BalanceOf<Self>,
            Self::Hash,
            BlockNumberFor<Self>,
            Self::AssetId,
        >;
        type AddressMapping: AddressMapping<Self::AccountId>;

        type VacuumEVMApi: VacuumAccess<Self>;

        /// A provider that will give us access to on_local_trigger
        type OnLocalTrigger: OnLocalTrigger<Self, BalanceOf<Self>>;

        /// Inject access to portal so contracts can use light clients
        type Portal: Portal<Self>;
    }

    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(_);

    /// Holds the amount of times the signal was posted or attempted to be posted
    #[pallet::storage]
    pub(crate) type Signals<T: Config> = StorageDoubleMap<_, Identity, T::Hash, Identity, u32, u32>;

    /// A mapping of precompile pointers
    #[pallet::storage]
    pub(crate) type PrecompileIndex<T: Config> = StorageMap<_, Identity, T::Hash, u8>;

    /// A mapping of a contract's address to its author.
    #[pallet::storage]
    #[pallet::getter(fn author_of)]
    pub(crate) type AuthorOf<T: Config> = StorageMap<_, Identity, T::AccountId, T::AccountId>;

    #[pallet::genesis_config]
    #[derive(frame_support::DefaultNoBound)]
    pub struct GenesisConfig<T: Config> {
        pub precompiles: Vec<(T::Hash, u8)>,
        #[serde(skip)]
        pub _marker: PhantomData<T>,
    }

    #[pallet::genesis_build]
    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
        fn build(&self) {
            for (hash, ptr) in &self.precompiles {
                <PrecompileIndex<T>>::insert(hash, ptr);
            }
        }
    }

    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        /// A signal event was bounced back, because a signal was already sent for the current step. [step, kind, xtx_id]
        SignalBounced((u32, SignalKind, T::Hash)),
        /// A signal event was bounced beyond the threshold. [step, kind, xtx_id]
        ExceededBounceThrehold((u32, SignalKind, T::Hash)),
        /// A module was instantiated from the registry [id, module_author, module_type, module_len]
        ModuleInstantiated((T::Hash, T::AccountId, ContractType, u32)),
        /// An author of a module was stored [contract, author]
        AuthorStored((T::AccountId, T::AccountId)),
        /// An author of a module was removed [contract]
        AuthorRemoved(T::AccountId),
    }

    #[derive(PartialEq)]
    #[pallet::error]
    pub enum Error<T> {
        /// A user exceeded the bounce threshold for submitting signals
        ExceededSignalBounceThreshold,
        /// You can't submit side effects without any side effects
        CannotTriggerWithoutSideEffects,
        /// The contract could not be found in the registry
        ContractNotFound,
        /// An origin could not be extracted from the buffer
        InvalidOrigin,
        /// The contract cannot be instantiated due to its type
        CannotInstantiateContract,
        /// The contract cannot remunerate due to its type
        ContractCannotRemunerate,
        // TODO: this is not implemented yet?
        /// The contract cannot have storage due to its type
        ContractCannotHaveStorage,
        /// The contract cannot generate side effects due to its type
        ContractCannotGenerateSideEffects,
        /// The precompile pointer was invalid
        InvalidPrecompilePointer,
        /// Invalid precompile arguments
        InvalidPrecompileArgs,
        /// Invalid arithmetic computation causes overflow
        InvalidArithmeticOverflow,
        DownstreamCircuit,
    }

    #[pallet::call]
    impl<T: Config> Pallet<T> {}
}

impl<T: Config> Pallet<T> {
    pub fn get_author(contract: &<T::Lookup as StaticLookup>::Source) -> Option<T::AccountId> {
        let contract = T::Lookup::lookup(contract.clone()).ok()?;

        log::debug!(target: LOG_TARGET, "Reading author {:?}", contract);
        Self::author_of(contract)
    }
}

impl<T: Config> Precompile<T, BalanceOf<T>> for Pallet<T> {
    fn lookup(dest: &T::Hash) -> Option<u8> {
        precompile::lookup::<T>(dest)
    }

    fn invoke_raw(precompile: &u8, args: &[u8], output: &mut Vec<u8>) {
        log::debug!(
            target: LOG_TARGET,
            "Invoking raw precompile {:?} with arguments: {:?}",
            precompile,
            args
        );
        precompile::invoke_raw::<T>(precompile, &mut &args.to_vec()[..], output)
    }

    fn invoke(
        args: PrecompileArgs<T, BalanceOf<T>>,
    ) -> Result<PrecompileInvocation<T, BalanceOf<T>>, DispatchError> {
        precompile::invoke(args)
    }
}

impl<T: Config> LocalStateAccess<T, BalanceOf<T>> for Pallet<T> {
    fn load_local_state(
        origin: &T::RuntimeOrigin,
        xtx_id: Option<&T::Hash>,
    ) -> Result<LocalStateExecutionView<T, BalanceOf<T>>, DispatchError> {
        <T as Config>::OnLocalTrigger::load_local_state(origin, xtx_id.cloned())
    }
}

impl<T: Config> VacuumAccess<T> for Pallet<T> {
    fn evm_order(
        origin: &T::RuntimeOrigin,
        vacuum_evm_order: VacuumEVMOrder,
    ) -> Result<bool, DispatchError> {
        <T as Config>::VacuumEVMApi::evm_order(origin, vacuum_evm_order)
    }

    fn evm_teleport_order(
        origin: &T::RuntimeOrigin,
        vacuum_evm_order: VacuumEVMTeleportOrder,
    ) -> Result<bool, DispatchError> {
        <T as Config>::VacuumEVMApi::evm_teleport_order(origin, vacuum_evm_order)
    }

    fn evm_confirm(
        origin: &T::RuntimeOrigin,
        vacuum_evm_order: VacuumEVMOrder,
    ) -> Result<bool, DispatchError> {
        <T as Config>::VacuumEVMApi::evm_confirm(origin, vacuum_evm_order)
    }

    fn evm_3d_order(
        origin: &T::RuntimeOrigin,
        vacuum_evm_order: VacuumEVM3DOrder,
    ) -> Result<bool, DispatchError> {
        <T as Config>::VacuumEVMApi::evm_3d_order(origin, vacuum_evm_order)
    }

    fn evm_submit_fault_proof(
        origin: &T::RuntimeOrigin,
        vacuum_evm_proof: VacuumEVMProof,
    ) -> Result<bool, DispatchError> {
        <T as Config>::VacuumEVMApi::evm_submit_fault_proof(origin, vacuum_evm_proof)
    }
}

impl<T: Config> Signaller<<T as frame_system::Config>::Hash> for Pallet<T> {
    type Result = Result<SignalOpcode, DispatchError>;

    fn signal(signal: &ExecutionSignal<<T as frame_system::Config>::Hash>) -> Self::Result {
        signal::signal::<T>(signal).map_err(|e| {
            // TODO: Decide what we want to do to users who try to bounce too many signals
            e.into()
        })
    }
}

impl<T: Config> Remuneration<T, BalanceOf<T>> for Pallet<T> {
    fn try_remunerate<Module: ModuleOperations<T, BalanceOf<T>>>(
        payee: &T::AccountId,
        module: &Module,
    ) -> Result<Remunerated<T::Hash>, DispatchError> {
        remuneration::try_remunerate::<T, Module>(payee, module)
    }

    fn try_remunerate_exact<Module: ModuleOperations<T, BalanceOf<T>>>(
        payee: &T::AccountId,
        amount: BalanceOf<T>,
        module: &Module,
    ) -> Result<Remunerated<T::Hash>, DispatchError> {
        remuneration::try_remunerate_exact::<T, Module>(payee, amount, module)
    }

    fn try_finalize(ledger_id: T::Hash, outcome: Outcome) -> DispatchResult {
        remuneration::try_finalize::<T>(ledger_id, outcome)
    }
}

impl<T: Config> ThreeVm<T, BalanceOf<T>> for Pallet<T> {
    fn peek_registry(
        id: &T::Hash,
    ) -> Result<
        RegistryContract<T::Hash, T::AccountId, BalanceOf<T>, BlockNumberFor<T>>,
        DispatchError,
    > {
        let contract = T::ContractsRegistry::fetch_contract_by_id(*id)
            .map_err(|_| Error::<T>::ContractNotFound)?;
        Ok(contract)
    }

    fn from_registry<Module, ModuleGen>(
        id: &T::Hash,
        module_generator: ModuleGen,
    ) -> Result<Module, DispatchError>
    where
        Module: ModuleOperations<T, BalanceOf<T>>,
        ModuleGen: Fn(Vec<u8>) -> Module,
    {
        let registry_contract = T::ContractsRegistry::fetch_contract_by_id(*id)
            .map_err(|_| Error::<T>::ContractNotFound)?;
        let contract_len = registry_contract.bytes.len();
        let mut module = module_generator(registry_contract.bytes);
        module.set_author(registry_contract.author.clone());
        module.set_type(*registry_contract.meta.get_contract_type());
        Self::deposit_event(Event::<T>::ModuleInstantiated((
            *id,
            registry_contract.author.account,
            *registry_contract.meta.get_contract_type(),
            contract_len as u32,
        )));
        Ok(module)
    }

    fn instantiate_check(kind: &ContractType) -> Result<(), DispatchError> {
        ensure!(
            kind.can_instantiate(),
            <Error<T>>::CannotInstantiateContract
        );
        Ok(())
    }

    fn storage_check(kind: &ContractType) -> Result<(), DispatchError> {
        ensure!(kind.has_storage(), <Error<T>>::ContractCannotHaveStorage);
        Ok(())
    }

    fn volatile_check(kind: &ContractType) -> Result<(), DispatchError> {
        ensure!(
            kind.can_generate_side_effects(),
            <Error<T>>::ContractCannotGenerateSideEffects
        );
        Ok(())
    }

    fn remunerable_check(kind: &ContractType) -> Result<(), DispatchError> {
        ensure!(kind.can_remunerate(), <Error<T>>::ContractCannotRemunerate);
        Ok(())
    }

    fn try_persist_author(
        contract: &T::AccountId,
        author: Option<&AuthorInfo<T::AccountId, BalanceOf<T>>>,
    ) -> Result<(), DispatchError> {
        if let Some(author) = author {
            if !AuthorOf::<T>::contains_key(contract) {
                AuthorOf::<T>::insert(contract, author.account.clone());
                Self::deposit_event(Event::<T>::AuthorStored((
                    contract.clone(),
                    author.account.clone(),
                )))
            }
        }
        Ok(())
    }

    fn try_remove_author(contract: &T::AccountId) -> Result<(), DispatchError> {
        if AuthorOf::<T>::contains_key(contract) {
            AuthorOf::<T>::remove(contract);
            Self::deposit_event(Event::<T>::AuthorRemoved(contract.clone()))
        }

        Ok(())
    }
}