GithubHelp home page GithubHelp logo

hyvm's Introduction

HyVM

logo     ✕    

...the execution core of nested.fi


HyVM is an Ethereum Virtual Machine (EVM) Hypervisor written in Huff, allowing the execution of arbitrary EVM Bytecode.

👉 See it in action with HyVM live playground


function execute() public returns (uint256 result) {
    // Bytecode for “3 + 4" and return the result
    (bool success, bytes memory data) = HyVM.delegatecall(hex”600360040160005260ff6000f3");
    result = abi.decode(data, (uint256));
}

What is an hypervisor?

According to vmware...

A hypervisor, also known as a virtual machine monitor or VMM, is software that creates and runs virtual machines (VMs). A hypervisor allows one host computer to support multiple guest VMs by virtually sharing its resources, such as memory and processing.

In our case, the HyVM allows to run an EVM on top the EVM and execute opcodes directly.

Why?

Using the HyVM gives a maximum of flexibility, it replaces using specific scripts to interact with external protocols. There is no limit on which interactions that can be created. Custom and complex logic with chained calls can be executed by the HyVM opening a lot possibilities. Repetive intructions and common ones could also be called as helper contract if needed.

Examples of use

Static functions / get rid of helpers

The most straightforward use is for readonly functions... no need to deploy helper contracts to do lots of things !

👉 For instance, see this gist which demonstrates how to fetch multiple balances on-chain at once.

More general use

Another way to use the HyVM is to use it as a library called with delegatecall. As shown below, a Contract Wallet allows to execute a delegatecall (or several), and thus to call the HyVM. The DApp managing the contract wallet gives the bytecode for the HyVM to execute. For example :

  • Swap multiple assets.
  • Approve & deposit.
image

How it works?

Calling the HyVM

The HyVM ingests raw bytecode (as input) via call or delegateCall, then processes the opcodes and executes them.

image

Bytecode processing

A pointer reads an opcode from the bytecode (extracted from calldata), translates it into a HyVM opcode implementation (more details below), executes and moves on to the next opcode. The pointer is stored in memory and updated each time it moves.

image

Each opcode is re-implemented to fit the HyVM memory layout and logic.

image

Memory Layout

When running a smart contract, you have the whole memory available (from 0x00 to infinity). However, the HyVM needs some memory for its internals. This memory is taken from the 0x00 offset. Every opcode call that accesses memory (ran by the host) will be fixed to skip this reserved memory.

The HyVM private memory layout is as follows:

  • [0x00-0x20] 👉 Execution pointer
  • [0x20-0x220] 👉 Jump table
  • [0x220-0x340] 👉 Memory reserved for debug purposes (see debug-utils.huff)
  • [0x220-0x460] (when contract contract verifier is enabled) 👉 Memory used to store contract verification call args & result. nb: It overlaps debug memory (because we dont need them both at the same time)

Thus, the actual memory of the host is starting at either 0x340 or 0x460 depending on the chosen configuration.

Addresses

Deployed at 0x36dAc1C6a72F94C13369Db9DAdCBD79ba5425019 on:

Getting Started

You will need:

You can find easm, the basic EVM assembly compiler that is used to compile tests here You can use pyevmasm to disassemble bytecode

How to deploy

Example of how to deploy to Polygon:

forge script --private-key XXXXXXXXXXXXX --mnemonic-indexes 4 --chain-id 137 --rpc-url https://polygon-rpc.com --froms 0x945f803f01F443616546d1F31466c0E7ACfF36f7 script/Deploy.s.sol --broadcast --gas-price 40000000000 --gas-limit 9632030 --legacy

hyvm's People

Contributors

oguimbal avatar obatirou avatar yashiru avatar maximebrugel avatar erjanmx avatar

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.