It’s complicated to stand out among hundreds of blockchain players. However, the Cosmos network has managed to create an “internet of blockchains” and earn the title of Blockchain 3.0.
Cosmos developers have also come up with an open-source tool, the Cosmos SDK. This is a modular cosmos blockchain application framework that provides developers with all the tools they need to write a custom blockchain, build a blockchain application, test the minimum viable product (MVP) version, and connect a product to the Cosmos network.
In this article, we describe what the Cosmos network is, how it works, and what technologies it uses. We also create an example of a decentralized token exchange using the Cosmos SDK.
This article will be useful for developers who are searching for information on how to create custom solutions using the Cosmos SDK. It will also be helpful for anyone interested in blockchain technologies and looking for an overview of the Cosmos network and examples of how it works.
Cosmos is a decentralized network that provides infrastructure supporting interoperability between existing blockchains and opportunities for creating new blockchains. Cosmos strives to empower existing blockchains by creating a network of distributed blockchains.
Although the token sale finished in April 2017, the main blockchain network, Cosmos Hub, was launched only in spring 2019. The project gathered 17 million USD in only 28 minutes. In the table below, you can see more fascinating numbers about the Cosmos network.
According to the initial plan, Cosmos will consist of numerous Hubs, divided into Zones. Each Hub will be an independent blockchain – either a native blockchain network or any external network – connected to other Hubs with the Inter-Blockchain Communication (IBC) protocol.
The primary goal for Cosmos is to significantly simplify the way blockchains communicate with each other, creating an “internet of blockchains.”
It also allows for building blockchains and applications, testing MVPs, launching public mainnets, and more. Developed by the Tendermint team, the Cosmos project is a convenient tool for developers rather than a ready business solution.
To reach a consensus, Tendermint uses a practical Byzantine Fault Tolerant (pBFT) algorithm that ensures security and scalability.
Tendermint is both a consensus engine and the pBFT consensus algorithm. It’s an application-agnostic engine that’s responsible for handling the networking and consensus layers of a blockchain. In practice, this means that Tendermint is responsible for propagating and ordering transaction bytes.
The idea behind the pBFT algorithm is that a fixed number of trusted members sign transactions before they’re transmitted to the network. With pBFT, the network functions correctly even if one-third of all devices are down.
This consensus algorithm is able to prevent such issues as:
- Long-range attacks — Similar to a 51% attack; makes a longer chain that rewrites the ledger in the attacker’s favor
- Nothing-at-stake — A theoretical security hole that can occur when there’s a fork in the blockchain
- Double-spending — A potential issue wherein the same digital token is spent more than once
- Censorship resistance – Assumes the presence of third parties that prevent someone from performing a specific operation
The Cosmos SDK, based on the Tendermint network, is a solution for implementing business logic in applications. It’s not just another blockchain network but an SDK for blockchain development. It reduces development time, providing programmers with the opportunity to focus on business logic.
Before we describe how the platform functions, let’s get acquainted with the main roles within the Cosmos network. Similar to the Tezos blockchain, Cosmos has validators and delegators, which are given both incentives and penalties for their work.
Validators guarantee that consensus is achieved in the Cosmos network, broadcasting votes that contain cryptographic signatures signed with their private keys. Each validator must have a safety deposit with stored Atoms.
On the Cosmos network launch day, the first 100 validators were announced. They were chosen according to their stake of tokens, which were sold in the ICO. The number of validators will increase by 13% each year for the following 10 years until there are 300.
Validators can accept any tokens as payment for handling transactions, set any exchange rates, and choose any transactions. There are several basic rules for all validators, such as being online and signing the correct transactions. All validators must have an opportunity to maintain a sustainable network infrastructure.
All rules and recommendations that concern validators are listed on the Validator FAQ page. If these rules are violated, a validator’s token stake can be reduced according to the agreed conditions within a certain Hub.
Delegators are Atom holders who delegate their tokens to validators. Delegators obtain part of the validators’ revenue in exchange for this. Delegating Atoms is risky because of the possibility to be scammed, so it stimulates delegators to choose validators carefully. By doing so, they improve the safety of the network.
Validators can’t be delegators at the same time. If a validator is going to delegate Atoms, they have to use their own unbonded tokens. A delegator is responsible for choosing a validator and for all of that validator’s actions.
Atom holders pay commission to their validators. Validators and delegators receive Atoms as commission for transactions based on the rules of a given Hub. When other blockchains are added to the network, commissions will be paid in various cryptocurrencies, and Atom will only be used for staking.
Both validators and delegators are rewarded with inflationary Atoms and commission for handling transactions. The initial inflation rate at genesis is 7%.
Atom holders who don’t vote and don’t delegate their tokens to validators will be penalized by inflation. Their stake will be reduced, as they’ll no longer receive newly issued tokens. This is designed to motivate Atom holders to use tokens.
Each validator has to open safety deposits – in other words, to freeze part of their tokens.
In case any malicious actions are spotted and confirmed (for example, two-third of validators reject the transaction and prove the validator is lying), the validator loses part of their Atoms. These tokens then are divided among other network members.
Cosmos Hub – with its Atom token – is the first Hub in the Cosmos ecosystem. Cosmos Hub connects various Zones that use the IBC protocol and facilitates the transfer of tokens between different Zones.
Cosmos can potentially be used for creating a distributed exchange (don’t confuse it with a decentralized exchange), allowing the transfer of assets between different blockchains while offline.
Cosmos Hub is responsible for maintaining a register of tokens in each Zone. Zones that are based on the Tendermint protocol comply with the IBC protocol. You can also use any consensus algorithm that ensures economic finality.
Cosmos Hub is the first Hub in the system, but a lot more are envisioned. For example, there could be more distributed “slow Hubs” and a smaller number of decentralized “fast Hubs.” Technically, there’s no significant difference between Zones and Hubs. As they grow, Zones can transform into Hubs, increasing the network capacity.
The safe exchange of currency and transfer of funds has become an issue even within a single blockchain for two major reasons:
- The invention of blockchain-based cryptocurrencies
- The increasing number of cryptocurrencies
As a result, exchanges and smart contracts have appeared.
The Cosmos SDK brings transactions to the next level. It has a mechanism that allows for establishing interactions between blockchains. So it’s possible to create an exchange in the form of smart contracts using only the SDK platform. Creating a smart contract is significantly simplified and improved with Cosmos SDK.
To be more specific, there are no smart contracts in the Cosmos SDK, as “business logic” has replaced them. And the possibilities for this business logic are vast.
The Cosmos SDK allows us to use a blockchain in the form of a distributed database and build distributed applications on it. This opens the potential for creating various blockchain-based projects: currency-backed tokens, development platforms, and solutions for music, gaming, and other industries.
Additional advantages are total control over the business logic and a much smaller number of potential vulnerabilities. This is important, as when creating smart contracts for other blockchains, vulnerabilities can hide in the contract’s code and runtime environment, which is also called a virtual machine.
Another reason for errors in smart contracts with complicated logic is the complexity of their development and debugging.
When developing solutions using the Cosmos SDK, we write business logic in the Go programming language. So, there are no limitations on launching and debugging business logic. This ensures quality development and testing of complex solutions.
The major problem of existing exchanges is the issue of trust when transferring funds to an exchange’s accounts.
News about hacked exchanges and customer withdrawals has become more frequent lately: seven crypto exchanges suffered large-scale hacking attacks in just the first six months of 2019, according to Cointelegraph.
The Cosmos SDK provides an opportunity to create a decentralized exchange (DEX) using the smart contract approach with no need to withdraw funds at all. Moreover, a decentralized exchange has no evident owner or server.
Read also: 5 Security Tips for Writing Smart Contracts
Let’s create a simplified example of such an exchange. We’ll develop a service for the Cosmos network that allows for creating orders to exchange tokens.
All orders are saved in the blockchain, and the exchange is performed automatically as soon as an order is signed by both parties. In this example, we don’t consider partial order fulfillment, although it also can be implemented.
In our case, an order is a contract for exchanging tokens in the ratio agreed when creating the order. In fact, an exchange will be executed automatically as soon as both parties agree to it. Another essential security advantage of an exchange is that funds aren’t transferred to transit accounts and aren’t blocked.
A mistake in logic or a cyber attack can’t lead to funds getting stuck or being transferred to the wrong account. Only the parties involved in the exchanging currency are involved in the contract, so there’s no need for third parties.
To create an example of a decentralized exchange, we’ll use Go and a text editor.
Since the Cosmos SDK allows us to see a blockchain as a distributed NoSQL database, the first thing to do is decide where to store our data. Our exchange will store orders in the blockchain. For example:
These structures are stored in a blockchain and are encoded with the Amino library.
The next step is to implement transaction handling for creating and executing orders. Custom data is stored in a blockchain and managed by transactions. Each transaction has its own block, which stores random data. The entire transaction and its data are secured from modification by being signed with a user’s private key.
To make an order, we need to create a transaction with a special message in its body. The message format is flexible. We’ll use the following:
After the message is created, it’s packed into the transaction, signed, and sent to the network. As the result of transaction handling, the blockchain receives information about the new order.
Apart from messages, we also have to create handlers for them. Handlers are called by a network node when requesting the node for transaction processing. If a handler knows the type of message in a transaction, the handler’s code is executed and the corresponding changes are made to the blockchain storage.
A transaction’s signature is also checked, and the mechanisms of the Tendermint network validate the changes within the blockchain. Then these changes are replicated among all nodes in the network.
When handlers are created, we can implement the exchange itself. The Cosmos SDK provides all necessary modules. All you need to do is combine them correctly. For example, transferring of tokens from one account to another is executed by calling just one function:
The Cosmos SDK also includes lots of modules like auth, bank, codec, keeper, and rest that significantly simplify the creation of blockchain solutions. Thanks to integrated REST support, creating web and desktop clients is also simple.
Moreover, the REST service allows for creating transactions that are signed with a key on the side of a thin client. This way, a user’s private key doesn’t leave the system and transactions can easily be created from any place, requiring no computation power, no place for a database, and no synchronization with the client. This is extremely convenient, especially for mobile payments.
How to Build a Parachain on Polkadot
The Cosmos blockchain is an ecosystem that makes unrelated blockchains and tokens compatible.
It’s already become an attractive platform to host decentralized applications for various industries and purposes. Thanks to the separation of the consensus and application layer logic as well as the modularity of the Cosmos SDK, programmers can develop applications with modules that fit their use cases.
Using Hubs and Zones, Cosmos has created a unique IBC protocol that enables message passing between two blockchains with independent consensus algorithms. Accepting streams of recent block commits forwarded to the Hubs, Zones create links between a foreign and a Cosmos native cryptocurrency.
At Apriorit, we have a team of dedicated developers with extensive experience in building blockchain solutions. Whether you want to build an app or blockchain with the Cosmos SDK or are looking to improve your project with any other blockchain platform, give us a call. We’re always ready to help.