Storing on Ethereum. Analyzing the costs

Albert Palau
Jul 17, 2018 · 4 min read

Original site: https://www.comunidadblockchain.com/storing-on-ethereum-analyzing-the-costs/

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

  • Volatile stack access: Stack
  • Volatile memory access: Memory
  • Non-volatile: storage

Furthermore, we also have available the context information:

  • Code associated to the contracts
  • Access to the data field of the transactions

How to operate with the different storage types

Stack

All Ethereum Virtual Machine operations (EVM opcodes), except the operations STOP, JUMPDEST and INVALID, use the stack both to read or to write on it. However, we are gonna see the operations that read or write data without performing any computation:

The stack depth ranges from 0 to a maximum of 1024

  • POP: Gets the value on the top of the stack (level 0)
  • PUSH1...PUSH32 (PUSHX): Inserts X bytes on top of the stack
  • DUP1…DUP16 (DUPX): Duplicates the value at X on top of the stack
  • SWAP1…SWAP16 (SWAPX): Swaps values on X and the top of the stack

Memory

The operations that access to this memory, either to read or to write, are:

  • CALLDATACOPY: Reads the data field of a transaction and load it in memory
  • CODECOPY: Reads the code associated to the current contract and load it in memory
  • EXTCODECOPY: Reads the code associated to an external contract and load it in memory
  • MLOAD: Reads, from the memory, a value
  • MSTORE: Stores in memory a word/32bytes value
  • MSTORE8: Stores in memory a 8bytes value

Storage

Unlike stack and memory, the data stored on this memory are persistent on the contract address for futures accesses. The operations to manipulate this memory are:

  • SLOAD
  • SSTORE

It’s worth to mention the “S” and “M” of memory and storage respectively.

Costs

All previous operations have a cost expressed in GAS units. When an user sends a transactions he/she gives a ETH price per GAS unit. The miners have configured a minimum GAS price so a higher minimum GAS will let more margin but discarding a bigger number of transaction. It generates a market where ETH is traded per GAS. The less the code consumes in terms of GAS, more efficient will be in terms of resources needed to execute the code. The following table shows the costs, in GAS, of the different operations depending of the size of the data:

Image for post
Image for post

The most expensive one is the non-volatile storage. Operate with the memory is similar to operate with the stack in term of GAS when the size is about the KB but it increases exponentially with the size. This relation is shown in the following picture:

Image for post
Image for post
3*<words>+ROUND(POWER(<words>,2)/512,0)

If we set a GAS price in ETH based on https://ethgasstation.info:

Image for post
Image for post
2018/01/14 12:17 AM

We can make an estimation, in terms of ETH, of the cost per storage type:

Image for post
Image for post
STD=5gwei, FAST=50gwei

Above table values are prices in ETH. The miners will include transaction with a higher price so when an user pays more per GAS, more fast the his/her transaction will be included in a block.

Last, we can set a $/ETH rate, for example, 1356.30$:

Image for post
Image for post
1356.3$ = 1 ETH, 2018/01/14 12:17 AM

Conclusions

Store data permanently on Ethereum is extremely expensive. It has no sense to use Ethereum to store data. It should stores only the required data to work properly and delegate the storage to other solutions: Swarm, Filecoin, IPFS, etc. A good idea is to store a Merkle Tree root hash as a tamper proof of data store on an external service.

Also, an unexpected and fast increase of ETH prices can increase the cost of the operations in terms of FIAT money to a high levels. Miners have to adjust their min ETH price per GAS, if it happens, to readjust the costs.

It’s important to analyze smart contracts in terms of GAS consumption to control the operational costs.

References

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Crypto News

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look

Create a free Medium account to get Crypto News in your inbox.

Albert Palau

Written by

A person who never made a mistake never tried anything new. Blockchain & Open source believer

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Albert Palau

Written by

A person who never made a mistake never tried anything new. Blockchain & Open source believer

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store