0
mapping(uint => uint) public myMap;

function store(uint key, uint val) public {
   myMap[key]=val;
}

function retrieve(uint key) public view returns (uint){
    return myMap[key];
}

Considering the above code, if we assume the size of uint is 256 bits(or 32 bytes), then can we store all possible 2^256 entries as key values in the mapping? each of these 2^256 can hold a uint value. So, the total space requirement to store the entire mapping data structure is (2^256)*32 bytes. Right? Since the state variables are stored in the blockchain itself, how it is possible to allocate and store such a huge memory in blockchain? What is the mechanism?

I have read an article, where it says for storage type state variables like mapping, we can populate a single entry (i.e. key-value pair). Only the caller need to pay the gas cost associated with a single entry writing. It also takes memory space for a single entry in the BC.

So, if that is the case- Can't I populate a mapping data structure using a loop(say, 10 lakhs entries in one go)? because then it consumes a huge gas cost? right? for e.g. the below code will not run due to excessive gas cost.

for(uint i=0;i<1000000;i++) { myMap[i]=2*i; }

*How many entries can be populated at max in a single go using a loop?

But if I can populate a single entry every time(by calling the store function, mentioned above) and calling this function for 10 lakhs times, hopefully, this will work. Isn't it?

I am a little bit confused, please help me to understand the concept.

5
  • What is the ultimate goal of your research? – Mad Jackal Feb 2 at 7:31
  • I am developing a DAPP, where I need to store a huge no of entries in a mapping (i.e. key-value pair). I can't run a loop to populate the entries due to gas cost restrictions. What is the alternative way to do the same? and also I would like to know, the mechanism/technology that solidity uses to store these kinds of data structures like mapping, array, etc (which are declared as state variables with the "storage" keyword) in Ethreum Blockchain. – Debendra Feb 3 at 6:27
  • For Solidity, it is necessary to develop algorithms that do not require iterative processing of large arrays. Or develop a process so that the corresponding functionality is brought to the Oracle. Or change the blockchain platform to some other one whose smart contracts better support iterative processing of large arrays. – Mad Jackal Feb 3 at 9:10
  • You can store as much as you want in a mapping, as long you pay gas for it. Each store is going to cost 20,000 gas. You can compute what would be a practical number that you would want to store. Also note that 2**256 is an obscenely large number. Even theoretically, you would have issues with keccak256 hash collision invalidating data in your mapping for such large numbers. – hrkrshnn Feb 3 at 21:34
  • Thanks Mad Jackal, hrkrshnn for your time. – Debendra Feb 8 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.