有很多不同的方法来运行Cosmos区块链的节点。您将探索如何使用simapp 进行此操作。
1、编译simapp
Cosmos SDK存储库包含一个名为 simapp 的文件夹。在这个文件夹中,您可以找到运行Cosmos SDK模拟版本的代码,这样您就可以在不实际与链交互的情况下测试命令。二进制文件称为simd
,您将使用它与节点交互。
首先,创建目录并将其更改为cosmos
文件夹,然后将cosmos-sdk
repo复制到该文件夹中:
$ mkdir cosmos
$ cd cosmos
$ git clone https://github.com/cosmos/cosmos-sdk
$ cd cosmos-sdk
请确保您使用的是相同版本:
$ git checkout v0.42.6
然后构建cosmos-sdk
:
$ make build
构建需要几分钟,并创建一个build
文件夹和一个名为simd
的simapp
二进制文件:
$ ls build
2、初始化 simapp
现在重置数据库。不仅在数据库已经初始化时运行此步骤,而且即使这是您第一次测试simapp
:
$ cd build
$ ./simd unsafe-reset-all
命令输出列出了设置为初始状态的所有文件及其位置。
是时候初始化应用程序了。初始化创建了创世块和初始链状态:
$ ./simd init demo
一个更易读的版本:
{
"app_message": {
"auth": {
"accounts": [ ],
"params": {
"max_memo_characters": "256",
"sig_verify_cost_ed25519": "590",
"sig_verify_cost_secp256k1": "1000",
"tx_sig_limit": "7",
"tx_size_cost_per_byte": "10"
}
},
"bank": {
"balances": [ ],
"denom_metadata": [ ],
"params": {
"default_send_enabled": true,
"send_enabled": [ ]
},
"supply": [ ]
},
"capability": {
"index": "1",
"owners": [ ]
},
"crisis": {
"constant_fee": {
"amount": "1000",
"denom": "stake"
}
},
"distribution": {
"delegator_starting_infos": [ ],
"delegator_withdraw_infos": [ ],
"fee_pool": {
"community_pool": [ ]
},
"outstanding_rewards": [ ],
"params": {
"base_proposer_reward": "0.010000000000000000",
"bonus_proposer_reward": "0.040000000000000000",
"community_tax": "0.020000000000000000",
"withdraw_addr_enabled": true
},
"previous_proposer": "",
"validator_accumulated_commissions": [ ],
"validator_current_rewards": [ ],
"validator_historical_rewards": [ ],
"validator_slash_events": [ ]
},
"evidence": {
"evidence": [ ]
},
"genutil": {
"gen_txs": [ ]
},
"gov": {
"deposit_params": {
"max_deposit_period": "172800s",
"min_deposit": [
{
"amount": "10000000",
"denom": "stake"
}
]
},
"deposits": [ ],
"proposals": [ ],
"starting_proposal_id": "1",
"tally_params": {
"quorum": "0.334000000000000000",
"threshold": "0.500000000000000000",
"veto_threshold": "0.334000000000000000"
},
"votes": [ ],
"voting_params": {
"voting_period": "172800s"
}
},
"ibc": {
"channel_genesis": {
"ack_sequences": [ ],
"acknowledgements": [ ],
"channels": [ ],
"commitments": [ ],
"next_channel_sequence": "0",
"receipts": [ ],
"recv_sequences": [ ],
"send_sequences": [ ]
},
"client_genesis": {
"clients": [ ],
"clients_consensus": [ ],
"clients_metadata": [ ],
"create_localhost": false,
"next_client_sequence": "0",
"params": {
"allowed_clients": [
"06-solomachine",
"07-tendermint"
]
}
},
"connection_genesis": {
"client_connection_paths": [ ],
"connections": [ ],
"next_connection_sequence": "0"
}
},
"mint": {
"minter": {
"annual_provisions": "0.000000000000000000",
"inflation": "0.130000000000000000"
},
"params": {
"blocks_per_year": "6311520",
"goal_bonded": "0.670000000000000000",
"inflation_max": "0.200000000000000000",
"inflation_min": "0.070000000000000000",
"inflation_rate_change": "0.130000000000000000",
"mint_denom": "stake"
}
},
"params": null,
"slashing": {
"missed_blocks": [ ],
"params": {
"downtime_jail_duration": "600s",
"min_signed_per_window": "0.500000000000000000",
"signed_blocks_window": "100",
"slash_fraction_double_sign": "0.050000000000000000",
"slash_fraction_downtime": "0.010000000000000000"
},
"signing_infos": [ ]
},
"staking": {
"delegations": [ ],
"exported": false,
"last_total_power": "0",
"last_validator_powers": [ ],
"params": {
"bond_denom": "stake",
"historical_entries": 10000,
"max_entries": 7,
"max_validators": 100,
"unbonding_time": "1814400s"
},
"redelegations": [ ],
"unbonding_delegations": [ ],
"validators": [ ]
},
"transfer": {
"denom_traces": [ ],
"params": {
"receive_enabled": true,
"send_enabled": true
},
"port_id": "transfer"
},
"upgrade": { },
"vesting": { }
},
"chain_id": "test-chain-Hrbd7m",
"gentxs_dir": "",
"moniker": "demo",
"node_id": "93d73b7f8fc1b74612146ba2342e54293c018b9a"
}
您可以在输出中找到chain_id
,在我们的构建中,它恰好被称为test-chain-Hrbd7m
。记下输出的名称,因为稍后将需要它来确定链 ID,通过标记--chain-id
将其传递给simapp
。
您可以通过以下命令检查初始配置:
$ cat ~/.simapp/config/genesis.json
3、准备你的帐户(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.
在实际使用Cosmos SDK时,它有助于清楚地理解这些概念。需要复习吗?请参阅“主要概念”一章中的“帐户”部分。
你也可以检查你的keys。它们保存在后端密匙环中,默认情况下是操作系统的密匙环:
$ ./simd keys list
正如你所预料的,你还没有任何keys:
[]
现在你可以添加一个新key:
$ ./simd keys add d9lab
它输出类似于:
[root@localhost build]# ./simd keys add d9lab
Enter keyring passphrase:
password must be at least 8 characters
Enter keyring passphrase:
Re-enter keyring passphrase:
- name: d9lab
type: local
address: cosmos12uj7a02737k7j0d53lhf2eectfktf78vl69de8
pubkey: cosmospub1addwnpepq0z9q7hy5t050r85r6u9cqgr82uhadg4dx7my7ktgfx6ue4htlfwzg767zh
mnemonic: ""
threshold: 0
pubkeys: []
**Important** write this mnemonic phrase in a safe place.
It is the only way to recover your account if you ever forget your password.
surface mom fiction tuna chase blanket work useful lucky confirm snap crystal peanut enrich sadness heavy voyage bachelor night crazy bargain original prefer else
您可以在上面输出的末尾看到助记符。这个单词序列是一个助记符,您可以使用它来恢复您的公钥和私钥。在生产环境中,助记符必须以可靠和保密的方式存储,作为密钥管理基础设施的一部分。
确认密钥已添加:
$ ./simd keys list
# 或
$ ./simd keys show d9lab
4、让自己成为一个合适的验证者
如前所述,Cosmos SDK区块链依赖于已识别的验证器来生成块。最初没有用于生成块的验证器。您处于进退两难的境地:初始化和未启动的链需要一个genesis帐户和验证器来进行引导。
让你的key,也就是账户,在创世文件中有一个初始余额:
$ ./simd add-genesis-account d9lab 100000000stake
这里附加在金额后面的是stake
后缀。根据创世文件,这个stake
代表这个链中tokens
的单位。因此,该命令将向您的帐户添加100000000
stake
。如果有疑问,你可以确认genesis.json
文件中的正确后缀。
grep -A 2 -B 2 denom ~/.simapp/config/genesis.json
你也可以在创世纪文件中确认你有一个初始余额:
$ grep -A 10 balances ~/.simapp/config/genesis.json
尽管有这个初始余额,在你运行你的区块链之前,你仍然需要逃脱第22条军规,并将你的引导交易包含在genesis文件中。
在这种情况下,为了使网络能够运行,您必须满足加权验证器的2/3阈值。
然而,你将是一个人在网络上,所以你可以下注任何数字或高于最低强制,即1000000stake
。但是,为了提醒自己诚实的节点投入大量资金是很重要的,您将100000000
股份中的70000000
股份投入到您刚刚创建的b9lab
帐户中。确保不要用完你所有的代币,这样你仍然可以支付gas ,这样你以后就不会用完代币。
别忘了用你自己的--chain-id
。
./simd gentx d9lab 70000000stake --chain-id test-chain-Hrbd7m
这证实了这一行动:
当你在自己的文件中创建了这个genesis交易后,用collect-gentxs
收集所有的genesis交易,将其包含在你的genesis文件中:
$ ./simd collect-gentxs
打印生成的genesis文件:
{
"app_message": {
"auth": {
"accounts": [
{
"@type": "/cosmos.auth.v1beta1.BaseAccount",
"account_number": "0",
"address": "cosmos12uj7a02737k7j0d53lhf2eectfktf78vl69de8",
"pub_key": null,
"sequence": "0"
}
],
"params": {
"max_memo_characters": "256",
"sig_verify_cost_ed25519": "590",
"sig_verify_cost_secp256k1": "1000",
"tx_sig_limit": "7",
"tx_size_cost_per_byte": "10"
}
},
"bank": {
"balances": [
{
"address": "cosmos12uj7a02737k7j0d53lhf2eectfktf78vl69de8",
"coins": [
{
"amount": "100000000",
"denom": "stake"
}
]
}
],
"denom_metadata": [ ],
"params": {
"default_send_enabled": true,
"send_enabled": [ ]
},
"supply": [
{
"amount": "100000000",
"denom": "stake"
}
]
},
"capability": {
"index": "1",
"owners": [ ]
},
"crisis": {
"constant_fee": {
"amount": "1000",
"denom": "stake"
}
},
"distribution": {
"delegator_starting_infos": [ ],
"delegator_withdraw_infos": [ ],
"fee_pool": {
"community_pool": [ ]
},
"outstanding_rewards": [ ],
"params": {
"base_proposer_reward": "0.010000000000000000",
"bonus_proposer_reward": "0.040000000000000000",
"community_tax": "0.020000000000000000",
"withdraw_addr_enabled": true
},
"previous_proposer": "",
"validator_accumulated_commissions": [ ],
"validator_current_rewards": [ ],
"validator_historical_rewards": [ ],
"validator_slash_events": [ ]
},
"evidence": {
"evidence": [ ]
},
"genutil": {
"gen_txs": [
{
"auth_info": {
"fee": {
"amount": [ ],
"gas_limit": "200000",
"granter": "",
"payer": ""
},
"signer_infos": [
{
"mode_info": {
"single": {
"mode": "SIGN_MODE_DIRECT"
}
},
"public_key": {
"@type": "/cosmos.crypto.secp256k1.PubKey",
"key": "A8RQeuSi30eM9B64XAEDOrl+tRVpvbJ6y0JNrma3X9Lh"
},
"sequence": "0"
}
]
},
"body": {
"extension_options": [ ],
"memo": "93d73b7f8fc1b74612146ba2342e54293c018b9a@192.168.159.128:26656",
"messages": [
{
"@type": "/cosmos.staking.v1beta1.MsgCreateValidator",
"commission": {
"max_change_rate": "0.010000000000000000",
"max_rate": "0.200000000000000000",
"rate": "0.100000000000000000"
},
"delegator_address": "cosmos12uj7a02737k7j0d53lhf2eectfktf78vl69de8",
"description": {
"details": "",
"identity": "",
"moniker": "demo",
"security_contact": "",
"website": ""
},
"min_self_delegation": "1",
"pubkey": {
"@type": "/cosmos.crypto.ed25519.PubKey",
"key": "DWOymvjGcTdP+emo7k1kdqsWzxgwimMOXpqh7Lw2pe0="
},
"validator_address": "cosmosvaloper12uj7a02737k7j0d53lhf2eectfktf78v6w3c45",
"value": {
"amount": "70000000",
"denom": "stake"
}
}
],
"non_critical_extension_options": [ ],
"timeout_height": "0"
},
"signatures": [
"s5x2n/lLuGAOUq0YdJRkxVY5WtcbVt5wZJ4uRUuQ02ULje9czOPikEWFFOm9Bb69GHZBpw0VP9AwbuSzzIGxhA=="
]
}
]
},
"gov": {
"deposit_params": {
"max_deposit_period": "172800s",
"min_deposit": [
{
"amount": "10000000",
"denom": "stake"
}
]
},
"deposits": [ ],
"proposals": [ ],
"starting_proposal_id": "1",
"tally_params": {
"quorum": "0.334000000000000000",
"threshold": "0.500000000000000000",
"veto_threshold": "0.334000000000000000"
},
"votes": [ ],
"voting_params": {
"voting_period": "172800s"
}
},
"ibc": {
"channel_genesis": {
"ack_sequences": [ ],
"acknowledgements": [ ],
"channels": [ ],
"commitments": [ ],
"next_channel_sequence": "0",
"receipts": [ ],
"recv_sequences": [ ],
"send_sequences": [ ]
},
"client_genesis": {
"clients": [ ],
"clients_consensus": [ ],
"clients_metadata": [ ],
"create_localhost": false,
"next_client_sequence": "0",
"params": {
"allowed_clients": [
"06-solomachine",
"07-tendermint"
]
}
},
"connection_genesis": {
"client_connection_paths": [ ],
"connections": [ ],
"next_connection_sequence": "0"
}
},
"mint": {
"minter": {
"annual_provisions": "0.000000000000000000",
"inflation": "0.130000000000000000"
},
"params": {
"blocks_per_year": "6311520",
"goal_bonded": "0.670000000000000000",
"inflation_max": "0.200000000000000000",
"inflation_min": "0.070000000000000000",
"inflation_rate_change": "0.130000000000000000",
"mint_denom": "stake"
}
},
"params": null,
"slashing": {
"missed_blocks": [ ],
"params": {
"downtime_jail_duration": "600s",
"min_signed_per_window": "0.500000000000000000",
"signed_blocks_window": "100",
"slash_fraction_double_sign": "0.050000000000000000",
"slash_fraction_downtime": "0.010000000000000000"
},
"signing_infos": [ ]
},
"staking": {
"delegations": [ ],
"exported": false,
"last_total_power": "0",
"last_validator_powers": [ ],
"params": {
"bond_denom": "stake",
"historical_entries": 10000,
"max_entries": 7,
"max_validators": 100,
"unbonding_time": "1814400s"
},
"redelegations": [ ],
"unbonding_delegations": [ ],
"validators": [ ]
},
"transfer": {
"denom_traces": [ ],
"params": {
"receive_enabled": true,
"send_enabled": true
},
"port_id": "transfer"
},
"upgrade": { },
"vesting": { }
},
"chain_id": "test-chain-Hrbd7m",
"gentxs_dir": "/root/.simapp/config/gentx",
"moniker": "demo",
"node_id": "93d73b7f8fc1b74612146ba2342e54293c018b9a"
}
如果您感到好奇,您可以在您的genesis中找到更新的gen_txs
字段。
"genutil": {
"gen_txs": [
{
"auth_info": {
"fee": {
"amount": [ ],
"gas_limit": "200000",
"granter": "",
"payer": ""
},
"signer_infos": [
{
"mode_info": {
"single": {
"mode": "SIGN_MODE_DIRECT"
}
},
"public_key": {
"@type": "/cosmos.crypto.secp256k1.PubKey",
"key": "A8RQeuSi30eM9B64XAEDOrl+tRVpvbJ6y0JNrma3X9Lh"
},
"sequence": "0"
}
]
},
"body": {
"extension_options": [ ],
"memo": "93d73b7f8fc1b74612146ba2342e54293c018b9a@192.168.159.128:26656",
"messages": [
{
"@type": "/cosmos.staking.v1beta1.MsgCreateValidator",
"commission": {
"max_change_rate": "0.010000000000000000",
"max_rate": "0.200000000000000000",
"rate": "0.100000000000000000"
},
"delegator_address": "cosmos12uj7a02737k7j0d53lhf2eectfktf78vl69de8",
"description": {
"details": "",
"identity": "",
"moniker": "demo",
"security_contact": "",
"website": ""
},
"min_self_delegation": "1",
"pubkey": {
"@type": "/cosmos.crypto.ed25519.PubKey",
"key": "DWOymvjGcTdP+emo7k1kdqsWzxgwimMOXpqh7Lw2pe0="
},
"validator_address": "cosmosvaloper12uj7a02737k7j0d53lhf2eectfktf78v6w3c45",
"value": {
"amount": "70000000",
"denom": "stake"
}
}
],
"non_critical_extension_options": [ ],
"timeout_height": "0"
},
"signatures": [
"s5x2n/lLuGAOUq0YdJRkxVY5WtcbVt5wZJ4uRUuQ02ULje9czOPikEWFFOm9Bb69GHZBpw0VP9AwbuSzzIGxhA=="
]
}
]
},
5、Create blocks
现在你可以启动你的单节点区块链:
$ ./simd start
在你运行命令的终端窗口中,你可以看到块正在生成和验证:
[root@localhost build]# ./simd start
2:30PM INF starting ABCI with Tendermint
2:30PM INF Starting multiAppConn service impl=multiAppConn module=proxy
2:30PM INF Starting localClient service connection=query impl=localClient module=abci-client
2:30PM INF Starting localClient service connection=snapshot impl=localClient module=abci-client
2:30PM INF Starting localClient service connection=mempool impl=localClient module=abci-client
2:30PM INF Starting localClient service connection=consensus impl=localClient module=abci-client
2:30PM INF Starting EventBus service impl=EventBus module=events
2:30PM INF Starting PubSub service impl=PubSub module=pubsub
2:30PM INF Starting IndexerService service impl=IndexerService module=txindex
2:30PM INF ABCI Handshake App Info hash= height=0 module=consensus protocol-version=0 software-version=
2:30PM INF ABCI Replay Blocks appHeight=0 module=consensus stateHeight=0 storeHeight=0
2:30PM INF asserting crisis invariants inv=0/11 module=x/crisis name=staking/module-accounts
在同一文件夹中打开一个新终端,并检查余额:
$ ./simd query bank balances $(./simd keys show d9lab -a)
打印:
6、发送交易
练习发送交易。要做到这一点,你将创建另一个名为student
的帐户,并将一些tokens 转移到该帐户:
$ ./simd keys add student
打印如下:
[root@localhost build]# ./simd keys add student
Enter keyring passphrase:
- name: student
type: local
address: cosmos1h43mcjku2a008jq9pqqzcd75gry7tsuutcn374
pubkey: cosmospub1addwnpepqd7vfdq7tcyzqlg9vwwg8c2ef0pzwqajpktekgwrdxgxlhz4s6jfqmek9j0
mnemonic: ""
threshold: 0
pubkeys: []
**Important** write this mnemonic phrase in a safe place.
It is the only way to recover your account if you ever forget your password.
vague tuition armor crane picture business organ balance strike town plate target rain provide depend weapon indoor logic uncover track puppy coral effort donate
在发送任何代币之前,请确认新账户的余额是否存在:
$ ./simd query bank balances $(./simd keys show student -a)
这个账户没有余额。您的区块链中还不存在新帐户。只有密钥对已生成并存储在您的密匙环中:
balances: []
pagination:
next_key: null
total: "0"
你需要发送一个交易来改变这个新账户的余额:
$ ./simd tx bank send $(./simd keys show d9lab -a) $(./simd keys show student -a) 10stake --chain-id test-chain-Hrbd7m
在签署和广播之前,您将被提示确认交易:
命令回显信息包括gas_used
等有用信息。
先检查一下student
账户的余额:
./simd query bank balances $(./simd keys show student -a)
现在再检查一下student
账户的余额:
balances:
- amount: "10"
denom: stake
pagination:
next_key: null
total: "0"
7、CLI路由
现在是时候编写一些Go代码了。simd
如何通过命令行界面进行交互?检查cosmos-sdk/simapp/simd/main.go文件:
package main
import (
"os"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/simd/cmd"
)
func main() {
rootCmd, _ := cmd.NewRootCmd()
if err := svrcmd.Execute(rootCmd, simapp.DefaultNodeHome); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)
default:
os.Exit(1)
}
}
}
cmd.NewRootCmd()函数是CLI处理程序。它通过“github.com/cosmos/cosmos-sdk/simapp/simd/cmd”行导入。它可以在cosmos-sdk/simapp/simd/cmd/root.go)文件下找到。
func NewRootCmd() (*cobra.Command, params.EncodingConfig)
其中,定义了基本属性,如应用程序名称:
rootCmd := &cobra.Command{
Use: "simd",
Short: "simulation app",
此外,观察Cobra
被导入并用于CLI重定向:
rootCmd.AddCommand(
genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome),
genutilcli.ValidateGenesisCmd(simapp.ModuleBasics),
AddGenesisAccountCmd(simapp.DefaultNodeHome),
tmcli.NewCompletionCmd(rootCmd, true),
NewTestnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
config.Cmd(),
)
另外,查看simapp/app.go,将导入每个模块和密钥管理员。首先你会看到一个相当大的模块列表(打开新窗口),大多数Cosmos-sdk应用程序都使用这些模块:
...
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/capability"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
...
在/cosmos-sdk/x/
文件夹中的模块是由在Cosmos栈上工作的几个组织维护的。要理解一个模块,最好的方法是查看相应的spec
文件夹。例如,查看cosmos-sdk/x/bank/spec/01_state.md以了解本节中使用的bank
模块的状态。
您是否需要对模块及其在Cosmos SDK中的作用进行概念复习?参见前一章的模块部分。