Crypto Exchange Crypto Exchange
Ctrl+D Crypto Exchange
Home > SOL > Info

Understand the contract gas cost after "Berlin"



The "Berlin" hard fork will be activated on April 15, and two of the EIPs included in the hard fork (EIP-2929 and EIP-2930) will affect the gas cost of transactions. This article will explain how the gas consumption of some opcodes is calculated before "Berlin" is activated, how EIP-2929 affects this, and how the Access List feature introduced in 2930 should be used. This article is very long, if you just want to know the conclusion, you can close the page after reading this part: Berlin hard fork changed the gas overhead of certain opcodes. If you hard-code the amount of gas that some operations can use in your application, these operations may freeze. If this happens, and your smart contract cannot be upgraded, users will need to use the "access list" function to use your application. The access list function can slightly reduce the gas overhead, but sometimes it may increase the total gas consumption. The geth client introduces a new RPC method called  eth_createAccessList  to simplify access list generation. Every opcode executed by the EVM has a corresponding Gas consumption. The cost of most opcodes is fixed: PUSH1  always costs 3 gas, while  MUL  costs 5 gas, etc. Some opcodes have variable cost: for example, the cost of the SHA3  opcode is determined by the length of the input value. Let's look at the  SLOAD  and  SSTORE  opcodes first, since these two opcodes are most influenced by "Berlin". Later we will talk about those address-targeted operations, such as all  EXT*  class opcodes and  CALL*  class opcodes, because their gas costs are also changed. CNBC host: Binance can still successfully acquire Voyager: Golden Finance reported that Jim Cramer, a well-known CNBC financial program host, posted on social media that Binance can still successfully acquire Voyager, but at this stage it cannot cooperate with the US Securities and Exchange Commission (SEC) Rivalry. The SEC’s refusal to let Binance complete the Voyager transaction may have been due to fears that Binance would prove to be “more illusory” than imagined. As previously reported, the SEC raised limited objections to Binance US’ proposed $1.02 billion acquisition of bankrupt cryptocurrency lending platform Voyager and questioned the adequacy of the information in the disclosure statement. [2023/1/5 10:23:27] Before the implementation of EIP-2929, the calculation method of SLOAD  overhead is simple: always consume 800 gas. So, there is nothing to expand. When it comes to the calculation of Gas consumption, the SSTORE  opcode is probably the most complicated. Because how much it consumes depends on the current value of the storage item slot, the new value to be written, and whether the storage item has been modified. We will only analyze a few scenarios to get a rough idea. If you want to know more, please read the EIP link attached at the end of this article. If the value of the storage item is changed from 0 to 1 (or any non-zero value), the Gas consumption is 20000 If the value of the storage item is changed from 1 to 2 (or any non-zero value), the Gas consumption is 5000 If the storage The value of the item is changed from 1 (or any non-zero value) to 0, and the consumption is also 5000, but you will get the gas subsidy after the transaction execution ends. We also do not discuss the gas return mechanism here, because it will not be affected by Berlin. In a transaction, if the storage item is not modified for the first time, each subsequent  SSTORE  will consume 800 gas Fed Barkin: Yes The concept of the central bank's digital currency is open: According to news on June 21, the Federal Reserve Barkin said that the threshold for using the balance sheet to increase tightening efforts is very high. It may be wise to adjust for balance sheet reduction, but this is "a scenario for the future". Any decision on whether to sell mortgage-backed securities is yet to be made. I'm still skeptical that the Fed needs a digital currency, but I'm open to the concept of a central bank digital currency. [2022/6/22 4:43:37] The details are not important here, what is important is that SSTORE  is expensive, and how much gas it consumes depends on multiple factors. EIP-2929 changes all of these values. But before we start, let’s talk about an important concept introduced by the EIP: the visited address and the storage key of the visited storage item. When an address or a key of a storage item is "used" in a transaction, the address (or key) will be considered "accessed" for the remainder of the transaction. For example, if you CALL (call) another contract in a transaction, the address of that contract will be marked as "visited". Similarly, if you SLOAD or SSTORE passed some storage item slots, those slots will also be considered accessed for the rest of the execution of the transaction. It doesn't matter which opcode is used, even if you only  SLOAD  a slot, the slot will be treated as visited when you use  SSTORE  next. Note: Keys to stored items are "intrinsic" in certain addresses, as explained in this EIP: Norwegian Shipping Company Valenius Wilhelmsen Invests in Dutch Automotive Logistics Blockchain Vinturas: Golden Finance Report, Norwegian Shipping Firm Wallenius Wilhemsen has invested in Vinturas, a Dutch automotive logistics blockchain platform. Simon White, executive vice president and chief digital officer at Valenius Williamson, said: "The Vinturas blockchain solution goes beyond traditional event-based customer data access and monetization platforms, providing partners with an A secure private network that only collaborates and shares data with its selected objects." (blockworks) [2022/4/13 14:22:30] When executing a transaction, maintain a collection: accessed_addresses: Set[Address] and  ;accessed_storage_keys: Set[Tuple[Address, Bytes32]] That is to say, when we say that a storage slot has been accessed, we actually mean: (address, storageKey)  has been accessed. After figuring out this concept, let's talk about the new Gas consumption calculation mode. Before the upgrade, the gas consumption of SLOAD  was fixed at 800. But after the upgrade, the Gas consumption depends on whether the storage slot has been accessed. If you have not visited yet, the consumption is 2100 gas; if you have visited, it consumes 100 gas. Therefore, if a storage item slot is already in the collection of "visited storage item keys", you can save 2000 gas. Let's compare them one by one. After the implementation of EIP-2929, the above examples will What has changed: Shiba Inu whales own 26% of the supply, SHIB net flow increased by 176%: December 25 news, Shiba Inu's correction reversal trend has just shown initial signs of slowing down, while the supply of whale-level addresses has Reached 2 billion US dollars, accounting for more than a quarter (26%) of the total supply. The net flow of SHIB has also declined, but it still maintains a significant growth of 176%. (U.Today) [2021/12/26 8: 04:09] If the storage item key has not been accessed, consume 22100 gas. If the storage item key has been accessed, consume 20000 gas. If the storage item key has not been accessed, consume 5000 gas. If the value is changed from 1 (or any non-zero value) to 0, the consumption remains unchanged, and the gas return mechanism remains unchanged. In a transaction, if the storage item is not modified for the first time, each subsequent  SSTORE  ; Both consume 100 gas. It can be seen that if a slot has been visited before, the first  SSTORE  operation on it will save 2100 gas (compared to never visited). The above text is really long-winded, Let's just make a table and summarize all the values mentioned above: Pay attention to the last line: it is no longer necessary to distinguish whether it has been accessed or not, because if it has been written before, it must have been accessed Visited. The total lock-up volume on the Avalanche chain reached 7.43 billion US dollars: According to news on October 21, Defi Llama data showed that the current total lock-up volume on the Avalanche chain reached 7.43 billion US dollars, a record high. At present, the total lock-up volume of Avalanche is low Ranked fifth in the public chain among Ethereum, BSC, Solana, and Terra. [2021/10/21 20:45:42] Another "Berlin" upgrade contains EIP 2930. This EIP adds a new type of transaction, you can include an "access list" in the transaction payload, which means that you can declare which addresses and storage slots should be considered "accessed" before the transaction is executed. For example, for a It takes 2100 gas to execute  SLOAD  for a slot that has not been accessed, but if the storage slot is included in the "access list" of the transaction, the consumption of the operation will be reduced to 100 gas. But if only the address and slot are Treating it as "visited" can reduce the gas consumption of the operation; and the access list can mark the address and slot as "visited"; doesn't that mean Can we put all these things in the access list to get a reduction in Gas consumption? Awesome, godsend gas! Well, not exactly, because you pay extra gas for every address or storage key you add. for example. Suppose we want to send a transaction to contract  A . We write an access list like this: Does this mean that we save gas every time we use the access list? Sadly, no, because filling the address in the access list also costs gas. (i.e.  "<address of A>" in our example) So far we've only discussed the  SLOAD  and  SSTORE  opcodes, but the "Berlin" upgrade changed other operations code. For example, the original Gas consumption of the CALL  opcode is fixed at 700, but after the implementation of 2929, if the called address is not in the access list, the consumption will be increased to 2600; if it is, it will be reduced to 100. Also, like the accessed storage keys, it doesn't matter which opcode accessed that address (for example, if the user first calls  EXTCODESIZE, the cost of this operation is 2600, but subsequent calls, As long as it is for the same address, whether it is  EXTCODESIZE, CALL  or  STATICCALL , it only consumes 100 gas. How does this design affect transactions with access lists? Suppose we send a transaction to contract A , and contract A calls contract B, and we write in the access list: we first need to pay 2400 gas for adding this address to the access list of this transaction, but the first opcode used by B It only needs to consume 100 gas instead of 2600 gas, which leaves 100 gas. If B also needs to use its storage items, and we know which keys it will use, we can also include these keys in the access list, and then Save 100 or 200 gas for each key operation (depending on whether the first opcode is  SLOAD  or  SSTORE). But why do we need to add another contract as an example? Can't we write like this ?Of course you can do this, but it's not worth it, because EIP-2929 specifies that the contract you call initially (that is, the destination of ) must be included in the list of  accessed_addresses , so You just spend an extra 2400 gas and get no benefit. So, looking back at the example we gave above: This is actually a waste, unless you add a few more storage item keys in it. If we assume that all storage item keys The first operation of all is  SLOAD, then you need at least 24 keys to earn back. The Geth client (from 1.10.2) will include a new  eth_createAccessList RPC method, which you can use to Generate an access list, just like using  eth_estimateGas , except that instead of the estimated Gas consumption, the returned data looks like this: I estimate that as time goes by, we will know more and more how to use this function, but Personally, I estimate that the pseudocode form of the method will look like this: To prevent the contract from bricking, it is worth reminding that the main purpose of the access list function is not to save Gas.


Golden Morning Post|The central bank issued the "Financial Institutions Anti-Money Laundering and Anti-Terrorist Financing Supervision and Management Measures"

Headline ▌The central bank issued the "Financial Institutions Anti-Money Laundering and Anti-Terrorist Financing Supervision and Management Measures".

How much can Bitcoin go up in 2021? Li Qiyuan, an old man in the currency circle for 10 years, tells you a reasonable shipping method.

Bobby Lee, founder and CEO of Ballet Wallet, and digital asset security expert, attended the "Supercomputing Convergence.

Four principles of digital currency "bookkeeping rules" system

At present, there is a trend of digitalization in the form of currency. It does not happen on the enterprise side, nor does it happen on the bank side.

Understand the contract gas cost after "Berlin"

The "Berlin" hard fork will be activated on April 15.

The four major application markets of NFT: Art, Collectibles, Metaverse, Games

Editor's Note: The Metaverse is exploding in the tech and gaming worlds. On March 10 this year, Roblox, a metaverse concept stock, had a market value of more than 40 billion US dollars on the first day of listing.

zkPorter: How will the breakthrough of L2 expansion bring 20000+ TPS?

Editor's note: zkSync is an Ethereum Layer 2 solution based on zkRollup technology developed by the Matter Labs team. Currently, platforms such as GitCoin, imToken, and Argent already support the zkSync solution.

How should the law look at the NFT going out of the circle?

NFT is "out of the circle"! From the blockchain world to the art world, the collection world, to the game world and social media.