【刘文彬】【源码解读】EOS测试插件:txn_test_gen_plugin.cpp

原文连接:醒者呆的博客园,https://www.cnblogs.com/Evswa...html

本文内容本属于《【精解】EOS TPS 多维实测》的内容,但因为在编写时篇幅过长,因此我决定将这一部分单独成文撰写,以便于理解。

关键字:eos, txn_test_gen_plugin, signed_transaction, ordered_action_result, C++, EOS插件算法

txn_test_gen_plugin 插件

这个插件是官方开发用来测试块打包交易量的,这种方式因为是直接系统内部调用来模拟transaction,没有中间通信的损耗,所以效率是很是高的,官方称经过这个插件测试到了8000的tps结果,而就个人测试结果来说,没有这么恐怖,但也能到2000了,熟不知,其余的测试手段,例如cleos,eosjs可能只有百级的量。下面,咱们一同来研究一下这个插件是如何实现以上功能的,过程当中,咱们也会思考EOS插件的架构体系,以及实现方法。经过本文的学习,若是有好的想法,咱们也能够本身开发一个功能强大的插件pr给eos,为EOS社区作出咱们本身的贡献。json

关于txn_test_gen_plugin插件的使用,很是易于上手,本文不作分析,这方面能够直接参考官方文档。

插件的总体架构

插件代码总体结构中,咱们上面介绍的核心功能的实现函数都是包含在一个结构体struct txn_test_gen_plugin_impl中。剩余的其余代码都是对插件自己的通信进行描述,包括如何调用,如何响应等,以及整个插件的生命周期的控制:网络

  • set_program_options,设置参数的阶段,是最开始的阶段,内容只设置了txn-reference-block-lag的值,默认是0,-1表明最新头区块。
  • plugin_initialize,这一时期就把包含核心功能的结构体txn_test_gen_plugin_impl加载到程序运行时内存中了,同时初始化标志位txn_reference_block_lag为txn-reference-block-lag的值。
  • plugin_startup,咱们经过基础插件http_plugin的支持得到了http接口的能力,这一时期,就暴露出来本插件的对外接口。
  • plugin_shutdown,调用stop_generation函数,重置标志位running为false,计时器关闭,打印关闭提示日志。

下面是对外暴露的三个接口之一的stop_generation函数的源码:架构

void stop_generation() {
    if(!running)
        throw fc::exception(fc::invalid_operation_exception_code);
    timer.cancel();
    running = false;
    ilog("Stopping transaction generation test");
}

接下来,咱们主要集中精力在结构体txn_test_gen_plugin_impl上,研究路线是以剩余两个接口分别为入口进行逐一分析。app

create_test_accounts 接口

关于这个接口,调用方法是curl

curl --data-binary '["eosio", "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"]' http://localhost:8888/v1/txn_test_gen/create_test_accounts

传入的参数是eosio以及其私钥。咱们进入到函数create_test_accounts中去分析源码。异步

准备知识

首先,整个函数涉及到的全部transaction都是打包存入到一个vector集合std::vector中去。async

trxs是一个事务集,它包含不少的trx,而其中每个trx包含一个actions集合vector

1、准备帐户

trxs的第一个trx,内容为帐户建立:函数

  • 定义3个帐户:txn.test.a,txn.test.b, txn.test.t
  • 辅助功能:controller& cc = app().get_plugin<chain_plugin>().chain();,经过cc能够随时调用本地区块链上的任意信息。
  • 经过fc::crypto::private_key::regenerate函数分别生成他们的私钥,要传入生成秘钥的seed。
  • 经过私钥直接调用get_public_key()便可得到公钥
  • 设置每一个帐户的owner和active权限对应的公钥,通常来说他们是相同的
  • 帐户的建立者均为咱们外部调用create_test_accounts接口时传入的帐户eosio,注意:eosio的私钥是经过字符串传入的,要经过fc::crypto::private_key转换成私钥对象
  • 将每个帐户的建立组装好成为一个action,存入trx的actions集合中去。
  • trx的actions成员已经设置完毕,完成剩余trx的组装工做,包括

    • expiration,经过cc得到当前头区块的时间,加上延迟时间,这里是30s,fc::seconds(30)
    • reference_block,值为经过cc获取当前的头区块,意思为本transaction的引用区块,全部的信息是引用的这个区块为头区块的环境
    • sign,签名,使用的是建立者eosio的私钥对象,上面咱们已经准备好了,签名的数据是data的摘要

      • 当前trx的actions中的元素的data并非如文首的transaction中的data的加密串的结构,而是明文的,这里的加密是数字摘要技术,感兴趣的朋友能够去《应用密码学初探》进行了解。
      • 摘要的源码函数是:sig_digest(chain_id, context_free_data),其中参数使用到了chain_id,而context_free_data就是上面提到的明文data内容,因此它是要与链id一块儿作数字摘要的(这一点我在使用eosjs尝试本身作摘要的时候并未想到)

这一部分的源码展现以下:

name newaccountA("txn.test.a");
name newaccountB("txn.test.b");
name newaccountC("txn.test.t");
name creator(init_name);

abi_def currency_abi_def = fc::json::from_string(eosio_token_abi).as<abi_def>();

controller& cc = app().get_plugin<chain_plugin>().chain();
auto chainid = app().get_plugin<chain_plugin>().get_chain_id();

fc::crypto::private_key txn_test_receiver_A_priv_key = fc::crypto::private_key::regenerate(fc::sha256(std::string(64, 'a')));
fc::crypto::private_key txn_test_receiver_B_priv_key = fc::crypto::private_key::regenerate(fc::sha256(std::string(64, 'b')));
fc::crypto::private_key txn_test_receiver_C_priv_key = fc::crypto::private_key::regenerate(fc::sha256(std::string(64, 'c')));
fc::crypto::public_key  txn_text_receiver_A_pub_key = txn_test_receiver_A_priv_key.get_public_key();
fc::crypto::public_key  txn_text_receiver_B_pub_key = txn_test_receiver_B_priv_key.get_public_key();
fc::crypto::public_key  txn_text_receiver_C_pub_key = txn_test_receiver_C_priv_key.get_public_key();
fc::crypto::private_key creator_priv_key = fc::crypto::private_key(init_priv_key);

//create some test accounts
{
    signed_transaction trx;
    
    //create "A" account
    {
        auto owner_auth   = eosio::chain::authority{1, {{txn_text_receiver_A_pub_key, 1}}, {}};
        auto active_auth  = eosio::chain::authority{1, {{txn_text_receiver_A_pub_key, 1}}, {}};
        trx.actions.emplace_back(vector<chain::permission_level>{{creator,"active"}}, newaccount{creator, newaccountA, owner_auth, active_auth});
    }
    //create "B" account
    {
        auto owner_auth   = eosio::chain::authority{1, {{txn_text_receiver_B_pub_key, 1}}, {}};
        auto active_auth  = eosio::chain::authority{1, {{txn_text_receiver_B_pub_key, 1}}, {}};
        trx.actions.emplace_back(vector<chain::permission_level>{{creator,"active"}}, newaccount{creator, newaccountB, owner_auth, active_auth});
    }
    //create "txn.test.t" account
    {
        auto owner_auth   = eosio::chain::authority{1, {{txn_text_receiver_C_pub_key, 1}}, {}};
        auto active_auth  = eosio::chain::authority{1, {{txn_text_receiver_C_pub_key, 1}}, {}};
        trx.actions.emplace_back(vector<chain::permission_level>{{creator,"active"}}, newaccount{creator, newaccountC, owner_auth, active_auth});
    }
    trx.expiration = cc.head_block_time() + fc::seconds(30);
    trx.set_reference_block(cc.head_block_id());
    trx.sign(creator_priv_key, chainid);
    trxs.emplace_back(std::move(trx));
}

2、token相关

trxs的第二个trx,内容为token建立和issue,为帐户转帐为以后的测试作准备

  • 为帐户txn.test.t设置eosio.token合约,以前在操做cleos set contract的时候能够经过打印结果发现,是有setcode和setabi两个步骤的。

    • setcode handler:

      • 设置handler的帐户为txn.test.t
      • 将wasm设置为handler的code,wasm是经过eosio.token合约的eosio_token_wast文件获取的,vector<uint8_t> wasm = wast_to_wasm(std::string(eosio_token_wast))
      • 将handler加上相关权限组装成action装入trx的actions集合中。
    • setabi handler:

      • 设置handler的帐户为txn.test.t
      • 设置handler的abi,将文件eosio_token_abi(json格式的)转成json转储为abi_def结构,而后经过fc::raw::pack操做将结果赋值给abi
      • 将handler加上相关权限组装成action装入trx的actions集合中。
  • 使用帐户txn.test.t建立token,标志位CUR,总发行量十亿,装成action装入trx的actions集合中。
  • issue CUR 给txn.test.t 600枚CUR,装成action装入trx的actions集合中。
  • 从txn.test.t转帐给txn.test.a 200枚CUR,装成action装入trx的actions集合中。
  • 从txn.test.t转帐给txn.test.b 200枚CUR,装成action装入trx的actions集合中。
  • trx的actions成员已经设置完毕,完成剩余trx的组装工做(同上),这里只介绍不一样的部分

    • max_net_usage_words,指定了网络资源的最大使用限制为5000个词。

这一部分的源码展现以下:

//set txn.test.t contract to eosio.token & initialize it
{
    signed_transaction trx;
    vector<uint8_t> wasm = wast_to_wasm(std::string(eosio_token_wast));
    setcode handler;
    handler.account = newaccountC;
    handler.code.assign(wasm.begin(), wasm.end());
    trx.actions.emplace_back( vector<chain::permission_level>{{newaccountC,"active"}}, handler);
    
    {
        setabi handler;
        handler.account = newaccountC;
        handler.abi = fc::raw::pack(json::from_string(eosio_token_abi).as<abi_def>());
        trx.actions.emplace_back( vector<chain::permission_level>{{newaccountC,"active"}}, handler);
    }
    {
        action act;
        act.account = N(txn.test.t);
        act.name = N(create);
        act.authorization = vector<permission_level>{{newaccountC,config::active_name}};
        act.data = eosio_token_serializer.variant_to_binary("create", fc::json::from_string("{\"issuer\":\"txn.test.t\",\"maximum_supply\":\"1000000000.0000 CUR\"}}"));
        trx.actions.push_back(act);
    }
    {
        action act;
        act.account = N(txn.test.t);
        act.name = N(issue);
        act.authorization = vector<permission_level>{{newaccountC,config::active_name}};
        act.data = eosio_token_serializer.variant_to_binary("issue", fc::json::from_string("{\"to\":\"txn.test.t\",\"quantity\":\"600.0000 CUR\",\"memo\":\"\"}"));
        trx.actions.push_back(act);
    }
    {
        action act;
        act.account = N(txn.test.t);
        act.name = N(transfer);
        act.authorization = vector<permission_level>{{newaccountC,config::active_name}};
        act.data = eosio_token_serializer.variant_to_binary("transfer", fc::json::from_string("{\"from\":\"txn.test.t\",\"to\":\"txn.test.a\",\"quantity\":\"200.0000 CUR\",\"memo\":\"\"}"));
        trx.actions.push_back(act);
    }
    {
        action act;
        act.account = N(txn.test.t);
        act.name = N(transfer);
        act.authorization = vector<permission_level>{{newaccountC,config::active_name}};
        act.data = eosio_token_serializer.variant_to_binary("transfer", fc::json::from_string("{\"from\":\"txn.test.t\",\"to\":\"txn.test.b\",\"quantity\":\"200.0000 CUR\",\"memo\":\"\"}"));
        trx.actions.push_back(act);
    }
    
    trx.expiration = cc.head_block_time() + fc::seconds(30);
    trx.set_reference_block(cc.head_block_id());
    trx.max_net_usage_words = 5000;
    trx.sign(txn_test_receiver_C_priv_key, chainid);
    trxs.emplace_back(std::move(trx));
}

发起请求

目前trxs集合已经包含了两个trx元素,其中每一个trx包含了多个action。下面要将trxs推送到链上执行

  • push_transactions函数,遍历trxs元素,每一个trx单独发送push_next_transaction
  • push_next_transaction函数,首先将trx取出经过packed_transaction函数进行组装成post的结构
  • packed_transaction函数,经过set_transaction函数对trx进行摘捡,使用pack_transaction函数进行组装
  • pack_transaction函数,就是调用了一下上面提过的fc::raw::pack操做,而后经过accept_transaction函数向链发起请求
  • accept_transaction函数,是chain_plugin的一个函数,它内部调用了incoming_transaction_async_method异步发起交易请求。

这部分代码比较杂,分为几个部分:

push_transactions函数:

void push_transactions( std::vector<signed_transaction>&& trxs, const std::function<void(fc::exception_ptr)>& next ) {
    auto trxs_copy = std::make_shared<std::decay_t<decltype(trxs)>>(std::move(trxs));
    push_next_transaction(trxs_copy, 0, next);
}

push_next_transaction函数:

static void push_next_transaction(const std::shared_ptr<std::vector<signed_transaction>>& trxs, size_t index, const std::function<void(const fc::exception_ptr&)>& next ) {
      chain_plugin& cp = app().get_plugin<chain_plugin>();
      cp.accept_transaction( packed_transaction(trxs->at(index)), [=](const fc::static_variant<fc::exception_ptr, transaction_trace_ptr>& result){
         if (result.contains<fc::exception_ptr>()) {
            next(result.get<fc::exception_ptr>());
         } else {
            if (index + 1 < trxs->size()) {
               push_next_transaction(trxs, index + 1, next);
            } else {
               next(nullptr);
            }
         }
      });
   }

packed_transaction函数,set_transaction函数以及pack_transaction函数的代码都属于本插件源码以外的EOS库源码,因为自己代码量也较少,含义在上面已经彻底解释过了,这里再也不粘贴源码。

accept_transaction函数也是EOS的库源码

void chain_plugin::accept_transaction(const chain::packed_transaction& trx, next_function<chain::transaction_trace_ptr> next) {
    my->incoming_transaction_async_method(std::make_shared<packed_transaction>(trx), false, std::forward<decltype(next)>(next));
}

incoming_transaction_async_method(app().get_method<incoming::methods::transaction_async>())

start_generation 接口

该接口的调用方法是:

curl --data-binary '["", 20, 20]' http://localhost:8888/v1/txn_test_gen/start_generation

参数列表为:

  • 第一个参数为 salt,通常用于“加盐”加密算法的值,这里咱们能够留空。
  • 第二个参数为 period,发送交易的间隔时间,单位为ms,这里是20。
  • 第三个参数为 batch_size,每一个发送间隔周期内打包交易的数量,这里也是20。

    翻译过来就是:每20ms提交20笔交易。

接下来,以start_generation 函数为入口进行源码分析。

start_generation 函数

  • 校验:

    • period的取值范围为(1, 2500)
    • batch_size的取值范围为(1, 250)
    • batch_size必须是2的倍数,batch_size & 1结果为假0才能够,这是一个位运算,与&,因此batch_size的值转为二进制时末位不能为1,因此就是2的倍数便可。
    • 对标志位running的控制。

这部分代码展现以下:

if(running)
    throw fc::exception(fc::invalid_operation_exception_code);
if(period < 1 || period > 2500)
    throw fc::exception(fc::invalid_operation_exception_code);
if(batch_size < 1 || batch_size > 250)
    throw fc::exception(fc::invalid_operation_exception_code);
if(batch_size & 1)
    throw fc::exception(fc::invalid_operation_exception_code);

running = true;
  • 定义两个action,分别是:

    • 帐户txn.test.a给txn.test.b转帐1000枚CUR
    • txn.test.b转给txn.test.a一样1000枚CUR

这部分代码展现以下:

//create the actions here
act_a_to_b.account = N(txn.test.t);
act_a_to_b.name = N(transfer);
act_a_to_b.authorization = vector<permission_level>{{name("txn.test.a"),config::active_name}};
act_a_to_b.data = eosio_token_serializer.variant_to_binary("transfer", fc::json::from_string(fc::format_string("{\"from\":\"txn.test.a\",\"to\":\"txn.test.b\",\"quantity\":\"1.0000 CUR\",\"memo\":\"${l}\"}", fc::mutable_variant_object()("l", salt))));

act_b_to_a.account = N(txn.test.t);
act_b_to_a.name = N(transfer);
act_b_to_a.authorization = vector<permission_level>{{name("txn.test.b"),config::active_name}};
act_b_to_a.data = eosio_token_serializer.variant_to_binary("transfer", fc::json::from_string(fc::format_string("{\"from\":\"txn.test.b\",\"to\":\"txn.test.a\",\"quantity\":\"1.0000 CUR\",\"memo\":\"${l}\"}", fc::mutable_variant_object()("l", salt))));

接下来,是对参数period和batch_size的储存为结构体做用域的变量以供结构体内其余函数调用,而后打印日志,最后调用arm_timer函数。

timer_timeout = period; // timer_timeout是结构体的成员变量
batch = batch_size/2; // batch是结构体的成员变量
ilog("Started transaction test plugin; performing ${p} transactions every ${m}ms", ("p", batch_size)("m", period));
arm_timer(boost::asio::high_resolution_timer::clock_type::now());

arm_timer 函数

从start_generation 函数过来,传入的参数是当前时间now,该函数主要功能是对计时器的初始化操做(计时器与文首的stop_generation函数中的关闭计时器呼应)。具体内容可分为两部分:

  • 设定计时器的过时时间,值为start_generation 接口的参数period与now相加的值,即从如今开始,过period这么久,当前计时器对象timer就过时。
  • 设定计时器的异步定时任务,任务体直接调用send_transaction函数,对函数的返回值进行处理,若是有报错信息(通常是服务停止)则调用stop_generation函数关闭插件。

    注意stop_generation函数关闭的是定时任务的无限递归,停止定时任务,中止发送测试交易。但它并无中止插件服务,咱们仍旧能够经过再次请求插件接口启动无限测试交易。

这部分代码以下:

void arm_timer(boost::asio::high_resolution_timer::time_point s) {
    timer.expires_at(s + std::chrono::milliseconds(timer_timeout));
    timer.async_wait([this](const boost::system::error_code& ec) {
        if(!running || ec)
            return;
        
        send_transaction([this](const fc::exception_ptr& e){
            if (e) {
                elog("pushing transaction failed: ${e}", ("e", e->to_detail_string()));
                stop_generation();
            } else { // 若是没有终止报错,则无限递归调用arm_timer函数,递归时传入的参数代替上面的now是当前timer对象的过时时间,这样在新的递归调用中,timer的建立会以这个时间再加上period,无间隔继续执行。
                arm_timer(timer.expires_at());
            }
        });
    });
}

send_transaction 函数

这个函数是本插件的核心功能部分,主要是发送测试交易,对transaction的处理,将咱们上面start_generation 函数中设置的两个action打包到transaction中去,以及对transaction各项属性的设置。具体步骤为:

  • 声明trxs,并为其设置大小为start_generation 接口中batch_size的值。

    std::vector<signed_transaction> trxs;
    trxs.reserve(2*batch);

接下来,与上面介绍的create_test_accounts 接口的帐户准备过程相同,准备私钥公钥,很少介绍。继续准备trx的参数:

  • nonce,是用来赋值context_free_actions的
  • context_free_actions:官方介绍一大堆,总之就是正常action是须要代价的,要确权,要占用主网资源什么的,因此搞了一个context_free_actions,字面意思就是上下文免费的action,这里权当测试用,填入的数据也是随机nonce组装的。
  • abi_serializer,用来序列化abi的,传入的system_account_name的abi值,它是在这里被赋值,然而是在结构体的做用域中被调用的。
  • reference_block_num的处理,引用区块,上面咱们也提到过,而这里面增长了一层判断,是根据标志位txn_reference_block_lag的值来比较,也就是说reference_block_num最后的值是最新区块号减去txn_reference_block_lag的值,可是最小值为0,不可为负数。
  • 经过reference_block_num得到reference_block_id

这部分代码以下:

controller& cc = app().get_plugin<chain_plugin>().chain();
auto chainid = app().get_plugin<chain_plugin>().get_chain_id();

fc::crypto::private_key a_priv_key = fc::crypto::private_key::regenerate(fc::sha256(std::string(64, 'a')));
fc::crypto::private_key b_priv_key = fc::crypto::private_key::regenerate(fc::sha256(std::string(64, 'b')));

static uint64_t nonce = static_cast<uint64_t>(fc::time_point::now().sec_since_epoch()) << 32;
abi_serializer eosio_serializer(cc.db().find<account_object, by_name>(config::system_account_name)->get_abi());

uint32_t reference_block_num = cc.last_irreversible_block_num();
if (txn_reference_block_lag >= 0) {
    reference_block_num = cc.head_block_num();
    if (reference_block_num <= (uint32_t)txn_reference_block_lag) {
        reference_block_num = 0;
    } else {
        reference_block_num -= (uint32_t)txn_reference_block_lag;
    }
}

block_id_type reference_block_id = cc.get_block_id_for_num(reference_block_num);

接下来,就是循环打包trx,咱们设置的batch_size比如是20,如今咱们已有两个action,每一个action对应一个trx,则循环只须要执行10次,每次执行两个trx便可实现,每一个trx相关的属性在上一阶段都已准备好。直接看代码吧。

for(unsigned int i = 0; i < batch; ++i) {
    {
        signed_transaction trx;
        trx.actions.push_back(act_a_to_b);
        trx.context_free_actions.emplace_back(action({}, config::null_account_name, "nonce", fc::raw::pack(nonce++)));
        trx.set_reference_block(reference_block_id);
        trx.expiration = cc.head_block_time() + fc::seconds(30);
        trx.max_net_usage_words = 100;
        trx.sign(a_priv_key, chainid);
        trxs.emplace_back(std::move(trx));
    }
    {
        signed_transaction trx;
        trx.actions.push_back(act_b_to_a);
        trx.context_free_actions.emplace_back(action({}, config::null_account_name, "nonce", fc::raw::pack(nonce++)));
        trx.set_reference_block(reference_block_id);
        trx.expiration = cc.head_block_time() + fc::seconds(30);
        trx.max_net_usage_words = 100;
        trx.sign(b_priv_key, chainid);
        trxs.emplace_back(std::move(trx));
    }
}

最后,执行

push_transactions(std::move(trxs), next);

这个部分与create_test_accounts 接口发起请求的部分一致,这里再也不重复展现。

总结

到这里为止,咱们已经彻底分析透了txn_test_gen_plugin 插件的内容。本文首先从大致上介绍了插件的架构,生命周期,通信请求与返回。接着介绍了核心结构体的内容,而后以对外接口为入口,沿着一条线将每一个功能的实现完整地研究清楚。经过本文的学习,咱们对于EOS插件的体系有了初步深入的理解,同时咱们也彻底搞清楚了txn_test_gen_plugin 插件的功能,以及它为何会达到一个比较高的tps的表现。

参考资料

  • EOSIO/eos
  • eos官方文档

相关文章和视频推荐

圆方圆学院聚集大批区块链名师,打造精品的区块链技术课程。 在各大平台都长期有优质免费公开课,欢迎报名收看。

公开课地址:https://ke.qq.com/course/345101

相关文章
相关标签/搜索