本文节选自电子书Netkiller Blockchain 手札
Netkiller Blockchain 手札
Mr. Neo Chan, 陈景峯 (BG7NYT)
中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>
文档始创于 2018-02-10
版权 © 2018 Netkiller(Neo Chan). All rights reserved.
网站:http://www.netkiller.cn |
---|
QQ:13721218 请注明 “读者” |
QQ 群:128659835 请注明 “读者” |
内容摘要
这一部关于区块链开发及运维的电子书.
为什么会写区块链电子书? 因为 2018 年是区块链年, 区块链是一个风口, 前几个风口我都错过了. 例如 web2.0, 云, 大数据等等, 都从身旁擦肩而过. 所以我要抓住这次.
这本电子书是否会出版 (纸质图书)? 不会, 因为互联网技术更迭太快, 纸质书籍的内容无法实时更新, 一本书动辄百元, 很快就成为垃圾, 你会发现目前市面的上区块链书籍至少是一年前写的, 内容已经过时, 很多例子无法正确运行. 所以我不会出版, 电子书的内容会追逐技术发展, 及时跟进软件版本的升级, 做到内容最新, 至少是主流.
这本电子书与其他区块链书籍有什么不同? 市面上大部分区块链书籍都是用 2/3 去讲区块链原理, 只要不到 1/3 的干货, 干货不够理论来凑, 通篇将理论或是大谈特谈区块链行业, 这些内容更多是头脑风暴, 展望区块链, 均无法落地实施. 本书与那些书籍完全不同, 不讲理论和原理, 面向应用落地, 注重例子, 均是干货.
写作原则, 无法落地的项目作者绝对不会写. 凡是写入电子的内容均具备可操作, 可落地.
电子书更新频率? 每天都会有新内容加入, 更新频率最迟不会超过一周, 更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master
本文采用碎片化写作, 原文会不定期更新, 请尽量阅读原文 http://www.netkiller.cn/blockchain/index.html
您的打赏是我的写作动力: http://www.netkiller.cn/blockchain/donations.html
接受 ETH 打赏: 0x3e827461Cc53ed7c75A29187CfF39629FCAE3661
3.2. EOS 安装
- 3.2.1. CentOS
- yum install -y centos-release-scl
- yum install -y devtoolset-7
- yum install -y git
- yum install -y gcc gcc-c++ make patch cmake automake autoconf \
- libtool ocaml doxygen graphviz-devel libicu-devel bzip2-devel gmp-devel python-devel gettext-devel
- cd /usr/local/src/
- git clone https://github.com/EOSIO/eos --recursive
- cd eos/
- # git submodule update --init --recursive
- ./eosio_build.sh
- [root@iZj6c7cj14ulhfndlmeicbZ eos]# ./eosio_build.sh
- Beginning build version: 1.2
- Wed May 2 03:15:34 UTC 2018
- User: root
- git head id: f537bc50b21a7807ff0ee3af83d8f560ce09afa5
- Current branch: * master
- ARCHITECTURE: Linux
- OS name: CentOS Linux
- OS Version: 7
- CPU speed: 2494Mhz
- CPU cores: 4
- Physical Memory: 7822 Mgb
- Disk install: /dev/vda1
- Disk space total: 492G
- Disk space available: 138G
- Checking Yum installation
- Yum installation found at /usr/bin/yum.
- Checking installation of Centos Software Collections Repository.
- The Centos Software Collections Repository, devtoolset-7 and Python3 are required to install EOSIO.
- Do you wish to install and enable this repository, devtoolset-7 and Python3 packages?
- 1) Yes
- 2) No
- #? 1
输入 1 回车继续
- Complete!
- YUM repository successfully updated.
- Checking YUM for installed dependencies.
- Package git found.
- Package autoconf found.
- Package automake found.
- Package libtool NOT found.
- Package ocaml.x86_64 NOT found.
- Package doxygen NOT found.
- Package graphviz-devel.x86_64 NOT found.
- Package libicu-devel.x86_64 NOT found.
- Package bzip2-devel.x86_64 NOT found.
- Package openssl-devel.x86_64 NOT found.
- Package gmp-devel.x86_64 NOT found.
- Package python-devel.x86_64 NOT found.
- Package gettext-devel.x86_64 NOT found.
- The following dependencies are required to install EOSIO.
- 1. libtool
- 2. ocaml.x86_64
- 3. doxygen
- 4. graphviz-devel.x86_64
- 5. libicu-devel.x86_64
- 6. bzip2-devel.x86_64
- 7. openssl-devel.x86_64
- 8. gmp-devel.x86_64
- 9. python-devel.x86_64
- 10. gettext-devel.x86_64
- Do you wish to install these dependencies?
- 1) Yes
- 2) No
- #?
输入 1 回车继续
- _______ _______ _______ _________ _______
- ( ____ \( ___ )( ____ \\__ __/( ___ )
- | ( \/| ( ) || ( \/ ) ( | ( ) |
- | (__ | | | || (_____ | | | | | |
- | __) | | | |(_____ ) | | | | | |
- | ( | | | | ) | | | | | | |
- | (____/\| (___) |/\____) |___) (___| (___) |
- (_______/(_______)\_______)\_______/(_______)
- EOSIO has been successfully built. 01:20:59
- To verify your installation run the following commands:
- /root/opt/mongodb/bin/mongod -f /root/opt/mongodb/mongod.conf &
- source /opt/rh/python33/enable
- export PATH=${HOME}/opt/mongodb/bin:$PATH
- cd /usr/local/src/eos/build; make test
- For more information:
- EOSIO website: https://eos.io
- EOSIO Telegram channel @ https://t.me/EOSProject
- EOSIO resources: https://eos.io/resources/
- EOSIO Stack Exchange: https://eosio.stackexchange.com
- EOSIO wiki: https://github.com/EOSIO/eos/wiki
这里跳过 make test 直接安装
- cd build
- make install
3.3. 启动 EOS
3.3.1. 单节点测试网
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --plugin eosio::wallet_api_plugin
区块数据保存在 ~/.local/share/eosio/nodeos/data
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# find ~/.local/share/eosio/nodeos/data
- /root/.local/share/eosio/nodeos/data
- /root/.local/share/eosio/nodeos/data/blocks
- /root/.local/share/eosio/nodeos/data/blocks/blocks.index
- /root/.local/share/eosio/nodeos/data/blocks/reversible
- /root/.local/share/eosio/nodeos/data/blocks/reversible/shared_memory.meta
- /root/.local/share/eosio/nodeos/data/blocks/reversible/shared_memory.bin
- /root/.local/share/eosio/nodeos/data/blocks/blocks.log
- /root/.local/share/eosio/nodeos/data/state
- /root/.local/share/eosio/nodeos/data/state/shared_memory.meta
- /root/.local/share/eosio/nodeos/data/state/shared_memory.bin
3.4. CLEOS 命令行
3.4.1. 钱包
3.4.1.1. 创建钱包
创建默认钱包
$ cleos wallet create
演示
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet list
- "/usr/local/bin/keosd" launched
- Wallets:
- []
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet create
- Creating wallet: default
- Save password to use in the future to unlock this wallet.
- Without password imported keys will not be retrievable.
- "PW5Hu6VtABuC75RmjSaPv6BcwofA5DQMJ9xHFeFeefmZGNsdknAKQ"
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet list
- Wallets:
- [
- "default *"
- ]
创建指定名称的钱包
$ cleos wallet create -n netkiller
操作演示
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet create -n netkiller
- Creating wallet: netkiller
- Save password to use in the future to unlock this wallet.
- Without password imported keys will not be retrievable.
- "PW5J8qAhMPotrUQAswbPabXZPJq85YVGuxofhGVxo19xcynAfZcqx"
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet list
- Wallets:
- [
- "default *",
- "netkiller *"
- ]
3.4.1.2. 钱包列表
$ cleos wallet list
3.4.1.3. 钱包锁
上锁
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet lock
- Locked: default
- $ cleos wallet lock -n netkiller
解锁
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet unlock
- password: Unlocked: default
- $ cleos wallet unlock -n netkiller
- 3.4.2. EOSIO Keys
3.4.2.1. 创建公钥和私钥
- $ cleos create key
- [root@iZj6c39y62jl5b1wmfv6u8Z etc]# cleos create key
- Private key: 5JXxZEQZNjyxNKSGHcdiAwE4uALykxwvgtAyLRxEygQJP9eULkH
- Public key: EOS69EZcBVwgRz3AbHheR3ZpeHtaoHAPyLXfvmsiqYMAtazN3WdiL
3.4.2.2. 导入私钥
$ cleos wallet import 5K8apwojp2U4mcv1xAAjP541QFUEhkRWxskYbL3ZzCq1VoBwuSX
3.4.2.3. 查看私钥
$ cleos wallet private_keys --password ${your_wallet_password}
3.4.3. 区块信息
3.4.3.1. 获得当前区块链信息
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get info
- {
- "server_version": "90fefdd1",
- "chain_id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",
- "head_block_num": 1063,
- "last_irreversible_block_num": 1062,
- "last_irreversible_block_id": "00000426ca6002e4097bb85032e357bceac77d90075e0020f45b26d397c2183b",
- "head_block_id": "00000427049a6f175fd5c13660651e7fe36ef8199e316bed0349a178c33f525b",
- "head_block_time": "2018-06-28T08:39:57.000",
- "head_block_producer": "eosio",
- "virtual_block_cpu_limit": 577784,
- "virtual_block_net_limit": 3033320,
- "block_cpu_limit": 199900,
- "block_net_limit": 1048576
- }
3.4.3.2. 获取指定区块数据
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get block 1063
- {
- "timestamp": "2018-06-28T08:39:57.000",
- "producer": "eosio",
- "confirmed": 0,
- "previous": "00000426ca6002e4097bb85032e357bceac77d90075e0020f45b26d397c2183b",
- "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
- "action_mroot": "b472502694c9f3fa5684f44edc4c34742708b2400690a49bb00a297b3d201456",
- "schedule_version": 0,
- "new_producers": null,
- "header_extensions": [],
- "producer_signature": "SIG_K1_Jzx3cvL6pDxEsxhFbqPasqBymxKhodiiWjVmgtifFEDzThdYfBTvVvvmTNTxaBLwBZ1AJxyuW1uR3J5nvKDwc3xnAgRuWk",
- "transactions": [],
- "block_extensions": [],
- "id": "00000427049a6f175fd5c13660651e7fe36ef8199e316bed0349a178c33f525b",
- "block_num": 1063,
- "ref_block_prefix": 918672735
- }
3.4.3.3. 从区块链获取交易信息
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get transaction cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f
- {
- "id": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f",
- "trx": null,
- "block_time": "2000-01-01T00:00:00.000",
- "block_num": 0,
- "last_irreversible_block": 1777,
- "traces": []
- }
3.4.3.4. 获得账号信息
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get account neo
- permissions:
- owner 1: 1 EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
- active 1: 1 EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
- memory:
- quota: unlimited used: 2.66 KiB
- net bandwidth:
- used: unlimited
- available: unlimited
- limit: unlimited
- cpu bandwidth:
- used: unlimited
- available: unlimited
- limit: unlimited
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get accounts EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
- {
- "account_names": [
- "neo"
- ]
- }
3.4.4. 智能合约 - EOS 代币
3.4.4.1. 编译智能合约
3.4.4.3. 创建账号
创建秘钥对
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos create key
- Private key: 5JFMTVk4EjWW54xk73AMRPf5JbpFV2Cm7vtgt1jr9zVaPgLmaLQ
- Public key: EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
导入私钥
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet import 5JFMTVk4EjWW54xk73AMRPf5JbpFV2Cm7vtgt1jr9zVaPgLmaLQ
- imported private key for: EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet keys
- [
- "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
- "EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr"
- ]
创建账号 neo
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet unlock
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos create account eosio neo EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr EOS7fcRYssRt5SXVnsPpRNzj86E9h5g62hBgKwr1NSzRmSpH9byZr
- executed transaction: e138b1e7557d76b3560b898942db942eb23b43f8387c60083741ab4d0680e139 200 bytes 311 us
- # eosio <= eosio::newaccount {"creator":"eosio","name":"neo","owner":{"threshold":1,"keys":[{"key":"EOS7fcRYssRt5SXVnsPpRNzj86E9h...
- warning: transaction executed locally, but may not be confirmed by the network yet
3.4.4.4. 部署合约 eosio.bios
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet unlock
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos set contract eosio /usr/local/src/eos/build/contracts/eosio.bios -p eosio
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos set contract eosio /usr/local/src/eos/build/contracts/eosio.bios -p eosio
- Reading WAST/WASM from /usr/local/src/eos/build/contracts/eosio.bios/eosio.bios.wasm...
- Using already assembled WASM...
- Publishing contract...
- executed transaction: c8589dc4ddb429765e86e78add1420461ce35a4edac7e08fe790e4b876a1ce29 3720 bytes 815 us
- # eosio <= eosio::setcode {"account":"eosio","vmtype":0,"vmversion":0,"code":"0061736d0100000001621260037f7e7f0060057f7e7e7e7e...
- # eosio <= eosio::setabi {"account":"eosio","abi":"0e656f73696f3a3a6162692f312e30050c6163636f756e745f6e616d65046e616d650f7065...
- warning: transaction executed locally, but may not be confirmed by the network yet
3.4.4.5. 创建账号 netkiller
创建账号 netkiller 重复上面步骤, 这个账号用于创建代币智能合约.
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos create key
- Private key: 5KVTLTRgLdKj4b5FkkFpYMhYdhimPip3dtdfnZAQVQxQBBV4oFq
- Public key: EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos wallet import 5KVTLTRgLdKj4b5FkkFpYMhYdhimPip3dtdfnZAQVQxQBBV4oFq
- imported private key for: EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos create account eosio netkiller EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH EOS5NyaD49BuTCScNEY8FPBCZ9t6VXThMAmFvgMg72XqcNVPXuEWH
- executed transaction: fc87fc5cb598a24b36bf3dc10c542d7425d319d33291029de1f0c412dadea233 200 bytes 301 us
- # eosio <= eosio::newaccount {"creator":"eosio","name":"netkiller","owner":{"threshold":1,"keys":[{"key":"EOS5NyaD49BuTCScNEY8FPB...
- warning: transaction executed locally, but may not be confirmed by the network yet
3.4.4.6. EOS 代币合约
cleos set contract netkiller /usr/local/src/eos/build/contracts/eosio.token
操作演示
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos set contract netkiller /usr/local/src/eos/build/contracts/eosio.token
- Reading WAST/WASM from /usr/local/src/eos/build/contracts/eosio.token/eosio.token.wasm...
- Using already assembled WASM...
- Publishing contract...
- executed transaction: a8bdeafdadd37b6a3b2bf1de908725028e51ae3d5f8a9e0f95e4d33b3b22b8be 8104 bytes 1411 us
- # eosio <= eosio::setcode {"account":"netkiller","vmtype":0,"vmversion":0,"code":"0061736d01000000017e1560037f7e7f0060057f7e7e...
- # eosio <= eosio::setabi {"account":"netkiller","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d6505...
- warning: transaction executed locally, but may not be confirmed by the network yet
code hash 有值表示合约部署成功
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get code netkiller
- code hash: 641f336aa1d08526201599c3c0ddb7a646e5ac8f9fd2493f56414d0422a0f957
code hash 为 0 表示合约部署失败
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get code netkiller
- code hash: 0000000000000000000000000000000000000000000000000000000000000000
3.4.4.7. 创建代币
- [root@iZj6c39y62jl5b1wmfv6u8Z eosio.token]# cleos wallet unlock
- password: Unlocked: default
- [root@iZj6c39y62jl5b1wmfv6u8Z eosio.token]# cleos push action netkiller create '["netkiller","1000 EOS",0,0,0]' -p netkiller
- executed transaction: 7266dda0b3fde45bd03fc0ab4fdece35e66cf6e616165f113434f63394f3fa33 120 bytes 488 us
- # netkiller <= netkiller::create {"issuer":"netkiller","maximum_supply":"1000 EOS"}
- warning: transaction executed locally, but may not be confirmed by the network yet
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos push action netkiller create '["netkiller","10000 NRC",0,0,0]' -p netkiller
- executed transaction: cbb45fcca9e140f24cd90fb6c0fbb781afcc3a1d6da4dc912a7fde79b26b7dd0 120 bytes 521 us
- # netkiller <= netkiller::create {"issuer":"netkiller","maximum_supply":"10000 NRC"}
- warning: transaction executed locally, but may not be confirmed by the network yet
查看合约信息
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get currency stats netkiller EOS
- {
- "EOS": {
- "supply": "1000 EOS",
- "max_supply": "1000 EOS",
- "issuer": "netkiller"
- }
- }
另一个方式
cleos push action netkiller create '{"issuer":"netkiller","maximum_supply":"10.0000 EOS","can_freeze": 1,"can_recall": 1,"can_whitelist": 1}' -p netkiller@active
3.4.4.8. 发放代币
cleos push action netkiller issue '["neo","1000 EOS","issue"]' -p netkiller
给 neo 账号发放 1000 个 EOS 币
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos push action netkiller issue '["neo","1000 EOS","issue"]' -p netkiller
- executed transaction: c60760dfbdad2face6917ff28015555f1cfc293d71eb7556fc2f7ec78591229b 128 bytes 1339 us
- # netkiller <= netkiller::issue {"to":"neo","quantity":"1000 EOS","memo":"issue"}
- # netkiller <= netkiller::transfer {"from":"netkiller","to":"neo","quantity":"1000 EOS","memo":"issue"}
- # neo <= netkiller::transfer {"from":"netkiller","to":"neo","quantity":"1000 EOS","memo":"issue"}
- warning: transaction executed locally, but may not be confirmed by the network yet
- cleos push action netkiller issue '{"to":"neo","quantity":"10.0000 EOS","memo":" 备注信息 "}' --permission netkiller@active
3.4.4.9. 查看代币余额
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get table netkiller neo accounts
- {
- "rows": [{
- "balance": "1000 EOS"
- }
- ],
- "more": false
- }
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get currency balance netkiller neo
- 1000 EOS
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get currency balance netkiller neo EOS
- 1000 EOS
3.4.4.10. 转账
- cleos push action eosio transfer '["eosio","netkiller","100 EOS",""]' -p eosio
- cleos push action contract transfer '{"from":"from_address","to":"to_address","quantity":"1.0000 EOS","memo":" 测试 "}' --permission neo@active
操作演示
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get currency balance netkiller netkiller EOS
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos push action netkiller transfer '["neo","netkiller","10 EOS","memo"]' -p neo
- executed transaction: 0e23837bd8a3a7876b2463cbde1d47a25d2ac2178bb42ddbccd3037416cc9e43 136 bytes 745 us
- # netkiller <= netkiller::transfer {"from":"neo","to":"netkiller","quantity":"10 EOS","memo":"memo"}
- # neo <= netkiller::transfer {"from":"neo","to":"netkiller","quantity":"10 EOS","memo":"memo"}
- warning: transaction executed locally, but may not be confirmed by the network yet
- [root@iZj6c39y62jl5b1wmfv6u8Z ~]# cleos get currency balance netkiller netkiller EOS
- 10 EOS
QQ:13721218 请注明 “读者” |
QQ 群:128659835 请注明 “读者” |
来源: https://www.qcloud.com/developer/article/1153621