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.
Contents:
What is the Cosmos network?
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. 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.
The technology behind the Cosmos network
The Cosmos ecosystem is based on the Tendermint consensus protocol and the Application Blockchain Interface (ABCI). It relies on the Proof-of-Stake consensus algorithm.
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.
Roles within the Cosmos network
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
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
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.
Incentives and penalties
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.
How does Cosmos work?
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.
Cosmos SDK and decentralized exchanges
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.
Also, check out our another article to learn more about smart contract attack vectors.
Read also: 5 Security Tips for Writing Smart Contracts
Creating a DEX using the Cosmos SDK
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:
type Order struct {
OrderID string `json:"OrderID"`
Buy sdk.Coins `json:"Buy"`
Sell sdk.Coins `json:"Sell"`
Owner sdk.AccAddress `json:"Owner"`
Buyer sdk.AccAddress `json:"Buyer"`
Created time.Time `json:"Created"`
Completed time.Time `json:"Completed"`
}
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:
type MsgCreateOrder struct {
Buy sdk.Coins
Sell sdk.Coins
Owner sdk.AccAddress
}
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:
keeper.coinKeeper.SendCoins(ctx, msg.Buyer, order.Owner, order.Buy)
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.
Read also:
How to Build a Parachain on Polkadot
Conclusion
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.