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
//! # ovmi
//! This library allows **Predicate** to be loaded in binary format and their functions invoked.
//!
//! # Introduction
//! Predicate is a DSL for resolving Layer 2 dispute logic,
//! and the Optimistic Virtual Machine gives simulated environment by Predicate.
//!
//! There are several types of Predicate, each of which has an interface that looks like this.
//! ## Atomic Predicate
//! - decideTrue
//! - decide
//!
//! ## CompiledPredicate
//! - payoutContractAddress
//! - isValidChallenge
//! - getChild
//! - decide
//! - decideTrue
//! - decideWithWitness
//!
//! ## DecidablePredicate
//! - decideWithWitness
//!
//! ## LogicalConnective
//! - isValidChallenge
//!
//! Each of these definitions can be imported and exported.
//!
//! OVM does not manage the state, but it can be called externally.
//!
//! # Loading and Validation
//! Before execution, a module must be validated. This process checks that the module is well-formed and makes only allowed operations.
//!
//! You can get the binary of predicate from json.
//! The json format is refer to: https://github.com/cryptoeconomicslab/wakkanay/blob/master/packages/ovm-transpiler/src/CompiledPredicate.ts
//! ```ignore
//! use ovmi::prepare;
//! let compiled_predicate = prepare::compile_from_json("<compiled_predicate_json>").unwrap();
//! let binary_predicate = compiled_predicate::encode();
//!
//! if let Err(err) = prepare::validate(binary_predicate) {
//!     panic!(err);
//! }
//! ```
//!
//! # Set external environments and.
//!
//! ## Example) Compiled Predicate Execute
//! ```ignore
//! use ovmi::prepare;
//!
//! type AccountId = u64;
//!
//! // Setting External environment.
//! struct MockExternalCall{..};
//! impl ExternalCall for MockExternalCall {
//!     ...
//! }
//!
//! fn call_execute(inputs: Vec<Vec<u8>>, inputs: PredicateCallInputs<AccountId>) -> ExecResult<AccountId> {
//!     let compiled_predicate = prepare::compile_from_json("<compiled_predicate_json>").unwrap();
//!     let (payout, address_input, bytes_inputs) = prepare::parse_inputs(inputs);
//!     let ext = MockExternalCall{..};
//!     let executable = prepare::executable_from_compiled(
//!         &mut ext,
//!         code: compiled_predicate,
//!         payout,
//!         address_inputs,
//!         bytes_inputs,
//!     );
//!     // execute and return value.
//!     CompiledExecutor::execute(&executable, inputs)
//! }
//! ```
//!
//! ## Example) Logical Connective Predicate Execute
//! ```ignore
//! use ovmi::prepare;
//!
//! type AccountId = u64;
//!
//! // Setting External environment.
//! struct MockExternalCall{..};
//! impl ExternalCall for MockExternalCall {
//!     ...
//! }
//!
//! fn call_execute(address: AccountId, inputs: PredicateCallInputs<AccountId>) -> ExecResult<AccountId> {
//!     let ext = MockExternalCall{..};
//!     let executable = prepare::logical_connective_executable_from_address(
//!         &mut ext,
//!         address,
//!     );
//!     // execute and return value.
//!     LogicalConnectiveExecutor::execute(&executable, inputs)
//! }
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![macro_use]

#[macro_export]
macro_rules! require {
    ($val:expr) => {
        if !($val) {
            return Err(crate::executor::ExecError::Require {
                msg: stringify!($val),
            });
        }
    };
}

#[macro_export]
macro_rules! require_with_message {
    ($val:expr, $message:expr) => {
        if !($val) {
            return Err(crate::executor::ExecError::Require { msg: $message });
        }
    };
}

#[macro_use]
pub extern crate alloc;
use alloc::{collections::btree_map::BTreeMap, vec::Vec};

pub mod compiled_predicates;
pub mod executor;
pub mod predicates;
pub mod prepare;
use codec::{Decode, Encode};
pub use compiled_predicates::CompiledPredicate;
#[cfg(feature = "std")]
pub use prepare::compile_from_json;

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

/// An opaque 32-byte cryptographic identifier.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct AccountId([u8; 32]);

/// An opaque Range(u128, u128).
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Hash))]
pub struct Range {
    pub start: u128,
    pub end: u128,
}