EEVM.Database.InMemory (eevm v0.1.0)

Copy Markdown View Source

In-memory EEVM.Database backend backed by plain maps. It implements every callback, commit/2 included, so EEVM.transact/4 and EEVM.Block.execute/4 return it with their changes applied.

The state is a map with these keys:

  • :accounts — %{address => %{balance, nonce, code, code_hash}}
  • :storage — %{address => %{slot => value}} (per-contract storage)
  • :codes — %{code_hash => code}
  • :block_hashes — %{number => hash}, served to BLOCKHASH

The pre-1.0 write and query functions (get_balance/2, set_balance/3, and so on) still work on this state but are deprecated; use the EEVM.Database functions on a database from new/1.

Summary

Functions

Returns keccak256 of empty code, the code hash of an account with no code.

Creates a new in-memory database state.

Creates a database with this backend; takes the options of init/1.

Types

state()

@type state() :: %{
  accounts: %{required(non_neg_integer()) => map()},
  storage: %{
    required(non_neg_integer()) => %{
      required(non_neg_integer()) => non_neg_integer()
    }
  },
  codes: %{required(binary()) => binary()},
  block_hashes: %{required(non_neg_integer()) => <<_::256>>}
}

Functions

empty_code_hash()

@spec empty_code_hash() :: <<_::256>>

Returns keccak256 of empty code, the code hash of an account with no code.

init(opts \\ [])

@spec init(keyword()) :: state()

Creates a new in-memory database state.

Options

  • :accounts — %{address => %{balance:, nonce:, code:}}; missing fields default to zero and no code (default %{})
  • :storage — %{address => %{slot => value}} (default %{})
  • :block_hashes — %{number => <<_::256>>} for BLOCKHASH (default %{})

Example

iex> state = EEVM.Database.InMemory.init(accounts: %{1 => %{balance: 100}})
iex> EEVM.Database.InMemory.account(state, 1)
{:ok, %{balance: 100, nonce: 0, code: <<>>, code_hash: EEVM.Database.InMemory.empty_code_hash()}}

new(opts \\ [])

@spec new(keyword()) :: EEVM.Database.t()

Creates a database with this backend; takes the options of init/1.

Example

iex> db = EEVM.Database.InMemory.new(accounts: %{1 => %{balance: 100}})
iex> EEVM.Database.get_balance(db, 1)
100