std:EVM/Effect

lib/EVM/Effect.omg

lib/EVM/Effect.omg291 lines
1{-  Effect-checked EVM actions and handler declarations.
2
3    An action quantifies over its interpreter's monad. Its permitted
4    operations occur in its type, so a captured write action cannot be run
5    as a view action. Handler tags carry the correspondingly typed body;
6    there is no independent Boolean claiming that a body is readonly.
7
8    `std:EVM/Contract` declares every handler of every entry through this
9    layer: its modes, its action and its handler tags are these.
10-}
11
12let Monad = std:Monad.Monad
13let Interfaces = std:Reflection/Interface
14
15let Signature = std:EVM/Signature
16
17let Base = std:EVM/Base
18
19let Mode = < Pure | View | Write | Payable >
20
21let Control =
22      \(Val Flag : Type) (m : Type -> Type) ->
23        { monad : Monad m
24        , words : Signature.Words Val Flag
25        , keccakText : Text -> Val
26        , branch : forall {a : Type} -> Flag -> m a -> m a -> m a
27        , switch : forall {a : Type} -> Val -> List (Base.Case Val (m a)) -> m a -> m a
28        , loop : Val -> List Val -> (Val -> List Val -> m (List Val)) -> m (List Val)
29        , share : forall {a : Type} -> List Val -> (List Val -> m a) -> m a
30        , revert : forall {a : Type} -> List (Signature.Chunk Val) -> m a
31        }
32
33let Read =
34      \(Val Flag : Type) (m : Type -> Type) ->
35        { frame : Base.Frame Val
36        , sload : Signature.Key Val -> m Val
37        , tload : Val -> m Val
38        , staticcall : Signature.StaticCall Val -> m Flag
39        , rdsize : m Val
40        , rdload : Val -> m Val
41        , bubble : forall {a : Type} -> m a
42        }
43
44let Mutation =
45      \(Val Flag : Type) (m : Type -> Type) ->
46        { sstore : Signature.Key Val -> Val -> m unit
47        , tstore : Val -> Val -> m unit
48        , log : List Val -> List (Signature.Chunk Val) -> m unit
49        , call : Signature.Call Val -> m Flag
50        }
51
52let Access =
53      \(kind : Mode) (Val Flag : Type) (extra : (Type -> Type) -> Row Type) (m : Type -> Type) ->
54        merge
55          { Pure = { ...extra m }
56          , View = { read : Read Val Flag m, ...extra m }
57          , Write = { read : Read Val Flag m, write : Mutation Val Flag m, ...extra m }
58          , Payable = { read : Read Val Flag m, write : Mutation Val Flag m, ...extra m }
59          }
60          kind
61
62-- Each action works with every extension of its required capability row.
63-- Ordinary implicit row instantiation admits a pure action in a read/write
64-- context and a read action in a write context, including captured helpers.
65-- A write action cannot require its missing fields from an arbitrary tail.
66let ActionFor =
67      \(capability : ((Type -> Type) -> Row Type) -> (Type -> Type) -> Type) (Val Flag a : Type) ->
68        forall {extra : (Type -> Type) -> Row Type} (m : Type -> Type)
69        -> Control Val Flag m
70        -> capability extra m
71        -> m a
72
73let Action = \(kind : Mode) (Val Flag a : Type) -> ActionFor (Access kind Val Flag) Val Flag a
74
75let monadFor =
76      \(capability : ((Type -> Type) -> Row Type) -> (Type -> Type) -> Type) (Val Flag : Type) ->
77        ({ pure = \{a : Type} (x : a) m c access -> c.monad.pure x
78         , bind =
79             \{a b : Type} x f {extra} m c access ->
80               c.monad.bind (x @extra m c access) (\(y : a) -> f y @extra m c access)
81         } :
82           Monad (ActionFor capability Val Flag))
83
84let monad
85    : forall (kind : Mode) {Val Flag : Type} -> Monad (Action kind Val Flag)
86    = \kind {Val} {Flag} -> monadFor (Access kind Val Flag) Val Flag
87
88let load
89    : forall {Val Flag : Type} -> Signature.Key Val -> Action Mode.View Val Flag Val
90    = \key m c access -> access.read.sload key
91
92let store
93    : forall {Val Flag : Type} -> Signature.Key Val -> Val -> Action Mode.Write Val Flag unit
94    = \key value m c access -> access.write.sstore key value
95
96-- Compatibility aliases: ordinary action use now infers this inclusion.
97let promotePure
98    : forall {Val Flag a : Type} -> Action Mode.Pure Val Flag a -> Action Mode.View Val Flag a
99    = \action -> action
100
101let promoteView
102    : forall {Val Flag a : Type} -> Action Mode.View Val Flag a -> Action Mode.Write Val Flag a
103    = \action -> action
104
105let control
106    : forall {Val Flag : Type} {m : Type -> Type} -> Base.Base Val Flag m -> Control Val Flag m
107    = \b ->
108        { monad = b.monad
109        , words = b.words
110        , keccakText = b.keccakText
111        , branch = b.branch
112        , switch = b.switch
113        , loop = b.loop
114        , share = b.share
115        , revert = b.revert
116        }
117
118let readAccess
119    : forall {Val Flag : Type} {m : Type -> Type} -> Base.Base Val Flag m -> Read Val Flag m
120    = \b ->
121        { frame = b.frame
122        , sload = b.sload
123        , tload = b.tload
124        , staticcall = b.staticcall
125        , rdsize = b.rdsize
126        , rdload = b.rdload
127        , bubble = b.bubble
128        }
129
130let mutation
131    : forall {Val Flag : Type} {m : Type -> Type} -> Base.Base Val Flag m -> Mutation Val Flag m
132    = \b -> { sstore = b.sstore, tstore = b.tstore, log = b.log, call = b.call }
133
134let runPure
135    : forall {Val Flag a : Type} {m : Type -> Type}
136      -> Base.Base Val Flag m
137      -> Action Mode.Pure Val Flag a
138      -> m a
139    = \{Val} {Flag} {a} {m} b action -> action @(\_ -> (||)) m (control b) {=}
140
141let runView
142    : forall {Val Flag a : Type} {m : Type -> Type}
143      -> Base.Base Val Flag m
144      -> Action Mode.View Val Flag a
145      -> m a
146    = \{Val} {Flag} {a} {m} b action -> action @(\_ -> (||)) m (control b) { read = readAccess b }
147
148let runWrite
149    : forall {Val Flag a : Type} {m : Type -> Type}
150      -> Base.Base Val Flag m
151      -> Action Mode.Write Val Flag a
152      -> m a
153    = \{Val} {Flag} {a} {m} b action ->
154        action @(\_ -> (||)) m (control b) { read = readAccess b, write = mutation b }
155
156let HandlerFor =
157      \(Action : Mode -> Type -> Type) (Args Result : Type) ->
158        < Pure : Args -> Action Mode.Pure Result
159        | View : Args -> Action Mode.View Result
160        | Write : Args -> Action Mode.Write Result
161        | Payable : Args -> Action Mode.Payable Result
162        >
163
164let Handler =
165      \(Val Flag Args Result : Type) -> HandlerFor (\kind -> Action kind Val Flag) Args Result
166
167let pure
168    : forall {Val Flag Args Result : Type}
169      -> (Args -> Action Mode.Pure Val Flag Result)
170      -> Handler Val Flag Args Result
171    = \{Val} {Flag} {Args} {Result} -> (Handler Val Flag Args Result).Pure
172
173let view
174    : forall {Val Flag Args Result : Type}
175      -> (Args -> Action Mode.View Val Flag Result)
176      -> Handler Val Flag Args Result
177    = \{Val} {Flag} {Args} {Result} -> (Handler Val Flag Args Result).View
178
179let write
180    : forall {Val Flag Args Result : Type}
181      -> (Args -> Action Mode.Write Val Flag Result)
182      -> Handler Val Flag Args Result
183    = \{Val} {Flag} {Args} {Result} -> (Handler Val Flag Args Result).Write
184
185let payable
186    : forall {Val Flag Args Result : Type}
187      -> (Args -> Action Mode.Payable Val Flag Result)
188      -> Handler Val Flag Args Result
189    = \{Val} {Flag} {Args} {Result} -> (Handler Val Flag Args Result).Payable
190
191let kindOf
192    : forall {Val Flag Args Result : Type} -> Handler Val Flag Args Result -> Mode
193    = \handler ->
194        merge
195          { Pure = \_ -> Mode.Pure
196          , View = \_ -> Mode.View
197          , Write = \_ -> Mode.Write
198          , Payable = \_ -> Mode.Payable
199          }
200          handler
201
202let run
203    : forall {Val Flag Args Result : Type} {m : Type -> Type}
204      -> Base.Base Val Flag m
205      -> Handler Val Flag Args Result
206      -> Args
207      -> m Result
208    = \{Val} {Flag} {Args} {Result} {m} b handler args ->
209        let nonpayable =
210              \action ->
211                b.branch
212                  (b.words.isZero b.frame.callvalue)
213                  action
214                  (b.revert ([] : List (Signature.Chunk Val)))
215        in  merge
216              { Pure = \body -> nonpayable (runPure b (body args))
217              , View = \body -> nonpayable (runView b (body args))
218              , Write = \body -> nonpayable (runWrite b (body args))
219              , Payable = \body -> runWrite b (body args)
220              }
221              handler
222
223-- A native interface supplies the exact handler field names and each
224-- parameter/result type. Metadata is folded from the same declarations
225-- that run interprets; no list of names or independently editable mode is
226-- accepted. Public codecs can use the recursive boundary descriptions in
227-- interface.foldHandlers.
228let declareWith =
229      \{Val Flag : Type}
230       {S : {} -> Type}
231       (description : Interfaces.Native.Package S)
232       (handlers : description.1 {=} (Handler Val Flag)) ->
233        let interface = description.2
234        in  { handlers
235            , interface
236            , metadata =
237                interface.foldHandlers
238                  {=}
239                  (Handler Val Flag)
240                  (List { name : Text, position : Optional Natural, mode : Mode })
241                  (\A B name position args result handler rest ->
242                     [ { name, position, mode = kindOf @Val @Flag @(A {=}) @(B {=}) handler } ]
243                     # rest)
244                  handlers
245                  []
246            , run =
247                \{m : Type -> Type} (b : Base.Base Val Flag m) ->
248                  interface.mapHandlers
249                    {=}
250                    (Handler Val Flag)
251                    (\(A B : Type) -> A -> m B)
252                    (\A B name position handler -> run @Val @Flag @A @B @m b handler)
253                    handlers
254            }
255
256let declare =
257      \{Val Flag : Type}
258       {S : {} -> Type}
259       (source : Reflect @1 {=} S)
260       {valid : Interfaces.NativeValid @S source} ->
261        declareWith @Val @Flag @S (Interfaces.native @S source @valid)
262
263in  { Mode
264    , ActionFor
265    , monadFor
266    , HandlerFor
267    , control
268    , readAccess
269    , mutation
270    , Control
271    , Read
272    , Mutation
273    , Access
274    , Action
275    , monad
276    , load
277    , store
278    , promotePure
279    , promoteView
280    , runPure
281    , runView
282    , runWrite
283    , Handler
284    , pure
285    , view
286    , write
287    , payable
288    , kindOf
289    , run
290    , declare
291    }
Your result
Complete file: lib/EVM/Effect.omg · with imports