2

I am looking at the docs, but cannot really understand what CREATE opcode does. I can tell that CREATE does create a new smart contract from a memory chunk, pass the gas value to this new contract, then returns. But before returning, does it execute the new contract?

This confuses me because looking at the disassembly code of the smart contract bytecode, after CREATE, I cannot see any call to CALL after that, but then I still see a call to RETURNDATASIZE, which I suppose only happen after CALL. Without CALL, where it get returned data from?

Any enlighten, please?

5

CREATE, in a way, does a built in CALL. What actually happens is that the data passed to call isn't the contract bytecode, it's the init bytecode.

When CREATE opcode is executed, the EVM creates a call frame in the context of the new contract (e.g. address(this) is the new contracts address). This executes the data passed to CREATE as the code, which in higher level languages is basically the constructor. At the end of this init stuff, it returns the actual code of the contract that is stored in the state trie.

The easiest way to think about it, which is also fairly accurate, is that the Solidity compiler takes all the executional code of the contract, compiles it to bytecode, and adds it as a return statement at the end of the constructor.

  • ah, so CREATE when creating a contract actually returns the contract bytecode, right? then after that, CALL will actually executes the contract? – user311703 Mar 27 at 20:22
  • Please could you tell me what is the best way to track/trace what is really happening at bytecode level? Does Remix a good tool to do this? – user311703 Mar 27 at 20:23
  • 1
    CREATE executes the code passed to it, and that code returns the contact bytecode, yes. Just wanted to be clear on that – flygoing Mar 27 at 20:40
  • 1
    If you need to a call a function after it's created, then yeah, there would be a CALL right after. Remix is a pretty good way to track/trace on the bytecode level, it has pretty good EVM debugging tools for stepping in/out of bytecode – flygoing Mar 27 at 20:41
  • 1
    In that case the init code would just return the code for your contract. The compiler basically creates a default constructor that does nothing but return the actual bytecode. – flygoing Mar 28 at 14:22

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.