测试如何用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,251评论 0 10
  • 2017.8.29。无为,焦点解决第46天。古代谚语云:牙痛不是病,疼起来要了命。从数天前感冒起,到现在的有小痛到...
    无为wyw阅读 282评论 0 0
  • 天一热,总能看见一众小仙女,巴巴跪求安利防水防汗防晕染的眼线笔。自己做功课搜?茫茫笔海,要动脑经又有选择恐惧症感觉...
    五月桃夭阅读 124评论 0 0
  • 故乡,是一曲不老的民谣 曲调悠扬,浅吟低唱 故乡,是一杯陈年佳酿 醇厚飘香,辣喉暖肠 故乡,是一抹心灵的惆怅 深夜...
    远方孤雁阅读 139评论 2 5
  • 对现代人来说,生命的意义,早就从身体里抽干。仅存的是对空虚感格外的恐惧,和与之俱来的无所事事。我们偶尔安静下来,总...
    南風沫夏的写字台阅读 1,351评论 12 16