Concept 8 of 8

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.

A native call and a Solidity call reaching the handlers of Counter
1 / 5

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)
Your result
Complete file: examples/evm/Counter.omg · with imports
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)
Your result
Complete file: examples/evm/Counter.omg · with imports

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)
  1. Two interfaces. Follow read: C.Routes.Both makes one handler reachable through two interfaces. Native callers use read; Solidity callers use count(). (lines 68–74: C.Routes.Both)
  2. Native only. addAll is routed with C.Routes.Native: it has a native header and no Solidity selector. (lines 54–57: C.Routes.Native)
  3. One handler, two names. read is routed to both interfaces: natively as read, and in Solidity as count(), by C.Name.Rename ("count", refl). The refl proves that "count" is a valid identifier, which the type checker computes. (lines 68–74: state.count.read)
  4. 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 of omegyu.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)
  5. 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:

  1. When no handler is payable, a call that sends value reverts with empty data. Otherwise each handler that is not payable checks it.

  2. 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.

  3. Call data shorter than four bytes goes to receive when it is empty, and to the fallback otherwise.

  4. 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.