Skip to content

A comprehensive guide on how to create a local Cosmos testnet with 2 validator nodes. (NOTE: this guide isn't meant to be utilized for production environment).

Notifications You must be signed in to change notification settings

ducmint864/Cosmos-local-testnet-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 

Repository files navigation

Tutorial: Simple cosmos testnet with 2 validator nodes

1. Prerequisites

Golang Ignite CLI 0.28 /home/<your-username>/go/bin added to PATH /usr/local/go/bin added to PATH

2. Create a test blockchain application using Ignite CLI

Create a binary in your terminal:

ignite scaffold chain <application-name>

Example:

ignite scaffold chain testchain

Build the chain:

cd <application-name>
ignite chain build

Cosmos SDK will automatically produce an executable binary CLI file, and we can use it as a command:

<application-name>d

* For example, if application-name is testchain, the command would be:
testchaind

* If the command is not recognized by the system, add /home/<your-username/go/bin/ go your PATH env variable as cosmos put the binary file in /home/<your-username>/go/bin/ by default

| From now on, we will assume that your application-name was testchain. Therefore, the binary cli file would be testchaind|

3. Setup testnet environment

3.1 Create testnet folder and nodes folder

cd ~
mkdir testnet && cd testnet
mkdir node1; mkdir node2; mkdir node1/node1home; mkdir node2/node2home

3.2 Create test accounts(keys) for each node

Create test account for node1:

testchaind keys add node1-account --home node1/node1home/

Create test account for node2:

testchaind keys add node2-account --home node2/node2home/

4. Initialize chain nodes

From the ~/testnet folder, run:

testchaind init node1 --home node1/node1home/
testchaind init node2 --home node2/node2home/

5. Add genesis accounts (Only run on 1 node)

From the ~/testnet folder, run:

testchaind genesis add-genesis-account $(testchaind keys show node1-account -a --home node1/node1home) 100000000000000000000000000stake --home node1/node1home
testchaind genesis add-genesis-account $(testchaind keys show node2-account -a --home node2/node2home) 100000000000000000000000000stake --home node1/node1home

6. Stake into the chain (Only run on 1 node)

From the ~/testnet folder, run:

testchaind genesis gentx node1-account 10000000000stake --fees 100000stake --home node1/node1home

Add the generated gentx transaction to genesis.json:

testchaind genesis collect-gentxs --home node1/node1home/ --trace

7. Start chain on node1

Before running 2 nodes, it's vital to get a single node up n running first. The first step to operating a node is to modify its config. Notably, the 'api server' and 'swagger' options should be enabled in the application config. Go to the node1/node1home/config/app.toml to change those options as follow:

###############################################################################
###                           API Configuration                             ###
###############################################################################

[api]

# Enable defines if the API server should be enabled.
enable = true

# Swagger defines if swagger documentation should automatically be registered.
swagger = true

In the same node1/node1home/config/app.toml file, provide a concrete value for the 'minimum-gas-price' chain parameter which is left blank by default. For example:

minimum-gas-prices = "0.1stake"

NOTE: Keep in mind that you can put your desired value. Nevertheless, "0.1stake" is a decent starting point for test-oriented chain.

Then start the chain:

testchaind start --home node1/node1home

8. Start chain on node2 and sync it with node1

8.1 Copy chain configuration from node1 to node2

First of all, go back to ~/testnet folder:

cd ~/testnet

Copy all the config files from node1 to node2 by running the below command:

  • genesis.json
  • app.toml
  • client.toml
  • config.toml
cp node1/node1home/config/{genesis.json,config.toml,app.toml,client.toml} node2/node2home/config/

Change every network port found in the 3 .toml files above to differ from the original ports occupied by node1

  • Open node2/node2home/config/app.toml file and edit ports as follow:
# Address defines the API server to listen on.
address = "tcp://localhost:1318"
# Address defines the gRPC server address to bind to.
address = "localhost:9092"
  • Open node2/node2home/config/config.toml file and edit ports as follow:
# TCP or UNIX socket address of the ABCI application,
# or the name of an ABCI application compiled in with the CometBFT binary
proxy_app = "tcp://127.0.0.1:26659"
# TCP or UNIX socket address for the RPC server to listen on
laddr = "tcp://127.0.0.1:26654"
# pprof listen address (https://golang.org/pkg/net/http/pprof)
pprof_laddr = "localhost:6062"
#######################################################
###           P2P Configuration Options             ###
#######################################################
[p2p]

# Address to listen for incoming connections
laddr = "tcp://0.0.0.0:26653"
  • Open node2/node2home/config/client.toml file and edit ports as follow:
# <host>:<port> to CometBFT RPC interface for this chain
node = "tcp://localhost:26654"

8.2 Add node1 to node2's list of peers

Open node2/node2home/config/config.toml file and edit the 'seeds' and 'persistent_peers' options as follow:

# Comma separated list of seed nodes to connect to
seeds="<node1-ID>@127.0.0.1:26656"

# Comma separated list of nodes to keep persistent connections to
persistent_peers="<node1-ID>@127.0.0.1:26656"

To get your node1-ID, run command:

testchaind comet show-node-id --home node1/node1home/

8.3 Start running

Before running node2, you should start node1 (if it hasn't been started yet) or restart (if it's been closed):

testchaind start --home node1/node1home/

Start node2 so that it run alongside of node1:

testchaind start --home node2/node2home/

NOTE: in local development, if using 127.0.0.1 and the node throws an error, you can try `localhost instead.

At this point, the screen output of the 2 nodes should be in sync

9. Upgrade node2's role to Validator (optional)

This section is aimed at upgrading the role of node2 from normal participant node to a validator node for our blockchain

9.1 Create a validator_info.json file

[WHY?] Because the command we're gonna use to make node2 a validator is testchaind tx staking create-validator. That command requires a .json file which entails information about the new validator to be passed in as command argument. Therefore, we go ahead to create a .json file:

cd ~/testnet/node2
touch validator_info.json

Put this json bundle into validator_info.json file, the file's content would look like this:

{
	"pubkey": {"@type":"/cosmos.crypto.ed25519.PubKey","key":"NODE2-VALIDATOR-PUBKEY"},
	"amount": "10000000000stake",
	"moniker": "node2",
	"identity": "",
	"website": "",
	"security": "",
	"details": "",
	"commission-rate": "0.1",
	"commission-max-rate": "0.2",
	"commission-max-change-rate": "0.01",
	"min-self-delegation": "1"
}

NOTE: you have to replace NODE2-VALIDATOR-PUBKEY with the real pubkey of node2's validator account. A convenient way to retrieve such pubkey is to run gentx command on node2-account and then copy the validator's pubkey from the result file. The steps are as follow: Run gentx command first:

testchaind genesis gentx node2-account 10000000000stake --home ./node2home/

Output>  Genesis transaction written to "node2home/config/gentx/gentx-98eae84d5327de5857952c909eb1ac1ce3d3bd6f.json"

Open the result file

nano ./node2home/config/gentx/gentx-98eae84d5327de5857952c909eb1ac1ce3d3bd6f.json

There should be a pubkey field like this in the result file:

				"pubkey": {
					"@type": "/cosmos.crypto.ed25519.PubKey",
					"key": "g3u70aVnX6B4CaeQuf+zE6qOTEyS12jl5Wnphn/kLLM="
				},

Copy the key. In this case it is: g3u70aVnX6B4CaeQuf+zE6qOTEyS12jl5Wnphn/kLLM=

Now re-open the node2/validator_info.json file and replace NODE2_VALIDATOR-PUBKEY with real value above:

{
	"pubkey": {"@type":"/cosmos.crypto.ed25519.PubKey","key":"g3u70aVnX6B4CaeQuf+zE6qOTEyS12jl5Wnphn/kLLM="},
	"amount": "10000000000stake",
	"moniker": "node2",
	"identity": "",
	"website": "",
	"security": "",
	"details": "",
	"commission-rate": "0.1",
	"commission-max-rate": "0.2",
	"commission-max-change-rate": "0.01",
	"min-self-delegation": "1"
}

9.2 Add validator

To add node2 as a validator, run command:

testchaind tx staking create-validator ./validator_info.json --from node2-account --home ./node2home --fees 100000stake --chain-id testchain

9.3 Verify that node2 has been added to the validator list of the chain

Since there are just 2 possible validators in the chain, you can print out the current set of validators and see if it contains node2's validator. Run the following command:

cd ~/testnet
testchaind q comet-validator-set --home node1/node1home/

If correct, the command should output details about 2 validators similar to this one:

block_height: "614"
pagination:
  next_key: null
  total: "2"
validators:
- address: cosmosvalcons1s6ew8n67xxyml2dyw0zqsclry08tulk70kcr06
  proposer_priority: "8750"
  pub_key:
    '@type': /cosmos.crypto.ed25519.PubKey
    key: g3u70aVnX6B4CaeQuf+zE6qOTEyS12jl5Wnphn/kLLM=
  voting_power: "10000"
- address: cosmosvalcons1nvqvtggjz9cy5hd73hslk97709drfe6cm9alnv
  proposer_priority: "-8750"
  pub_key:
    '@type': /cosmos.crypto.ed25519.PubKey
    key: /VRvqELNJhOe+u5ty1nft2INkhLo7lu/7RljxQJPoO0=
  voting_power: "10000"

NOTE: Apparently, the pubkey value of the first validator in the output is: g3u70aVnX6B4CaeQuf+zE6qOTEyS12jl5Wnphn/kLLM, we can indeed verify that it matches with node2 validator's pubkey, so node2 has been successfully registered as a validator of the chain.

About

A comprehensive guide on how to create a local Cosmos testnet with 2 validator nodes. (NOTE: this guide isn't meant to be utilized for production environment).

Topics

Resources

Stars

Watchers

Forks