使用docker快速在Ubuntu上安装eosio本地测试环境

1、docker获取eosio镜像:

(1)官网获取:这种方式跟自己网络有很大关系,有的网不好的很难下载下来,所以推荐方式(2)

1docker pull eosio/eos:v1.4.2 2

(2)从以下获取镜像的.tar文件,放到Ubuntu中,

其中的eosio.tar下载地址为:

压缩镜像文件下载地址

使用如下命令导入为镜像文件即可,需要等待一两分钟时间。

1docker load --input ./eosio.tar 2
1docker images 2使用此命令查看导入镜像如下: 3

说明镜像导入成功。

2、启动镜像:

1docker run --name eosio \ 2 --publish 7777:7777 \ 3 --publish 192.168.124.128:6666:6666 \ 4 --volume /home/wxp/contracts:/home/wxp/contracts \ 5 --detach \ 6 eosio/eos:v1.4.2 \ 7 /bin/bash -c \ 8 "keosd --http-server-address=0.0.0.0:6666 --verbose-http-errors & exec nodeos -e -p eosio \ 9 --plugin eosio::producer_plugin \ 10 --plugin eosio::chain_api_plugin \ 11 --plugin eosio::history_plugin \ 12 --plugin eosio::history_api_plugin \ 13 --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='*'" 14 15

 查看启动容器是否已经运行:

1docker ps 2 3

成功启动eosio镜像后,看到如下图所示:

查看容器日志:

1docker logs eosio 2 3

 

 访问节点

1访问节点信息: 2curl http://127.0.0.1:7777/v1/chain/get_info 3返回节点信息: 4{"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"} 5

设置别名以后就可以使用cleos访问节点,但是别名需要持久化,不然重启就不存在:

1alias cleos='docker exec -it eosio /opt/eosio/bin/cleos --url http://127.0.0.1:7777 --wallet-url http://127.0.0.1:6666' 2 3

持久化别名:在~/.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'

1sudo vim ~/.bashrc 2source ~/.bashrc 3

source 目的使修改内容生效。

3、安装编译工具CDT 

1wget https://github.com/eosio/eosio.cdt/releases/download/v1.3.2/eosio.cdt-1.3.2.x86_64.deb 2sudo apt install ./eosio.cdt-1.3.2.x86_64.deb 3

测试是否可以使用,写一个helloword合约代码。

1#include <eosiolib/eosio.hpp> 2#include <eosiolib/print.hpp> 3 4using namespace eosio; 5 6class hello : public contract { 7 public: 8 using contract::contract; 9 10 [[eosio::action]] 11 void hi( name user ) { 12 print( "Hello, ", user); 13 } 14}; 15 16EOSIO_DISPATCH( hello, (hi)) 17

编译文件命令: 

1sudo eosio-cpp -o hello.wasm hello.cpp -abigen 2

出现hello.abi/hello.wasm/hello.cpp这个三个文件说明编译成功: 

4、部署hello合约到本地环境。

(1) 创建默认钱包:

1cleos wallet create --to-console 2 3Creating wallet: default 4Save password to use in the future to unlock this wallet. 5Without password imported keys will not be retrievable. 6"PW5JgHL1PYAvjLA4JxaacoKP9nw1FzXZQdAHyafMsGCt4Qfg4Jyhn" 7

其中这个PW5JgHL1PYAvjLA4JxaacoKP9nw1FzXZQdAHyafMsGCt4Qfg4Jyhn这个秘钥要保存好,需要用来解锁钱包。

(2)创建秘钥对,作为账户秘钥:

1cleos create key --to-console 2Private key: 5K8MSLiziqYcrHTMqnyAU2Tkgq8pYbt7wRmjMPPfjz2wg5iSTjm 3Public key: EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC 4 5

(3)导入刚才创建的5K8MSLiziqYcrHTMqnyAU2Tkgq8pYbt7wRmjMPPfjz2wg5iSTjm这个秘钥

1wxp@ubuntu:~/contracts/hello$ cleos wallet import 2private key: imported private key for: EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC 3 4

 这里必须导入eosio秘钥5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3,这个用来创建账户,其中eosio秘钥是系统中配置的,不需要自己生成。

1wxp@ubuntu:~/contracts/hello$ cleos wallet import 2private key: imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV 3 4

查看导入的秘钥:

1wxp@ubuntu:~/contracts/hello$ cleos wallet keys 2[ 3 "EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC", 4 "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV" 5] 6 7

(4)创建账户

1wxp@ubuntu:~/contracts/hello$ cleos create account eosio bob EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC 2executed transaction: 23d7d32edd060688a3db1b5f0c766cbd19f64428144a79a1eb52b4681a4ba702 200 bytes 7737 us 3# eosio <= eosio::newaccount {"creator":"eosio","name":"bob","owner":{"threshold":1,"keys":[{"key":"EOS5J8FYcp9yb8zHQXSVvGFU4ahPH... 4warning: transaction executed locally, but may not be confirmed by the network yet 5

 

其中的EOS4zX9qr1iw9bPX3FFBCugjWwUPM4aSRAUeyvUoPt3Hzut9eNgsC这个是公钥(2)中的创建秘钥对公钥。

warning: transaction executed locally, but may not be confirmed by the network yet 这个警告账户创建成功。

一个秘钥对可以同时创建多个账户。

(5)查看账户信息:

1wxp@ubuntu:~/contracts/hello$ cleos get account bob 2created: 2018-12-26T09:17:15.500 3permissions: 4 owner 1: 1 EOS5J8FYcp9yb8zHQXSVvGFU4ahPH7weVGnvVqz6vT8nUynA9WWFp 5 active 1: 1 EOS5J8FYcp9yb8zHQXSVvGFU4ahPH7weVGnvVqz6vT8nUynA9WWFp 6memory: 7 quota: unlimited used: 2.66 KiB 8 9net bandwidth: 10 used: unlimited 11 available: unlimited 12 limit: unlimited 13 14cpu bandwidth: 15 used: unlimited 16 available: unlimited 17 limit: unlimited 18

(6)部署合约到本地环境:

1xp@ubuntu:~/contracts/hello$ cleos set contract bob /home/wxp/contracts/hello 2Reading WASM from /home/wxp/contracts/hello/hello.wasm... 3Publishing contract... 4executed transaction: 7276772e0998e4fbf6f3711d4ccaaf6f8d751cd66b49db3931fd7ee30ed8bb44 2576 bytes 2945 us 5# eosio <= eosio::setcode {"account":"bob","vmtype":0,"vmversion":0,"code":"0061736d0100000001771660037f7e7f0060027f7e00600000... 6# eosio <= eosio::setabi {"account":"bob","abi":"0e656f73696f3a3a6162692f312e30000202686900020475736572046e616d65016105617373... 7warning: transaction executed locally, but may not be confirmed by the network yet ] 8 9

注意:这个路径/home/wxp/contracts/hello,是在启动容器  --volume /home/wxp/contracts:/home/wxp/contracts \这个设置的,一定要必须在本地环境设置一致,不然部署不成功,找不到wasm文件。

(7)测试部署合约:

1wxp@ubuntu:~/contracts/hello$ cleos push action bob hi '["bob"]' -p bob 2executed transaction: f68d38e8b17ef83aa843c4da7e9892b47adc01c3d46a63fe3a61ef7891fe9afb 104 bytes 414 us 3# bob <= bob::hi {"user":"bob"} 4>> Hello, bob 5warning: transaction executed locally, but may not be confirmed by the network yet ] 6 7

Hello, bob 打印这个说明测试成功。

代码交流 2021