1、docker获取eosio镜像:
(1)官网获取:这种方式跟自己网络有很大关系,有的网不好的很难下载下来,所以推荐方式(2)
docker pull eosio/eos:v1.4.2
(2)从以下获取镜像的.tar文件,放到Ubuntu中,
其中的eosio.tar下载地址为:
压缩镜像文件下载地址
使用如下命令导入为镜像文件即可,需要等待一两分钟时间。
docker load --input ./eosio.tar
docker images
使用此命令查看导入镜像如下:
说明镜像导入成功。
2、启动镜像:
docker run --name eosio \
--publish 7777:7777 \
--publish 192.168.124.128:6666:6666 \
--volume /home/wxp/contracts:/home/wxp/contracts \
--detach \
eosio/eos:v1.4.2 \
/bin/bash -c \
"keosd --http-server-address=0.0.0.0:6666 --verbose-http-errors & exec nodeos -e -p eosio \
--plugin eosio::producer_plugin \
--plugin eosio::chain_api_plugin \
--plugin eosio::history_plugin \
--plugin eosio::history_api_plugin \
--plugin eosio::http_plugin -d /mnt/dev/data --config-dir /mnt/dev/config --http-server-address=0.0.0.0:7777 --access-control-allow-origin=* --contracts-console --http-validate-host=false --filter-on='*'"
查看启动容器是否已经运行:
docker ps
成功启动eosio镜像后,看到如下图所示:
查看容器日志:
docker logs eosio
访问节点
访问节点信息:
curl http://127.0.0.1:7777/v1/chain/get_info
返回节点信息:
{"server_version":"60c8bace","chain_id":"cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f","head_block_num":3689,"last_irreversible_block_num":3688,"last_irreversible_block_id":"00000e68002c0b7bb81d4d9f85b7e9aba45c9762ecd61a93ba821c8a45b975d8","head_block_id":"00000e696a3f665cdb678ea14776df80de32477c217e419152c0b1f0b20dde0b","head_block_time":"2018-12-26T09:05:45.500","head_block_producer":"eosio","virtual_block_cpu_limit":7988105,"virtual_block_net_limit":41964237,"block_cpu_limit":199900,"block_net_limit":1048576,"server_version_string":"v1.4.2"}
设置别名以后就可以使用cleos访问节点,但是别名需要持久化,不然重启就不存在:
alias cleos='docker exec -it eosio /opt/eosio/bin/cleos --url http://127.0.0.1:7777 --wallet-url http://127.0.0.1:6666'
持久化别名:在~/.bashrc这个文件最好一行添加 :
alias cleos='docker exec -it eosio /opt/eosio/bin/cleos --url http://127.0.0.1:7777 --wallet-url http://127.0.0.1:6666'
sudo vim ~/.bashrc
source ~/.bashrc
source 目的使修改内容生效。
3、安装编译工具CDT
wget https://github.com/eosio/eosio.cdt/releases/download/v1.3.2/eosio.cdt-1.3.2.x86_64.deb
sudo apt install ./eosio.cdt-1.3.2.x86_64.deb
测试是否可以使用,写一个helloword合约代码。
#include
#include
using namespace eosio;
class hello : public contract {
public:
using contract::contract;
[[eosio::action]]
void hi( name user ) {
print( "Hello, ", user);
}
};
EOSIO_DISPATCH( hello, (hi))
编译文件命令:
sudo eosio-cpp -o hello.wasm hello.cpp -abigen
出现hello.abi/hello.wasm/hello.cpp这个三个文件说明编译成功:
4、部署hello合约到本地环境。
(1) 创建默认钱包:
cleos wallet create --to-console
Creating wallet: default
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5JgHL1PYAvjLA4JxaacoKP9nw1FzXZQdAHyafMsGCt4Qfg4Jyhn"
其中这个PW5JgHL1PYAvjLA4JxaacoKP9nw1FzXZQdAHyafMsGCt4Qfg4Jyhn这个秘钥要保存好,需要用来解锁钱包。
(2)创建秘钥对,作为账户秘钥:
cleos create key --to-console
Private key: 5K8MSLiziqYcrHTMqnyAU2Tkgq8pYbt7wRmjMPPfjz2wg5iSTjm
Public key: EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC
(3)导入刚才创建的5K8MSLiziqYcrHTMqnyAU2Tkgq8pYbt7wRmjMPPfjz2wg5iSTjm这个秘钥
wxp@ubuntu:~/contracts/hello$ cleos wallet import
private key: imported private key for: EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC
这里必须导入eosio秘钥5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3,这个用来创建账户,其中eosio秘钥是系统中配置的,不需要自己生成。
wxp@ubuntu:~/contracts/hello$ cleos wallet import
private key: imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
查看导入的秘钥:
wxp@ubuntu:~/contracts/hello$ cleos wallet keys
[
"EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC",
"EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
]
(4)创建账户
wxp@ubuntu:~/contracts/hello$ cleos create account eosio bob EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC
executed transaction: 23d7d32edd060688a3db1b5f0c766cbd19f64428144a79a1eb52b4681a4ba702 200 bytes 7737 us
# eosio <= eosio::newaccount {"creator":"eosio","name":"bob","owner":{"threshold":1,"keys":[{"key":"EOS5J8FYcp9yb8zHQXSVvGFU4ahPH...
warning: transaction executed locally, but may not be confirmed by the network yet
其中的EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC这个是公钥(2)中的创建秘钥对公钥。
warning: transaction executed locally, but may not be confirmed by the network yet 这个警告账户创建成功。
一个秘钥对可以同时创建多个账户。
(5)查看账户信息:
wxp@ubuntu:~/contracts/hello$ cleos get account bob
created: 2018-12-26T09:17:15.500
permissions:
owner 1: 1 EOS5J8FYcp9yb8zHQXSVvGFU4ahPH7weVGnvVqz6vT8nUynA9WWFp
active 1: 1 EOS5J8FYcp9yb8zHQXSVvGFU4ahPH7weVGnvVqz6vT8nUynA9WWFp
memory:
quota: unlimited used: 2.66 KiB
net bandwidth:
used: unlimited
available: unlimited
limit: unlimited
cpu bandwidth:
used: unlimited
available: unlimited
limit: unlimited
(6)部署合约到本地环境:
xp@ubuntu:~/contracts/hello$ cleos set contract bob /home/wxp/contracts/hello
Reading WASM from /home/wxp/contracts/hello/hello.wasm...
Publishing contract...
executed transaction: 7276772e0998e4fbf6f3711d4ccaaf6f8d751cd66b49db3931fd7ee30ed8bb44 2576 bytes 2945 us
# eosio <= eosio::setcode {"account":"bob","vmtype":0,"vmversion":0,"code":"0061736d0100000001771660037f7e7f0060027f7e00600000...
# eosio <= eosio::setabi {"account":"bob","abi":"0e656f73696f3a3a6162692f312e30000202686900020475736572046e616d65016105617373...
warning: transaction executed locally, but may not be confirmed by the network yet ]
注意:这个路径/home/wxp/contracts/hello,是在启动容器 --volume /home/wxp/contracts:/home/wxp/contracts \这个设置的,一定要必须在本地环境设置一致,不然部署不成功,找不到wasm文件。
(7)测试部署合约:
wxp@ubuntu:~/contracts/hello$ cleos push action bob hi '["bob"]' -p bob
executed transaction: f68d38e8b17ef83aa843c4da7e9892b47adc01c3d46a63fe3a61ef7891fe9afb 104 bytes 414 us
# bob <= bob::hi {"user":"bob"}
>> Hello, bob
warning: transaction executed locally, but may not be confirmed by the network yet ]
>> Hello, bob 打印这个说明测试成功。