Concept 7 of 8

Storage layout from types

How a contract's storage layout is computed from the type of its state, and why a layout comes with the proof that it is valid.

A contract's state is a record type. std:EVM/State/Layout.derive computes from that type where each field is stored: in which 256-bit slot, and at which bit. The layout is an ordinary value, which a program can inspect, and it comes with the proof that it satisfies the invariants that the generated code relies on.

The fields of a state record placed in two 256-bit storage slots
1 / 4

Step 1 of 4: The state

let State =
      { owner : Word 160
      , paused : Bool
      , count : Word 64
      , total : Word 256
      }
Your result
Complete example · with imports
Full example and walkthrough
Normal form
let L = std:EVM/State/Layout
let State =
      { owner : Word 160
      , paused : Bool
      , count : Word 64
      , total : Word 256
      }
let layout : L.Selection State = L.derive State
-- Where each field of the layout's records starts, and its width.
let width = std:EVM/State/Layout/Address.width
let Placed = { name : Text, offset : Natural, bits : Natural }
let placed =
      \(d : L.Description) ->
        std:List.concatMap
          (\(node : L.Node) ->
             merge
               { Fields =
                   std:List.map
                     (\(m : L.Member) ->
                        { name = m.name
                        , offset = m.offset
                        , bits = width d.nodes m.child
                        })
               }
               node.form
               otherwise (\_ -> ([] : List Placed)))
          d.nodes
in  placed (L.describe layout)
Your result
Complete example · with imports

Normal form

[ { bits = 64, name = "count", offset = 0 }
, { bits = 160, name = "owner", offset = 64 }
, { bits = 1, name = "paused", offset = 224 }
, { bits = 256, name = "total", offset = 256 }
]
  1. The state. The state of a contract is a record type. Its fields take 160, 1, 64 and 256 bits. (lines 2–7: let State)
  2. A layout with its proof. L.derive State computes a layout from the type. A value of L.Selection State is a layout together with the proof that it is valid. L.derive takes that proof as an implicit argument, which the elaborator fills because the check computes to no problems. (lines 8: L.Selection State)
  3. Packed in slot 0. The fields are placed in the canonical order of their labels: count, owner, paused, total. The first three take 225 bits, so they share slot 0, at bits 0, 64 and 224. Each short interval on the ruler is one byte; long ticks mark 64 bits. (lines 8: L.derive State)
  4. The next slot. total takes 256 bits, more than the 31 left in slot 0, so it starts at bit 256, the start of slot 1. The program lists the fields of L.describe layout, and its normal form below gives the offsets. (lines 28: L.describe layout)

Validity is a type

L.Selection State is a dependent pair: the description of State that reflection derives, and a layout checked against it, which is a description of the layout with the proof that L.problems finds nothing wrong with it. The public field names the problems function in the validator. L.select builds one from a description that you write, with the proof as an implicit argument, and L.checked checks a computed description and returns either its problems or the selection:

Type
let L = std:EVM/State/Layout
in  { select = L.select, checked = L.checked }
Your result
Complete example · with imports

Type

{ checked :
    forall {A : Type}
    -> std:EVM/State/Layout/Description.Of A
    -> std:EVM/State/Layout/Description.Description
    -> std:Result.Result (List Text) (std:EVM/State/Layout.Selection A)
, select :
    forall {A : Type}
           (source : std:EVM/State/Layout/Description.Of A)
           (description : std:EVM/State/Layout/Description.Description)
           {valid : std:EVM/State/Layout.Valid source description}
    -> std:EVM/State/Layout.Selection A
}

The proof is computed, not assumed. A description whose fields overlap fails the check with "storage members overlap", so no proof of its validity exists, and L.select refuses it. The predicate states the invariants that the access code relies on, not the policy that L.derive follows, so a layout that you write and a derived one have the same type. lib/EVM/State/Layout/Validate.omg defines it.

What the derived layout does

lib/EVM/State/Layout/Derive.omg is the policy of L.derive:

In a contract

Counter declares a state type once. Its assembly binds state for the handlers: state.count.read reads the current count and state.count.write next updates it. Neither expression names a slot. In Token, state.balances owner selects one account; the key comes from the call, not from the layout.

let storage = C.storage State
let layout : std:EVM/State/Layout.Selection State = std:EVM/State/Layout.derive State
let contract =
      storage.bind
        (\state ->
Your result
Complete file: examples/evm/Counter.omg · with imports

The compiler accepts this program

The generated code reads and writes each field where the layout puts it, and docs/evm.md describes the storage format.