您正在查看: EOS-新手教程 分类下的文章

开源EOS浏览器eosweb

std::string 与 abieos::public_key 互转

#include "abieos_numeric.hpp"
#include <stdio.h>
#include <cassert>

using namespace abieos;


int main() {
  std::string pubkey_str = std::string("EOS6S7kyRBUnwhKwfz41E7hCv9swCzT6AicNb7Skiz4LAeY7wG9WJ");
  abieos::public_key pubkey = string_to_public_key(pubkey_str);
  std::string decoded_pubkey_str = public_key_to_string(pubkey);
  assert(decoded_pubkey_str == pubkey_str);
  printf("Succcess!\n");
}

源代码地址:https://github.com/cppfuns/abieos-numeric-c-14/blob/master/test.cpp

EOSIO Block Producer Heartbeat

目前想检测侧链个节点的健康状况,此文将测试插件
插件源代码地址:https://github.com/LiquidEOS/eos-producer-heartbeat-plugin
前端项目地址:https://github.com/LiquidEOS/eos-producer-heartbeat

检测的原理就是,提前准备个账号,部署好收集节点信息的合约
合约地址:https://github.com/LiquidEOS/eos-producer-heartbeat/blob/master/eosio_docker/contracts/heartbeat/heartbeat.cpp

各个节点定时(heartbeat-period)将数据push到这个合约(heartbeat-contract)。

源代码编译插件

cd <eosio-source-dir>
cd plugins
git clone https://github.com/LiquidEOS/eos-producer-heartbeat-plugin.git producer_heartbeat_plugin
cd producer_heartbeat_plugin
git checkout tags/v1.5.01
cd ../..
git apply plugins/producer_heartbeat_plugin/install.patch
./eosio_build.sh -s "EOS"
sudo ./eosio_install.sh

新创建账户,并设置权限

如果是测试,这一步非必要的。

创建key

$ cleos create key --to-console
Private key: 5JPxA1FPp2fMJj2hh1ax8cuJMQchffCi3GnBcobwL5SJhyxMS9n
Public key: EOS6pZfrTRMesCnSaE1XwYSFFUtsdGeH6Ljj6xGWxFk4N5Z3T13KT

下文中 PRODUCERACCT 为出块节点账户

创建账户

cleos system newaccount PRODUCERACCT eosheartbeat EOS6pZfrTRMesCnSaE1XwYSFFUtsdGeH6Ljj6xGWxFk4N5Z3T13KT EOS6pZfrTRMesCnSaE1XwYSFFUtsdGeH6Ljj6xGWxFk4N5Z3T13KT  --stake-net '50.00 EOS' --stake-cpu '50.00 EOS'  --buy-ram-kbytes 10000

创建一个自定义的heartbeat权限

cleos set account permission PRODUCERACCT heartbeat '{"threshold":1,"keys":[{"key":"EOS6pZfrTRMesCnSaE1XwYSFFUtsdGeH6Ljj6xGWxFk4N5Z3T13KT","weight":1}]}' "active" -p PRODUCERACCT@active

将PRODUCERACCT的heartbeat权限,付给eosheartbeat,用于执行heartbeat

cleos set action permission PRODUCERACCT eosheartbeat heartbeat heartbeat

黑名单合约权限,同上

cleos set action permission PRODUCERACCT theblacklist sethash heartbeat

config添加插件支持

plugin = eosio::producer_heartbeat_plugin
heartbeat-period = 1500
heartbeat-signature-provider = HEARTBEAT_PUB_KEY=KEY:HEARTBEAT_PRIVATE_KEY
heartbeat-contract = eosheartbeat
heartbeat-permission = heartbeat
heartbeat-oncall = telegram:johndoe

稍后测试后,在做补充

EOS合约内解析json

示例代码

//demo.hpp
#pragma once

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include "json.hpp"
#include <string>

using json = nlohmann::json;
using eosio::contract;
using eosio::multi_index;
using eosio::print;

class[[eosio::contract]] demo : public contract
{
  public:
    using contract::contract;

    [[eosio::action]] void demoaction();
};
//demo.cpp
#include "demo.hpp"
using json = nlohmann::json;

// ACTION
void demo::demoaction()
{
    json demoInfo = R"(
    {
        "demoInfo": {
            "hello": "world"
        }
    }
    )"_json;
    std::string demoInfoString = demoInfo.dump();
    json demoInfoJson = json::parse(demoInfoString);
    print("It worked\n");
}

EOSIO_DISPATCH(demo, (demoaction))

json.hpp从https://github.com/nlohmann/json/releases 下载并将其放在同一目录中demo.hpp。

参考

https://github.com/EOSIO/eosio.cdt/issues/110