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.
Step 1 of 4: The state
let State =
{ owner : Word 160
, paused : Bool
, count : Word 64
, total : Word 256
}Full example and walkthrough
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)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 }
]- 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) - A layout with its proof.
L.derive Statecomputes a layout from the type. A value ofL.Selection Stateis a layout together with the proof that it is valid.L.derivetakes that proof as an implicit argument, which the elaborator fills because the check computes to no problems. (lines 8:L.Selection State) - 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) - The next slot.
totaltakes 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 ofL.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:
let L = std:EVM/State/Layout
in { select = L.select, checked = L.checked }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:
A field that fits in the rest of the current slot starts where the previous field ended; otherwise it starts at the next slot.
A field that holds a list, a blob or a mapping is placed after the fields that do not, on a slot boundary.
A mapping in the root object whose key has a fixed width of at most 160 bits, the width of an address, is direct: an array of slots indexed by the key, with no hash. Every other mapping is hashed.
Hashed objects live in the top block of the reservation.
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 ->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.