Storing large arrays on the Ethereum blockchain
Asked Answered
S

1

6

In the process of studying Ethereum smart contracts, the question arose of how to store large structures inside a contract (array or map). There is a contract

pragma solidity ^ 0.8.6;

contract UselessWorker {

    uint32 public successfullyExecutedIterations = 0;


    mapping (uint32 => uint32 [6]) items;
    
    event ItemAdded (uint32 result);
    
    function doWork (int _iterations) public {
        for (int32 i = 0; i <_iterations; i ++) {
          items [successfullyExecutedIterations] = [uint32 (block.timestamp), successfullyExecutedIterations, 10, 10, 10, 10];
          successfullyExecutedIterations ++;
        }
        emit ItemAdded (successfullyExecutedIterations);
    }
}

The doWork method fills the map items with arrays of numbers, called by an external script. At the same time, the more records appear in items, the faster disk space is consumed (for 1,000,000 the blockchain size is about 350 MB, for 2,000,000 about 1.1 GB, for 19,000,000 about 22 GB.This is the size of the .ethereum/net/geth/chaindata/ folder).

I am testing it on a private network, so the price of gas does not bother me. I run it with the command

geth --mine --networkid 999 --datadir ~ / .ethereum / net --rpccorsdomain "" --allow-insecure-unlock --miner.gastarget 900000000 --rpc --ws --ws.port 8546 --ws.addr "127.0.0.1" --ws.origins "" --ws.api "web3, eth" --rpc.gascap 800000000

According to estimates, one record in the map should take about 224 bytes (7 * 32 bytes) and for 19M records it should be about 4.2 GB.

It feels like a memory leak is taking place. Or I don't understand well how memory is allocated for storing map.

Can anyone suggest why blockchain is consuming so much disk space?

Schear answered 10/8, 2021 at 15:34 Comment(0)
M
1

The increase in disk space is expected because each record in the mapping consumes storage on the blockchain. In Ethereum, storage is allocated in 32-byte chunks, and even though each record is estimated to take about 224 bytes (7 * 32 bytes), additional storage is required for metadata and indexing.

Maihem answered 19/7, 2023 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.