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:
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
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
The command output lists all of the files set to their initial state with their locations.
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.
Now you can add a new key:
Copy
$ ./simd keys add b9lab
- 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.
Note: 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.
Note: Do not forget to use your own --chain-id.
Copy
$ ./simd gentx b9lab 70000000stake --chain-id test-chain-rT4wZY
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
- 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)
balances: []
pagination:
next_key: null
total: "0"
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. You need to send a transaction to change this new account's balance:
Copy
$ ./simd tx bank send $(./simd keys show b9lab -a) $(./simd keys show student -a) 10stake --chain-id test-chain-rT4wZY
{"body":{"messages":[{"@type":"/cosmos.bank.v1beta1.MsgSend","from_address":"cosmos1nw793j9xvdzl2uc9ly8fas5tcfwfetercpdfqq","to_address":"cosmos1m95dh3uc2s7fkn4w6v3ueux3sya96dhdudwa24","amount":[{"denom":"stake","amount":"10"}]}],"memo":"","timeout_height":"0","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[],"fee":{"amount":[],"gas_limit":"200000","payer":"","granter":""},"tip":null},"signatures":[]}
confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: ""
gas_used: "0"
gas_wanted: "0"
height: "0"
info: ""
logs: []
raw_log: ""
timestamp: ""
tx: null
txhash: D2CCFD91452F8C144BB1E7B54B9723EE3ED85925EE2C8AD843392721D072B895
You should be prompted to confirm the transaction before signing and broadcasting.
The command output includes useful information, such as gas_used.
Now check the balance of the student account again:
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.