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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! # Ovm Module
//!
//! The Ovm module provides functionality for handling layer2 dispute logics.
//! This refer to: https://github.com/cryptoeconomicslab/ovm-contracts/blob/master/contracts/UniversalAdjudicationContract.sol
//!
//! - [`ovm::Trait`](./trait.Trait.html)
//! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html)
//!
//! ## Overview
//! Ovm module is the substrate pallet to archive dispute game defined by predicate logic.
//!
//!
//!
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(deprecated)]

use codec::{Decode, Encode};
use frame_support::{
    decl_error, decl_event, decl_module, decl_storage,
    dispatch::DispatchResult,
    ensure,
    traits::Get,
    weights::{DispatchClass, FunctionOf, Pays, Weight},
    StorageMap,
};
use frame_system::{self as system, ensure_signed};

pub use ovmi::executor::ExecError;
pub type ExecResult<T> = Result<Vec<u8>, ExecError<<T as system::Config>::AccountId>>;

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::crypto::UncheckedFrom;
use sp_runtime::{
    traits::{Hash, Zero},
    RuntimeDebug,
};
use sp_std::marker::PhantomData;
use sp_std::{collections::btree_map::BTreeMap, prelude::*, rc::Rc, vec::Vec};

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

pub mod predicate;
pub mod traits;

use predicate::{ExecutionContext, PredicateLoader, PredicateOvm};
use traits::{Ext, NewCallContext, PredicateAddressFor};

/// PredicateContract wrapped Predicate and initial arguments.
///
/// Required functions of each PredicateContract:
/// - isValidChallenge
/// - decide
///
/// isValidChallenge validates valid child node of game tree.
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq, Eq)]
pub struct PredicateContract<CodeHash> {
    pub predicate_hash: CodeHash,
    pub inputs: Vec<u8>,
}

/// Property stands for dispute logic and we can claim every Properties to Adjudicator Contract.
/// Property has its predicate address and array of input.
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq, Eq)]
pub struct Property<AccountId> {
    /// Indicates the address of Predicate.
    pub predicate_address: AccountId,
    /// Every input are bytes. Each Atomic Predicate decode inputs to the specific type.
    pub inputs: Vec<Vec<u8>>,
}

/// The game decision by predicates.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq)]
pub enum Decision {
    Undecided,
    True,
    False,
}

/// ChallengeGame is a part of L2 dispute. It's instantiated by claiming property.
/// The client can get a game instance from this module.
#[derive(Encode, Decode, RuntimeDebug, PartialEq, Eq)]
pub struct ChallengeGame<Hash, BlockNumber> {
    /// Property of challenging targets.
    pub property_hash: Hash,
    /// challenges inputs
    pub challenges: Vec<Hash>,
    /// the result of this challenge.
    pub decision: Decision,
    /// the block number when this was issued.
    pub created_block: BlockNumber,
}

/// Definition of the cost schedule and other parameterizations for optimistic virtual machine.
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Eq, RuntimeDebug)]
pub struct Schedule {
    /// Version of the schedule.
    pub version: u32,

    /// Cost of putting a byte of code into storage.
    pub put_code_per_byte_cost: Weight,
}

// 500 (2 instructions per nano second on 2GHZ) * 1000x slowdown through wasmi
// This is a wild guess and should be viewed as a rough estimation.
// Proper benchmarks are needed before this value and its derivatives can be used in production.
const OVM_INSTRUCTION_COST: Weight = 500_000;

impl Default for Schedule {
    fn default() -> Schedule {
        Schedule {
            version: 0,
            put_code_per_byte_cost: OVM_INSTRUCTION_COST,
        }
    }
}

/// In-memory cache of configuration values.
///
/// We assume that these values can't be changed in the
/// course of transaction execution.
pub struct OvmConfig {
    pub schedule: Schedule,
    pub max_depth: u32, // about down 30.
}

impl OvmConfig {
    fn preload<T: Config>() -> OvmConfig {
        OvmConfig {
            schedule: <Module<T>>::current_schedule(),
            max_depth: T::MaxDepth::get(),
        }
    }
}

/// Atomic Predicate AccountId List.
/// It is inject when runtime setup.
pub struct AtomicPredicateIdConfig<AccountId, Hash> {
    pub not_address: AccountId,
    pub and_address: AccountId,
    pub or_address: AccountId,
    pub for_all_address: AccountId,
    pub there_exists_address: AccountId,
    pub equal_address: AccountId,
    pub is_contained_address: AccountId,
    pub is_less_address: AccountId,
    pub is_stored_address: AccountId,
    pub is_valid_signature_address: AccountId,
    pub verify_inclusion_address: AccountId,
    pub secp256k1: Hash,
}

pub struct SimpleAddressDeterminer<T: Config>(PhantomData<T>);
impl<T: Config> PredicateAddressFor<T::Hash, T::AccountId> for SimpleAddressDeterminer<T>
where
    T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>,
{
    fn predicate_address_for(
        code_hash: &T::Hash,
        data: &[u8],
        origin: &T::AccountId,
    ) -> T::AccountId {
        let data_hash = T::Hashing::hash(data);

        let mut buf = Vec::new();
        buf.extend_from_slice(code_hash.as_ref());
        buf.extend_from_slice(data_hash.as_ref());
        buf.extend_from_slice(origin.as_ref());

        UncheckedFrom::unchecked_from(T::Hashing::hash(&buf[..]))
    }
}

type PredicateHash<T> = <T as system::Config>::Hash;
type ChallengeGameOf<T> =
    ChallengeGame<<T as system::Config>::Hash, <T as system::Config>::BlockNumber>;
pub type PropertyOf<T> = Property<<T as system::Config>::AccountId>;
type AccountIdOf<T> = <T as system::Config>::AccountId;
type PredicateContractOf<T> = PredicateContract<<T as system::Config>::Hash>;

pub trait Config: system::Config {
    /// The maximum nesting level of a call/instantiate stack.
    type MaxDepth: Get<u32>;

    /// During the dispute period defined here, the user can challenge.
    /// If nothing is found, the state is determined after the dispute period.
    type DisputePeriod: Get<Self::BlockNumber>;

    /// A function type to get the contract address given the instantiator.
    type DeterminePredicateAddress: PredicateAddressFor<PredicateHash<Self>, Self::AccountId>;

    /// The hashing system (algorithm) being used in the runtime (e.g. Keccak256).
    type HashingL2: Hash<Output = Self::Hash>;

    /// ExternalCall context.
    type ExternalCall: Ext<Self> + NewCallContext<Self>;

    type AtomicPredicateIdConfig: Get<AtomicPredicateIdConfig<Self::AccountId, Self::Hash>>;

    /// The overarching event type.
    type Event: From<Event<Self>> + Into<<Self as system::Config>::Event>;
}

decl_storage! {
    trait Store for Module<T: Config> as Ovm {
        /// Current cost schedule for contracts.
        pub CurrentSchedule get(fn current_schedule) config(): Schedule = Schedule::default();

        /// A mapping from an original code hash to the original code, untouched by instrumentation.
        pub PredicateCodes get(fn predicate_codes): map hasher(identity) PredicateHash<T> => Option<Vec<u8>>;

        /// A mapping between an original code hash and instrumented ovm(predicate) code, ready for execution.
        pub PredicateCache get(fn predicate_cache): map hasher(identity) PredicateHash<T> => Option<predicate::PrefabOvmModule>;

        /// Mapping the predicate address to Predicate.
        /// Predicate is handled similar to contracts.
        pub Predicates get(fn predicates): map hasher(blake2_128_concat)
         T::AccountId => Option<PredicateContractOf<T>>;

        /// Mapping the game id to Challenge Game.
        pub Games get(fn games): map hasher(blake2_128_concat) T::Hash => Option<ChallengeGameOf<T>>;
    }
}

decl_event!(
    pub enum Event<T>
    where
        AccountId = <T as system::Config>::AccountId,
        Property = PropertyOf<T>,
        Hash = <T as system::Config>::Hash,
        BlockNumber = <T as system::Config>::BlockNumber,
    {
        /// (predicate_address: AccountId);
        PutPredicate(Hash),
        /// (predicate_address: AccountId);
        InstantiatePredicate(AccountId),
        /// (game_id: Hash, property: Property, created_block: BlockNumber)
        PropertyClaimed(Hash, Property, BlockNumber),
        /// (gameId: Hash, challenge_game_id: Hash)
        PropertyChallenged(Hash, Hash),
        /// (game_id: Hash, decision: bool)
        PropertyDecided(Hash, bool),
        /// (game_id: Hash, challengeGameId: Hash)
        ChallengeRemoved(Hash, Hash),
    }
);

decl_error! {
    /// Error for the staking module.
    pub enum Error for Module<T: Config> {
        /// Does not exist game
        DoesNotExistGame,
        /// setPredicateDecision must be called from predicate
        MustBeCalledFromPredicate,
        /// index must be less than challenges.length
        OutOfRangeOfChallenges,
        /// game is already started
        GameIsAlradyStarted,
        /// property is not claimed
        PropertyIsNotClaimed,
        /// challenge is already started
        ChallengeIsAlreadyStarted,
        /// challenge is not in the challenge list
        ChallengeIsNotInTheChallengeList,
        /// challenge property is not decided to false
        ChallengePropertyIsNotDecidedToFalse,
        /// challenge list is not empty
        ChallengeListIsNotEmpty,
        /// dispute period has not been passed
        DisputePeriodHasNotBeenPassed,
        /// undecided challenge exists
        UndecidedChallengeExists,
    }
}

decl_module! {
    pub struct Module<T: Config> for enum Call where origin: T::Origin {
        /// During the dispute period defined here, the user can challenge.
        /// If nothing is found, the state is determined after the dispute period.
        const DisputePeriod: <T as system::Config>::BlockNumber = T::DisputePeriod::get();

        type Error = Error<T>;

        fn deposit_event() = default;

        fn on_runtime_upgrade() -> Weight {
            migrate::<T>();
            // TODO: weight
            0
        }

        /// Stores the given binary Wasm code into the chain's storage and returns its `codehash`.
        /// You can instantiate contracts only with stored code.
        #[weight = FunctionOf(
            |args: (&Vec<u8>,)| Module::<T>::calc_code_put_costs(args.0),
            DispatchClass::Normal,
            Pays::Yes
        )]
        pub fn put_code(
            origin,
            predicate: Vec<u8>
        ) -> DispatchResult {
            let _ = ensure_signed(origin)?;
            let schedule = Self::current_schedule();
            match predicate::save_code::<T>(predicate, &schedule) {
                Ok(predicate_hash) => {
                    Self::deposit_event(RawEvent::PutPredicate(predicate_hash));
                },
                Err(err) => return Err(err.into()),
            }
            Ok(())
        }

        /// Deploy predicate and made predicate address as AccountId.
        /// TODO: weight
        #[weight = 100_000]
        pub fn instantiate(origin, predicate_hash: PredicateHash<T>, inputs: Vec<u8>) {
            let origin = ensure_signed(origin)?;

            // Calc predicate address.
            let predicate_address = T::DeterminePredicateAddress::predicate_address_for(
                &predicate_hash,
                &inputs,
                &origin
            );
            let predicate_contract = PredicateContract {
                predicate_hash,
                inputs,
            };

            <Predicates<T>>::insert(&predicate_address, predicate_contract);

            Self::deposit_event(RawEvent::InstantiatePredicate(predicate_address));
        }

        /// Claims property and create new game. Id of game is hash of claimed property
        /// TODO: weight
        #[weight = 100_000]
        pub fn claim(origin, claim: PropertyOf<T>) {
            let origin = ensure_signed(origin)?;
            Self::only_from_dispute_contract(&origin, &claim)?;
            // get the id of this property
            let game_id = Self::get_property_id(&claim);
            ensure!(
                Self::started(&game_id),
                Error::<T>::GameIsAlradyStarted,
            );

            let game = Self::create_game(game_id);
            <Games<T>>::insert(game_id, game);
           Self::deposit_event(RawEvent::PropertyClaimed(game_id, claim, Self::block_number()));
        }

        /// Challenge to an existing game instance by a property.
        ///
        /// challenge will be added to `challenges` field of challenged game instance.
        /// if property does not exist, revert.
        /// if challenge with same property was made before, revert.
        ///
        /// TODO: weight
        #[weight = 100_000]
        pub fn challenge(origin, property: PropertyOf<T>, challenge_property: PropertyOf<T>) {
            let origin = ensure_signed(origin)?;
            Self::only_from_dispute_contract(&origin, &property)?;

            // validation
            let id = Self::get_property_id(&property);
            ensure!(
                Self::started(&id),
                Error::<T>::PropertyIsNotClaimed,
            );

            let challenging_game_id = Self::get_property_id(&challenge_property);
            ensure!(
                Self::started(&challenging_game_id),
                Error::<T>::ChallengeIsAlreadyStarted,
            );

            // start challenging game
            let challenge_game = Self::create_game(challenging_game_id);
            <Games<T>>::insert(challenging_game_id, challenge_game);

            // add challenge to challenged game's challenge list
            let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
            game.challenges.push(challenging_game_id);
            <Games<T>>::insert(id, game);
            Self::deposit_event(RawEvent::PropertyChallenged(id, challenging_game_id));
        }

        /// remove challenge
        /// set challenging game decision to false and remove it from challenges field of challenged game
        /// if property does not exist, revert.
        /// if challenge property does not exist, revert.
        ///
        /// TODO: weight
        #[weight = 100_000]
        pub fn remove_challenge(origin, property: PropertyOf<T>) {
            let origin = ensure_signed(origin)?;
            Self::only_from_dispute_contract(&origin, &property)?;

            let id = Self::get_property_id(&property);
            ensure!(
                Self::started(&id),
                Error::<T>::PropertyIsNotClaimed,
            );

            let challenging_game_id = Self::get_property_id(&property);
            ensure!(
                Self::started(&challenging_game_id),
                Error::<T>::ChallengeIsAlreadyStarted,
            );

            let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
            let _ = Self::find_index(&game.challenges, &challenging_game_id)
                .ok_or(Error::<T>::ChallengeIsNotInTheChallengeList)?;

            let challenge_game = Self::games(&challenging_game_id).ok_or(Error::<T>::DoesNotExistGame)?;
            ensure!(
                challenge_game.decision == Decision::False,
                Error::<T>::ChallengePropertyIsNotDecidedToFalse,
            );

            // remove challenge
            game.challenges = game
                .challenges
                .into_iter()
                .filter(|challenge| challenge != &challenging_game_id)
                .collect();
            <Games<T>>::insert(id, game);
            Self::deposit_event(RawEvent::ChallengeRemoved(id, challenging_game_id));
        }

        /// set game result to given result value.
        /// only called from dispute contract
        ///
        /// TODO: weight
        #[weight = 100_000]
        pub fn set_game_result(origin, property: PropertyOf<T>, result: bool)
        {
            let origin = ensure_signed(origin)?;
            Self::only_from_dispute_contract(&origin, &property)?;

            let id = Self::get_property_id(&property);
            ensure!(
                Self::started(&id),
                Error::<T>::PropertyIsNotClaimed,
            );

            let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
            ensure!(
                game.challenges.len() == 0,
                Error::<T>::ChallengeListIsNotEmpty,
            );

            game.decision = Self::get_decision(result);
            Self::deposit_event(RawEvent::PropertyDecided(id, result));
        }

        /// settle game
        /// settle started game whose dispute period has passed.
        /// if no challenge for the property exists, decide to true.
        /// if any of its challenges decided to true, decide game to false.
        /// if undecided challenge remains, revert.
        ///
        /// TODO: weight
        #[weight = 100_000]
        pub fn settle_game(origin, property: PropertyOf<T>)
        {
            let origin = ensure_signed(origin)?;
            Self::only_from_dispute_contract(&origin, &property)?;

            let id = Self::get_property_id(&property);
            ensure!(
                Self::started(&id),
                Error::<T>::PropertyIsNotClaimed,
            );

            let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
            ensure!(
                game.created_block < Self::block_number() - T::DisputePeriod::get(),
                Error::<T>::DisputePeriodHasNotBeenPassed,
            );

            for challenge in game.challenges.iter() {
                let decision = Self::get_game(challenge).ok_or(Error::<T>::DoesNotExistGame)?.decision;
                if decision == Decision::True {
                    game.decision = Decision::False;
                    Self::deposit_event(RawEvent::PropertyDecided(id, false));
                    return Ok(());
                }
                ensure!(
                    decision == Decision::Undecided,
                    Error::<T>::UndecidedChallengeExists,
                );
            }
            game.decision = Decision::True;
            <Games<T>>::insert(id, game);
            Self::deposit_event(RawEvent::PropertyDecided(id, true));
        }
    }
}

fn migrate<T: Config>() {
    // TODO: When runtime upgrade, migrate stroage.
    // if let Some(current_era) = CurrentEra::get() {
    //     let history_depth = HistoryDepth::get();
    //     for era in current_era.saturating_sub(history_depth)..=current_era {
    //         ErasStartSessionIndex::migrate_key_from_blake(era);
    //     }
}

impl<T: Config> Module<T> {
    fn calc_code_put_costs(code: &Vec<u8>) -> Weight {
        <Module<T>>::current_schedule()
            .put_code_per_byte_cost
            .saturating_mul(code.len() as Weight)
    }

    // ======= main ==========
    /// Perform a call to a specified contract.
    ///
    /// This function is similar to `Self::call`, but doesn't perform any address lookups and better
    /// suitable for calling directly from Rust.
    pub fn bare_call(
        origin: T::AccountId,
        dest: T::AccountId,
        input_data: Vec<u8>,
    ) -> ExecResult<T> {
        Self::execute_ovm(origin, |ctx| ctx.call(dest, input_data))
    }

    pub fn bare_challenge(
        origin: T::AccountId,
        property: PropertyOf<T>,
        challenge_property: PropertyOf<T>,
    ) -> DispatchResult {
        Self::only_from_dispute_contract(&origin, &property)?;

        // validation
        let id = Self::get_property_id(&property);
        ensure!(Self::started(&id), Error::<T>::PropertyIsNotClaimed,);

        let challenging_game_id = Self::get_property_id(&challenge_property);
        ensure!(
            Self::started(&challenging_game_id),
            Error::<T>::ChallengeIsAlreadyStarted,
        );

        // start challenging game
        let challenge_game = Self::create_game(challenging_game_id);
        <Games<T>>::insert(challenging_game_id, challenge_game);

        // add challenge to challenged game's challenge list
        let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
        game.challenges.push(challenging_game_id);
        <Games<T>>::insert(id, game);
        Self::deposit_event(RawEvent::PropertyChallenged(id, challenging_game_id));
        Ok(())
    }

    pub fn bare_settle_game(origin: T::AccountId, property: PropertyOf<T>) -> DispatchResult {
        Self::only_from_dispute_contract(&origin, &property)?;

        let id = Self::get_property_id(&property);
        ensure!(Self::started(&id), Error::<T>::PropertyIsNotClaimed,);

        let mut game = Self::games(&id).ok_or(Error::<T>::DoesNotExistGame)?;
        ensure!(
            game.created_block < Self::block_number() - T::DisputePeriod::get(),
            Error::<T>::DisputePeriodHasNotBeenPassed,
        );

        for challenge in game.challenges.iter() {
            let decision = Self::get_game(challenge)
                .ok_or(Error::<T>::DoesNotExistGame)?
                .decision;
            if decision == Decision::True {
                game.decision = Decision::False;
                Self::deposit_event(RawEvent::PropertyDecided(id, false));
                return Ok(());
            }
            ensure!(
                decision == Decision::Undecided,
                Error::<T>::UndecidedChallengeExists,
            );
        }
        game.decision = Decision::True;
        <Games<T>>::insert(id, game);
        Self::deposit_event(RawEvent::PropertyDecided(id, true));
        Ok(())
    }

    fn execute_ovm(
        origin: T::AccountId,
        func: impl FnOnce(&mut ExecutionContext<T>) -> ExecResult<T>,
    ) -> ExecResult<T> {
        let cfg = Rc::new(OvmConfig::preload::<T>());
        let schedule = Rc::new(cfg.schedule.clone());
        let vm = Rc::new(PredicateOvm::new(Rc::clone(&schedule)));
        let loader = Rc::new(PredicateLoader::new(Rc::clone(&schedule)));
        let mut ctx = ExecutionContext::top_level(origin.clone(), cfg, vm, loader);

        func(&mut ctx)
    }

    // ======= callable ======
    /// Get of true/false the decision of property.
    pub fn is_decided(property: &PropertyOf<T>) -> Decision {
        let game = match Self::games(Self::get_property_id(property)) {
            Some(game) => game,
            None => return Decision::Undecided,
        };
        game.decision
    }

    /// Get of true/false the decision of game id.
    pub fn is_decided_by_id(id: T::Hash) -> Decision {
        let game = match Self::games(&id) {
            Some(game) => game,
            None => return Decision::Undecided,
        };
        game.decision
    }

    /// Get of the instatiated challenge game from claim_id.
    pub fn get_game(claim_id: &T::Hash) -> Option<ChallengeGameOf<T>> {
        Self::games(claim_id)
    }

    /// Get of the property id from the propaty itself.
    pub fn get_property_id(property: &PropertyOf<T>) -> T::Hash {
        T::Hashing::hash_of(property)
    }

    pub fn is_challenge_of(property: &PropertyOf<T>, challenge_property: &PropertyOf<T>) -> bool {
        if let Some(game) = Self::get_game(&Self::get_property_id(property)) {
            if let Some(_) =
                Self::find_index(&game.challenges, &Self::get_property_id(challenge_property))
            {
                return true;
            }
        }
        false
    }

    /// check if game of given id is already started.
    pub fn started(id: &T::Hash) -> bool {
        if let Some(game) = Self::games(id) {
            game.created_block != <T as system::Config>::BlockNumber::zero()
        } else {
            false
        }
    }

    // ======= helper =======
    pub fn block_number() -> <T as system::Config>::BlockNumber {
        <system::Module<T>>::block_number()
    }

    pub fn is_decidable(property_id: &T::Hash) -> bool {
        let game = match Self::games(property_id) {
            Some(game) => game,
            None => return false,
        };

        if game.created_block > Self::block_number() - T::DisputePeriod::get() {
            return false;
        }

        // check all game.challenges should be false
        game.challenges.iter().all(|challenge| {
            if let Some(challenging_game) = Self::games(challenge) {
                return challenging_game.decision == Decision::False;
            }
            false
        })
    }

    fn create_game(id: T::Hash) -> ChallengeGameOf<T> {
        ChallengeGame {
            property_hash: id,
            /// challenges inputs
            challenges: vec![],
            /// the result of this challenge.
            decision: Decision::Undecided,
            /// the block number when this was issued.
            created_block: Self::block_number(),
        }
    }

    fn get_decision(result: bool) -> Decision {
        if result {
            return Decision::True;
        }
        Decision::False
    }

    fn find_index<Hash: PartialEq>(array: &Vec<Hash>, item: &Hash) -> Option<usize> {
        array.iter().position(|hash| hash == item)
    }

    // ======= modifier =======
    fn only_from_dispute_contract(
        origin: &T::AccountId,
        property: &PropertyOf<T>,
    ) -> DispatchResult {
        ensure!(
            &property.predicate_address == origin,
            Error::<T>::MustBeCalledFromPredicate,
        );
        Ok(())
    }
}