Cosmos-SDK新手指南 - build, run and debug

Cosmos-SDK新手指南

准备Cosmos-SDK源代码

Cosmos SDK源代码采用了Go1.11引入的模块系统,所以既可以使用go get方式下载源代码,也可以直接git clone

Since v0.33.x, the SDK has upgraded to use Golang’s new module system.

使用go get方式(不推荐)

  1. 准备好GOPATH
  2. 执行go get -v github.com/cosmos/cosmos-sdk命令,执行完毕后,Cosmos SDK源代码会被clone到$GOPATH/src/github.com/cosmos目录下面。不过由于项目根目录下没有Go文件,所以get命令没办法下载依赖。
  3. cd到$GOPATH/src/github.com/cosmos目录下,然后执行go get -v ./...命令,这样就会下载全部依赖(需要很长时间)。
  4. 还是在$GOPATH/src/github.com/cosmos目录下,执行go build ./...命令构建项目。

使用git clone方式(推荐)

  1. 任意目录执行git clone https://github.com/cosmos/cosmos-sdk.git命令,clone最新的Cosmos SDK源代码。
  2. 如果Cosmos SDK源代码被clone到了GOPATH下面(比如使用上面提到的go get命令),则需要设置GO111MODULE环境变量,激活Go1.11模块系统: export GO111MODULE=on
  3. 如果想要构建项目,cd到clone下来的cosmos-sdk目录,执行go build ./...命令即可(会自动下载依赖)。
  4. ~~执行go mod vendor命令可以在项目根目录下生成vendor目录,~~这样用GoLand直接打开工程就可以(需要启用vgo:GoLand > Preferences > Go > Go Modules (vgo) -> Enable Go Modules (vgo) integration)。

连接cosmos测试网

参考:https://github.com/cosmos/testnets

目前最新测试网的genesis.json对应分支是v0.33.0
对应的测试网浏览器:https://gaia-13k3.bigdipper.live/

cd /Users/user/lab/cosmos-sdk
git checkout -b v0.33.0
make tools 
make install

gaiad unsafe-reset-all

curl https://raw.githubusercontent.com/cosmos/testnets/master/gaia-13k/genesis.json > ~/.gaiad/config/genesis.json

shasum -a 256 ~/.gaiad/config/genesis.json  #should equals 48519942c69dbda18fd9dfba949bca5591ad67772bff669fc8649d796b9cf3c3

修改config.toml中的seeds = “”为

seeds = "[email protected]:26656"

moniker为节点名称,可以随便取一个

moniker = "not_a_good_example_name"

启动测试节点

 gaiad start &> gaiad.log &

观察日志:

tail -f gaiad.log

测试网节点不稳定,可能要等比较久,才可以看到连接到节点并开始下块:

I[2019-04-21|22:38:46.069] Starting ABCI with Tendermint                module=main
E[2019-04-21|22:38:47.912] Connection failed @ recvRoutine (reading byte) module=p2p [email protected]:26656 conn=MConn{35.203.176.214:26656} err=EOF
E[2019-04-21|22:38:47.912] Stopping peer for error                      module=p2p peer="Peer{MConn{35.203.176.214:26656} c24f496b951148697f8a24fd749786075c128f00 out}" err=EOF
E[2019-04-21|22:39:17.556] Dialing failed                               module=pex [email protected]:57622 err="dial tcp 35.245.28.33:57622: connect: connection refused" attempts=0
E[20

I[2019-04-21|23:02:16.490] Executed block                               module=state height=1 validTxs=0 invalidTxs=0
I[2019-04-21|23:02:16.955] Committed state                              module=state height=1 txs=0 appHash=7D3741F98EA0105F04272B56DE71444B83F5CB2E3F132FB485A596F7E1F8450E
I[2019-04-21|23:02:17.084] Executed block                               module=state height=2 validTxs=0 invalidTxs=0
I[2019-04-21|23:02:17.096] Committed state                              module=state height=2 txs=0 appHash=CB879370DF1193F74E2A384CE1956F772C170619C8AF0880DF3EEB83D0744188
I[2019-04-21|23:02:17.236] Executed block                               module=state height=3 validTxs=0 invalidTxs=0
I[2019-04-21|23:02:17.249] Committed state                              module=state height=3 txs=0 appHash=6640C21021121A072FC4220EA78A759FAE5457A7983BF9CD8B912DF803AB3C69
I[2019-04-21|23:02:17.397] Executed block                               module=state height=4 validTxs=0 invalidTxs=0
I[2019-04-21|23:02:17.407] Committed state                              module=state height=4 txs=0 appHash=203A4F84D4B5E5E3A979ED518624C97B3F5B90AD6D3C965ED7D3DC09AE69E7C2

运行gaiacli status查看本地节点运行情况:

{
    "node_info": {
        "channels": "4020212223303800",
        "id": "221c98807b8089f184dfeb1da58c8c7bb6ecfca2",
        "listen_addr": "tcp://0.0.0.0:26656",
        "moniker": "BJ00609",
        "network": "gaia-13003",
        "other": {
            "rpc_address": "tcp://0.0.0.0:26657",
            "tx_index": "on"
        },
        "protocol_version": {
            "app": "0",
            "block": "10",
            "p2p": "7"
        },
        "version": "0.31.5"
    },
    "sync_info": {
        "catching_up": true,
        "latest_app_hash": "5EE2FBDECE3673EBD5EB8C671108F7D574E732C8B176530E513C02BFC01E9175",
        "latest_block_hash": "D6DBE86E08CC1B79BDAB30DA213C6907DE66CABD14E6480943024B70200DF99B",
        "latest_block_height": "10869",
        "latest_block_time": "2019-04-09T23:14:20.669409459Z"
    },
    "validator_info": {
        "address": "A31C1B2CE5A98878D3B19E9E00487823C3E24A7C",
        "pub_key": {
            "type": "tendermint/PubKeyEd25519",
            "value": "doyDnbRidnzzViRyl9B6isDbuHE4Vb5+k8Ov884watQ="
        },
        "voting_power": "0"
    }
}

BJ00609 ~ $ gaiacli query account cosmos1wtv0kp6ydt03edd8kyr5arr4f3yc52vp5g7na0 --chain-id=gaia-13003

Vesting Account:
  Address:          cosmos1wtv0kp6ydt03edd8kyr5arr4f3yc52vp5g7na0
  Pubkey:           cosmospub1addwnpepqd3xtcfgysaydlrs8hpaqeprdqua6wpx6dldklwwykgd9pq9c90vuea50zc
  Coins:            246681067muon
  AccountNumber:    391
  Sequence:         275
  OriginalVesting:  5000000000muon
  DelegatedFree:    10945770001muon
  DelegatedVesting:
  EndTime:          1518652800
BJ00609 ~/.gaiad/config $ gaiacli keys list
NAME:	TYPE:	ADDRESS:					PUBKEY:

BJ00609 ~/.gaiad/config $ gaiacli keys add testnet_key
Enter a passphrase to encrypt your key to disk:
Repeat the passphrase:

NAME:	TYPE:	ADDRESS:					PUBKEY:
testnet_key	local	cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw	cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs

BJ00609 ~/.gaiad/config $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id='gaia-13003'
ERROR: {"codespace":"sdk","code":9,"message":"account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw does not exist"}

测试网faucet 目前不能正常使用,需要在communiy中问别人要测试网的tokens.

The faucet on https://faucet.cosmos.network/  does NOT work now.<br />
Can someone please send me some  gaia-13003 testnet  tokens?

my account address is:  cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
Great Thanks.

别人打测试币后可以在 gaia-13003测试网浏览器 这里看到

Mira - Ztake.org Send 10,000 muon to cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw.

要等本地的gaiad同步到相应的块(包含相应打测试币的交易)后,才能查询到帐户信息:

BJ00609 ~ $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:
  Coins:         10000muon
  AccountNumber: 1164
  Sequence:      0

Your cosmosvalconspub can be used to create a new validator by staking tokens. You can find your validator pubkey by running:

BJ00609 ~ $ gaiad tendermint show-validator
cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln

执行create-validatortx后, 会打印出相应交易的hash:

BJ00609 ~ $ gaiacli tx staking create-validator --amount=10000muon --pubkey=cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln  --moniker=hongmin --chain-id=gaia-13003 --sequence=0  --commission-rate=0.05 --commission-max-rate=0.2 --commission-max-change-rate=0.1 --min-self-delegation=1 --from=cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
{"chain_id":"gaia-13003","account_number":"1164","sequence":"0","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"moniker":"hongmin","identity":"","website":"","details":""},"commission":{"rate":"0.050000000000000000","max_rate":"0.200000000000000000","max_change_rate":"0.100000000000000000"},"min_self_delegation":"1","delegator_address":"cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw",
"validator_address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a",
"pubkey":"cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln",
"value":{"denom":"muon","amount":"10000"}}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: Y
Password to sign with 'testnet_key':
Response:
  TxHash: 376211AA34A3F8D5E8DABC52AFFAEBF968CE509E381C4E3F3F36EDBE30FD9456

https://gaia-13k3.bigdipper.live/transactions/376211AA34A3F8D5E8DABC52AFFAEBF968CE509E381C4E3F3F36EDBE30FD9456

执行gaiacli query staking validators --chain-id=gaia-13003 可以以查看网络中所有validator的状态.

执行gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003 可查看刚创建的validator的状态:

BJ00609 ~ $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Unbonded
  Tokens:                     10000
  Delegator Shares:           10000.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           0
  Unbonding Completion Time:  1970-01-01 00:00:00 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-23 06:07:38.010687 +0000 UTC

因为这里10000muon, 并未达到Minimum Self Delegation的值(1 * 1000000), 所以一直处于Unbonded状态.
详细可参考

其它操作

查询某个帐户对外的delegate情况:

BJ00609 ~/.gaiad/config $ gaiacli query staking delegations cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
Delegation:
  Delegator: cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Validator: cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Shares:    10000.000000000000000000

取消delegation:

BJ00609 ~/.gaiad/config $ gaiacli tx staking unbond cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a 10000muon --from=cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
{"chain_id":"gaia-13003","account_number":"1164","sequence":"1","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgUndelegate","value":{"delegator_address":"cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw","validator_address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a","amount":{"denom":"muon","amount":"10000"}}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: Y
Password to sign with 'testnet_key':
Response:
  TxHash: EC28FD7F48DB3BFDD8B8B4CF3933CEA0AA30582748F818F16D4668E872318BB1
https://gaia-13k3.bigdipper.live/transactions/EC28FD7F48DB3BFDD8B8B4CF3933CEA0AA30582748F818F16D4668E872318BB1

取消delegation后已经查询不到delegate信息:

@BJ00609 ~/.gaiad/config $ gaiacli query staking delegations cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003

@BJ00609 ~/.gaiad/config $ 

重新查询帐户状态, 之前delegate出去的token已经回到帐户:

@BJ00609 ~/.gaiad/config $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:        cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs
  Coins:         10000muon
  AccountNumber: 1164
  Sequence:      2

查询相应validator staking状态时,发现相应validator已经不存在:

@BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
ERROR: No validator found with address cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a

重新create-valiidator:

BJ00609 ~/.gaiad/config $ gaiacli tx staking create-validator --amount=2muon --pubkey=cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln  --moniker=hongmin --chain-id=gaia-13003 --sequence=0  --commission-rate=0.05 --commission-max-rate=0.2 --commission-max-change-rate=0.1 --min-self-delegation=1 --from=cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
{"chain_id":"gaia-13003","account_number":"1164","sequence":"2","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgCreateValidator","value":{"description":{"moniker":"hongmin","identity":"","website":"","details":""},"commission":{"rate":"0.050000000000000000","max_rate":"0.200000000000000000","max_change_rate":"0.100000000000000000"},"min_self_delegation":"1","delegator_address":"cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw","validator_address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a","pubkey":"cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln","value":{"denom":"muon","amount":"2"}}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: Y
Password to sign with 'testnet_key':
Response:
  TxHash: 400758CEC6D9E1F37F4FCADFB85F3AB45D10C054CABA27AC01506932E27BCDCA


@BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Unbonded
  Tokens:                     2
  Delegator Shares:           2.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           0
  Unbonding Completion Time:  1970-01-01 00:00:00 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC


@BJ00609 ~/.gaiad/config $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:        cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs
  Coins:         9998muon
  AccountNumber: 1164
  Sequence:      3


@BJ00609 ~/.gaiad/config $ gaiacli query staking delegations cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
Delegation:
  Delegator: cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Validator: cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Shares:    2.000000000000000000


BJ00609 ~/.gaiad/config $ gaiacli tx staking delegate cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a 9000muon --from=cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
{"chain_id":"gaia-13003","account_number":"1164","sequence":"3","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgDelegate","value":{"delegator_address":"cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw","validator_address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a","amount":{"denom":"muon","amount":"9000"}}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: y
Password to sign with 'testnet_key':
Response:
  TxHash: 1662F2DFF1614689CC28518B549027ED913749701A9169EB097843BA1B6DA1F6


BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Unbonded
  Tokens:                     9002
  Delegator Shares:           9002.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           0
  Unbonding Completion Time:  1970-01-01 00:00:00 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC

查询模块的参数值:

@BJ00609 ~/.gaiad/config $ gaiacli query gov params
Must specify these options: --chain-id  when --trust-node is false
@BJ00609 ~/.gaiad/config $ gaiacli query gov params --chain-id=gaia-13003
Voting Params:
  Voting Period:      48h0m0s
Tally Params:
  Quorum:             0.334000000000000000
  Threshold:          0.500000000000000000
  Veto:               0.334000000000000000
Deposit Params:
  Min Deposit:        100000muon
  Max Deposit Period: 48h0m0s


BJ00609 ~/.gaiad/config $ gaiacli query staking params --chain-id=gaia-13003
Params:
  Unbonding Time:    72h0m0s
  Max Validators:    100
  Max Entries:       7
  Bonded Coin Denom: muon

之前因为这里10000muon, 并未达到Minimum Self Delegation的值(1 * 1000000), 所以一直处于Unbonded状态.
详细可参考

后来在riot拿到别人给的一些测试token后, 可以成功变成Bonded状态的Validator了.

My test validator is inactive with not enough self delegation, https://gaia-13k3.bigdipper.live/transactions/376211AA34A3F8D5E8DABC52AFFAEBF968CE509E381C4E3F3F36EDBE30FD9456

Can someone please send me some testnet token  for gaia-13003,   enough for me to become validator in the testnet?
which at least 1000000 moun,  to satisfy the lowest self-minimal-delegation.

My address on gaia-13003 is cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
BJ00609 ~/.gaiad/config $  gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:        cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs
  Coins:         1000998muon
  AccountNumber: 1164
  Sequence:      4


BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Unbonded
  Tokens:                     9002
  Delegator Shares:           9002.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           0
  Unbonding Completion Time:  1970-01-01 00:00:00 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC


BJ00609 ~/.gaiad/config $ gaiacli query staking delegations cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
Delegation:
  Delegator: cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Validator: cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Shares:    9002.000000000000000000


BJ00609 ~/.gaiad/config $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:        cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs
  Coins:         1000998muon
  AccountNumber: 1164
  Sequence:      4



BJ00609 ~/.gaiad/config $ gaiacli tx staking delegate cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a 1000000muon --from=cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
{"chain_id":"gaia-13003","account_number":"1164","sequence":"4","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgDelegate","value":{"delegator_address":"cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw","validator_address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a","amount":{"denom":"muon","amount":"1000000"}}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: y
Password to sign with 'testnet_key':
Response:
  TxHash: E1C82BFD86A0E711A80D6FBE97F5FBACC63EA7AAE8D1EAF73F2E1E8A5609079A


BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Bonded
  Tokens:                     1009002
  Delegator Shares:           1009002.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           0
  Unbonding Completion Time:  1970-01-01 00:00:00 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC


BJ00609 ~/.gaiad/config $ gaiacli query staking delegations cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
Delegation:
  Delegator: cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Validator: cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Shares:    1009002.000000000000000000

  
BJ00609 ~/.gaiad/config $ gaiacli query account cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw  --chain-id=gaia-13003
Account:
  Address:       cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw
  Pubkey:        cosmospub1addwnpepqvcqt4qckcq8tqhevjrnv496dhzqu7pg2vdlmef52utynu6586kmj9fmvcs
  Coins:         998muon
  AccountNumber: 1164
  Sequence:      5

validator下线一段时间后就会被Jail掉 :

BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     true
  Status:                     Unbonding
  Tokens:                     1009002
  Delegator Shares:           1009002.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           341158
  Unbonding Completion Time:  2019-05-03 17:33:32.84166642 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC

可以通过unjail transaction将validator重新unjail:

BJ00609 ~/.gaiad/config $ gaiacli tx slashing unjail --from cosmos1hr5s6zhrtamefv6pya6qkug7hhgt7zcwew85tw --chain-id=gaia-13003
{"chain_id":"gaia-13003","account_number":"1164","sequence":"5","fee":{"amount":null,"gas":"200000"},"msgs":[{"type":"cosmos-sdk/MsgUnjail","value":{"address":"cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a"}}],"memo":""}

confirm transaction before signing and broadcasting [Y/n]: y
Password to sign with 'testnet_key':
Response:
  TxHash: 6B15569909D3302E6BB80AFC5CABA8E394211835B8309470E401DA493B67E01A

稍等即可看到validator已经恢复bonded状态:

BJ00609 ~/.gaiad/config $ gaiacli query staking validator cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a --chain-id=gaia-13003
Validator
  Operator Address:           cosmosvaloper1hr5s6zhrtamefv6pya6qkug7hhgt7zcwu6np8a
  Validator Consensus Pubkey: cosmosvalconspub1zcjduepqw6xg88d5vfm8eu6ky3ef05r63tqdhwr38p2mul5ncwhl8n3sdt2qchypln
  Jailed:                     false
  Status:                     Bonded
  Tokens:                     1009002
  Delegator Shares:           1009002.000000000000000000
  Description:                {hongmin   }
  Unbonding Height:           341158
  Unbonding Completion Time:  2019-05-03 17:33:32.84166642 +0000 UTC
  Minimum Self Delegation:    1
  Commission:                 rate: 0.050000000000000000, maxRate: 0.200000000000000000, maxChangeRate: 0.100000000000000000, updateTime: 2019-04-25 04:53:39.396881119 +0000 UTC

单节点,本地的,手动的测试网

# You can run all of these commands from your home directory
cd $HOME

# Initialize the genesis.json file that will help you to bootstrap the network
gaiad init --chain-id=testing testing

# Create a key to hold your validator account
gaiacli keys add validator

# Add that key into the genesis.app_state.accounts array in the genesis file
# NOTE: this command lets you set the number of coins. Make sure this account has some coins
# with the genesis.app_state.staking.params.bond_denom denom, the default is staking
gaiad add-genesis-account $(gaiacli keys show validator -a) 200000000stake,900000000validatortoken

# Generate the transaction that creates your validator
gaiad gentx --name validator

# Add the generated bonding transaction to the genesis file
gaiad collect-gentxs

# Now its safe to start `gaiad`
gaiad start

运行起来后, 查看日志可看到,单个节点已经在不断出块

I[2019-04-23|01:21:51.978] Starting ABCI with Tendermint                module=main
E[2019-04-23|01:21:52.129] Couldn't connect to any seeds                module=p2p
I[2019-04-23|01:21:57.189] Executed block                               module=state height=1 validTxs=0 invalidTxs=0
I[2019-04-23|01:21:57.198] Committed state                              module=state height=1 txs=0 appHash=43C84FFE5DAECCE6BD966AF5B9A0CDE5C1D5E33679C36325A6AF58D7A0499AA1
I[2019-04-23|01:22:02.268] Executed block                               module=state height=2 validTxs=0 invalidTxs=0
I[2019-04-23|01:22:02.275] Committed state                              module=state height=2 txs=0 appHash=3C92ABD10C65FB9114046F1B5777EE7400FD396751C3B93A3F5E3CA32B35437D
I[2019-04-23|01:22:07.440] Executed block                               module=state height=3 validTxs=0 invalidTxs=0

多节点,本地的,自动的测试网

请参照 https://www.jianshu.com/p/0c2527e52fcd 这个文档进行操作.
会通过

make build-linux
# Build tendermint/gaiadnode image
make build-docker-gaiadnode
make localnet-start

会使用docker compose, 启动4个docker validator节点.

阅读源代码之前

调试

  • go进程需要调试时, 编译时需要加入选项, 让其编译后带上debug符号
  • 修改cosmos-sdk中编译gaiad的Makefile, 加上编译选项-gcflags=all="-N -l"
BJ00609 ~/lab/cosmos-sdk (v0.33.0) $ grep gcflags ~/lab/cosmos-sdk/Makefile
BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' -gcflags=all="-N -l"
  • 重新编译: make install
  • 可以看到编译时已经带上了相关gcflags参数
BJ00609 ~/lab/cosmos-sdk (v0.33.0) $ make install
--> Ensure dependencies have not been modified
all modules verified
go install -mod=readonly -tags "netgo ledger" -ldflags '-X github.com/cosmos/cosmos-sdk/version.Version=0.34.0-43-g93e8f467 -X github.com/cosmos/cosmos-sdk/version.Commit=93e8f467fedcc99028974431dedbd111e8037c57 -X "github.com/cosmos/cosmos-sdk/version.BuildTags=netgo ledger" -X github.com/cosmos/cosmos-sdk/version.GoSumHash=e0ca484ac917a19baa19f4147c5622c734f2be3f5dcc960f95b84dbff3d55494' -gcflags=all="-N -l" ./cmd/gaia/cmd/gaiad
  • 可以通过命令行调试, dlv调试工具手册
  • 安装dlv: go get -u github.com/go-delve/delve/cmd/dlv
  • 按上文 #单节点,本地的,手动的测试网 单节点模式启动gaiad后.
  • 通过gops命令找到gaiad进程号. 如果没有gops, 需要安装: go get -u github.com/google/gops
BJ00609 ~ $ gops
12928 12173 gaiad                          go1.12.4 /Users/j/go/bin/gaiad
  • attach gaiad进程: dlv attach 12928
  • 设置断点: b GaiaApp.initFromGenesisState
  • 通过b命令可设置断点, c命令继续执行直到断点处
(dlv) c
> github.com/cosmos/cosmos-sdk/cmd/gaia/app.(*GaiaApp).initFromGenesisState() ./lab/cosmos-sdk/cmd/gaia/app/app.go:258 (hits goroutine(1):1 total:1) (PC: 0x4c31ebb)
Warning: listing may not match stale executable
   253:			Tags:             tags,
   254:		}
   255:	}
   256:
   257:	// initialize store from a genesis state
=> 258:	func (app *GaiaApp) initFromGenesisState(ctx sdk.Context, genesisState GenesisState) []abci.ValidatorUpdate {
   259:		genesisState.Sanitize()
   260:
   261:		// load the accounts
   262:		for _, gacc := range genesisState.Accounts {
   263:			acc := gacc.ToAccount()
(dlv) bt
 0  0x0000000004c31ebb in github.com/cosmos/cosmos-sdk/cmd/gaia/app.(*GaiaApp).initFromGenesisState
    at ./lab/cosmos-sdk/cmd/gaia/app/app.go:258
 1  0x0000000004c332a7 in github.com/cosmos/cosmos-sdk/cmd/gaia/app.(*GaiaApp).initChainer
    at ./lab/cosmos-sdk/cmd/gaia/app/app.go:321
 2  0x0000000004c40c07 in github.com/cosmos/cosmos-sdk/cmd/gaia/app.(*GaiaApp).initChainer-fm
    at ./lab/cosmos-sdk/cmd/gaia/app/app.go:310
 3  0x0000000004b3cd14 in github.com/cosmos/cosmos-sdk/baseapp.(*BaseApp).InitChain
    at ./lab/cosmos-sdk/baseapp/baseapp.go:357

本文由 CoinEx Chain团队 Chase & Hongmin pair写作,转载无需授权

1 Like