# Run a Node, API, and CLI
In this first section, you will learn how to run a blockchain and discover how to interact with it.
There are different ways to run a node of a Cosmos blockchain. You will explore how to do so using
simapp (opens new window).
Before you start working with
simapp, take a look at what you are going to do:
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:
Make sure you are using the same version used at the time of writing:
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
Confirm that you got what you expected:
This should return:
If instead you get this error message:
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
Then recreate your Docker image.
To help you ring-fence this exercise, you can use a Git-ignored (opens new window) subfolder of the repository:
Run this step not only when the database has already been initialized but even if this is the first time you are testing
Time to initialize the application. The initialization creates the genesis block and an initial chain state. Pick a chain id, for instance
Here is a more readable version of the same initial chain state:
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 inspect the initial configuration with:
# Prepare your account
It helps to understand the concepts clearly when working hands-on with the Cosmos SDK. Need a refresher? See the section on Accounts in the Main Concepts chapter.
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
As you might have expected, you do not have any keys yet:
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:
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:
# Make yourself a proper validator
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
stake to your account.
Confirm in the genesis file itself that you have an initial balance:
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
Which confirms the action:
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:
This prints the resulting genesis file:
If you are curious, you can find the updated
gen_txs field in your genesis.
# Create blocks
Now you can start your single-node blockchain:
You can see blocks being produced and validated in the terminal window where you ran the command:
Open a new terminal in the same folder to check balances. First extract
alice's address value:
Then check her balance:
# Send a transaction
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:
Before sending any tokens confirm that the balance of the new account is absent:
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:
You can replace with your own value.
Whenever you need it, you can call back the transaction information using this transaction hash:
Now check the balance of the student account again:
That is a
10 stake, as expected.
# CLI routing
Now it is time for a bit of Go code. How does the
simd interact via the command-line interface? Inspect the
cmd.NewRootCmd() (opens new window) function is the CLI handler. It is imported via the
"github.com/cosmos/cosmos-sdk/simapp/simd/cmd" (opens new window) line. It can be found in the
In it, basic properties such as the application name are defined:
In addition, observe that Cobra is imported and used for the CLI to redirect:
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.
Do you need a conceptual refresher about modules and their role in the Cosmos SDK? See the Modules section in the previous chapter.
To summarize, this section has explored:
- 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
simdso 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.