如何使用Starport运行多节点区块链?以及如何使用Starport或Cosmos SDK运行多节点区块链?
Great question! Let's break this down into two clear sections—first using Starport (the streamlined tool built for Cosmos chains) which simplifies most of the setup, then covering the manual Cosmos SDK approach for when you need full control over every step.
1. Running a Multi-Node Blockchain with Starport
Starport abstracts away a lot of repetitive config work, making multi-node setups quick to spin up. Here's your step-by-step guide:
- Prerequisite: Make sure Starport is installed (check with
starport version; if not, follow the official install steps for your OS). - Initialize your chain project:
starport scaffold chain mychain cd mychain - Start your first (main) node:
Runstarport chain serve—this will auto-generate keys, create the genesis file, and start the node. Note down:- The main node's moniker (default is
mychain) - The node ID (displayed in the logs, or fetch later with
starport tendermint show-node-id) - The P2P address (usually
tcp://127.0.0.1:26656)
- The main node's moniker (default is
- Set up the second node:
Open a new terminal, navigate back to yourmychaindirectory, then:- Generate a new key for the second node:
starport keys add node2 - Initialize the second node's config (use a custom home directory to avoid overwriting the main node's files):
starport chain init node2 --home ./node2 - Copy the main node's genesis file to the second node (this ensures both nodes start with the same state):
cp ~/.mychain/config/genesis.json ./node2/config/ - Fix port conflicts (each node needs unique ports):
Edit./node2/config/config.tomland update these values:proxy_app = "tcp://127.0.0.1:26668"(changed from 26658)laddr = "tcp://127.0.0.1:26667"(changed from 26657)p2p.laddr = "tcp://0.0.0.0:26666"(changed from 26656)
- Add the main node as a seed for the second node:
In./node2/config/config.toml, find theseedsfield and set it to:
Replaceseeds = "<main-node-id>@127.0.0.1:26656"<main-node-id>with the node ID you noted earlier. - Start the second node:
starport chain start --home ./node2
- Generate a new key for the second node:
- Verify synchronization:
Send a test transaction from the main node's account to the second node's account:
Then check the balance on the second node:starport tx bank send alice <node2-address> 100stake --chain-id mychain-1
If the balance updates, your nodes are syncing correctly!starport query bank balances <node2-address> --home ./node2
2. Running a Multi-Node Blockchain with Cosmos SDK (Manual Approach)
If you want to skip Starport and work directly with the Cosmos SDK, here's how to set up a multi-node chain manually:
- Prerequisite: Have a Cosmos SDK-based chain project (you can use the official template: clone it, build with
make install, and ensure the chain binary is in your PATH). We'll usemychaindas the chain binary name here. - Set up the main node:
- Initialize the main node:
mychaind init main-node --chain-id mychain-1 - Add a genesis account:
mychaind keys add main-account mychaind add-genesis-account main-account 100000000stake - Generate and collect the genesis transaction:
mychaind gentx main-account 10000000stake --chain-id mychain-1 mychaind collect-gentxs - Start the main node:
Fetch the node ID withmychaind startmychaind tendermint show-node-idand note the P2P address (tcp://127.0.0.1:26656by default).
- Initialize the main node:
- Set up the second node:
Open a new terminal, then:- Initialize the second node with a custom home directory:
mychaind init node2 --chain-id mychain-1 --home ./node2 - Add a key for the second node:
mychaind keys add node2-account --home ./node2 - Copy the main node's genesis file:
cp ~/.mychaind/config/genesis.json ./node2/config/ - Resolve port conflicts:
Edit./node2/config/config.tomland update:proxy_app = "tcp://127.0.0.1:26668"laddr = "tcp://127.0.0.1:26667"p2p.laddr = "tcp://0.0.0.0:26666"
Then edit./node2/config/app.tomlto update:grpc.address = "0.0.0.0:9091"api.address = "tcp://0.0.0.0:1318"
- Add the main node as a seed:
In./node2/config/config.toml, set:seeds = "<main-node-id>@127.0.0.1:26656" - Start the second node:
mychaind start --home ./node2
- Initialize the second node with a custom home directory:
- Check sync status:
Runmychaind status --home ./node2and compare thelatest_block_heightwith the main node's status. If they match, your nodes are in sync. You can also send a test transaction like in the Starport section to confirm functionality.
Pro Tips
- For remote nodes (not on the same machine), replace
127.0.0.1with the main node's public IP and ensure ports 26656 (P2P) are open in firewalls. - Always keep your mnemonics secure—never share them publicly.
- If you run into sync issues, check the logs for errors (look for
connection refusedor port conflicts first).
内容的提问来源于stack exchange,提问作者Sandeep Kumar
相关产品推荐
相关产品推荐

