EIP-2330 The EXTSLOAD opcode (EXTSLOAD opcode)

No call decision · EVM & execution · EIP status Stagnant · created 2019-10-29

Adds a new EVM instruction called EXTSLOAD that allows reading another contract's storage value directly, without a contract call.

Why it is needed

Currently, even reading a single storage value from another contract requires making a CALL to go through a view function. This process adds, on top of the cost of the call itself, the cost of ABI encoding and decoding and function dispatch, and if the view function performs an SLOAD on other slots the caller doesn't want, that cost is also fully borne by the caller. Looking up registry-like contracts that essentially store a simple mapping, such as ERC-20 or ENS, incurs these same costs every time, making gas fees expensive and hard to predict in advance. Under this structure, a contract must spend far more gas each time than the value it actually intends to expose.

What changes

Today

After EIP-2330

Who is affected

In depth

Specification

The new instruction EXTSLOAD (0x5c) behaves similarly to SLOAD, but additionally specifies the contract to read from. It pops two values from the stack in order, contract (address) and slot (storage address), and pushes the value at that slot from the contract's storage. If the contract account does not exist, it pushes 0.

Gas cost (pre-Verkle)

Gas is calculated as ACCOUNT_ACCESS_COST + STORAGE_READ_COST. ACCOUNT_ACCESS_COST is 0 if the address is already in the accessed_addresses set, otherwise it is COLD_ACCOUNT_ACCESS_COST. STORAGE_READ_COST is WARM_STORAGE_READ_COST if the storage key is already in the accessed_storage_keys set, otherwise COLD_STORAGE_READ_COST. These values follow EIP-2929's access list scheme exactly.

Gas cost (post-Verkle)

After the transition to Verkle Trees, a single account's storage is spread across the entire global tree, so ACCOUNT_ACCESS_COST is no longer needed. In this case, gas only charges STORAGE_READ_COST, calculated the same way as pre-Verkle.

Comparison with existing methods

Even without this EIP, a contract that wants to expose its entire state can create a getter function that performs an SLOAD and returns the value as is. In that case, the cost structure is one CALL cost plus N SLOAD costs. The fact that EXTSLOAD only charges one COLD_ACCOUNT_ACCESS_COST per account plus N STORAGE_READ_COSTs is justified by this comparison. Another alternative is to have the value and a proof supplied externally and verified via BLOCKHASH, but this only applies to a previous block's state and cannot be used for the current block's latest state.

Discussion of conflict with the object-oriented model

There is criticism that EXTSLOAD breaks object-oriented encapsulation by allowing direct reads of another contract's storage. However, in the EVM, each SLOAD incurs a real cost (20 gwei, about $0.20 at an ETHUSD price of 2000), which differs from the object-oriented model of ordinary software. It is also noted that marking a contract's variable as "private" does not make it encrypted or unreadable, and there have been cases of flawed designs that mistakenly treated it as if it were encrypted.

Backwards compatibility and security

Since this adds a new instruction, it is fully compatible with existing behavior. The implementation is also similar to SLOAD, making it easy for each client to add. On the security side, it is explained that a called contract A can reenter the caller contract B and read B's state, which B cannot prevent, but since no state is modified, this is not a problem. Reentrancy guards are usually only placed on write functions, so even without EXTSLOAD, A could already do the same thing through B's view function.

Current status

This proposal assumes EIP-2929's access list scheme (requires 2929) and was written on 2019-10-29. Its status is Stagnant, and it is not currently being actively worked on.

Original EIP · ← EIPs