Building a Decentralized App with Cosmos SDK

Why TruStory chose to build on Cosmos SDK and an overview of its architecture

Shane Vitarana
Cosmos Ecosystem Blog
7 min readAug 3, 2019

Note: The Cosmos ecosystem is constantly growing and expanding. This article’s content is now outdated. For the latest content, see https://cosmos.network/.

Why Cosmos SDK?

TruStory started out with the goal of building a decentralized app that had the look and feel of a normal app that people are used to. We didn’t want our users to wait for confirmations every time they performed an action, or have to deal with pending transactions. The app also called for a lot of fine-grained transactions based on events fired at specific times. So immediately, building on Ethereum mainnet was out of the picture. We needed something that was more responsive and robust. So we figured an Ethereum-based sidechain might work.

We built an early prototype on Loom SDK, but the framework wasn’t mature enough for our needs at that time. We did, however, take a liking to Tendermint, the BFT-consensus middleware that powers Loom. It allows for fast finality (~5 seconds), which is critical to the user experience of a social app with frequent interaction.

Next, we decided to try out Cosmos SDK since it’s from the same team that built Tendermint. We loved what we saw:

  • A statically typed, compiled programming language (Go)
  • A well-architected blockchain framework
  • Modular architecture
  • Highly configurable
  • Built-in governance

Another thing that sold us on Cosmos is the ability to build a community of stakeholders around a network. We think networks of the future can be more egalitarian, giving users and stakeholders more control and ownership over the platform. Companies like Airbnb are asking the SEC to make changes to the rules to allow hosts to own a stake in the company. There’s a good chance that social networks of the future will be run more like co-ops than traditional companies.

Users and stakeholders alike will be able to participate as little or as much as they want, as consumers of the service, as network operators (validators), or both. In addition, token holders will be able to delegate their tokens to validators, and passively participate in the network.

Building on Cosmos SDK gives us the technical capability to implement user ownership and governance natively. Users and validators can vote on upgrades and new features, and help uphold the values and guidelines of the network and user community. Users are incentivized to be good citizens and moderate the network. We have already seen successful blockchain governance in action on the Cosmos Hub, the first mainnet launch of a network built on the SDK. Nearly every proposal so far has seen over 60% voting participation.

Building a network that is self-sustaining and self-governing can fix a lot of the issues with current social networks. It is no secret that Facebook has issues with content moderation and privacy.

Staking is DeFi

The incentive structure of TruStory is based around curating content by staking on it. Staking on content can be thought of as the evolution of the Facebook Like button. Likes are great for signaling association, but they don’t provide any sort of reward to the user for investing their time in curating content. Similar to how validators stake ATOM to provide skin-in-the-game and secure the Cosmos Hub, in TruStory, users stake on user-generated content. In the process, users earn interest in the form of TRU, TruStory’s native token.

Zaki Manian at Interchain Conversations

An incentive structure wouldn’t be complete without disincentives. TruStory content is moderated by users who have earned enough TRU. Users can be slashed for bad behavior, where they are jailed for a period of time. Jailed users lose some of their TRU as punishment.

TruStory also plans on implementing staking at the infrastructure level. Namely, power users can run nodes of the TruStory blockchain, called validators. Validators are responsible for securing the TruStory blockchain by signing transactions and proposing blocks in a proof-of-stake network. Similar to TruStory users, validators stake TRU and are slashed for bad behavior such as downtime and double-signing blocks.

TruStory Architecture Overview

The first version of TruStory was a Schelling Point game based around validating and verifying content but recently pivoted to a platform for more constructive debate around claims. Cosmos SDK was instrumental in this pivot. We were able to fork our blockchain while migrating all data over to the new chain.

Every Cosmos chain is bootstrapped with a Genesis File that defines the initial state of the blockchain. To fork the blockchain, a new genesis file can be created with a command-line tool that exports the current app state. Migrations can be run on this genesis file, upgrading it with whatever is needed to bootstrap a new chain. This process is similar to database migrations common in iterative development and brings a more agile development methodology to blockchain development.

Here’s a high-level view of the basic TruStory architecture.

TruStory Architecture

TruChain Zone contains the validator set that runs TruStory blockchain nodes (TruChain). TruChain is the part that is built with Cosmos SDK and implemented in Go. The code is open source, and available at https://github.com/trustory/truchain.

TruAPI, also written in Go, is a GraphQL light client that is responsible for querying and broadcasting transactions to the chain. It also communicates with a Postgres database for client-specific data that doesn’t need to live on-chain. This includes data such as chat logs and user profiles that aren’t associated with the value of TRU. GraphQL enables the web and mobile clients to perform queries that seamlessly merge data from both the chain and database.

The native mobile and web clients are written in React Native and React Native for Web respectively. This allows a high level of code sharing between native mobile (iOS and Android) and web. They talk to the chain via TruAPI.

A few microservices also power TruStory, such as a push notification service that works by observing events from Tendermint. Push notifications can be very powerful when combined with governance, as users can be notified directly of new proposals and get reminders to vote.

A TruChain node is built around a few core custom modules:

  • claim
  • staking [1]
  • slashing [1]
  • bank [1]

The claim module is mostly a data store for claims. It contains all claim content and high level metadata about each claim. Here’s an example of a claim about man-made climate change on TruStory.

The staking module provides all functionality around staking on arguments. A nice feature of Cosmos SDK is block-level events that run before and after each block is processed. After each block, we check for expiring stakes and distribute rewards.

The slashing module implements punishments for bad behavior in the app and rewards users for moderating bad content. For example, if an argument is downvoted by a certain number of users, the argument creator and upvoters get some of their stake slashed. This helps maintain an incentive structure that keeps the community in check.

The bank module keeps track of token supply and all transactions by users. It mainly powers the wallet functionality of the app.

Each TruChain node also provides a CLI and REST interface that will be used by validators for staking at the network level.

Check out Cosmos Module Generator for a tool that generates much of the boilerplate required for custom modules.

Should You Build on Cosmos?

As a fan of decentralized technologies, I’m very excited about Ethereum 2.0. However, it’s still in development and won’t be ready for at least another year. Cosmos Hub, however, launched in March 2019, and has multiple projects already running testnets. After the implementation of IBC (Inter-Blockchain Communication protocol), Cosmos chains like TruChain will be able to talk to the Cosmos Hub, and eventually transfer assets between Bitcoin and Ethereum via peg zones.

Most decentralized apps do not need the security of thousands of proof-of-work Ethereum nodes. A proof-of-stake sidechain or a custom blockchain secured by a hundred, or even a few dozen nodes would suffice. As a bonus, you can create a governance structure that fits the needs of your app, with a more structured way to handle forks, upgrades, and decisions pertaining to the future of the network. Cosmos SDK is one of the best options to build this future with.

However, building a validator community and running a sovereign chain may not fit the needs of every project. Some may want to buy into an existing validator set and the security it provides. Parity’s Polkadot, for example, is built around a shared security model where each chain connects to a relay chain and inherits its security. While Cosmos currently relies on each chain providing its own security, there are plans to offer shared security in the future.

Another advantage of custom Cosmos chains is not having the requirement of paying gas fees in a platform token (like ETH on Ethereum). These gas fees are often going to parties that have nothing to do with your app or project. Cosmos allows gas to be paid in the native token of your app and gives validators control over the fees they would like to charge.

Check out the Chinese translation of this post by Olivia Yike.

[1] TruStory’s staking, slashing, and bank modules are different from the Cosmos SDK modules of the same names.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Cosmos Ecosystem Blog

Cosmos is the internet of blockchains - an ever-expanding ecosystem of interconnected, blockchain-based apps and services. Powered by the Interchain Stack, Cosmos boasts over 100 IBC-enabled chains.

Responses (1)

What are your thoughts?

Hello Shane, I am Ellie. I think your post is helpful for those who want to know more about Cosmos. Can I translate this into Chinese to distribute to others who cannot read English? Definitely, I will clarify that this article is written by you. Do you think that is fine to translate? Thank you for your contribution!