evm v0.1.1 EVM.VM

The core of the EVM which runs operations based on the opcodes of a contract during a transfer or message call.

Link to this section Summary

Functions

Runs a single cycle of our VM returning the new state, defined as O in the Yellow Paper, Eq.(131)

Runs a cycle of our VM in a recursive fashion, defined as X, Eq.(122) of the Yellow Paper. This function halts when return is called or an exception raised

This function computes the Ξ function Eq.(116) of the Section 9.4 of the Yellow Paper. This is the complete result of running a given program in the VM

Link to this section Types

Link to this type output()
output() :: binary

Link to this section Functions

Runs a single cycle of our VM returning the new state, defined as O in the Yellow Paper, Eq.(131).

Examples

iex> state = MerklePatriciaTree.Trie.new(MerklePatriciaTree.Test.random_ets_db(:evm_vm_test))
iex> EVM.VM.cycle(state, %EVM.MachineState{pc: 0, gas: 5, stack: [1, 2]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])})
{%MerklePatriciaTree.Trie{db: {MerklePatriciaTree.DB.ETS, :evm_vm_test}, root_hash: <<128>>}, %EVM.MachineState{pc: 1, gas: 5, stack: [3]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])}}

Runs a cycle of our VM in a recursive fashion, defined as X, Eq.(122) of the Yellow Paper. This function halts when return is called or an exception raised.

TODO: Add gas to return

Examples

iex> EVM.VM.exec(%{}, %EVM.MachineState{pc: 0, gas: 5, stack: [1, 2]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])})
{%{}, %EVM.MachineState{pc: 2, gas: 5, stack: [3]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])}, <<>>}

iex> EVM.VM.exec(%{}, %EVM.MachineState{pc: 0, gas: 5, stack: []}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add])})
{%{}, %EVM.MachineState{pc: 6, gas: 5, stack: [8]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add])}, ""}

iex> EVM.VM.exec(%{}, %EVM.MachineState{pc: 0, gas: 5, stack: []}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add, :push1, 0x00, :mstore, :push1, 0, :push1, 32, :return])})
{%{}, %EVM.MachineState{active_words: 1, memory: <<0x08::256>>, pc: 13, gas: 5, stack: []}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add, :push1, 0x00, :mstore, :push1, 0, :push1, 32, :return])}, <<0x08::256>>}

This function computes the Ξ function Eq.(116) of the Section 9.4 of the Yellow Paper. This is the complete result of running a given program in the VM.

Examples

# Full program
iex> EVM.VM.run(%{}, 5, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add, :push1, 0x00, :mstore, :push1, 0, :push1, 32, :return])})
{%{}, 5, %EVM.SubState{}, <<0x08::256>>}

# Program with implicit stop
iex> EVM.VM.run(%{}, 5, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add])})
{%{}, 5, %EVM.SubState{}, ""}

# Program with explicit stop
iex> EVM.VM.run(%{}, 5, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :stop])})
{%{}, 5, %EVM.SubState{}, ""}

# Program with exception halt
iex> EVM.VM.run(%{}, 5, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])})
{nil, 5, %EVM.SubState{}, ""}