Introduction | Anatomy of Ethereum's storage costs

in #btc6 years ago

When we develop smart contracts, we have several different types of available storage:


Volatile stack access: stack

Volatile memory access: memory

Non-volatile: Storage (Storage)

In addition, we also provide background information:


Contract-related code

Access the data field of the transaction

How to use different storage types

Stack

Except for the STOP, JUMPDEST, and INVALID operations, all Ethereum virtual machine operations (EVM opcodes) use the stack to read or write. However, there are operations that can read or write data without performing any calculations:


Stack depth from 0 to maximum 1024


POP: Get the value of the top of the stack (level 0)

PUSH1...PUSH32 (PUSHX): Insert X bytes at the top of the stack

DUP1...DUP16 (DUPX): copy the value at the top of the stack X

SWAP1...SWAP16 (SWAPX): Exchange the values

at X and the top of the stack

RAM

Access to this memory (read or write) is:


CALLDATACOPY: read the data field of the transaction and load it into memory

CODECOPY: Read the code associated with the current contract and load it into memory

EXTCODECOPY: Read the code associated with the external contract and load it into memory

MLOAD: read a value from memory

MSTORE: store a word or a 32-byte value in memory

MSTORE8: Store an 8-byte value in memory

storage

Unlike stacks and memory, the data stored in this memory is permanent at the contract address of the transaction visit. The operations to maintain this memory are:


SLOAD

SSTORE

Pay attention to the "S" and "M" that represent storage and memory, respectively.


cost

All of the above operations have operating costs in Gas. When a user initiates a transaction, he/she will quote the price of Gas in ETH. The miners have already configured the lowest Gas price, and the higher minimum Gas price will bring more profit to the miners, but will need to give up more deals. This creates a market for trading ETH in units of Gas. The less Gas the code consumes, the more efficient the resources needed to execute the code. The following table shows the Gas overhead for different opcodes based on the size of the data:

The most expensive of these is non-volatile storage. When the data size is a few kilobytes, the memory operation is similar to the cost of using the stack operation, but as the data size increases, the cost increases exponentially. This relationship is shown below:

If we set Gas's ETH price based on the https://ethgasstation.info website:

We can use the ETH price to estimate the cost of each storage type:

The value in the above table is the price in ETH. The miner will pack a higher-priced deal, so when the user pays more ETH for the unit Gas, his/her transaction will be packaged into the block faster.

Finally, we can set a dollar to ETH exchange rate, for example, $1356.30:

 

in conclusion

Permanently storing data in Ethereum is extremely expensive. There is no point in using Ethereum to store data. Ethereum should only store the data needed for normal operation and delegate the data store to other solutions: Swarm, Filecoin, IPFS, and more. A good idea is to store the root hash value of the Merkel tree as a data tamper proof on an external server.


In addition, ETH prices are unpredictable, and the potential for rapid growth can result in increased operating overhead (in legal tender) to a higher level. If this happens, the miner must adjust the minimum ETH price per unit of Gas to re-adjust the cost.


It is important to control the cost of operations by analyzing the gas overhead of smart contracts.



Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 62804.67
ETH 2444.00
USDT 1.00
SBD 2.71