Blockchains are very powerful. Bitcoin, Ethereum, and others have demonstrated possible outcomes when blockchain-related solutions are adopted. They have proven reliable and robust while offering various services that may be unique to each chain. However, there has been a lingering problem of scalability, usability, interoperability, and sovereignty.
Imagine combining the power of all blockchains and doing away with those nagging issues. You Guessed Right! That will result in a scalable and seamless system where every chain could benefit from the functionalities of other networks.
Instead of just imagining, Cosmos created a system that enables blockchains to connect. That system thrives on a hub - Cosmos Hub.
In this article, I will explain all about Cosmos Hub. I will also introduce you to the basics of the Cosmos SDK. But what is Cosmos?
What is Cosmos?
Cosmos is an evolving open-source decentralized network of independent blockchains that communicate with one another using the IBC (Inter-Blockchain Communication) protocol. It is referred to as The Internet of Blockchains, and each blockchain connected to it is called a ZONE.
It is based on the idea of interoperability and customization. Customization implies that Cosmos does not dictate how zones are created and managed, while interoperability means that Cosmos makes it easy for zones to communicate and share data among themselves. One advantage of building on Cosmos is that it handles the Consensus and Network layer of a blockchain allowing the developers to focus on the application layer only.
Cosmos is a Proof-of-Stake (POS) blockchain. Those who validate the network must prove that they have something at stake. For example, they may stake many coins, and the Cosmos network will lock them up so that if they are fraudulent in the future, the coins become compensation to the network.
Cosmos solved the problem of scalability, usability, interoperability, and sovereignty in the following way:
Problem | Solution |
---|---|
Scalability | BFT (Byzantine Fault Tolerant) consensus algorithm |
Usability | Cosmos SDK (Software Development Kit) |
Interoperability | IBC protocol |
Sovereignty | Custom Governance - Allowing individual blockchian application to setup their own governance structure |
In all, Cosmos is working hard to create an ecosystem that is trustless and permissionless, yet it will be private. Private in the sense that it does not interfere with the privacy of the nodes in its network.
Definition of Terms
BFT is an algorithm that ensures that the whole network works well even if a few members are acting fraudulently. As long as these fraudulent members are less than 33% of those who have voting power, the network will work fine. Those with voting power are those with something at stake. Cosmos uses a type of BFT consensus engine known as Tendermint. Cosmos uses Tendermint because it provides the best support for building POS blockchains.
Cosmos SDK is a powerful development kit created by Cosmos to provide all the tools required to build a blockchain application on top of the Cosmos network. The SDK helps with operations such as staking, minting, and distributing ATOM tokens. The Cosmos network has its native token called the ATOM token. I will elaborate more on the SDK below as the article progresses.
The IBC protocol is a kind of agreement or contract entered by zones to enable them to interact with each other and exchange resources. The protocol allows one blockchain (for example, the Algorand blockchain) to send funds to another chain (like Bitcoin) via Cosmos. How? IBC will convert the Algorand native token to a Bitcoin native token and then send it to the wallet of the Bitcoin user.
Trustless is a term in web3 used to describe a system where blockchains connected to a network do not have to bother verifying the authenticity of the other nodes in a network. They can go ahead with transacting with the network with no fear because the network does the verification for them.
A network that allows its nodes node to interact with each other without first obtaining permission, approval, or authorization is called a permissionless network.
So how exactly does Cosmos use the Cosmos Hub?
Cosmos Hub
The question that arises when the idea of connecting blockchains comes is: How will Cosmos keep a clean connection between blockchains? That is where the Cosmos Hub comes into play.
A hub is the center of anything. In like manner, the Cosmos Hub is the center of the Cosmos network. All zones connect to the Cosmos Hub and communicate via the hub.
So instead of having one blockchain connect to every other chain in the network by itself, each blockchain plugs into the hub. The hub keeps track of every zone. When a zone requires resources from another, the hub gets the resources from the zone that has them and passes them to the zone calling for them.
Blockchains built on the Cosmos network connect directly to the hub, while blockchains like Bitcoin, Ethereum, Algorand, and Solana will need to connect to the hub using a PEG ZONE.
A Peg Zone is an adaptor that enables blockchains from a different origin other than Cosmos, to subscribe to the Cosmos network by connecting to the Cosmos Hub. The Cosmos ecosystem will now look like this:
I already pointed out that the Cosmos SDK is used to build blockchains on the Cosmos network. How? I will provide that answer in the next section.
Cosmos SDK
This is an open-source framework that provides toolings for building POS blockchains on the Cosmos network using Tendermint. It contains different modules that make it easy for developers to quickly cook up their blockchains. The modules include staking (POS), governance, inflation, and minting.
The Cosmos SDK is the most advanced framework for building custom application-specific blockchains. An application-specific blockchain is a type of chain that is customized for just one main function, and its governance is managed by the developers. It is different than building a blockchain on an existing chain like a virtual machine (VM) such as the Ethereum Virtual Machine(EVM) because Cosmos allows developers to set up their custom governance. Those building on a VM does not have such liberty. To build on a VM means that a developer must adhere to the rules set up by the VM.
Cosmos SDK uses two types of encoding protocol for data processing, Amino, and Protobuf (Protocol Buffers). Amino used to be the encoding protocol, but due to its drawbacks in recent times, Cosmos SDK is not moving towards Protobuf, specifically gogoprotobuf. Protobuf is preferred because it offers cross-language support, excellent developer experience, and extensive toolings. One of the tools that protobuf provides is endpoints and events.
The Cosmos network exposes endpoints (such as gRPC and daemon client) and events via web sockets for every node in the ecosystem. What are gRPCs, daemon client, events, and web sockets? How are they used in the Cosmos ecosystem?
gRPC
It is vital to understand RPC (Remote Procedure Call) to grasp what gRPC entails. RPC is a method used to communicate and exchange assets between programs on different computers. Distributed systems use this method.
gRPC is a type of RPC first implemented by Google. It is an open-source framework for communicating between microservices irrespective of the programming language used. It uses protobuf encoding.
Many companies and individuals are employing gRPC due to the binary encoding format used to transport data which is faster than using JSON. Another reason is that it makes use of HTTP2 to scale performance.
Zones on the Cosmos network use gRPCs to interact and share resources. The default port is 9090
, and the code below is used to subscribe to it:
RegisterGRPCServer(grpc.Server)
The grpc.Server
in the code above provides access to all the gRPC query and transaction requests.
gRPC provides a gateway for a developer who cannot use gRPC for any reason such as the browser not being HTTP2 compatible or the developer feeling more comfortable using REST. It is called the gRPC-gateway.
The gRPC-gateway provides an alternative REST endpoint for every gRPC endpoint. So for every gRPC method defined, the corresponding REST endpoint is defined as an option like in the following code:
// AllBalances queries the balance of all coins for a single account
rpc AllBalances(QueryAllBalancesRequest) returns (QueryAllBalancesResponse) {
option (google.api.http).get = "/cosmos/bank/v1beta1/balances/{address}";
}
Daemon Client
This is the main endpoint of the Cosmos SDK, also referred to as the full-node client. The endpoint runs the state machine and connects to other nodes running the same state machine to exchange resources. This exchange happens with the help of the gRPC request and response system.
It is created by running the main
function of the Cosmos SDK. The code below is a sample of what the function may look like:
package main
import (
"os"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/simd/cmd"
)
func main() {
rootCmd, _ := cmd.NewRootCmd()
if err := svrcmd.Execute(rootCmd, "", simapp.DefaultNodeHome); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}
The code above is the main
function and is a part of a larger application found here on GitHub.
The full-node client also has access to a start
command that an end-user can call. When the command is triggered, it starts a node. It will look something like this:
# For an example app named "app", the following command starts the full-node.
appd start
# Using the Cosmos SDK's own simapp, the following commands start the simapp node.
simd start
The start
command begins by retrieving the state of the application and its configurations from a config
file provided when the daemon client was created. These details are now used to create a new instance of the application, which then instantiates a Tendermint node. The node can now be started. This is the point to harness all the cool functions of the gRPC.
As the node starts and communicates with other nodes, it might notice that it is not in sync with others. It uses the gRPC protocol to request updates from other nodes. These nodes will also use the gRPC endpoint to respond with the necessary feedback.
Events and Web-Sockets
I will discuss these concepts in this section because they are closely related.
Events are objects containing reports about the process execution of an application, like blockchains. Events In the Cosmos network work in the same manner.
Web socket is a two-way communication stream that allows for parallel transfer of information, to and fro, over a network.
People who subscribe to events, such as service providers, use web sockets to subscribe to an event. The code below exemplifies how to subscribe to it:
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='eventCategory' AND eventType.eventAttribute='attributeValue'"
}
}
The Cosmos Developer Community
Cosmos is a community-led ecosystem. It enjoys great support from developers as an open-source network. A good demonstration is that they continue to contribute to the project and build packages to help make Cosmos easily available for as many developers as possible. One such package is the Cosmos JavaScript SDK.
Cosmos JavaScript SDK
This is an open-source JavaScript SDK created by konstellation to support address generation and verification in Cosmos. The purpose is to enable developers to create offline signature functions for varying transaction messages. These functions use the RPC protocol.
Developers have to install the SDK and connect to the RPC nodes to get access to those functions. The command below installs it via NPM:
npm install @konstellation/cosmosjs
The following code is used to connect to the RPC nodes:
const sdk = require("@konstellation/cosmosjs");
const chain = sdk({
apiUrl: 'http://localhost:1317',
nodeUrl: 'http://localhost:26657',
chainId: 'darchub',
});
This exposes all the functions. Two widely used ones are Transfer
and TransferFromAccount
, which are used for sending transactions on the Cosmos Hub. See sample codes below:
- Transfer method
const res = await chain.transfer({
from: account.getAddress(),
accountNumber: account.getAccountNumber(),
sequence: account.getSequence(),
privateKey: account.getPrivateKey(),
publicKey: account.getPublicKey(),
to: address,
amount: {amount, denom},
memo,
fee: {amount, denom},
gas,
});
This function sends transactions from one party to the other. It gets the address and account number of the person logged in from the state. It also requires public
and private
keys for the transaction to go through.
- TransferFromAccount method
const res = await chain.transferWithAccount({
from: account,
to: address,
amount: {amount, denom},
memo,
fee: {amount, denom},
gas
});
This function authorizes a 3rd party (from
) to send transactions on behalf of the one doing the authorization.
The documentation here contains the full list of functions.
Frequently Asked Questions
- What is the Cosmos Hub and why is it important?
The Cosmos Hub is an open-source point of contact for blockchains that want to join the Cosmos ecosystem and interact with one another. It is important because it serves as a mediator between all chains in the network. What this implies is that whenever one blockchain needs a resource from another blockchain, It sends the request to the hub and the hub does the hard work of getting the requested resource for the blockchain asking for it.
- Which tokens are allowed on the Cosmos Hub?
The native Cosmos token is ATOM. However, any coin can be used in the ecosystem. This transaction is handled by the IBC.
- Can blockchains originating from sources other than the Cosmos SDK connect to the Cosmos Hub?
Yes. Blockchains that are not built with the Cosmos SDK can connect to the hub as side chains using a peg zone.
Conclusion
This article has been an eye-opener as to what blockchains can achieve if they collaborate. Cosmos is doing a great job of building the future of web3 through its ecosystem by using the Cosmos Hub to enhance collaboration among the existing blockchains and others yet to come.
The article explained what Cosmos is all about, the power of the Cosmos hub, the well-packaged Cosmos SDK, and the Cosmos JavaScript SDK that exposes handy functions to manage transactions. You will agree that Cosmos has a well-grounded community of developers.
There is a lot in store for the web3 community. Please check out the following documentation for maximum benefit:
Keep Building!
Top comments (0)