9

I am wondering, what is the purpose of the EXTCODECOPY bytecode instruction? I don't see any advantage in copying the code of another contract in memory.

I am a little bit confused, because of the existence of other instructions, such as DELEGATECALL that really "use" the code of another contract

3
  • 3
    One use case I can think of is validating that the code of a smart contract can be trusted.
    – Henk
    Commented Oct 1, 2018 at 15:38
  • Do you mean, something like, "I copy the code in memory, compute the hash and check if it corresponds to the sha3 version I know?"
    – Briomkez
    Commented Oct 1, 2018 at 15:42
  • yes, something like that
    – Henk
    Commented Oct 1, 2018 at 16:53

2 Answers 2

7

It is used to check and compare a contract's bytecode, similar to comparing a known hash of a download file to confirm its legitimacy. The best description of why it is useful (and suggestions as to how to make it better), can be seen here in EIP 1052.

Many contracts need to perform checks on a contract's bytecode, but do not necessarily need the bytecode itself. For instance, a contract may want to check if another contract's bytecode is one of a set of permitted implementations, or it may perform analyses on code and whitelist any contract with matching bytecode if the analysis passes.

3

An unconventional of EXTCODECOPY is to use a deployed contract as the "storage" of another contract, as is is done by this library: https://github.com/0xsequence/sstore2

The rationale is that writing/reading data in the contract code region can become cheaper than writing/reading data in the storage region.

  • Cheaper storage reads (vs SLOAD) after 32 bytes
  • Cheaper storage writes (vs SSTORE) after 32 bytes (auto-gen key)
  • Cheaper storage writes (vs SSTORE) after 96 bytes (custom key)

This blog post from Zefram Lou has additional information about it.

Your Answer

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

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