The Cosmos SDK repository contains a folder called simapp(opens new window). In this folder you can find the code to run a simulated version of the Cosmos SDK, so you can test commands without actually interacting with your chain. The binary is called simd and you will be using it to interact with your node.
First, create and change the directory into a cosmos folder, and then clone the cosmos-sdk repo into that folder:
Copy
$ mkdir cosmos
$ cd cosmos
$ git clone https://github.com/cosmos/cosmos-sdk
$ cd cosmos-sdk
Make sure you are using the same version used at the time of writing:
Copy
$ git checkout v0.45.4
Now build cosmos-sdk. If you use Docker, with the help of a ready-made multi-stage creation(opens new window) you create a new Docker image that contains the compiled simd:
Confirm that you got what you expected:
This should return:
Copy
0.45.4
If instead you get this error message:
Copy
exec format error
It means the executable was compiled with the wrong CPU architecture. It may happen on Arch Linux. To fix this, you may update the Makefile to force the CPU architecture to yours. For instance, if you are on an Intel CPU, that would be amd64:
To help you ring-fence this exercise, you can use a Git-ignored(opens new window) subfolder of the repository: private.
Run this step not only when the database has already been initialized but even if this is the first time you are testing simapp:
Copy
$ rm -rf ./private/.simapp
Time to initialize the application. The initialization creates the genesis block and an initial chain state. Pick a chain id, for instance learning-chain-1:
It is good practice to append a number, such as -1, at the end of a more meaningful chain id, such as learning-chain. This allows you to increment this number if and when you introduce a hard-fork to your chain.
You can also inspect your keys. These are held in one of the backend keyrings, which by default is that of the operating system or of the test. To ring-fence them too, and to ensure consistency, you will use the test backend and also save them in ./private/.simapp:
As you might have expected, you do not have any keys yet:
Copy
[]
Now you can add a new key:
It does not ask for any passphrase, and saves them in the clear in ./private/.simapp/keyring-test. Remember that these keys are only for testing, so you do not need to worry. When done, this prints something similar to:
Copy
- name: alice
type: local
address: cosmos1nw793j9xvdzl2uc9ly8fas5tcfwfetercpdfqq
pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"A6TrsRO/OH91fAEFLohw7RwFB832NRsRWhQvE2t8cfLK"}'
mnemonic: ""
**Important:** write this mnemonic phrase in a safe place. It is the only way to recover your account if you ever forget your password.
ivory uniform actual spot floor vessel monster rose yellow noise smile odor veteran human reason miss stadium phrase assault puzzle sentence approve coral apology
You can see the mnemonic at the end of the above output. This sequence of words is a mnemonic that you can use to recover your public and private keys. In a production setting, the mnemonic must be stored in a reliable and confidential fashion as part of the key-management infrastructure.
Confirm that the key has been added with:
You can also confirm that the key has been added with:
As previously explained, a Cosmos SDK blockchain relies on identified validators to produce blocks. Initially there is no validator to generate blocks. You are in a catch-22 situation: your initialized and unstarted chain needs a genesis account and a validator for bootstrapping purposes.
You must make your key, also known as an account, have an initial balance in the genesis file. For that, you need to know the staking denomination:
With this, you can give enough to alice in the genesis:
Appended here to the amount is the stake suffix. Therefore, this command adds 100000000stake to your account.
Confirm in the genesis file itself that you have an initial balance:
Copy
$ grep -A 10 balances ./private/.simapp/config/genesis.json
Despite this initial balance, before you run your blockchain you still need to escape the catch-22 and include your bootstrap transactions in the genesis file.
In this scenario, for your network to even run you must meet the 2/3rds threshold of the weighted validators.
Because you will be alone on the network you can stake any number at or above the minimum enforced(opens new window), i.e. 1000000stake. However, to remind yourself that it is important that honest nodes stake a large amount, stake 70000000stake of the 100000000stake in the alice account you just created. Make sure not to use all of your tokens, so you can still pay for gas and so you don't run out of tokens later.
Do not forget to use your own --chain-id.
Which confirms the action:
Copy
Genesis transaction written to "/Users/alice/cosmos/cosmos-sdk/private/.simapp/config/gentx/gentx-cf6bff39bb84da39d214138ebba8bcba4ccb848d.json"
After you have created this genesis transaction in its own file, collect all the genesis transactions with collect-gentxs to include them in your genesis file. Here you have only one anyway:
Practice sending a transaction. You can send tokens to any valid address. For instance, you can send tokens to an address whose private key you do not know. Head to Mintscan for the Cosmos Hub(opens new window), and pick a cosmos1... address from one of the latest transactions. For instance:
This account does not have a balance. Indeed, although you picked the address from the Cosmos Hub, and therefore bob has a balance on the Cosmos Hub, it does not yet exist on your local learning blockchain.
You need to send a transaction to change this new account's balance:
You should be prompted to confirm the transaction before signing and broadcasting:
The command output could include useful information, such as gas_used. However, here it did not have time to collect the information because the command returned before the transaction was included in a block. Take note of the transaction hash. In the above example, it is:
Also, look at simapp/app.go(opens new window), in which each module and key keeper will be imported. The first thing you will see is a considerable list of modules that are used by most Cosmos-sdk applications:
The modules in the /cosmos-sdk/x/ folder are maintained by several organisations working on the Interchain Stack. To understand a module, the best way is to have a look at the respective spec folder. For example, look at the cosmos-sdk/x/bank/spec/01_state.md(opens new window) to understand the state of the bank module which you used in this section.
How to run and to interact with a blockchain by using simapp, which contains the code necessary to run a simulated version of the Cosmos SDK called simd so you can test commands without actually interacting with your chain.
How to compile and initialize simapp, and to inspect the initial configuration of your chain's genesis state.
How to prepare your account, including how to add, confirm, and inspect your keys, and review your mnemonic.
How to make yourself a proper validator, by adding and confirming the presence of an initial balance and including bootstrap transactions in the genesis file.
How to start your single-node blockchain, observe blocks being created through the terminal window, and check the balances.
How to practice sending transactions to another account and transferring tokens to it, and checking the balance of the new account to confirm the successful transfer.
CLI routing with the examination of the initial Go code, revealing various aspects of your nascent chain.