Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines EIP-7, which introduces DELEGATECALL, an opcode for executing code from another contract.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

See the following articles for more information about DELEGATECALL.

https://chaldene.net/solidity-delegatecall

Overview

  • Block >= 1,150,000 on Mainnet
  • Block >= 494,000 on Morden
  • Block >= 0 on future testnets

These values specify when the upgrade becomes active on each chain. It applies from block 1,150,000 on mainnet and block 494,000 on Morden. For future testnets, the upgrade is active from block 0.

The proposal introduces a new smart contract instruction (opcode) called DELEGATECALL, assigned to opcode 0xf4. Its behavior resembles that of the existing CALLCODE opcode, with an important distinction.

CALLCODE executes another contract's code in the calling contract's context, but sets the sender and value for the new call. In contrast, DELEGATECALL propagates the sender and value from the parent execution context into the child execution context.

The resulting call retains the original sender and value, allowing more flexible contract designs and preserving relevant context across contract interactions.

CALLCODE

When contract A uses CALLCODE to invoke code from contract B, B's code runs using A's storage.

The sender (msg.sender) is contract A's address. B is not directly called in its own execution context. For example, if A supplies a call value of 5 ETH, the call executes in A's context and B's balance does not increase.

DELEGATECALL

When A uses DELEGATECALL to invoke code from B, B's code also runs using A's storage.

However, the sender (msg.sender) and value are propagated unchanged from the parent context. If an EOA called A directly, that EOA remains the sender in the delegated execution.

The code from B is executed as though it were part of A. If the original call to A carried 5 ETH, the delegated code observes that value through msg.value.

In summary, both opcodes execute code in the parent contract's context, but DELEGATECALL also preserves the original sender and value. This allows flexible interactions and closer integration between contracts.

Translation note: The Japanese source states that a delegated call transfers the example's 5 ETH to contract B. That statement is inaccurate. DELEGATECALL does not itself transfer ETH to the code-providing contract; the execution context and balance remain those of A. CALLCODE likewise does not increase B's balance.

Specification

The DELEGATECALL instruction takes six parameters.

  • gas
    • The maximum requested gas available for execution.
    • Gas measures the computational cost of blockchain operations.
  • to
    • The destination address containing the code to execute, normally another smart contract address.
  • in_offset
    • The starting memory offset of the input data.
  • in_size
    • The size of the input data in bytes.
  • out_offset
    • The starting memory offset for the output data.
  • out_size
    • The size of the memory region reserved for output data.

An important point is that there is no additional gas stipend. The called execution receives the applicable forwarded gas. As with CALLCODE, no new account is created by the call, and the original proposal describes the upfront gas cost as constant. Unused gas is returned in the usual way.

The sender (CALLER) and supplied value (VALUE) behave identically in the calling and delegated execution contexts, reflecting the preservation of the original context.

The call-depth limit of 1024 also applies to DELEGATECALL, preventing indefinitely deep contract calls.

Additional Notes

The key property of DELEGATECALL is its preservation of the parent context's sender (msg.sender) and value (msg.value). This makes it easier to delegate particular operations or functionality to another contract.

js
~calldatacopy(0, 0, ~calldatasize())
if ~calldataload(0) < 2**253:
    ~delegate_call(msg.gas - 10000, $ADDR1, 0, ~calldatasize(), ~calldatasize(), 10000)
    ~return(~calldatasize(), 10000)
elif ~calldataload(0) < 2**253 * 2:
    ~delegate_call(msg.gas - 10000, $ADDR2, 0, ~calldatasize(), ~calldatasize(), 10000)
    ~return(~calldatasize(), 10000)
...

The example demonstrates a contract that uses input data to select different addresses for DELEGATECALL.

It allows a single contract to route different tasks to different implementations. The historical example discusses distributing gas usage and working around the then-relevant 3M gas constraint. It checks the initial portion of the input, branches according to a range of values, and delegates execution to the corresponding address.

js
if ~calldataload(0) / 2**224 == 0x12345678 and self.owner == msg.sender:
    self.delegate = ~calldataload(4)
else:
    ~delegate_call(msg.gas - 10000, self.delegate, 0, ~calldatasize(), ~calldatasize(), 10000)
    ~return(~calldatasize(), 10000)

This example allows a contract to change its delegate address dynamically. It can thereby update its behavior and execute code providing different functionality.

The delegate address is updated when specified conditions are satisfied, such as the invocation of a particular function by the appropriate sender.

These examples demonstrate how DELEGATECALL expands contract flexibility and functionality. Preserving the parent context, particularly its sender and value, supports complex interactions and modular architectures.

Possible Objection

One might argue that the behavior of DELEGATECALL could be reproduced by placing the sender's information directly in the first 20 bytes of calldata.

However, this approach requires compiling smart contract code specifically to interpret the embedded sender information. The same code could not necessarily operate unchanged in both delegated execution and direct execution contexts.

This alternative may require separate versions of the contract code for different execution environments, increasing complexity and reducing flexibility and reusability.

In contrast, DELEGATECALL allows another contract's code to execute while preserving the sender and value without requiring such special handling.

Citation

Vitalik Buterin (@vbuterin), "EIP-7: DELEGATECALL," Ethereum Improvement Proposals, no. 7, November 2015. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7.

Conclusion

EIP-7 introduces DELEGATECALL, an opcode for executing another contract's code while preserving the original calling context.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/