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.42.6
Then build cosmos-sdk:
Copy
$ make build
The build takes a few minutes and creates a build folder and a simapp binary named simd:
Now reset the database. 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
$ cd build
$ ./simd unsafe-reset-all
The command output lists all of the files set to their initial state with their locations.
Copy
3:58PM INF Removed all blockchain history dir=/Users/b9lab/.simapp/data
3:58PM INF Generated private validator file keyFile=/Users/b9lab/.simapp/config/priv_validator_key.json stateFile=/Users/b9lab/.simapp/data/priv_validator_state.json
Time to initialize the application. The initialization creates the genesis block and an initial chain state:
You can find your chain_id in your output, which in our build happens to be called test-chain-rT4wZY. Make a note of your output's name, as you will need it later to determine the chain ID by passing it to simapp via the flag --chain-id.
You can also inspect your keys. These are held in the backend keyring, which by default is that of the operating system:
Copy
$ ./simd keys list
As you might have expected, you do not have any keys yet:
Copy
[]
Now you can add a new key:
Copy
$ ./simd keys add b9lab
Which prints something similar to:
Copy
- name: b9lab
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.
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 validator for bootstrapping purposes.
Make your key, also known as an account, have an initial balance in the genesis file:
Appended here to the amount is the stake suffix. This stake represents the unit for the tokens in this chain as per the genesis file. Therefore, this command adds 100000000stake to your account. If in doubt, you can confirm the proper suffix in the genesis.json file with:
Copy
$ grep -A 2 -B 2 denom ~/.simapp/config/genesis.json
You can also confirm in the genesis file itself that you have an initial balance:
Copy
$ grep -A 10 balances ~/.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 you must meet the 2/3 threshold for validation, so you must stake at least 70000000stake of your 100000000stake in the b9lab account you just created. Make sure to not use all your stake, so you still have tokens to pay for gas.
Copy
Genesis transaction written to "/Users/muratoener/.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 it in your genesis file:
Practice sending a transaction. To do this, you are going to create another account named "student" and transfer some tokens to that account:
Copy
$ ./simd keys add student
Which prints:
Copy
- name: student
type: local
address: cosmos1m95dh3uc2s7fkn4w6v3ueux3sya96dhdudwa24
pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AgDYHucSs5vZ4viGyyoC0Qz6M7/+fEdqgOesEmeTdPE/"}'
mnemonic: ""
**Important** record this mnemonic phrase in a safe place. It is the only way to recover your account if you ever forget your password.
gown all scissors page panel table hill acoustic junior run winter cement mass clump moon adjust glare never satoshi easily illness hip rib multiply
Before sending any tokens confirm that the balance of the new account is absent:
Copy
$ ./simd query bank balances $(./simd keys show student -a)
This account does not have a balance. The new account does not yet exist in your blockchain. Only the key pair has been generated and stored in your keyring:
The modules in the /cosmos-sdk/x/ folder are maintained by several organisations working on the Cosmos 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.