evm v0.1.11 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_2))
iex> EVM.VM.cycle(state, %EVM.MachineState{program_counter: 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_2}, root_hash: MerklePatriciaTree.Trie.empty_trie_root_hash}, %EVM.MachineState{program_counter: 1, gas: 2, 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{program_counter: 0, gas: 5, stack: [1, 2]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])})
{%{}, %EVM.MachineState{program_counter: 2, gas: 2, stack: [3]}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:add])}, <<>>}

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

iex> EVM.VM.exec(%{}, %EVM.MachineState{program_counter: 0, gas: 24, stack: []}, %EVM.SubState{}, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add, :push1, 0x00, :mstore, :push1, 32, :push1, 0, :return])})
{%{}, %EVM.MachineState{active_words: 1, memory: <<0x08::256>>, gas: 0, program_counter: 13, stack: []}, %EVM.SubState{logs: "", refund: 0, suicide_list: []}, %EVM.ExecEnv{machine_code: <<96, 3, 96, 5, 1, 96, 0, 82, 96, 32, 96, 0, 243>>}, <<8::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(%{}, 24, %EVM.ExecEnv{machine_code: EVM.MachineCode.compile([:push1, 3, :push1, 5, :add, :push1, 0x00, :mstore, :push1, 32, :push1, 0, :return])})
{%{}, 0, %EVM.SubState{}, <<0x08::256>>}

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

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

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