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 ?

  • 1
    The answer to your question is more or less - Yes. – goodvibration Oct 1 '20 at 15:06
  • where does opcode come into play ? nodes have bytecodes of smart contracts. – Nika Kurashvili Oct 1 '20 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). – goodvibration Oct 1 '20 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)'). – goodvibration Oct 1 '20 at 15:21
  • 1
    More or less - yes. – goodvibration Oct 1 '20 at 15:29

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
  • Globals: ORIGIN, CALLER, ...

For more see ethervm.io

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.