Skip to content
EVM - Gas refunds and Memory expansion cost

EVM - Gas refunds and Memory expansion cost

Memory in EVM is a critical resource that should be carefully managed due to the decentralized nature of the blockchain. It is one of the key factors in computation, which in turn affects the cost of executing smart contracts. To get a good understanding of memory expansion costs and gas refunds, let's break these concepts down and examine them in detail.

Memory Expansion Cost in Ethereum

EVM, in general, operates on the concept of a "gas" system, a mechanism that helps prevent spamming and abuse of the network by making it costly to conduct operations that require computational power, such as contract execution or transactions. Memory management is a crucial aspect of this system, particularly since memory on the Ethereum Virtual Machine (EVM) isn't a free resource.

When an operation is conducted that increases the memory size of the EVM, a certain amount of gas is required, and thus a corresponding cost is incurred. This expense is known as the memory expansion cost and it's intended to prevent users from burdening the network with operations that require large amounts of memory. The precise cost parameters for such memory-related operations are defined in Ethereum's Yellow Paper, specifically in Appendix G titled "Fee Schedule".

To quantify memory expansion costs, Ethereum employs a formula that comprises two components. Expressed in terms of gas, the memory expansion cost (G_memory) is given by:

G_memory = cost_per_byte * num_bytes + (num_bytes/32)^2

Here, cost_per_byte refers to the cost assigned to a single byte of memory, and num_bytes denotes the number of bytes that a given operation is aiming to allocate. The product of these two values provides the linear portion of the memory expansion cost, charging a fixed amount of gas for each byte of memory that the operation requires.

The second part of the formula (num_bytes/32)^2 shows that a quadratic cost associated with memory expansion, commonly referred to as the "overhead". The intention behind this quadratic cost is to provide a deterrent against operations that seek to consume excessive memory. By squaring the memory allocation (after dividing it by 32), this cost increases more rapidly for larger memory allocations. This effectively penalizes large memory allocations and thus promotes efficient use of memory.

In essence, the memory expansion cost is a critical tool in EVM's resource management strategy. It helps to strike a balance between enabling dynamic use it's memory and ensuring that this use doesn't unduly burden the network. It encourages developers to be judicious and efficient in their allocation of memory in smart contracts and transactions, fostering an environment of computational frugality and strategic planning.

Gas Refunds

Gas refunds provide a way for smart contracts to reclaim some of the gas spent during execution. The SSTORE and SELFDESTRUCT operations are the primary generators of gas refunds.

  1. SSTORE: The SSTORE operation modifies contract storage. If this operation changes a storage slot from a non-zero value to zero, it results in a gas refund. This is defined in Ethereum protocol's yellow paper as follows:

refund = (original_value != 0 && new_value == 0) ? R_sclear : 0

where:

  • refund is the amount of gas to be refunded,
  • R_sclear is the constant for the amount of gas refunded for clearing a storage slot,
  • original_value is the original value in the storage slot, and
  • new_value is the value the storage slot is being updated to.
  1. SELFDESTRUCT: The SELFDESTRUCT operation destroys a contract and frees space on the blockchain. Using SELFDESTRUCT provides a substantial gas refund. The refund formula is as follows:

refund = (selfdestructed == false) ? R_selfdestruct : 0

where:

  • refund is the amount of gas to be refunded,
  • R_selfdestruct is the constant for the amount of gas refunded for the SELFDESTRUCT operation, and
  • selfdestructed is a boolean indicating whether the contract has already been self-destructed.

However, Ethereum sets an upper limit on the amount of gas that can be refunded in a transaction to half the total gas used by the transaction. This limitation prevents misuse of the refund mechanism and ensures contracts cannot perform costly operations for free by subsequently offsetting the cost with refunds.

Let's illustrate this using an example.

Suppose a contract aims to perform an operation that requires a significant amount of memory expansion. Let's say this operation needs 5000 units of memory (for simplicity, we're talking in memory units rather than in specific words or bytes). The cost for this expansion, given our earlier formula, is quadratic.

C_memory = k_memory * (word_count + 1) ^ 2

Substituting word_count with 5000 units and assuming k_memory equals 1 for simplicity, the cost of this memory expansion operation would be:

C_memory = 1 * (5000 + 1)^2 = 25002001 gas

This is a considerable amount of gas, and the contract will need to pay this cost upfront to execute the operation.

Now, consider the contract attempts to offset this cost by invoking SSTORE operations, which set storage slots from non-zero values to zero, thereby generating gas refunds. However, Ethereum restricts the total amount of gas that can be refunded in a transaction to half of the total gas used by the transaction.

In our scenario, let's say the total gas used by the transaction is 30000000 gas. This means the maximum gas refundable would be:

Max Refund = 0.5 * 30000000 = 15000000 gas

This limit on gas refunds falls significantly short of the gas required for our memory-expanding operation. So, despite generating gas refunds via SSTORE operations, the contract would not be able to offset the high cost of memory expansion. This inability to fully recover the costs emphasizes the role of gas refunds and memory expansion costs in maintaining a balance of incentives for efficient resource use in the Ethereum network.

Considering these two mechanisms, one might wonder if gas refunds could be used to offset the cost of memory expansion. However, Ethereum's stringent restrictions on the total gas refundable make such a tactic largely unworkable. A contract that significantly expands its memory would incur a steep, quadratic cost for the expansion. Given the refund limit, it would not be able to recuperate this cost through gas refunds.

References:

  1. Ethereum Yellow Paper
  2. Ethereum Improvement Proposal (EIP) 3298
  3. Ethereum Improvement Proposal (EIP) 3529