Code Monkey home page Code Monkey logo

koinos-mock-vm's Introduction

koinos-mock-vm

Koinos-Mock-VM is a NodeJS application/package that allows you to run WASM Koinos smart contracts without deploying the contracts to the Koinos blockchain. It is a tool that you can leverage to build unit tests for your smart contracts.

Requirements:

You need to install NodeJS v16 or higher download

Installation:

# with npm
npm install -g @roamin/koinos-mock-vm

# with yarn
yarn global add @roamin/koinos-mock-vm

Interact with the mock vm

koinos-mock-vm leverages the Koinos system calls to enable a wasm contract to interact with the mock vm, which means that any Koinos wasm contract can use it out of the box.

The put_object system call is used to insert mock data in the database powering the mock vm. When calling a system call, like get_entry_point, the mock vm will pull the mock data that was initially set in the database. The metadata system space is used to store the mock data. (space zone = "", space id = 0, system = true)

List of keys used to store the mock data:

  • entry_point:
    • object of type koinos.chain.value_type with the int32_value property set with the entry point
    • used by the system call get_entry_point
  • contract_arguments:
    • object of type bytes set with the contract arguments
    • used by the system call get_contract_argument
  • contract_result:
    • object of type bytes set with the contract result
    • used by the system call set_contract_result
  • contract_id:
    • object of type bytes set with the contract id
    • usde by the system call get_contract_id
  • head_info:
    • object of type koinos.chain.head_info set with the head info
    • used by the system call get_head_info
  • caller:
    • object of type koinos.chain.caller_data set with the caller data info
    • used by the system call get_caller
  • last_irreversible_block:
    • object of type koinos.chain.value_type with the uint64_value property set with the last irreversible block height
    • used by the system call get_last_irreversible_block
  • transaction:
    • object of type koinos.protocol.transaction set with the transaction info
    • used by the system calls get_transaction and get_transaction_field
  • operation:
    • object of type koinos.protocol.operation set with the operation info
    • used by the system calls get_operation
  • block:
    • object of type koinos.protocol.block set with the block info
    • used by the system calls get_block and get_block_field
  • authority:
    • object of type koinos.chain.list_type. Each koinos.chain.value_type elements represent an authorization:
      • int32_value: value of the koinos.chain.authorization_type
      • bytes_value: bytes of the account address
      • bool_value: "autorized" boolean
    • used by the system call require_authority:
      • will use the require_authority arguments (type and account) to lookup the autorization in the previously set koinos.chain.list_type
  • call_contract_results:
    • object of type koinos.chain.list_type. Each koinos.chain.value_type elements represent a contract call result:
      • bytes_value: bytes of the contract call result
    • used by the system call call_contract:
      • will use the previously set koinos.chain.list_type to return a call result. The call contract results are FIFO meaning that the first call_contract will use the first element you set in the list, the second call the second element in the list, etc...
  • logs:
    • object of type koinos.chain.list_type. Each koinos.chain.value_type has its string_value set with a log
    • used by the system call log
  • events:
    • object of type koinos.chain.list_type. Each koinos.chain.value_type has its bytes_value set with a koinos.protocol.event_data object
    • used by the system call event
  • exit_code:
    • object of type koinos.chain.exit_contract_arguments
    • used by the system call exit_contract

The following keys are additional commands that allow you to interact with the mock vm's database:

  • reset:
    • whatever object as argument
    • will reset the database
  • rollback_transaction:
    • whatever object as argument
    • will restore the backup made in the previous commit_transaction
  • commit_transaction:
    • whatever object as argument
    • will save a backup of the actual state of the database (useful when trying to build unit tests around transaction reversions)

Example for setting the contract id in an AssemblyScript Koinos smart contract:

const metadataSpace = new chain.object_space();
metadataSpace.system = true;
metadataSpace.id = 0;

System.putBytes(metadataSpace, 'contract_id', Base58.decode('1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe'));

const contractId = System.getContractId();
System.log('contractId: ' + Base58.encode(contractId));
// will print contractId: 1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe

Usage as a CLI

You can execute multiple smart contracts in one run, they will be executed in the order they appear in the command parameters. The koinos-mock-vm database will be shared between each execution allowing you to build complex executions cases that involve several contracts.

koinos-mock-vm <path to contract wasm 1> <path to contract wasm 2> ... <path to contract wasm n>

Usage as a NodeJS package

See index.js file in the bin folder.

CLI Example

koinos-mock-vm contract.wasm

[Starting VM] 1 contracts to execute
[Execution started] contract.wasm
[Log] entryPoint: 3282800625
[Log] contract_arguments: myArgs
[Log] contract_id: 1DQzuCcTKacbs9GGScRTU1Hc8BsyARTPqe
[Execution completed] in 16.232577ms contract.wasm
[Stopping VM] exit code 0

koinos-mock-vm's People

Contributors

joticajulian avatar mvandeberg avatar roaminro avatar sgerbino avatar

Stargazers

 avatar  avatar

Watchers

 avatar

koinos-mock-vm's Issues

bug: getNextObject getPrevObject should work if the key provided is unknown

How to reproduce?

key = new Uint8Array(1); // this is an unknown key
result = getNextObject(space, key);

In the current implementation result is always null because it can not find the key. However, the existence of this key should be optional. It should use it as reference to get the lower bound and search from that point https://github.com/koinos/koinos-chain/blob/3a2974927b627f6f3fcc5cadabfa5578ea68808b/libraries/state_db/state_db.cpp#L452

[BUG]: retBuffer is not correctly initialized

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

the problem I have is when I call 2 times get_object

the first object is returned, that part is OK.
but if the second one is "null", then the first one is returned.

So, at the end I receive 2 times the same value, and I expect 1 value and null.

The issue seems to be related with the initialization of retBuf, which is taking previous data.

Expected behavior

No response

Steps to reproduce

No response

Environment

- OS:

Anything else?

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.