Analysis of Gas Cost for KEY ID Users after Istanbul Upgrade

Yao Xiang
MYKEY Lab
Published in
5 min readJan 14, 2020

Intro

On December 8th, Ethereum completed Istanbul Hard Fork, carrying 6 new approved changes(EIPs). Among these changes, 4 of 6 are related to gas expenditure, to be specific:

EIP-1108 repriced the elliptic curve arithmetic precompiles and EIP-2028 reduced the gas cost of Calldata, which would greatly assist a number of privacy solutions and scaling solutions on Ethereum.

EIP-1884 repriced for trie-size-dependent opcodes and EIP-2200 provided a structured definition of net gas metering changes for SSTORE opcode.

Ethereum’s metering

When a transaction is executed in Ethereum, all codes are compiled to OPCODE which is similar to assembly languages. Each OPCODE is priced so-called gas price with units of GAS. For example, ADD costs 3 GAS, the total gas a transaction consumes depends on the complied OPCODE and its price.

Why repricing?

There’re two reasons that Ethereum reprices: (1)To encourage some types of applications. (2)To approach a balance between gas expenditure and resource consumption.

EIP-1108 and EIP-2028 are a result of reason (1), which is to encourage the application of privacy solutions and scaling solutions such as zk-SNARK or zk-Rollup. Presently, few dapps using ZKP on Ethereum, the fee reduction benefits the privacy solutions using ZKP.

EIP-1884 and EIP-2200 updated the gas cost based on actual resource consumption. Each opcode conducts to certain resource consumption (CPU, memory, network, etc.), the gas cost is constant before updated while some opcodes are resource incentive. With ethereum developing, especially for the state growth, some trie-dependent opcodes get underpriced. For example, the “storage_reads” and “account_reads” are the two most significant factors contributing to the block processing time. In 2016, EIP-150 repriced gas cost of SLOAD and BALANCE.

According to EIP-1884, SLOAD is the most resource occupied OPCODE per unit gas(Milliseconds per Mgas). EIP-150 increases the cost of SLOAD by 3, causing a steep drop, from around 67 to 23. Nevertheless, at block 7M it was averaging on around 150 — almost sixfold in 3 years.

It is supposed that increasing the cost of SLOAD by 4(from 200 to 800) would bring it back down to around 40. However, it is to be expected that it will rise again in the future, and may need future repricing. The other adjustments are:

  • The BALANCE (0x31) operation changes from 400 to 700 gas,
  • The EXTCODEHASH (0x3F) operation changes from 400 to 700 gas,
  • A new opcode, SELFBALANCE is introduced at 0x47.

The implementation of KEY ID protocol — forwarding contract

KEY ID protocol is a self-sovereign identity protocol aiming at providing a simple and secure solution for mass adaption. Based on the smart contract, the KEY ID protocol achieves a recoverable blockchain account. Besides, the fee friction between user and blockchain is reduced using meta-transaction. In a word, the user of KEY ID can recover its account with recovery code or with the help of emergency contact, get access to multiple blockchains smoothly with a unified metering unit. More info: MYKEY: a Trustless Beauty of Balance

The user account in the KEY ID protocol is a smart contract actually. In most cases, the logic is complex and the deploy cost is expensive. Besides, once the contract needs to be upgraded, the contract needs to be deployed again. The high cost is not suitable for large-scale applications.

There’s a solution which is called a forwarding contract, Vitalik explained it in detail. In brief, forwarding contract is a smart contract that “takes any input, and then immediately makes a DELEGATECALL with that input to a specified address that stores the real code that is meant to be executed. “ The cost of forwarding contract is lower, although each call to the contract thereafter will cost an additional ~1100 gas.

KEY ID protocol adapts forwarding contract, the reason is that before the Istanbul Upgrade, create an account using the forward contract will cost 88,319 gas while it costs 851,154 gas without it. How much is the additional cost in each call? Here we test an empty hello action, the additional cost is 22,275 gas-21,418 gas =857 gas. Regardless of inflation and sunk cost, forward contract is cost-effective when the quantity of transactions is below (851,154–88,330)/857~= 900. Most users of KEY ID protocol is a novice user which means they doesn’t send transactions frequently. Meanwhile, the additional cost is paid by the user itself with unified network credit, so MYKEY chooses forwarding contract mode other than a normal mode to lower the threshold of opening an account.

Forwarding contract mode is regarded as a normal member of a shop while the normal mode is like a VIP member which means VIP member enjoys a discount for each commodity with an additional membership fee.

Gas cost analysis of KEY ID contract

After Istanbul upgrade, the gas cost of transferring Ether & ERC-20 token reduces of EOAs. How about the contract account? We tested the influence on the KEY ID account.

Under forwarding contract mode, the gas cost comparison before and after Istanbul upgrade:

So the gas cost that creating a KEY ID account and transferring Ether & Erc20 both reduces. EIP-2028 mainly contributes to the reduction because it cuts down the gas cost of Calldata (GTXDATANONZERO) from its current value of 68 gas per byte to 16 gas per byte, although EIP-1884 increases the cost of SLOAD which is frequently used in forwarding.

As mentioned before, forwarding contract mode is cost-effective with average usage. What about after Istanbul? Here’s the test result below:

Difference between forwarding contract mode and normal mode

To sum up, after Istanbul, forwarding contract mode economizes ~765000 gas per account while takes an additional 1630 gas per call. So after Istanbul, the economic boundary is 765000/1630 = ~470 gas, half of which is before Istanbul.

In short, after Istanbul, the advantage of forwarding contract decays. With the potential go up of SLOAD cost, the advantage will decay gradually.

Conclusion

This paper analyses the changes in gas cost for KEY ID account after Istanbul upgrade, especially discusses the influence on forwarding contract, arriving the conclusion below:

1. The cost of creating a KEY ID account and transferring Ether & Erc20 both reduces.

2. The advantage of forwarding contract mode weakens in comparison to normal mode. However, account of KEY ID will still be deployed using forwarding contract because (1) gas cost is high of normal mode; (2) average user doesn’t send transactions frequently; (3) the contract may be upgraded.

3. With the ethereum state-trie growing, It is to be expected that gas cost of SLOAD and other trie-size-dependent opcodes will rise again in the future and may need future repricing. Needs long term observation.

--

--