Dispatch through two interfaces
How one record of methods gives a contract a native interface and a Solidity interface, and how a call reaches its handler through either.
An Omegyu contract can answer two kinds of calls: Solidity ABI calls, which existing tools and contracts make, and native calls, in an encoding of Omegyu's own types. A contract declares its methods once, in one record, and routes each method to one interface or to both. The compiler builds both dispatch tables from that record.
Step 1 of 5: Two interfaces
, 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)Full example and walkthrough
, addAll =
C.define
(C.on
(C.Routes.Native C.Name.Field)
(C.writing { Args = { items : List (Word 256) }, Result = Word 256 }))
(\p ->
do write {
initial <- state.count.read;
save
(std:List.foldLeft
(\acc n -> std:Optional.andThen acc (checked.add n))
(Some initial)
p.items)
})
, 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)EVM interfaceruntime 815 bytes, deployment 868 bytes
0x1003e2d2 add(uint256)
0x584e235d addTimes(uint256,uint8)
0xd09de08a increment()
0x06661abd count()
0x4f4d47010510660c add (nonpayable)
0x4f4d4701baf65249 addAll (nonpayable)
0x4f4d470121e9d791 addTimes (nonpayable)
0x4f4d4701cdcc9267 increment (nonpayable)
0x4f4d470117d0e339 read (view)- Two interfaces. Follow
read:C.Routes.Bothmakes one handler reachable through two interfaces. Native callers useread; Solidity callers usecount(). (lines 68–74:C.Routes.Both) - Native only.
addAllis routed withC.Routes.Native: it has a native header and no Solidity selector. (lines 54–57:C.Routes.Native) - One handler, two names.
readis routed to both interfaces: natively asread, and in Solidity ascount(), byC.Name.Rename ("count", refl). Thereflproves that"count"is a valid identifier, which the type checker computes. (lines 68–74:state.count.read) - A native call. A native call starts with an 8-byte header: the prefix
4f4d4701, then the first 4 bytes of the Keccak-256 hash ofomegyu.native.v1:read. The runtime code tests the prefix first, then looks the header up among the native methods. (lines 68–74:native = C.Name.Field) - A Solidity call. A Solidity call starts with the selector of
count(),06661abd. No Solidity selector may equal the prefix, which the compiler checks, so the call goes to the Solidity table and reaches the same handler,state.count.read. (lines 68–74:C.Name.Rename ("count", refl))
The order of the tests
The runtime code tests a call in this order, as the dispatch of
lib/EVM/Contract.omg stages it:
When no handler is payable, a call that sends value reverts with empty data. Otherwise each handler that is not payable checks it.
When the contract has Solidity methods, one test of the native prefix in the first four bytes comes next. A native call goes on to the switch on its 8-byte header.
Call data shorter than four bytes goes to
receivewhen it is empty, and to the fallback otherwise.The other calls go to the switch on the Solidity selector, whose default is the fallback.
A native header with an unknown selector reaches the fallback through the
Solidity switch. Counter's handlers are not payable, and it has no
receive or fallback, so a call that sends value, short call data and an
unknown selector all revert.
Native headers
A native method's header is the prefix 0x4f4d4701, the ASCII letters
OMG and the byte 1, and then the first four bytes of the Keccak-256 hash
of omegyu.native.v1: followed by the method's name. In a composed
contract, the name is qualified by its owner. The prefix makes a native
call and a Solidity call distinct by their first four bytes, so the two
tables never compete for a call.
Building a contract
omegyu evm build compiles a contract and prints its runtime code, its init
code and both tables. The EVM back end describes the
command and its output, and Omegyu vs. Solidity compares
the generated code with solc's.