2

I know that smart contract is deployed as bytecode.

When we call a function from application, we use the encoding with the help of ABI and we get some bytes. let's say 220 bytes.

These are the bytes we send in data property of the transaction to the smart contract.

So, our bytes have gotten to smart contract.

Question 1) is it the EVM who will take transaction's data(which in bytes) and then look at the bytecode and then figure out how transaction's data is related to which part of the bytecode and then figure it out ?

6
  • 1
    The answer to your question is more or less - Yes. Commented Oct 1, 2020 at 15:06
  • where does opcode come into play ? nodes have bytecodes of smart contracts. Commented Oct 1, 2020 at 15:08
  • 1
    The bytecode of the contract is available in the blockchain. The transaction contains the address of the contract (from field), a pointer to the function within that contract, in the form of the hash of that function's name and parameter types (data field), and the values of the parameters (also data field). Commented Oct 1, 2020 at 15:17
  • 1
    For example, suppose you have function func(uint256 x, address y), then you can find this function's selector within your contract's bytecode. In this case, you can generate the function selector, for example, using web3.js, by taking the first 4 bytes (8 hexadecimal characters) from the returned value of Web3.utils.keccak256('func(uint256,address)'). Commented Oct 1, 2020 at 15:21
  • 1
    More or less - yes. Commented Oct 1, 2020 at 15:29

1 Answer 1

2

When a smart contract has been deployed its bytecode is saved on the chain. To execute the contract you send a transaction with the smart contracts address as recipient. Additional data can be passed to the contract as calldata which can be imagined as parameters to the contracts execution.

When the node validates the transaction it notices that the recipient of the transaction is a smart contract. For the execution of the contract it sets up an instance of the EVM which initializes the:

  • Bytecode of the contract
  • Storage (permanent storage)
  • Stack
  • Memory (similar to Heap)
  • Calldata
  • Global variables like msg.sender, now, etc.

Then it begins the execution of the bytecode at the first instruction. The bytecode can access all of the above via instructions:

  • Storage: SLOAD, SSTORE
  • Stack: PUSH, POP, ...
  • Memory: MLOAD, MSTORE
  • Calldata: CALLDATALOAD, CALLDATASIZE, CALLDATACOPY
  • Globals: ORIGIN, CALLER, ...

For more see ethervm.io

2
  • Thank you for this answer. When you write "Then it begins the execution of the bytecode at the first instruction" does that mean that the smart contract starts execution always at the first byte/opcode of the bytecode? How does it get to the part of the contract's bytecode that codes for the method that the user wanted to call? Is the EVM aware of function identifiers? Or is it just used by the client in order to better understand what is supposed to happen? Commented Jun 13, 2022 at 19:13
  • 1
    Yes, execution always starts at the first byte in the bytecode. The EVM itself is not aware of function identifiers, instead the language you use, e.g. Solidity, will put a jump table at the beginning of your contract's bytecode which checks the function signature which is sent through the calldata and jumps to the relevant part in the bytecode.
    – Bobface
    Commented Jun 29, 2022 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.