Cosmos开发(一)Ubuntu下编译Cosmos Hub gaia 开始探索之旅

Cosmos项目是个有着宏伟目标的区块链项目。在DPOS+BFT的共识引擎的基础上,Cosmos提出了更大的区块链未来和蓝图:区块链开发简便,互通互联。Cosmos设计了区块链的基础设施和生态,区块链开发者只需要调用Cosmos-SDK,开发Plugin,处理特有业务。Cosmos项目已经不只是提供DPOS+BFT共识机制的引擎这么简单。它在打造区块链的基础设施和开发生态。在Cosmos的基础上,设计开发区块链就是在Cosmos上写个应用程序。Cosmos的框架协助实现了一些区块链相关的基本功能:
1)区块链的数据存储以及共识机制(DPOS+BFT)
2)各个应用(区块链)之间的token的互换(IBC)
3)现有区块链的接入(Peg Zone)
在Cosmos框架的基础上开发一条区块链,开发人员唯一要做的事情,就是编写符合ABCI接口的应用程序(解释区块数据,业务处理)。

一、今天我们就开始搭建一个cosmos节点
有一些感念,我们需要了解一下。
什么是gaia
是Cosmos Hub的Cosmos SDK应用程序的名称。它有两个主要入口点:

  1. gaiad :Gaia守护程序,运行gaia应用程序的完整节点。
  2. gaiacli:Gaia命令行界面,可以与Gaia全节点进行交互。

gaia 使用以下模块构建在Cosmos SDK上:

  • x/auth:帐户和签名。
  • x/bank:令牌转移。
  • x/staking:放样逻辑。
  • x/mint:通货膨胀逻辑。
  • x/distribution:费用分配逻辑。
  • x/slashing:削减逻辑。
  • x/gov:治理逻辑。
  • x/ibc:区块链转移。
  • x/params:处理应用级参数。

关于Cosmos Hub:Cosmos Hub是第一个在Cosmos Network中推出的Hub。Hub的作用是促进区块链之间的转移。如果区块链通过IBC连接到集线器,它会自动获得对连接到它的所有其他区块链的访问权限。Cosmos Hub是一个公开的Proof-of-Stake链。它的赌注令牌称为Atom。

安装gaia之前,我们需要安装go环境。
注意:go的版本需要再1.12.9以上

wget https://storage.googleapis.com/golang/go1.12.9.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.12.9.linux-amd64.tar.gz

接下来编辑当前用户的环境变量:

vi ~/.profile

添加以下内容:

export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH

接下来,让我们安装最新版本的Gaia。这次我们安装的是v2.0.0版本。

git clone -b v2.0.0 https://github.com/cosmos/gaia
cd gaia && make install

安装完成后,我们验证一下
输入一下命令:
1、gaiad version –long

cuijb@cuijb-VirtualBox:~/go/src/github.com/cosmos/gaia$ gaiad version --long
name: gaia
server_name: gaiad
client_name: gaiacli
version: 2.0.0
commit: 3c70fee433956ba32e35705193a5f7a7d5b63277
build_tags: netgo,ledger
go: go version go1.12.9 linux/amd64

2、gaiacli version –long

cuijb@cuijb-VirtualBox:~/go/src/github.com/cosmos/gaia$ gaiacli version --long
name: gaia
server_name: gaiad
client_name: gaiacli
version: 2.0.0
commit: 3c70fee433956ba32e35705193a5f7a7d5b63277
build_tags: netgo,ledger
go: go version go1.12.9 linux/amd64

二、加入主网
1、设置新节点,初始化节点
这里我们自己可以定义一个节点
首先,初始化节点并创建必要的配置文件:
在我们编译安装好cosmos之后,我们需要初始化这个节点,在初始化之后,会生成相应的配置文件。
初始化节点

gaiad init  jambeau

执行后,会产生如下信息:

gaiad init jambeau
{"app_message":{"accounts":[],"auth":{"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":{"send_enabled":true},"crisis":{"constant_fee":{"amount":"1000","denom":"stake"}},"distribution":{"base_proposer_reward":"0.010000000000000000","bonus_proposer_reward":"0.040000000000000000","community_tax":"0.020000000000000000","delegator_starting_infos":[],"delegator_withdraw_infos":[],"fee_pool":{"community_pool":[]},"outstanding_rewards":[],"previous_proposer":"","validator_accumulated_commissions":[],"validator_current_rewards":[],"validator_historical_rewards":[],"validator_slash_events":[],"withdraw_addr_enabled":true},"genutil":{"gentxs":null},"gov":{"deposit_params":{"max_deposit_period":"172800000000000","min_deposit":[{"amount":"10000000","denom":"stake"}]},"deposits":null,"proposals":null,"starting_proposal_id":"1","tally_params":{"quorum":"0.334000000000000000","threshold":"0.500000000000000000","veto":"0.334000000000000000"},"votes":null,"voting_params":{"voting_period":"172800000000000"}},"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":"600000000000","max_evidence_age":"120000000000","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":null,"exported":false,"last_total_power":"0","last_validator_powers":null,"params":{"bond_denom":"stake","max_entries":7,"max_validators":100,"unbonding_time":"1814400000000000"},"redelegations":null,"unbonding_delegations":null,"validators":null},"supply":{"supply":[]}},"chain_id":"test-chain-ZiYkl9","gentxs_dir":"","moniker":"jambeau","node_id":"35bcac91edcbba5201ddcb872f047beb74e844ce"}

会自动生成以下文件

cuijb@cuijb-VirtualBox:~/.gaiad/config$ tree
.
├── app.toml
├── config.toml
├── genesis.json
├── node_key.json
└── priv_validator_key.json

我们也可以通过修改配置文件,来修改配置

 ~/.gaiad/config/config.toml

您可以编辑该~/.gaiad/config/app.toml文件以启用反垃圾邮件机制并拒绝低于最低汽油价格的传入交易:

# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

##### main base config options #####

# The minimum gas prices a validator is willing to accept for processing a
# transaction. A transaction's fees must meet the minimum of any denomination
# specified in this config (e.g. 0.25token1;0.0001token2).
minimum-gas-prices = ""

# HaltHeight contains a non-zero height at which a node will gracefully halt
# and shutdown that can be used to assist upgrades and testing.
halt-height = 0
~

到这里,你的节点已经完成初始化了。

2 、Genesis&Seeds
复制Genesis文件
将testnet的genesis.json文件获取到gaiad的config目录中。

curl https://raw.githubusercontent.com/cosmos/launch/master/genesis.json > $HOME/.gaiad/config/genesis.json

注意我们使用启动仓库中的latest目录,其中包含mainnet的详细信息,如最新版本和genesis文件

要验证配置运行的正确性:

gaiad start

三、加入测试网络
1、我们要加入测试网络,需要准备一下条件。

  • 修改config.toml增加种子信息。
  • 修改genesis.json文件

2、修改config.toml文件,增加种子信息。
查找测试网络的种子信息,参考如下地址:
https://github.com/cosmos/testnets/

August 8, 2019 00:00 GMT -Gaia 13005
Download the genesis here: genesis

$ shasum -a 256 genesis.json
d3d98e86ecdd3691bd88251e6c71acef6307cd3cbbe923f73609c6a5671c8c6f  -
Seed nodes:

35b9658ca14dd4908b37f327870cbd5007ee06f1@116.203.146.149:26656
c24f496b951148697f8a24fd749786075c128f00@35.203.176.214:26656
6be0856f6365559fdc2e9e97a07d609f754632b0@cosmos-gaia-13004-seed.nodes.polychainlabs.com:26656

在这里插入图片描述
这里有个坑,大家要注意,上面的genesis文件,不正确,也许是cosmos团队文件上传有误造成的,
我们如何得到 genesis文件,我们反复测试,需要选在最新发布的genesis文件就好了,然后替换掉init的文件。
我们下载gaia-13k文件就好了。
在这里插入图片描述
config.toml文件和genesis.json存储的问题在
~/.gaiad/config/下面

~/.gaiad/config/
cuijb@cuijb-VirtualBox:~/.gaiad/config$ tree
.
├── addrbook.json
├── app.toml
├── config.toml
├── genesis.json
├── node_key.json
└── priv_validator_key.json
0 directories, 6 files

修改了以上两步,我们就开启测试网络

gaiad start

如下显示,表明网络启动正常

cuijb@cuijb-VirtualBox:~/.gaiad/config$ gaiad start
I[2019-09-04|15:08:13.015] Starting ABCI with Tendermint                module=main
E[2019-09-04|15:08:19.266] Error dialing seed                           module=p2p err="dial tcp 35.203.176.214:26656: i/o timeout" seed=c24f496b951148697f8a24fd749786075c128f00@35.203.176.214:26656
E[2019-09-04|15:08:20.306] Error dialing seed                           module=p2p err="dial tcp 104.197.51.143:26656: i/o timeout" seed=6be0856f6365559fdc2e9e97a07d609f754632b0@104.197.51.143:26656
E[2019-09-04|15:08:20.778] Error dialing seed                           module=p2p err="auth failure: secret conn failed: EOF" seed=35b9658ca14dd4908b37f327870cbd5007ee06f1@116.203.146.149:26656
E[2019-09-04|15:08:20.778] Couldn't connect to any seeds                module=p2p
E[2019-09-04|15:08:51.792] Error dialing seed                           module=p2p err="dial tcp 104.197.51.143:                                26656: i/o timeout" seed=6be0856f6365559fdc2e9e97a07d609f754632b0@104.197.51.143:26656
E[2019-09-04|15:08:52.915] Stopping peer for error                      module=p2p peer="Peer{MConn{35.203.176.2                                14:26656} c24f496b951148697f8a24fd749786075c128f00 out}" err=EOF
E[2019-09-04|15:08:52.915] MConnection flush failed                     module=p2p peer=c24f496b951148697f8a24fd                                749786075c128f00@35.203.176.214:26656 err="write tcp 192.168.1.130:55316->35.203.176.214:26656: use of closed ne                                twork connection"
E[2019-09-04|15:09:21.109] dialing failed (attempts: 1): auth failure: secret conn failed: read tcp 192.168.1.13                                0:55218->54.153.125.220:46656: read: connection reset by peer module=pex addr=23baa7a2496c33a67b92941b928201fdf6                                9c0bc0@54.153.125.220:46656
E[2019-09-04|15:09:21.219] dialing failed (attempts: 1): auth failure: secret conn failed: read tcp 192.168.1.13                                0:60520->116.203.146.149:26656: read: connection reset by peer module=pex addr=35b9658ca14dd4908b37f327870cbd500                                7ee06f1@116.203.146.149:26656
E[2019-09-04|15:09:21.444] Stopping peer for error                      module=p2p peer="Peer{MConn{149.202.43.7                                8:26656} 95952a84f9342264744d5e3c5bdeafc51de80b53 out}" err=EOF
E[2019-09-04|15:09:21.445] MConnection flush failed                     module=p2p peer=95952a84f9342264744d5e3c                                5bdeafc51de80b53@149.202.43.78:26656 err="write tcp 192.168.1.130:35050->149.202.43.78:26656: use of closed netw                                ork connection"
E[2019-09-04|15:09:21.465] Stopping peer for error                      module=p2p peer="Peer{MConn{51.15.121.63                                :26656} dcbe32e354dd53ec9f0c47e6bb5acf03f70a920c out}" err=EOF
E[2019-09-04|15:09:21.470] Stopping peer for error                      module=p2p peer="Peer{MConn{54.80.187.25                                :26656} c0dcf3e5da1e9f5a696a7ff7c2594263190f97cf out}" err=EOF
E[2019-09-04|15:09:21.471] MConnection flush failed                     module=p2p peer=c0dcf3e5da1e9f5a696a7ff7                                c2594263190f97cf@54.80.187.25:26656 err="write tcp 192.168.1.130:57248->54.80.187.25:26656: use of closed networ                                k connection"
E[2019-09-04|15:09:21.646] Stopping peer for error                      module=p2p peer="Peer{MConn{80.71.57.38:                                26656} ae9cdb48aae0b62cc21720087d16f79a33e776da out}" err=EOF
E[2019-09-04|15:09:21.779] dialing failed (attempts: 1): dial tcp 13.250.239.168:26656: i/o timeout module=pex a                                ddr=a73ed88a6c436d9a1172271918c72e4630a5a5ee@13.250.239.168:26656
E[2019-09-04|15:09:21.779] dialing failed (attempts: 1): dial tcp 13.229.186.224:26656: i/o timeout module=pex a                                ddr=03a7f9a91d5cbc49efbe4b940a5885a6cc439524@13.229.186.224:26656
E[2019-09-04|15:09:21.780] dialing failed (attempts: 1): dial tcp 52.192.89.183:26656: i/o timeout module=pex ad                                dr=48045c382f314d503f5f714c167136d16ad87d96@52.192.89.183:26656
I[2019-09-04|15:09:38.178] Executed block                               module=state height=1 validTxs=0 invalidTxs=0
I[2019-09-04|15:09:42.563] Committed state                              module=state height=1 txs=0 appHash=2D4A5B04E38609B90A4F2F07F8BA1B4547160A30F5B3530D37878826AB7D42BE
I[2019-09-04|15:09:42.889] Executed block                               module=state height=2 validTxs=0 invalidTxs=0
I[2019-09-04|15:09:42.969] Committed state                              module=state height=2 txs=0 appHash=4568EEB57ABF9F95D357EBE29F1A852F5D53221DE6CBF05A421825EAF263DDDD
I[2019-09-04|15:09:43.053] Executed block                               module=state height=3 validTxs=0 invalidTxs=0
I[2019-09-04|15:09:43.148] Committed state                              module=state height=3 txs=0 appHash=B5EAC8930D85FD9EB4D4CC5C63E08B7F8E6404AC55799BF5F3438A1645B5984F
I[2019-09-04|15:09:43.213] Executed block                               module=state height=4 validTxs=0 invalidTxs=0
I[2019-09-04|15:09:43.276] Committed state                              module=state height=4 txs=0 appHash=2E8D26DC83F87633C41DBB78FBB68AAA1D922F78AC26AC280EB60A97CFF1C894
...
...
...

三、创建一个钱包、账户,查询账户信息。
3.1 创建一个钱包
创建一个钱包,命名为jambeau,需要连续输入两次8位密码,然后确认。

cuijb@cuijb-VirtualBox:~/.gaiad/data$ gaiacli keys add jambeau
Enter a passphrase to encrypt your key to disk:
Repeat the passphrase:

- name: jambeau
  type: local
  address: cosmos16t07t3sy07x46d89n9ygr0s8fcx484cxsvwxms
  pubkey: cosmospub1addwnpepqglyclyg5wldh8wry8zqggvq5242ud5020svwdl7r0wjgne66g5lx3pff9f
  mnemonic: ""
  threshold: 0
  pubkeys: []

说明你的钱包创建成功了
3.2查询账户列表

cuijb@cuijb-VirtualBox:~/.gaiad$ gaiacli keys list
- name: jambeau
  type: local
  address: cosmos16t07t3sy07x46d89n9ygr0s8fcx484cxsvwxms
  pubkey: cosmospub1addwnpepqglyclyg5wldh8wry8zqggvq5242ud5020svwdl7r0wjgne66g5lx3pff9f
  mnemonic: ""
  threshold: 0
  pubkeys: []

3.2、查询账户信息

cuijb@cuijb-VirtualBox:~/.gaiad$ gaiacli q account cosmos1zdkfdqr7uj4cd3vrz2grg3ypy3adajvxsdu292--chain-id gaia-13006
ERROR: {"codespace":"sdk","code":9,"message":"account cosmos1zdkfdqr7uj4cd3vrz2grg3ypy3adajvxsdu292 does not exist"}

为什么会显示错误信息呢?因为,你的账户没有钱,这样显示,很正确。我们就想办法给这个账户充点钱吧。存入一些钱后,该帐户将被激活。
我们可以去这个网站https://riot.im/app/#/login 申请代币。
注册收,输入 show me the money! cosmos1zdkfdqr7uj4cd3vrz2grg3ypy3adajvxsdu292
等待几分钟收,查询。

cuijb@cuijb-VirtualBox:~/gaiadata$ gaiacli q account cosmos1zdkfdqr7uj4cd3vrz2grg3ypy3adajvxsdu292 --chain-id gaia-13006
|
  address: cosmos1zdkfdqr7uj4cd3vrz2grg3ypy3adajvxsdu292
  coins:
  - denom: muon
    amount: "10000000"
  pubkey: ""
  accountnumber: 45197
  sequence: 0

代币已经到你账户了。
下一节,我们开始研究验证人。

恭喜你,你已经成功启动了测试网络节点。

2019年9月14日整理于深圳。


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/jambeau/article/details/100526302