Building on Cosmos (Part 1/3): Launching an application-specific blockchain

Stoyan Dimitrov
Eco Engineering
Published in
7 min readJan 7, 2022
Photo by SpaceX on Unsplash

In this tutorial we demonstrate how to build your own application-specific blockchain in a few hours. Cosmos is the most well developed platform for building application-specific chains. We

It used to be the case that Dapps would all be built on top of the ethereum chain, but recently companies like Binance and Facebook, and many others, have opted to build their own custom chains. The reasons for this change to app specific blockchains and away from the early blockchain monoliths are numerous. For instance, instead of having to build around ethereum’s contract based VM, we can develop our own blockchain that is specialized to their application. With our own blockchain we free ourselves from the constraints that general purpose VM chains like ethereum impose. We do not need to compete with other applications for a share of a chain’s limited resources. We don’t need to accept a general solution for our particular application, nor the sovereignty or token of another chain just to use our own application.

We will use the cosmos-sdk, the first all-in-one developer framework that lets you build your own blockchain. The cosmos-sdk lets us build custom blockchains using a composable, modular system of components (it’s developed by the All in Bits team, who you can read about here). The sdk allows us to focus on building the application logic of our chain, while allowing us to drop in blockchain functionality from an ecosystem of developers. This greatly accelerates our app development, and provides us with audited blockchain code that we don’t have to build from scratch.

Prior to cosmos-sdk, we would have had to either build everything from scratch, or clone another chain and modify it. In this 3 part tutorial series, we will build our own chain and show just how efficient blockchain development has become.

The Game

In this tutorial, we will implement an app that we will call “The Button Game”. This is a simple game that will let us touch on the basic parts that go into making an application specific blockchain, like data storage, accounts, state transactions, etc. The game charges players a fee every time they click a button, and places their fees into a pool that makes up the reward prize for the winning player. In order for any player to win the reward, they have to have been the last to click the button as well as have waited at least 3 blocks to have been created since the button was pressed. Since all players can play at any time, it becomes very difficult for anyone to make it 3 blocks without someone else having clicked the button. Whichever player managed to do this can make a call to claim treasure and take everyone’s deposit, thereby ending the game. Granted, this game is no Counter-Strike, but we’re intentionally keeping it simple in order to focus on the process of building our blockchain.

Dev Setup

We will be programing this project in golang, so make sure you have golang 1.12.0+ installed on your machine, as you will need module support enabled to compile the project. If you are on a Mac you can install golang with brew by following this tutorial and making sure to enable go modules in your .bash, .zshrc or .bashrc :

To check your go version, just run this on your command line:

You will also need to have git working on your machine. Each section of the tutorial has all of its code in its own branch to make it easier to follow along. You can also checkout each section’s code from its branch if you don’t feel like typing it out yourself. The project can be found at: https://github.com/eco/button

App

Our application will be broken down into 2 main components. One will be our blockchain and game logic, and the other will be the CLI programs for interacting with the chain and playing our game. Our game logic and most of our coding will be placed in a module in the ‘/x/button’ package. We have created a base project that has all of our boilerplate code already written, which takes care of setting up the cosmos-sdk so that we can focus on building with it. In a nutshell, the code gives us a skeleton of a blockchain and command line tools that we can start using immediately, instead of having to write them ourselves each time. We will go over what each part of that code does as we advance through the tutorial.

Now’s the time for us to get the project and launch our own blockchain. Go ahead and clone the project into a working directory and checkout the first branch:

Once you’ve cloned the project and checked out the 1_base branch, let’s compile and run it. First we will get all of the dependencies, then compile them, and finally start the chain up with an initialization script that is already in the repo.

If everything worked out correctly, you should see “make” install the tools, then create two accounts and finally start the chain from block 0. The chain will hang for a bit finding peers, then give up and start mining since we are the only node on the network. That’s how easy it was to create your own chain!

make all terminal output

Let’s now go over what the make file and “initChain.sh” script do. The makefile builds and installs two CLI programs that we will build upon later in this tutorial. The first program is called “bt” and is responsible for initializing our blockchain and running a validator for it. The second program is called “btcli” and is a handy command line app that lets us create accounts and send messages to our blockchain. We will use this tool to later define and send app specific messages to our chain.

The “initChain.sh” first deletes any previous chain data by deleting the directories that would store it, and then initializes a new test chain. After that, it creates 2 accounts and adds them to the genesis file of the chain with some initial funds. The genesis file holds the initial state of our chain at block 0, and is used to initialize our application on start-up with accounts, amongst other things. Finally, the script starts our blockchain daemon.

Let’s use the “btcli” command to check that the accounts that the script created are there. Open a new terminal and type:

You should get back the two accounts for Alice and Bob, along with their address and public key. Feel free to take some time and explore all of the subcommands that are part of our btcli tool.

The above output tells us what accounts we have keys for, but we also want to be able to check the balances of accounts on our blockchain. In order to do this we’ll need to add an account’s query command to our tool! We can do this by modifying our ‘cmd/btcli/main.go’ file to import our application codec, and then add the account module’s commands to our tool.

First, we add a call to get our application codec in main before we give it to the queryCmd function. The codec is used to marshal/unmarshal messages from their protobuf compressed format into something more usable in our code. Then we’ll add our new command to the root command. This is a feature of the cobra library, which allows us to easily nest commands.

There we have it. We’ve just defined a new sub-command and gave it a name and description. We then need imported the account modules commands and added them to our sub-command.

Throughout this tutorial, we will use git branches to keep you checkpointed. You can always checkout the project in case you miss something when going through this tutorial. Your code should now look like our branch, 2_add_account_query.

You might have to update your dependencies by running:

You can check out the branch by going into our project directory, and then typing into your console:

Now let’s rebuild our project and launch our chain again.

Take a look at our btcli tool now; we can see that it has our query command as one of its subcommands.

We can now check the account balances of Alice and Bob, using our own local blockchain daemon as a source, so make sure the bt tool is running.

If the blockchain daemon is not running, you might get a “connection refused”. If the daemon isn’t already running you can start it with:

The command queries the blockchain for Alice's account balance. Since we need to pass the command Alice’s address, we make a small function to return the address from the name of the key.

We have now built our own chain, and launched it. Let’s quickly review the steps that got us here so fast.

  1. We set up our dev environment and installed the golang.
  2. We cloned the base project from github. The project already had the cosmos-sdk set up with some convenience CLI commands ready. This allowed us to immediately jump in to building our game.
  3. We compiled and started our own chain. This thought us how to use the make file. We then went into over what the make command does under the hood for us.
  4. We added a new function to the CLI command that allowed us to query accounts and their balances from our local validator daemon. We were able to them query the account balances that we created.

In the next part of the tutorial, we will add our button game logic to our chain and dive deeper into how we can store state on chain and process messages sent from our game players.

--

--