2

I have a smart contract to store user information in Ethereum blockchain. For every new transaction entry gets added into map or array. This looks too costly in terms of storage. If I use map or array to store 1 lakh (100KB) user information then for every new transaction I will have to add a new entry to the map and plus 1 lakh [existing transactions] in the state.

Is there any other way to optimize this issue or are all smart contracts written in this way only?

3
  • Why wouln't you update the existing user information to prevent duplication of data? – Shawn Tabrizi Aug 19 '18 at 7:38
  • @ShawnTabrizi: Consider the case of adding new customer who is not in the list yet. – Shahid Hussain Aug 19 '18 at 9:18
  • not only costly, but it may be illegal to store user info on the blockchain, since some legislation protect user's privacy. – Nulik Aug 19 '18 at 13:11
0

You should not be storing large quantities of metadata in a blockchain. It implies misuse and the cost of many thousands of replicas will discourage you from doing it.

It's not a 1:1 replacement for a database.

A common alternative to consider is using a blockchain to authenticate documents stored elsewhere. Documents of arbitrary size can be proven authentic using with reference to a 32-byte hash stored on-chain.

A blockchain can, for example, provide a distributed proof of the authenticity of information stored in other places that are better suited to the storage of heavy objects.

Hope it helps.

6
  • I am storing documents in an IPFS network and hash in blockchain. Along with Hash i will have to store other user information. I could see people are using either map or array as storage. If i have an user base of 1lakh then map size of of each new transaction would be current map size(1lakh)+1. Because in blockchain each transaction is getting appended hence this way of storage will consume lot of space. – Shahid Hussain Aug 19 '18 at 9:16
  • No. The mapping inserts do no get more expensive with scale. Each one costs the same regardless of what was there before. In fact, it's vital that you make the cost consistent at any scale so your app will scale. – Rob Hitchens Aug 19 '18 at 9:18
  • Thanks Rob for quick response. So you suggest for such kind of applications better to go with mapping. Actually this is my first application i am developing so wanted to know the correct storage approach to proceed. – Shahid Hussain Aug 19 '18 at 9:22
  • I use them both. Arrays don't get any more expensive either. It's a iteration over arrays that needs to be avoided at all costs. This explainer covers a sort of swiss army knife pattern: medium.com/@robhitchens/solidity-crud-part-1-824ffa69509a – Rob Hitchens Aug 19 '18 at 9:40
  • 1
    Misunderstanding. Blockchain maintains state in the contract, on the chain. Transactions record inputs that change the state. They do not repeat the state redundantly. As you suggested, that would be terribly inefficient. In your example, you would write ~100 bytes (128) for each insert. The storage cost would show up in transaction cost (gas). It would keep costing 100 (128) bytes to write additional records in a mapping, or append to an array. The cost does not vary with a size of what was there before. – Rob Hitchens Aug 21 '18 at 16:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.