本文包含两部分:
1 如何在电脑上EOS Dawn 1.0版本运行起来,如果你是windows系统,这篇文章不适合你.或者你可以考虑用docker虚拟机的版本,在官方文档之中,有提到这部分: 在docker中运行EOS
有需要的地方可以留言给我.我会在另外一篇帖子之中,写一下如何使用docker跑eos. 如果我不犯懒的话.. XD
2 如何上传并执行一个示例合约Currency. 这部分中,讲一个currency智能合约如何上传到区块链,并且如何用eosc对账户进行创建账号,转账,查账操作的.
声明:官方文档为英文,建议对EOS的技术层面感兴趣的还是阅读英文文本为主.
本文主要是对官方文档的缩略概括, 不排除会有理解失误的地方.有问题和疑惑的可以留言给我,一起探讨.
要看更详尽的内容,请参考官方文档
一 编译
编译过程比较流畅,按照官方教程来即可.我在编译过程中没有遇到什么问题,如果需要帮助的话,可以留言.
官方文档
把最主要的部分列出如下:
1 构建编译/开发环境
Setting up a build/development environment
for Ubuntu
假设你把eos放在主目录中.
## download the source code and related submodules and build
cd
git clone https://github.com/eosio/eos --recursive
cd eos
./build.sh ubuntu
for Mac
xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
git clone https://github.com/eosio/eos --recursive
cd eos
./build.sh darwin
2 修改配置文件,运行节点
在编译好的目录中,找到你的genesis.json的位置
cd ~/eos/build/programs/eosd/data-dir
用你喜欢的编译器,打开data-dir之中的config.ini,
注意,
-
/path/to/eos/source/genesis.json
需要替换为你的文档中genesis.json的实际位置 - 原有的config文件中会有一行
enable-stale-production = false
, 将其改为:
enable-stale-production = true
然后,把如下的改动加入到config.ini中.
# Load the testnet genesis state, which creates some initial block producers with the default key
genesis-json = /path/to/eos/source/genesis.json
# Enable production on a stale chain, since a single-node test chain is pretty much always stale
enable-stale-production = true
# Enable block production with the testnet producers
producer-name = inita
producer-name = initb
producer-name = initc
producer-name = initd
producer-name = inite
producer-name = initf
producer-name = initg
producer-name = inith
producer-name = initi
producer-name = initj
producer-name = initk
producer-name = initl
producer-name = initm
producer-name = initn
producer-name = inito
producer-name = initp
producer-name = initq
producer-name = initr
producer-name = inits
producer-name = initt
producer-name = initu
# Load the block producer plugin, so you can produce blocks
plugin = eos::producer_plugin
# Wallet plugin
plugin = eos::wallet_api_plugin
# As well as API and HTTP plugins
plugin = eos::chain_api_plugin
plugin = eos::http_plugin
eosd是生产节点, 现在,可以启动eosd了.
cd ~/eos/build/programs/eosd/
./eosd
命令行下,会有如下的信息:
1575001ms thread-0 chain_controller.cpp:235 _push_block ] initm #1 @2017-09-04T04:26:15 | 0 trx, 0 pending, exectime_ms=0
1575001ms thread-0 producer_plugin.cpp:207 block_production_loo ] initm generated block #1 @ 2017-09-04T04:26:15 with 0 trxs 0 pending
1578001ms thread-0 chain_controller.cpp:235 _push_block ] initc #2 @2017-09-04T04:26:18 | 0 trx, 0 pending, exectime_ms=0
1578001ms thread-0 producer_plugin.cpp:207 block_production_loo ] initc generated block #2 @ 2017-09-04T04:26:18 with 0 trxs 0 pending
...
二 示例合约: Currency
EOS创建之后,会有多个程序, 可以在~/eos/build/programs
中找到它们:
eosd - 服务器端区块链节点组件 server-side blockchain node component
eosc - 命令行接口,与区块链交互用 command line interface to interact with the blockchain
eos-walletd - EOS 钱包
launcher - 用于区块链节点网络构建和部署关于launcher
这部分,将会提到如何使用eosc程序,以及如何运行一个示例的EOS智能合约
1 运行节点
cd ~/eos/build/programs/eosd/
./eosd
2 创建钱包,导入账户私钥
每个合约都需要一个关联的账户,所以,需要首先创建钱包
cd ~/eos/build/programs/eosc/
./eosc wallet create # Outputs a password that you need to save to be able to lock/unlock the wallet
在这个例子中,我们导入inita这个测试账户的私钥,在config.ini可以找到:
./eosc wallet import 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
3 为示例"Currency"合约创建账户
创建两组public/private pair, 用于owner_key和active_key
cd ~/eos/build/programs/eosc/
./eosc create key # owner_key
./eosc create key # active_key
上述命令,会产生两组如下的
Private key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Public key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
注意,要把产生的key存好,后面会用到.
Run thecreate command where and PUBLIC_KEY_1andPUBLIC_KEY_2are the values generated by thecreate keycommand
运行创建账户的命令, 账户inita授权创建currency这个账户 (inita is the account authorizing the creation of thecurrencyaccount), 而PUBLIC_KEY_1 PUBLIC_KEY_2 这两个,是前面的create key命令所产生的公钥.
./eosc create account inita currency PUBLIC_KEY_1 PUBLIC_KEY_2
You should then get a json response back with a transaction ID confirming it was executed successfully.
Go ahead and check that the account was successfully created
你会看到一个json返回结果, 带有transaction ID,确认账户已经创建成功.
如下命令用于检查账户是否创建成功
./eosc get account currency
如果一切顺利,你会看到如下的返回结果:
{ "name": "currency", "eos_balance": 0, "staked_balance": 1, "unstaking_balance": 0, "last_unstaking_time": "2106-02-07T06:28:15"}
现在,导入active private key到钱包.
还记得上面我们创建了两组public/private key么? 第一组是owner key, 第二组是active key.
这里提到的active private key, 指的是第二组active key的私钥.
./eosc wallet import XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
4 上传示例合约"currency"到区块链
在上传智能合约前,确认一下账户里面没有合约:
./eosc get code currency code
返回结果:
hash: 0000000000000000000000000000000000000000000000000000000000000000
现在,可以上传智能合约了:
./eosc set contract currency ../../contracts/currency/currency.wast ../../contracts/currency/currency.abi
我在这里的时候遇到了一个坑.因为先前编译时候,没等全部工作完成,就直接开始运行eosc,上传智能合约了,导致在contrancts/currency文件夹中,没有找到编译好的currency.wast文件.
耐心等待第一步的./build.sh ubuntu
命令运行完成,再来执行智能合约的上传命令,就可以了.
执行了上传智能合约的命令后,会看到json格式的返回结果,其中有transaction_id. 现在,示例合约已经上传成功了.
可以通过如下命令来确认代码上传完成:
./eosc get code currency
返回结果类似:
code hash: 9b9db1a7940503a88535517049e64467a6e8f4e9e03af15e9968ec89dd794975
下一步,需要确认currency contract的初始账户余额:
./eosc get table currency
结果:
currency account{ "rows": [{ "account": "account", "balance": 1000000000 } ], "more": false}
5 用示例的currency contract操作转账
所有人都可以在任何时候向任意的账户发送任意信息,但是,如果发送的信息缺少必要的权限,智能合约可能会拒绝掉该信息.信息不是"从"任何人那里发出的,而是在一个或者更多的账户的"允许"下,在对应的权限级别下,发送的( they are sent "with permission of" one or more accounts and permission levels).
如下的命令展示了一条转账信息是如何发送到"currency"合约的.
信息的内容是'{"from":"currency","to":"inita","amount":50}'
在这里例子中,我们要求currency 合约把资金从自身账户转移到其它账户.这需要得到currency 合约的许可.
命令如下:
./eosc push message currency transfer '{"from":"currency","to":"inita","amount":50}' --scope currency,inita --permission currency@active
如下是转账信息的一般格式,可以看出,currency 账户只被引用了一次,用于指明应该向哪个合约发送该条交易信息.
./eosc push message currency transfer '{"from":"${usera}","to":"${userb}","amount":50}' --scope ${usera},${userb} --permission ${usera}@active
这里我们设定--scope ... 参数,是为了给予currency contract读写权限,使其可以修改相应用户的账户余额(to give the currency contract read/write permission to those users so it can modify their balances). 之后的版本中,scope会自动确认.
提交交易信息后,会收到包含transaction_id记录的json格式的返回结果,确认交易成功.
6. 读取示例合约"currency"的账户余额
现在查看一下前面的交易中两个账户的状态.
So now check the state of both of the accounts involved in the previous transaction.
./eosc get table inita currency account
返回结果: { "rows": [{ "account": "account", "balance": 50 } ], "more": false}
./eosc get table currency currency account
返回结果: { "rows": [{ "account": "account", "balance": 999999950 } ], "more": false}
As expected, the receiving accountinitanow has a balance of50tokens, and the sending account now has50less tokens than its initial supply.
现在,转账的接收方账户inita有50代币,而转出方账户现在比转账前少了50代币.