# `EEVM.Database.InMemory`
[🔗](https://github.com/mw2000/eevm/blob/v0.1.0/lib/eevm/database/in_memory.ex#L1)

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`.

# `state`

```elixir
@type state() :: %{
  accounts: %{required(non_neg_integer()) =&gt; map()},
  storage: %{
    required(non_neg_integer()) =&gt; %{
      required(non_neg_integer()) =&gt; non_neg_integer()
    }
  },
  codes: %{required(binary()) =&gt; binary()},
  block_hashes: %{required(non_neg_integer()) =&gt; &lt;&lt;_::256&gt;&gt;}
}
```

# `empty_code_hash`

```elixir
@spec empty_code_hash() :: &lt;&lt;_::256&gt;&gt;
```

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

# `init`

```elixir
@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`

```elixir
@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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
