EOS区块链技术开发(⼀)环境搭建
不要看我的??博⽂了,以下博⽂就是我当时随便写写的,现在也没有什么时间详细修改。如果有问题可以提问。
注:由于EOSIO仍在不断更新,所以以下内容的⼀些步骤可能会在将来某天失效。本开发⼿册编写时间是在2018.10⾄2018.11.可能会不断更新。
0 前⾔——About development
1.
2.
3. Start develop
First you need enough general knowledge about EOS then you can use your programming skill for EOS.
编写合约:
==>创建账号:create account [OPTIONS] creator name OwnerKey [ActiveKey]
like:cleos create account eosio hello EOS5YScRTMfjR69M4k4pP5qjAzDRfxtP9DY1Rx4WrZP4JnYj4MTRP -p
eosio@active
==>编译合约eosio-cpp -o hello.wasm hello.cpp –abigen
==>设置合约:cleos set contract hello $CONTRACTS_DIR/hello -p hello@active
==>运⾏:cleos push action hello hi ‘[“bob”]’ -p bob@active
4.EOS的ABI⽂件。
1 EOS开发环境搭建
所谓开发环境就是⼤家所理解的开发环境…不多说了。我们把区块链和操作系统做个类⽐吧。这⼀步就相当于是把操作系统在⾃⼰计算机上装好。
2 EOS测试环境搭建(Configure Nodeos)
所谓测试环境,就是你要现在本地建⽴⼀个启动⼀个EOS区块链的私有链。前⾯的开发环境就相当于是
装好了操作系统,这⼀步是要启动这个操作系统,让其运⾏起来。
2.1 Creating and Launching a Single Node Testnet
启动本地测试⽹(单节点):
nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --contracts-console
参数说明:
-e -Enable block production, even if the chain is stale
-p <producer_name> - ID of producer controlled by this node
--contracts-console:To print contract's output to console by default add:
关于EOS测试⽹启动的设置:
修改⼀个叫做config.ini的⽂件即可,主要是针对插件 --plugin,⽂件位置:
cd ~/Library/Application*Support/eosio/nodeos/config
thread技术我遇到了⼀些问题,第⼀个是wallet_api_plugin不到。于是我先不使⽤这个插件。⽽是:
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
info 2018-10-22T13:24:31.501 thread-0 producer_plugin.cpp:1490 produce_block ] Produced #373 @ 2018-10-22T1 3:24:31.500 signed by eosio [trxs: 0, lib: 372, confirmed: 0]
…
22T13:24:50.500 signed by eosio [trxs: 0, lib: 380, confirmed: 0]
info 2018-10-22T13:24:51.000 thread-0 producer_plugin.cpp:1490 produce_block ] Produced #382 @ 2018-10-22T1 3:24:51.000 signed by eosio [trxs: 0, lib: 381, confirmed: 0]
^Z
[1]+ Stopped nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
MacBook-Plou:data treaser$ fg
nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
…
info 2018-10-22T13:24:54.003 thread-0 producer_plugin.cpp:1490 produce_block ] Produced #386 @ 2018-10-22T1 3:24:54.000 signed by eosio [trxs: 0, lib: 385, confirmed: 0]
^Z
[1]+ Stopped nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
以上就是区块链启动成功了。console输出就是每个区块的数据。然后可以暂停⼀下。(如果不暂停,⼀直跑就会很耗电…)
如果暂停了以后,再次运⾏启动测试⽹的命令:nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
出现database dirty flag set的错误,需要进⼊:
~/Library/Application Support/eosio/nodeos/data
把blocks 和 state ⽬录删掉。重启nodeos。
(说实话这样⼲⾮常蠢,但是⽬前我还没到替代⽅案,GitHub上的相应的issue也是还没有解决⽅案)
⽽如果是⽤kill (-9) pid的办法来退出nodeos也是⼀样的问题。(ATN:nodeos每次重启,原来创建的账户就都没了。)
总结:这⾥说明了启动本地测试⽹后,你可以暂停这个⽹络。但是如果因为某种原因结束了这个跑测试⽹的进程,你们你需要删除
~/Library/Application Support/eosio/nodeos/data下的⼀些数据才能再次启动,否则会出错,不让你重新启动。
2.2管理钱包(在CDTtutorial中已经学过)
关于钱包的⼀些列操作命令回顾:
创建钱包:cleos wallet create --to-console
打开钱包:cleos wallet open
罗列钱包:cleos wallet list
解锁钱包:cleos wallet unlock(需要⽤到私钥)
把私钥导⼊钱包:cleos wallet create_key(可以得到公钥)
查看钱包中的私钥:cleos wallet keys
钱包是什么?是存储你密钥的地⽅。
2.3管理账户
(1)创建账户:cleos create account [OPTIONS] creator name OwnerKey [ActiveKey]
注:这其实是⼀笔交易,因为花费ram。OwnerKey和ActiveKey都是公钥,说明⼀个账号被绑定了2个密钥,这是因为不同密钥的权限是不同的。OwnerKey对的权限⼤,ActiveKey对的权限⼩。EOS的权限系统也是⾮常复杂的。
这⾥你可能需要⾃⼰产⽣⼀对密钥对。通过命令:cleos create key —to-console
(2)导⼊私钥到钱包:cleos wallet import
你必须把产⽣的私钥导⼊到wallet⾥,这样你才能⽤公钥来创建账户!!直接⼿动导⼊。导⼊后,你也只能查看到了这⼏个的公钥,私钥⾃然不会显⽰,但是已经存到了wallet⾥。
这⾥最重要的是还需要把eosio的私钥导⼊到wallet⾥:
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
然后cleos create account eosio token {public-OwnerKey} {public-ActiveKey}
⽐如:
cleos create account eosio tokentest EOS5hU9GFTV4qsBCjAeRU1aMLKd2QFhpz858MXWdqoJiewZmtHEux EOS5HoU5MJ5rr1fn2aM9P4dXR1UEhHzAN GpFFNU8ZjHpEfJfeZPoP
实测:创建账户需要nodeos跑起来。并且要包含wallet_api_plugin?好像不⽤,我没⽤wallet_api_plugin,跑了其他插件,但是能够正常创建账号。
(2)查看账户:cleos get account ACCOUNTNAME –json(-j)
⽐如:
cleos get account tokentest --json
得到输出:
{
"account_name": "tokentest",
"head_block_num": 443,
"head_block_time": "2018-10-23T01:21:04.500",
"privileged": false,
"last_code_update": "1970-01-01T00:00:00.000",
"created": "2018-10-23T01:18:35.500",
"ram_quota": -1,
"net_weight": -1,
"cpu_weight": -1,
"net_limit": {
"used": -1,
"available": -1,
"max": -1
},
"cpu_limit": {
"used": -1,
"available": -1,
"max": -1
},
"ram_usage": 2724,
"permissions": [{
"perm_name": "active",
"parent": "owner",
"required_auth": {
"threshold": 1,
"keys": [{
"key": "EOS5HoU5MJ5rr1fn2aM9P4dXR1UEhHzANGpFFNU8ZjHpEfJfeZPoP",
"weight": 1
}
],
"accounts": [],
"waits": []
}
},{
"perm_name": "owner",
"parent": "",
"required_auth": {
"threshold": 1,
"keys": [{
"key": "EOS5hU9GFTV4qsBCjAeRU1aMLKd2QFhpz858MXWdqoJiewZmtHEux",
"weight": 1
}
],
"accounts": [],
"waits": []
}
}
],
"total_resources": null,
"self_delegated_bandwidth": null,
"refund_request": null,
"voter_info": null
}
也可以使⽤cleos get accounts PUBLICEKEY
来查看此⼀个公钥对应的⽤户名是否存在,要注意,这⾥加了”s“。
2.4管理智能合约
(1)编译合约:很简单
(2)部署合约:以ken为例。
先进⼊合约代码⽂件所在的⽬录:
ken
由于这⾥已经编译好了,所以不需要再重新编译,直接部署合约就⾏啦。
然后:
cleos set contract CONTRACT_ACCOUNT_NAME{path_to_contract_folder} {path_to_wasm_file} {path_to_abi_file}
以我为例:
cleos set contract tokentest . ./ken.wasm ./ken.abi
输出为:
Reading WASM from ./
executed transaction: cdcd9913fd8be204c0fa82b26082c86a1810458aca00798ac6ae564e6d9eb5e1 8104 bytes 10298 us
# eosio <= eosio::setcode {"account":"tokentest","vmtype":0,"vmversion":0,"code":"0061736d010********
# eosio <= eosio::setabi {"account":"tokentest","abi":" warning: transaction executed locally, but may not be confirmed by the network yet ]
这⾥要注意是wasm⽂件⽽不是wast⽂件,我看的教程⾥写错了。
然后你可以验证⼀下:cleos get code tokentest会得到了⼀些输出。
使⽤token这部署好的智能合约。这你要了解token合约⾥开放那些动作给你使⽤。与智能合约的交互(去中⼼化意义下的服务器)都是通过
cleos push action CONTRACT_ACCOUNT_NAME action options
的模式来进⾏的。
1)在这⾥,先来创建token,交互命令为:
cleos push action tokentest create '{"issuer":"tokentest","maximum_supply":"1000000.0000 TKN","can_freeze":"0","can_recall":"0","can_whitelist":"0"}' -p to kentest
输出为:
executed transaction: 8c6aed748071fc422bb42e306d89684ae1631e99b198ba30a9b218c21a0cdabb 120 bytes 2733 us
# tokentest <= tokentest::create {"issuer":"token","maximum_supply":"1000000.0000 TKN"}
warning: transaction executed locally, but may not be confirmed by the network yet
2)创建完了之后可以来分发token:
cleos push action tokentest issue '{"to":"tokentest","quantity":"1000.0000 TKN","memo":""}' -p tokentest
Let’s summarize all the steps so far. We first installed EOSIO and we
set up our development environment. We installed the executables
needed for contract development and we have started our first testnet
node which generates blocks on the blockchain. We learned how to
generate public/private keys and import them into our wallet. We
created an account using our public keys – OwnerKey & ActiveKey and we
deployed ken contract. As a finale, we transferred funds
using ken contract between the accounts.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论