BloChaDev - Cosmos SDK

0 128
Avatar for zh
Written by
4 years ago

Next article in my BlockChain Development series. It is a logical extension on the previous article about Tendermint Core. If you remember Tendermint core give you a ready to go state-machine replication engine a.k.a blockchain. Great foundation, but coding actual state machines on top of it is not so easy. And in fact it is not the job for any core to do this.

Here the Cosmos project comes. The project describes itself as "...a decentralized network of independent parallel blockchains, each powered by BFT consensus algorithms like Tendermint consensus...". They have very active OSS development - more then 40 active repositories on GitHub! It is impossible to talk about all of them in one article so lets mention just some more popular ones:

  • Gaia - Cosmos Hub - "...allow fully sovereign blockchains to interact with one another using a protocol called IBC..."

  • Scaffold - Scaffolding for Cosmos-SDK based application

  • Cosmos-SDK - A Framework for Building High Value Public Blockchains

  • Ethermint - Ethereum on Tendermint using Cosmos-SDK

Scaffold

For people coming from the Rails development background scaffold is the same as rails scaffold utility - creating a boilerplate of models, views and controllers code (named here Keepers, Handlers etc.). Some times good to start with, but some times too generic and too much changes needed. Mixed fillings on this one...

Cosmos SDK tutorials suggesting starting from the nameservice example and more specifically starting with the scaffold tool , so I tried first:

$ scaffold app lvl-1 zh hello_cosmos
$ cd hello_cosmos
# for some reason need to remove -mod=readonly in the Makefile
# because of: go: updates to go.sum needed, disabled by -mod=readonly
$ make

Good! Compilation gave me two binaries installed in $GOLANG/bin/ directory, so I can start them directly and check the available options:

$ appd
$ appcli

Names of the binaries can be changed and will be different for the different projects. appd is a very thin wrapper around tendermint daemon starting command. appcli is a wrapper around abci-cli for sending commands to that daemon. Still not too much as a functionality added to the tendermint, basically same options can be used - init, tx, query etc., but it so much more convenient now: different daemon and cli names for every project, different config directories ( ~/.appd/ and ~/.appcli/ on OS X) for different projects etc.

The interesting part begins, when you start adding custom modules. They will add / change behaviour of the basic commands, usually adding some subcommands to them:

$ cd x/
$ scaffold module zh hello_cosmos nameservice

I added the new module, but as I mentioned above scaffold is too generic - a lot of changes needed to make this module really doing something useful.

So I started changing different files, following the tutorial, but they lost me somewhere in the middle... To much was going on... And I'm lazy... Started searching for an easier way... And DA-DA-AAAA - found SDK Tutorials repo - all of the tutorials code, already finished! Nice. So I small tip:

Do not start with scaffolding, clone SDK tutorials and start studing / changing already working code - manual suggesting nameservice or scavenge.

Cosmos SDK

  • A Framework for Building High Value Public Blockchains

  • Main programming language: Go

  • GitHub repository

I want to talk more also about Cosmos SDK (maybe in separate article...), because it is the real mother of dragons, producing more and more awesome new tools. When some of them became too big, they usually move it to a separate repository as the Gaia Hub, mentioned above. Just look on the modules directory ! Real code treasure:

  • Auth - Authentication of accounts and transactions for Cosmos SDK application

  • Bank - Token transfer functionalities

  • Staking - Proof-of-stake layer for public blockchains

  • Mint - Creation of new units of staking token

Your own CryptoCoin in less than 5 min

SDK hellochain tutorial mentioning, that this example is broken, so I extracted the essential parts of it in a separate repository, something between scaffold and hellochain:

  • More then scaffold - the starter part, providing creating users and coin exchange

  • Less then hellochain SDK tutorial - no greeter module

$ git clone https://github.com/zh/hw_chain.git
$ cd hw_chain
$ make
$ ./init.sh  # create and provision two users - test1 and test2
...
  "name": "test1",
  "type": "local",
  "address": "cosmos1n80wglue...
...
$ hwd start
...starting ABCI with Tendermint                module=main

Done. You have running blockchain and you can exchange HelloWorldCoin (HWC) on top of it:

  • Get user test1 balance

    $ hwcli query account $(hwcli keys show test1 -a)
    ..."address": "cosmos1n80wglue8uepm06axpqj69l6q0jmjemantu4fh",
        "coins": [
          {
            "denom": "hwc",
            "amount": "1000000"
          },
    ...
  • Get user test2 balance

    $ hwcli query account $(hwcli keys show test2 -a)
    ...
    "coins": [
          {
            "denom": "hwc",
            "amount": "1000"
          },
    ...
  • User test1 sending 50 HWC to user test2 (transaction with confirmation)

    $ hwcli tx send $(hwcli keys show test1 -a) $(hwcli keys show test2 -a) 50hwc
    ...
    "msgs": [
        {
            ...
            "amount": [
              {
                "denom": "hwc",
                "amount": "50"
              }
    confirm transaction before signing and broadcasting [y/N]: y
    {
      ...
      "txhash": "E701569...",
      ...
    }
    $ hwcli query account $(hwcli keys show test2 -a) # get user test2 balance
    ...
    "coins": [
          {
            "denom": "hwc",
            "amount": "1050"
          },
    
    

Summary

Cosmos is a modern and very active project. A lot of great stuff coming almost every day from them: connecting different blockchains, game of zones etc. It is a real pleasure to use their SDK. With Tendermint for the state-machine replication engine and Cosmos SDK for building secure blockchain applications the possibilities are endless.

For recent news follow Cosmos on twitter, reddit or sign on for their newsletter.

Additional information

Coming next: Hm,... still not sure, maybe Zilliqa or Polkadot ...

P.S. I know I will not become rich, writing these articles, but contributing a small amount of BCH will be hugely appreciatedwill support my work and keep me going.

1
$ 0.00
Sponsors of zh
empty
empty
empty
Avatar for zh
Written by
4 years ago

Comments

I want to talk more also about Cosmos SDK (maybe in separate article...), because it is the real mother of dragons, producing more and more awesome new tools. When some of them became too big, they usually move it to a separate repository as the Gaia Hub, mentioned above. Just look on the modules directory ! Real code treasure:

One of my favorite☝☝

$ 0.00
4 years ago

It too hard for me to understand being new in cryoto. But it wikk be useful to other ms

$ 0.00
4 years ago