Building an application specific blockchain using Cosmos SDK Part-6

Harish Bhawnani
Coinmonks
5 min readApr 7, 2022

--

Welcome to the last part of this series and here we will be covering a very important feature- TestSimulation.

Photo by Jeswin Thomas on Unsplash

Similar to module manager, we have a simulation manager defined in app.go . It is basically an object containing a list of all those modules which implement the simulation package. Every module have their own simulation package which contains primary functions for test simulation: store decoders, randomized genesis state and parameters, weighted operations and proposal contents. We do have these methods registered on module with the help of starport (under simulation.go)

Note that, along with messages, starport also scaffolds the simulation weighted operations for each message type.(module_simulation.go). Let’s change the weighted numbers for each message type -

We will be defining randomised parameters for each message type and then sign and send the transactions to the simulated chain to verify if things work as expected. Note that simulation also helps us to test invariants registered on any module if any.

An Invariant is a function that helps us to detect bugs early on and act upon them to limit their potential consequences (e.g. by halting the chain).

We will use small utility function to sign and send the transactions to simulated chain -

We will define simulation function as well for each of the message-

Once simulation methods are defined for every message, we can test the simulation for say, 200 blocks -

This will simulate the chain for block height 200 and will invoke the simulation of each module.We will get the output for all different modules simulation and our custom transaction messages simulation. It will also generate stats.json (status for each test-case)and state.json(blockchain state after simulation) files.

Test results from simulation of deal module -

Other test cases for keepers, client CLI and genesis can be referred from source code.

What’s Next -

Kudos! You made it till the end. We can further improve our dapp and make it more production ready. Other useful tasks one can continue with -

  • Develop frontend for online store and integrate it with deal dapp. Run txs via Keplr wallet.
  • Create a dashboard for query details fetched from blockchain.
  • Subscribe to custom events in frontend via tendermint web-socket.
  • Create a rating service to facilitate consumers rating for order delivery service.
  • Optimise the data structure and remove the expired contracts from store in module method EndBlock itself.
  • Deploy dapp over a test-net with 5 different nodes using docker-compose and test it locally.
  • Enable ibc to integrate with the bandchain oracle. Get the price info from oracle to normalise the pricing for different kind of orders.

--

--