Solidity – Difference Between Solc’s Bin Bytecode Versus Bin Runtime

bytecodecontract-deploymentdeployed-bytecodesolc

Solc says that bin-runtime is the runtime part of the contracts. What exactly is the runtime bytecode part and how does it differ from bin bytecode output?

Best Answer

TL;DR

bin-runtime is the code that is actually placed on the blockchain. The regular bin output is the code placed on the blockchain plus the code needed to get this code placed on the blockchain, the code of the constructor.

Longer answer

The basics of the Ethereum Virtual Machine is defined in Section 9.1 of the Ethereum Yellow Paper.

To answer this question fully, you must first know how a smart contract is created. A smart contract is created by sending a transaction with an empty "to" field. When this is done, the Ethereum virtual machine (EVM) runs the bytecode which is set in the init byte array[1] which is a field that can contain EVM bytecode -- the binary code for executing logic on Ethereum. The EVM bytecode that is then stored on the blockchain is the value that is returned by running the content of init on the EVM. The bytecode can refer to itself through the opcode CODECOPY. This transfers the currently running bytecode to the EVM memory. The CODECOPY opcode reads three values on the stack where two of those values are pointers to the bytecode, one marking the beginning and one marking the end of what should be copied to memory. The RETURN opcode is then used, along with the correct values placed on the stack, to return bytecode from the initial run of the EVM code. RETURN reads and removes two pointers from the stack. These pointers define the part of the memory that is a return value. The return value of the initial contract creating run of the bytecode defines the bytecode that is stored on the blockchain and associated with the address on which you have created the smart contract.

The code that is compiled but not stored on the blockchain is thus the code needed to store the correct code on the blockchain but also any logic that is contained in a (potential) constructor of the contract.

The bin-runtime argument can be used to verify that some specific Solidity source code is placed on a specific address. Compiling the source code that a person claims represents the smart contract on an address on the blockchain and comparing that to the binary code that is actually stored on the address eth.getCode(contractaddress) means that you can be sure what the source code for the contract is.

See the Ethereum Yellow Paper, section 4.3 for how smart contracts are created.

Related Topic