CosmWasm Storage Benchmarks

Posted underFadroma &Tech Talks
Cover Image for CosmWasm Storage Benchmarks

The problem

Persistence is an important aspect of smart contracts. In CosmWasm, smart contract developers have access to a fundamental storage primitive: the key-value store. In the case of Secret Network, libraries like Fadroma and secret-toolkit build upon the CosmWasm KV store to implement additional storage structures.

secret-toolkit predominantly uses binary blob pages, where each page contains multiple items in the form of JSON bytes. Presumably, this is done to reduce the number of FFI calls required to fetch data from the CosmWasm storage runtime. However, this dramatically increases the complexity of the implementation of those storage structures.

In contrast, Fadroma uses data mappings where each operation (insert or get) for each item is a call (or more, depending on the data structure) to storage.

The purpose of this repository is to measure the gas efficiency of these distinct approaches with the purpose to hopefully derive novel and better approaches to more efficiently implement such storage structures.

The approach would of course greatly depend on the functionality desired by the structure.

Case study: Map

Just like a regular map, the implementations allow inserting and getting values from the map, based on a given key. They also allow for iterating over all the values inserted.

It should be noted that the secret-toolkit version also allows for iterating over the keys (as opposed to values), whereas Fadroma doesn’t.

The benchmark here is the contract accepts N number of items which it then inserts one by one into the map, then uses the map iterator to iterate over the values inserted, collecting them into a Vec to include in the response (just so that we have something to do with those values). Finally, it removes all the values previously inserted from the map.

The result is, even when bypassing Fadroma’s binary serialization by using JSON, it is still slightly more efficient.

Conclusion

We can see quite an impactful gas optimization in when serializing the contract storage with Fadroma.

Results

N itemssecret-toolkit (baseline)Fadroma (w/ JSON serialization)Fadroma (w/ binary serialization)
105131850071 (2.43% cheaper)47472 (7.49% cheaper)
206583463556 (3.46% cheaper)58479 (11.17% cheaper)
50109160103702 (5.00% cheaper)91792 (15.91% cheaper)
100182156170392 (6.46% cheaper)147534 (19.01% cheaper)
200325335303299 (6.77% cheaper)256324 (21.21% cheaper)
500759707703917 (7.34% cheaper)586556 (22.79% cheaper)
100014828801369681 (7.63% cheaper)1138502 (23.22% cheaper)
Results of storage benchmarks between secret-toolkit and Fadroma storage

Plot

Resources


Asparuh Kamenov
About the author:

Asparuh Kamenov


More Stories

Cover Image for Overview of the Ethereum Rollup Ecosystem

Overview of the Ethereum Rollup Ecosystem

Ethereum’s blockchain scalability problem is well known and the focus of much research and development. The network can only process […]

Read more
Cover Image for The modern stack for Ethereum dApps

The modern stack for Ethereum dApps

The advancments in the blockchain ecosystem are happening at a rapid pace. As people say 1 year in crypto is […]

Read more

Have a project in mind?

We have the expertise! Drop us a line and lets talk!