What is behind Solidity Bytecode?

Leonid Astakhov
Intrachain Insights
2 min readOct 19, 2018

Dear Intrachain Community,

In my last blog post I have elaborated on Solidity which is the most popular contract oriented language in Ethereum ecosystem. This week I want to further highlight some of the elements of solidity.

Compiling Solidity into EVM bytecode

The EVM bytecode does not have support for functions despite that smart contracts are rendered as a set of functions in Solidity. Therefore, the Solidity compiler introduces the function dispatching mechanism as a first part of contract implementation. Each function is uniquely identified by a signature, based on its name and type parameters. When the function is invoked its signature is passed to the called contract. If it matches to some function the execution jumps to the corresponding function code, otherwise it jumps to the fallback function. This is a special function with no name and parameters, which can be arbitrarily provided. The fallback function is also executed when the contract is passed an empty signature. This, for instance, happens when sending ether to the contract.

Contract invocation

Solidity provides three different constructs to invoke a contract from another contract also allowing to send ether. All these constructs are compiled using the same bytecode instruction. The result is that the same behavior can be implemented in several ways, with subtle differences.

As described above there are three different ways to invoke a contract:

  1. call invokes a function (of another contract, or of itself), and transfers ether to the callee. E.g., one can invoke the function f1 of contract c as follows: c.call.value(amount)(bytes4(sha3(“f1(uint256)”)),n); where the called function is identified by the first 4 bytes of its hashed signature, amount determines how many wei have to be transferred to c, and n is the actual parameter of f1. Remarkably, if a function with the given signature does not exist at address c, then the fallback function of c is executed, instead.
  2. send is used to transfer ether from the running contract to some recipient r as in r.send(amount). After the ether has been transferred, send executes recipient’s fallback.
  3. delegatecall is similar to call , with the difference that the invocation of the called function is run in the caller environment. For instance, executing c.delegatecall.value(amount)(bytes4(sha3(“f1(uint256)”)),n), if f1 contains the variable this, it refers to the caller address and not to c, and in case of ether transfer to some recipient d.send(amount)- the ether is taken from the caller balance.

I hope this blog post gave you a better idea on the functions of solidity. In the following weeks, I will share with you technological insights into the blockchain world. Stay tuned to learn more.

--

--