测试如何用Cosmos创建并交易通证(test how to issue and transfer tokens using cosmos)

本测试尝试用cosmos-sdk的最新版本在本地构建一个两个节点(fincoinfabcoin)的区块链(finfab-chain),并发行两种通证,一个称为FinCoin, 另外一个称为FabCoin。之后,进行一次FabCoin的交易,从一个账户转100个通证到另外一个账户。

This test is an attempt to use the latest cosmos-sdk source to run a blockchain called (finfab-chain) with two nodes, fincoin node and fabcoin node. Then issue two tokens, FinCoin token and FabCoin token and transfer 100 FabCoin from one account to the other account.


系统准备 (system)

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial

$ go version
go version go1.9.3 linux/amd64

$ echo $GOPATH 
/opt/projects/go

下载源代码并编译 (download source and compile)

$ go get github.com/cosmos/cosmos-sdk
$ cd $GOPATH/src/github.com/cosmos/cosmos-sdk
$ make get_tools
cd tools && make get_tools
make[1]: Entering directory '/opt/projects/github.com/cosmos/cosmos-sdk/tools'
Dep is already installed.  Run 'make update_tools' to update.
make[1]: Leaving directory '/opt/projects/github.com/cosmos/cosmos-sdk/tools'

$ make update_tools
cd tools && make update_tools
make[1]: Entering directory '/opt/projects/github.com/cosmos/cosmos-sdk/tools'
Updating dep
go get -u -v github.com/golang/dep/cmd/dep
github.com/golang/dep (download)
make[1]: Leaving directory '/opt/projects/github.com/cosmos/cosmos-sdk/tools'

$ make get_vendor_deps
--> Running dep ensure
Gopkg.lock was already in sync with imports and Gopkg.toml
(1/49) Wrote github.com/mattn/go-isatty@v0.0.3
(2/49) Wrote github.com/pkg/errors@v0.8.0
(3/49) Wrote github.com/pelletier/go-toml@v1.1.0
(4/49) Wrote gopkg.in/yaml.v2@v2.2.1
(5/49) Wrote github.com/gorilla/context@v1.1.1
(6/49) Wrote github.com/davecgh/go-spew@v1.1.0
(7/49) Wrote github.com/rcrowley/go-metrics@master
(8/49) Wrote github.com/mitchellh/mapstructure@master
(9/49) Wrote github.com/ebuchman/fail-test@master
(10/49) Wrote github.com/pmezard/go-difflib@v1.0.0
(11/49) Wrote github.com/fsnotify/fsnotify@v1.4.7
(12/49) Wrote github.com/bgentry/speakeasy@v0.1.0
(13/49) Wrote github.com/golang/snappy@master
(14/49) Wrote github.com/hashicorp/hcl@master
(15/49) Wrote github.com/gorilla/mux@v1.6.2
(16/49) Wrote github.com/go-logfmt/logfmt@v0.3.0
(17/49) Wrote github.com/go-kit/kit@v0.6.0
(18/49) Wrote github.com/spf13/afero@v1.1.0
(19/49) Wrote github.com/go-stack/stack@v1.7.0
(20/49) Wrote github.com/golang/protobuf@v1.0.0
(21/49) Wrote github.com/gorilla/websocket@v1.2.0
(22/49) Wrote github.com/spf13/cast@v1.2.0
(23/49) Wrote github.com/tendermint/go-crypto@v0.6.2
(24/49) Wrote github.com/tendermint/go-wire@v0.7.3
(25/49) Wrote github.com/spf13/cobra@v0.0.3
(26/49) Wrote github.com/tendermint/iavl@v0.7.0
(27/49) Wrote github.com/spf13/jwalterweatherman@master
(28/49) Wrote github.com/btcsuite/btcd@master
(29/49) Wrote github.com/tendermint/tmlibs@v0.8.3-rc0
(30/49) Wrote github.com/spf13/pflag@v1.0.1
(31/49) Wrote github.com/jmhodges/levigo@master
(32/49) Wrote github.com/howeyc/crc16@master
(33/49) Wrote github.com/syndtr/goleveldb@master
(34/49) Wrote github.com/inconshreveable/mousetrap@v1.0
(35/49) Wrote github.com/tendermint/abci@v0.10.3
(36/49) Wrote github.com/spf13/viper@v1.0.2
(37/49) Wrote golang.org/x/crypto@master
(38/49) Wrote github.com/kr/logfmt@master
(39/49) Wrote github.com/tendermint/tendermint@v0.19.5-rc1
(40/49) Wrote github.com/tendermint/ed25519@master
(41/49) Wrote github.com/stretchr/testify@v1.2.1
(42/49) Wrote github.com/magiconair/properties@v1.8.0
(43/49) Wrote github.com/tendermint/go-amino@0.9.9
(44/49) Wrote github.com/gogo/protobuf@v1.0.0
(45/49) Wrote google.golang.org/genproto@7fd901a49ba6a7f87732eb344f6e3c5b19d1b200
(46/49) Wrote google.golang.org/grpc@v1.7.5
(47/49) Wrote golang.org/x/net@master
(48/49) Wrote golang.org/x/text@v0.3.0
(49/49) Wrote golang.org/x/sys@master

$ make install
go install -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=187be1a5" ./cmd/gaia/cmd/gaiad
go install -ldflags "-X github.com/cosmos/cosmos-sdk/version.GitCommit=187be1a5" ./cmd/gaia/cmd/gaiacli

Cosmos gaia 版本确认 (cosmos gaia version check)

$ gaiad version
0.17.2-187be1a5

$ gaiacli version
0.17.2-187be1a5

创世纪设定 (genesis configuration)

$ gaiad init gen-tx --name=fincoin --home=/opt/data/cosmos/fincoin
{
  "app_message": {
    "secret": "inner space weird carry near high moral slot hero drum smart satoshi boat vital assault abandon"
  },
  "gen_tx_file": {
    "node_id": "c519ca2cc2257eb62a95a61c45268ade1ee35d59",
    "ip": "172.x.x.x",
    "validator": {
      "pub_key": {
        "type": "AC26791624DE60",
        "value": "hNwHCN3J/FSa5nCJDgikkSh3znhv9ZrRZUU+kkvw1Uc="
      },
      "power": 100,
      "name": ""
    },
    "app_gen_tx": {
      "name": "fincoin",
      "address": "3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11",
      "pub_key": {
        "type": "AC26791624DE60",
        "value": "hNwHCN3J/FSa5nCJDgikkSh3znhv9ZrRZUU+kkvw1Uc="
      }
    }
  }
}

$ gaiad init gen-tx --name=fabcoin --home=/opt/data/cosmos/fabcoin
{
  "app_message": {
    "secret": "mushroom practice juice burst expire scorpion frozen clay catalog erosion draft bulk blush rug arrive abandon"
  },
  "gen_tx_file": {
    "node_id": "fc93204300fedd2076d9ec82bf7be589b30ec923",
    "ip": "172.x.x.x",
    "validator": {
      "pub_key": {
        "type": "AC26791624DE60",
        "value": "R3l0Y86zKYhaKnqliVu/sQInGUiYB/8AXNbp1hAsE7k="
      },
      "power": 100,
      "name": ""
    },
    "app_gen_tx": {
      "name": "fabcoin",
      "address": "FB983823EC3A541193E61DBF1DAF8CFF4C90F02E",
      "pub_key": {
        "type": "AC26791624DE60",
        "value": "R3l0Y86zKYhaKnqliVu/sQInGUiYB/8AXNbp1hAsE7k="
      }
    }
  }
}

$ gaiacli keys list
NAME:   ADDRESS:                    PUBKEY:
fabcoin FB983823EC3A541193E61DBF1DAF8CFF4C90F02E    1624DE6220C6EEE1CCD437A5214499631559379D64023B1C83EA56DCE03EA64076303CEDDD
fincoin 3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11    1624DE6220E82D7F4126AC9881C9C06A3EB33A293926DC15F4EE1B59EAD9339546F8ACB237


更改设定文件 (modify config files)

因为我们要在本地跑两个节点,所以缺省的设定会有端口冲突,因此需要更改其中一个节点的设定。

Because we will be running two nodes on the local machine, in order to avoid port conflicts, we will change the port settings for one node.

  • 端口设定更改 (change ports)
$ cd /opt/data/cosmos/fabcoin/config
$ cp config.toml config.toml.orig
$ cat config.toml.orig | sed -e 's/466/467/g' > config.toml
$ diff config.toml config.toml.orig 
8c8
< proxy_app = "tcp://127.0.0.1:46758"
---
> proxy_app = "tcp://127.0.0.1:46658"
54c54
< laddr = "tcp://0.0.0.0:46757"
---
> laddr = "tcp://0.0.0.0:46657"
67c67
< laddr = "tcp://0.0.0.0:46756"
---
> laddr = "tcp://0.0.0.0:46656"

  • 更改fabcoin节点的名称(编辑/opt/data/cosmos/fabcoin/config/config.toml文件) (change default node name in file /opt/data/cosmos/fabcoin/config/config.toml)
# A custom human readable name for this node
moniker = "fabnode"
  • 更改fincoin节点的名称(编辑/opt/data/cosmos/fincoin/config/config.toml文件) (change default node name in file /opt/data/cosmos/fincoin/config/config.toml)
# A custom human readable name for this node
moniker = "finnode"

finfab-chain区块链上初始创世纪交易 (initiate genesis transactions on the finfab-chain blockchain)

$ cd /opt/data/cosmos

$ cp -a fabcoin/config/gentx/. fincoin/config/gentx/

$ cp -a fincoin/config/gentx/. fabcoin/config/gentx/

$ gaiad init --gen-txs --home=/opt/data/cosmos/fabcoin --chain-id=finfab-chain
{
  "chain_id": "finfab-chain",
  "node_id": "fc93204300fedd2076d9ec82bf7be589b30ec923",
  "app_message": null
}

$ gaiad init --gen-txs --home=/opt/data/cosmos/fincoin --chain-id=finfab-chain
{
  "chain_id": "finfab-chain",
  "node_id": "c519ca2cc2257eb62a95a61c45268ade1ee35d59",
  "app_message": null
}

运行节点 (run the blockchain nodes)

可以用两种方式跑节点,一种直接从IDE里启动,再者就是从命令行。

There are two ways we can run the nodes, either from the IDE or from the command line.

  • IDE (GoLand) 启动 (run from GoLand IDE)


  • 命令行 (run from command line)

$ gaiad start --home=/opt/data/cosmos/fabcoin
$ gaiad start --home=/opt/data/cosmos/fincoin

账户确认 (verify accounts)

fabcoin账户拥有1000个fabcoinTokenfincoin账户拥有1000个fincoinToken

fabcoin account has 1000 fabcoinTokenfincoin account has 1000 fincoinToken

$ gaiacli validatorset
{"block_height":17,"validators":[{"address":"6084207D042C1B35C631A279F2B3AA9DE404CD83","pub_key":{"type":"AC26791624DE60","value":"hNwHCN3J/FSa5nCJDgikkSh3znhv9ZrRZUU+kkvw1Uc="},"voting_power":100,"accum":-100},{"address":"D7F2FD3FC1535920EC695125613F08C53C569C7A","pub_key":{"type":"AC26791624DE60","value":"R3l0Y86zKYhaKnqliVu/sQInGUiYB/8AXNbp1hAsE7k="},"voting_power":100,"accum":100}]}

$ gaiacli keys list
NAME:   ADDRESS:                    PUBKEY:
fabcoin FB983823EC3A541193E61DBF1DAF8CFF4C90F02E    1624DE6220C6EEE1CCD437A5214499631559379D64023B1C83EA56DCE03EA64076303CEDDD
fincoin 3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11    1624DE6220E82D7F4126AC9881C9C06A3EB33A293926DC15F4EE1B59EAD9339546F8ACB237

$ gaiacli account FB983823EC3A541193E61DBF1DAF8CFF4C90F02E
{
  "type": "6C54F73C9F2E08",
  "value": {
    "address": "FB983823EC3A541193E61DBF1DAF8CFF4C90F02E",
    "coins": [
      {
        "denom": "fabcoinToken",
        "amount": 1000
      },
      {
        "denom": "steak",
        "amount": 50
      }
    ],
    "public_key": null,
    "sequence": 0
  }
}

$ gaiacli account 3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11
{
  "type": "6C54F73C9F2E08",
  "value": {
    "address": "3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11",
    "coins": [
      {
        "denom": "fincoinToken",
        "amount": 1000
      },
      {
        "denom": "steak",
        "amount": 50
      }
    ],
    "public_key": null,
    "sequence": 0
  }
}


通证交易 (token transfer)

  • fabcoin转100个通证给fincoin (默认的账户密码是1234567890) (100 tokens transferred from fabcoin account to fincoin account, default password is 1234567890)
$ gaiacli send --amount=100fabcoinToken --to=3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11 --name=fabcoin --chain-id=finfab-chain
Defaulting to next sequence number: 0
Password to sign with 'fabcoin':
Committed at block 339. Hash: 2F169D62F07FCF2EBDF33AA95599178BFC43D83C
  • 交易确认 (transaction confirmation)
$ gaiacli account FB983823EC3A541193E61DBF1DAF8CFF4C90F02E
{
  "type": "6C54F73C9F2E08",
  "value": {
    "address": "FB983823EC3A541193E61DBF1DAF8CFF4C90F02E",
    "coins": [
      {
        "denom": "fabcoinToken",
        "amount": 900
      },
      {
        "denom": "steak",
        "amount": 50
      }
    ],
    "public_key": {
      "type": "AC26791624DE60",
      "value": "xu7hzNQ3pSFEmWMVWTedZAI7HIPqVtzgPqZAdjA87d0="
    },
    "sequence": 1
  }
}

$ gaiacli account 3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11
{
  "type": "6C54F73C9F2E08",
  "value": {
    "address": "3A8E0270F2D5D6128DC4C77B5656C0ADB5379C11",
    "coins": [
      {
        "denom": "fabcoinToken",
        "amount": 100
      },
      {
        "denom": "fincoinToken",
        "amount": 1000
      },
      {
        "denom": "steak",
        "amount": 50
      }
    ],
    "public_key": null,
    "sequence": 0
  }
}

fabcoin账户拥有900个fabcoinTokenfincoin账户拥有100个fabcoinToken和1000个fincoinToken

Now fabcoin account has 900 fabcoinTokenfincoin account has 100 fabcoinToken and 1000 fincoinToken


参考 (Reference)

http://cosmos-sdk.readthedocs.io/en/develop/index.html
https://github.com/cosmos/testnets

推荐阅读更多精彩内容