Actions and modes
How the type of a contract's action says which operations of the chain it may run, so that a view cannot write, and a read can run wherever a write may.
A contract's handler is an action: a value that describes what to do with
the chain, which the compiler turns into code. An action does not reach the
chain's operations by itself. It receives them as a record, and the type of
that record depends on the handler's mode: Pure, View, Write or
Payable. An action cannot use an operation that it is not given, so the
type checker checks the mode of every handler.
Step 1 of 5: Select an access record
let E = std:EVM/Effect
let peek
: forall {extra : (Type -> Type) -> Row Type} (m : Type -> Type)
-> E.Access E.Mode.View Natural Bool extra m
-> E.Mutation Natural Bool m
= \{extra} m access -> access.write
in peekFull example and walkthrough
let E = std:EVM/Effect
let peek
: forall {extra : (Type -> Type) -> Row Type} (m : Type -> Type)
-> E.Access E.Mode.View Natural Bool extra m
-> E.Mutation Natural Bool m
= \{extra} m access -> access.write
in peekError E0006
error[E0006]: the record has no field `write`
┌─ example.omg:6:35
│
6 │ = \{extra} m access -> access.write
│ ^^^^^ unknown field
│
= its fields are: `read`
- Select an access record.
E.Accessis the type of the record of operations that an action receives. It depends on the mode, one of the alternatives ofE.Mode:Pure,View,WriteandPayable. (lines 4:E.Access) - Leave extra operations open. The caller supplies
extra. Sincepeekmust work for any such row, it cannot rely onextracontaining a write operation. (lines 3:extra : (Type -> Type) -> Row Type) - Choose a read-only view. A view receives
read: storage loads, static calls and the results of calls. (lines 4:E.Mode.View) - Ask for a mutation. The result type says
peekreturns a record of write operations,E.Mutation. AWriteorPayablehandler receives that record. Its argument only grantsViewaccess. (lines 5:E.Mutation) - A view cannot write.
peekselectsaccess.writefrom its argument. View access promises nowritefield, so the type checker refuses the program with E0006. A view can still run whereverreadis given, as the next section shows. (lines 6:access.write)
A read where a write is allowed
An action is polymorphic in extra, the operations that it does not name.
A view therefore works where a write is expected, with write in its
extra, without a conversion:
let C =
std:EVM/Contract.withSoliditySignals { Events = <>, Errors = <> }
let State = { count : Word 256 }
let storage = C.storage State
let inWrite : storage.Bindings -> C.Action State C.Mode.Write (Word 256)
= \state ->
let current : C.Action State C.Mode.View (Word 256) = state.count.read
in current
in inWriteThe compiler accepts this program
The converse does not hold: a write action cannot take its write field
from an arbitrary extra, so it is refused where a view is expected.
lib/EVM/Effect.omg defines the modes and the
records of operations.
Handlers in a contract
Each method of examples/evm/Counter.omg
declares its mode. C.writing gives a handler the operations of a write:
let methods =
C.methods
{ increment =
C.define
(C.writing { Args = {}, Result = {} })
(\_ ->
do write {
previous <- state.count.read;
state.count.write (wrapping.add previous (Word/fromNatural 256 1))
})The compiler accepts this program
C.reading gives read the operations of a view, so the handler can only
read the counter:
, read =
C.define
(C.on
(C.Routes.Both
{ native = C.Name.Field, solidity = C.Name.Rename ("count", refl) })
(C.reading { Args = {}, Result = Word 256 }))
(\_ -> state.count.read)The compiler accepts this program
None of Counter's handlers is payable, so its runtime code checks once, before it looks for a handler, that a call sends no value. Dispatch through two interfaces follows a call from there.
Why the mode is in the type
The operations are fields of a record that the action receives, so an action is an ordinary function. The compiler applies it to the operations of the EVM and computes the result, and the record of operations computes away. What a handler cannot do is decided by the type checker, before any code is generated, and needs no analysis of the generated code.