2 unstable releases

0.3.0 Jun 10, 2021
0.1.0 Aug 1, 2020

#4 in #prepare

46 downloads per month
Used in 2 crates

Apache-2.0

160KB
4K SLoC

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

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

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

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)
}

Dependencies

~4.5MB
~96K SLoC