以太坊c++版本rpc接口添加

安装依赖库

apt install libargtable2-dev
apt install libhiredis-dev

升级jsonrcpcpp,修改CMakeLists.txt

DOWNLOAD_NAME jsonrcpcpp-1.0.0.tar.gz
DOWNLOAD_NO_PROGRESS 1
URL https://github.com/cinemast/libjson-rpc-cpp/archive/v1.0.0.tar.gz
URL_HASH SHA256=888c10f4be145dfe99e007d5298c90764fb73b58effb2c6a3fc522a5b60a18c6
为:
DOWNLOAD_NAME jsonrcpcpp-1.0.0.tar.gz
DOWNLOAD_NO_PROGRESS 1
URL https://github.com/cinemast/libjson-rpc-cpp/archive/v1.0.0.tar.gz
URL_HASH SHA256=888c10f4be145dfe99e007d5298c90764fb73b58effb2c6a3fc522a5b60a18c6

1:在cmake集成JsonRpcCpp

把ProjectJsonRpcCpp.cmake 和 FindMHD.cmake 两个文件拷贝到cmake 目录

2: 添加 HttpServer 源代码

SafeHttpServer.cpp
SafeHttpServer.h

3:集成HttpJsonRpc

添加遍历定义:
int jsonRPCURL = -1;
bool adminViaHttp = false;
std::string rpcCorsDomain = "";
std::string httpsKey = "";
std::string httpsCert = "";

添加参数处理:

if (vm.count("json-rpc-port"))
    {
        jsonRPCURL = vm["json-rpc-port"].as<short>();
    }
    if (vm.count("admin-via-http"))
    {
        string m = vm["admin-via-http"].as<string>();
        if (isTrue(m))
            adminViaHttp = true;
        else if (isFalse(m))
            adminViaHttp = false;
        else
        {
            cerr << "Bad " << "--admin-via-http" << " option: " << m << "\n";
            return -1;
        }
    }
    if (vm.count("rpccorsdomain"))
    {
        rpcCorsDomain = vm["rpccorsdomain"].as<string>();

        string::size_type pos = rpcCorsDomain.find_first_not_of('"');
        if (pos != string::npos)
        {
            rpcCorsDomain = rpcCorsDomain.substr(pos);
        }

        pos = rpcCorsDomain.find_last_not_of('"');
        if (pos != string::npos)
        {
            rpcCorsDomain = rpcCorsDomain.substr(0, pos+1);
        }

        cdebug << "rpcCorsDomain=" << rpcCorsDomain;
    }
    if (vm.count("https_key"))
    {
        httpsKey = vm["rpccorsdomain"].as<string>();
        cdebug << "httpsKey=" << httpsKey;
    }
    if (vm.count("https_cert"))
    {
        httpsCert = vm["rpccorsdomain"].as<string>();
        cdebug << "httpsCert=" << httpsCert;
    }

在程序启动时启动httpJsonRpc 服务器

if (jsonRPCURL > -1)
    {
        using FullServer = ModularServer<
            rpc::EthFace,
            rpc::NetFace, rpc::Web3Face, rpc::PersonalFace,
            rpc::AdminEthFace, rpc::AdminNetFace,
            rpc::DebugFace, rpc::TestFace
        >;

        rpc::AdminEth* adminEth = nullptr;
        rpc::PersonalFace* personal = nullptr;
        rpc::AdminNet* adminNet = nullptr;
        if (adminViaHttp)
        {
            personal = new rpc::Personal(keyManager, *accountHolder, *web3.ethereum());
            adminEth = new rpc::AdminEth(*web3.ethereum(), *gasPricer.get(), keyManager, *sessionManager.get());
            adminNet = new rpc::AdminNet(web3, *sessionManager.get());
        }

        auto ethFace = new rpc::Eth(*web3.ethereum(), *accountHolder.get());
        rpc::TestFace* testEth = nullptr;
        if (testingMode)
            testEth = new rpc::Test(*web3.ethereum());

        jsonrpcHttpServer.reset(new FullServer(
            ethFace,
            new rpc::Net(web3), new rpc::Web3(web3.clientVersion()), personal,
            adminEth, adminNet,
            new rpc::Debug(*web3.ethereum()),
            testEth
        ));

        unsigned SensibleHttpThreads = 1;
        auto httpConnector = new SafeHttpServer(jsonRPCURL, httpsKey, httpsCert, SensibleHttpThreads);
        httpConnector->setAllowedOrigin(rpcCorsDomain);
        jsonrpcHttpServer->addConnector(httpConnector);
        jsonrpcHttpServer->StartListening();

        cout << "JSONRPC Admin Session Key: " << jsonAdmin << "\n";
    }

结束时关闭服务器

if (jsonrpcHttpServer.get())
        jsonrpcHttpServer->StopListening();

修改libweb3jsonrpc/CMakeLists.txt 文件

target_link_libraries(web3jsonrpc ethashseal webthree libjson-rpc-cpp::server) 
改为
target_link_libraries(web3jsonrpc ethashseal webthree libjson-rpc-cpp::server JsonRpcCpp::Server) 

set(sources AccountHolder.cpp
    AccountHolder.h
    AdminEth.cpp
    AdminEth.h
    AdminEthFace.h
    AdminNet.cpp
    AdminNet.h
    AdminNetFace.h
    Debug.cpp
    Debug.h
    DebugFace.h
    Eth.cpp
    Eth.h
    EthFace.h
    IpcServer.h
    IpcServerBase.cpp
    IpcServerBase.h
    JsonHelper.cpp
    JsonHelper.h
    ModularServer.h
    Net.cpp
    Net.h
    NetFace.h
    Personal.cpp
    Personal.h
    PersonalFace.h
    SessionManager.cpp
    SessionManager.h
    Test.cpp
    Test.h
    TestFace.h
    Web3.cpp
    Web3.h
    Web3Face.h )
改为
set(sources AccountHolder.cpp
    AccountHolder.h
    AdminEth.cpp
    AdminEth.h
    AdminEthFace.h
    AdminNet.cpp
    AdminNet.h
    AdminNetFace.h
    Debug.cpp
    Debug.h
    DebugFace.h
    Eth.cpp
    Eth.h
    EthFace.h
    IpcServer.h
    IpcServerBase.cpp
    IpcServerBase.h
    JsonHelper.cpp
    JsonHelper.h
    ModularServer.h
    Net.cpp
    Net.h
    NetFace.h
    Personal.cpp
    Personal.h
    PersonalFace.h
    SessionManager.cpp
    SessionManager.h
    Test.cpp
    Test.h
    TestFace.h
    Web3.cpp
    Web3.h
    Web3Face.h
    SafeHttpServer.cpp
    SafeHttpServer.h )

最终编译命令

/usr/bin/c++  -O2 -g main.cpp -o aleth ../libethereum/libethereum.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_program_options-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_program_options-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libintx.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_fiber-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_context-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_filesystem-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_system-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_log-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libboost_thread-mt-x64.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libethash.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libkeccak.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libleveldb.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libsnappy.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libcrc32c.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libcryptopp.a -lpthread /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libscrypt.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libjsonrpccpp-server.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libjsonrpccpp-common.a ../deps/lib/libjsonrpccpp-server.a ../deps/lib/libjsonrpccpp-common.a /root/.hunter/_Base/c022f0c/af070fb/93b97cb/Install/lib/libjsoncpp.a /usr/lib/x86_64-linux-gnu/libmicrohttpd.so -lhiredis -largtable2 -lgcov 

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 ancjf@163.com

文章标题:以太坊c++版本rpc接口添加

本文作者:ancjf

发布时间:2018-09-15, 16:06:05

最后更新:2020-09-15, 09:09:25

原始链接:http://ancjf.com/2018/09/15/%E4%BB%A5%E5%A4%AA%E5%9D%8Ac++%E7%89%88%E6%9C%ACrpc%E6%8E%A5%E5%8F%A3%E6%B7%BB%E5%8A%A0/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏