1、下载eos源码
git clone https://github.com/eosio/eos--recursive
对于git clone,–recursive选项,会自动初始化并更新仓库中的每一个子模块。递归克隆该项目下的所有项目。
克隆完成后,进入克隆的eos项目目录。eosio_build.sh这个shell脚本文件,可以帮我们直接安装好所有的依赖,并编译。EOS使用C++开发,CMake编译,依赖于Clang、CMake、Boost、OpenSSL、LLVM、secp256k1-zkp、MongoDB等。通过下面的命令执行这个文件。
2、./eosio_build.sh
3、./eosio_install.sh
4、启动node节点
./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin
如果遇到 Block log was not setup properly with genesis information
在~/.local/share目录下删除工程目录(该目录重启会重新生成)
rm -rf eosio
再次启动
默认的配置和数据存储路径 ~/.local/share
5、启动钱包文件
nohup ./keosd --http-server-address=127.0.0.1:8889 &
6、创建钱包
work@sh-gdex-08:/opt/soft/eos/eos/build/programs/cleos$ ./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.
"PW5K8RXu5ABXcapr29E3Sd8qUfcgV83172Jfw8UFGxb7hU7DLNW6g"
解锁钱包
./cleos --url http://127.0.0.1:8888 --wallet-url http://127.0.0.1:8889 wallet unlock --password PW5K8RXu5ABXcapr29E3Sd8qUfcgV83172Jfw8UFGxb7hU7DLNW6g
7、创建密钥对
work@sh-gdex-08:/opt/soft/eos/eos/build/programs/cleos$ ./cleos create key --to-console
Private key: 5KghVifeM9W4yRGgkUUS6hZa6iX3ZpdswQJDL9acByQwa3QU7Dx
Public key: EOS8PDcNojAswBkd6iWX4DdnB2z6KB5j4Z8thLXUg2dXZ3NBrqKK3
8、配置文件中的 公私钥
signature-provider = EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
9、导入私钥
work@sh-gdex-08:/opt/soft/eos/eos/build/programs/cleos$ ./cleos wallet import
private key: imported private key for: EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
10、部署智能合约
work@sh-gdex-08:/opt/soft/eos/eos/build/programs/cleos$ ./cleos set contract eosio ../../../build/contracts/eosio.bios -p eosio
Reading WASM from /opt/soft/eos/eos/build/contracts/eosio.bios/eosio.bios.wasm...
Publishing contract...
executed transaction: 0e17b9508357b784b8843ddb0fca8703c9ff4bf2b32095a6b0e82ddc59b6b01e 4352 bytes 1209 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 ]
编译的时候 没有完全编译 所以一些可执行文件没有生成成功 故从其他服务器上copy过来已经有的
scp eosiocpp work@sh-gdex-08:/opt/soft/eos/eos/build/tools
1、在/opt/soft/eos/eos/build/programs/cleos下面创建了Hello文件夹 在Hello文件夹下面创建了一个 Hello.cpp 文件
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
//所有的智能合约都继承自contract类
class Hello : public eosio::contract {
public:
using contract::contract;
/// @abi action
void hi( account_name user ) {
print( "Hello, ", name{user} );
}
};
EOSIO_ABI( Hello, (hi) )
2、生成.wast文件
../../tools/eosiocpp -o ./Hello/Hello.wast ./Hello/Hello.cpp
但一直报错 说 找不到 eosiolib/eosio.hpp 这个文件
该文件源码所在的目录:/opt/soft/eos/eos/contracts/eosiolib
编译之后的目录:/opt/soft/eos/eos/build/contracts/eosiolib
安装之后的目录:/usr/local/eosio/include/eosio
编译之后的目录存在eosiolib库但库中没有eosio.hpp文件
故将此文件从源码中拷贝了过来
还是报同样的错
安装目录中没有eosiolib库 故怀疑在编译Hello.cpp文件的时候从安装目录读取的
所以把eosiolib库从编译之后的目录中拷贝安装目录
还是报同样的错
然后我在build目录下 执行 sudo make install 都执行ok 但还是没有解决这个问题
最后发现了问题的原因 其实很简单 因为你当前cpp文件引入 hpp文件 写的是相对路径 你随便放入一个目录 当然找不到该文件啦
3、生成./abi文件
./build/tools/eosiocpp -g ./Hello/Hello.abi ./Hello/Hello.cpp
4、部署合约
./build/programs/cleos/cleos wallet unlock --password
./build/programs/cleos/cleos set contract test Hello
Reading WASM from Hello/Hello.wasm...
Publishing contract...
executed transaction: cb3600150671ba433b2f9bfe09937e63f2617c37155c2e5f1cbc46112ac62472 1792 bytes 14128 us
# eosio <= eosio::setcode {"account":"test","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e60027e7e0...
# eosio <= eosio::setabi {"account":"test","abi":"0e656f73696f3a3a6162692f312e30000102686900010475736572046e616d6501000000000...
warning: transaction executed locally, but may not be confirmed by the network yet ]
注:之所以新建一个
Hello
目录是因为 EOS 要求合约所在的目录必须与合约的名字相同。
./build/programs/cleos/cleos get code test
code hash: 1d0070ffc528f1268ad1f6c5ba998245811318bdcfd8827432d56e24c69a476c
./build/programs/cleos/cleos push action test hi '{"user":"eos"}' -p test
executed transaction: 885a1f667a63658f5abe13435ea6989d19672fade8badc2e8544480b4031a271 104 bytes 1742 us
# test <= test::hi {"user":"eos"}
warning: transaction executed locally, but may not be confirmed by the network yet ]
但是此时你却看不到我们的合约打印信息,怎么会事呢?原来,默认情况下,系统是不在控制下打印的,为了在控制台上看到我们合约的输出,要在启动节点时加上 --contracts-console
,完整的命令如下:
./build/programs/nodeos/nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --contracts-console
现在再执行一次上面调用合约的命令,就可以看到下图中打印的内容:
同时在节点运行的控制台上也可以看到下图,要特别注意红框中的内容,那里就是我们合约执行的一些内容。