很久没谈《天龙八部》了。
eosio整个系统中,transaction占据着十分重要的位置。我们在区块链上的任何有效操作,都代表着有transaction被执行了。在执行的过程中,push_transaction是不可以被忽略的。例如我们创建账户的时候,会通过push_transaction写到区块信息中,我们进行转账也会push_transaction写到区块信息中,今天我们来看看push_transaction作为区块信息写入的入口,背后做了哪些操作,交易信息是如何写入到区块中的。
本文主要包含以下内容:
1、push_transaction的天龙八步
我们平时在代码调试或者阅读的过程中,总免不了使用cleos命令行,比如我们创建账户就需要使用:
cleos system newaccount eosio yourname pubkey pubkey --stake-net "10.0000 EOS" --stake-cpu "10.0000 EOS" --buy-ram-bytes 1000
那么我们在这个命令输入之后都进行了哪些操作呢?在创建新用户的过程中,我们给新用户抵押了资源,购买了内存,在cleos的main.cpp中,我们以帮助新用户购买RAM为例,可以看到该命令调用了系统合约中的buyram方法,那么我们如何来一步步找到buyram这个action执行的地方呢,我们可以分为八步来看:
//第一步:设置cleos命令行中传入的参数
add_standard_transaction_options(createAccount);
//第二步:根据公钥、私钥等创建账户
auto create = create_newaccount(creator, account_name, owner_key, active_key);
//第三步:创建购买ram等其他操作的action,在这里我们可以看到调用了系统合约中的buyram方法
create_action(tx_permission.empty() ? vector<chain::permission_level>{{creator,config::active_name}} : get_account_permissions(tx_permission),
config::system_account_name, N(buyram), act_payload);
}
//第四步:send_action
send_actions( { create, buyram, delegate }
//第五步:push_action
auto result = push_actions( move(actions), extra_kcpu, compression);
//第六步:将action写到transaction中并push_transaction
fc::variant push_actions(std::vector<chain::action>&& actions, int32_t extra_kcpu, packed_transaction::compression_type compression = packed_transaction::none ) {
signed_transaction trx;
trx.actions = std::forward<decltype(actions)>(actions);
return push_transaction(trx, extra_kcpu, compression);
}
//第七步:通过回调,调用chain_plugin中的push_transaction
call(push_txn_func, packed_transaction(trx, compression));
//第八步:chain_plugin将transaction信息异步写入到区块中
void read_write::push_transaction(const read_write::push_transaction_params& params, next_function<read_write::push_transaction_results> next)
{
//处理
}
创建账户的时候是如此,其他的链上操作也基本类似,感兴趣的可以去一一查看,接下来我们要看看天龙八步中的第八步,交易信息是如何写入区块中的。
2、push_transaction背后的操作
我们通过以前的文章可以了解到,区块的生成是以producer_plugin为入口,而后在chain的controller中实际完成的,那么上面天龙八步中的第八步是如何将交易transaction信息异步发送至producer_plugin中的呢。我们在来看chain_plugin中的transaction,可以看到其中使用了incoming::methods::transaction_async异步调用的方式,一步步的走下去:
app().get_method<incoming::methods::transaction_async>()(pretty_input, true, [this, next](const fc::static_variant<fc::exception_ptr, transaction_trace_ptr>& result)
//transaction_async的定义
using transaction_async = method_decl<chain_plugin_interface, void(const packed_transaction_ptr&, bool, next_function<transaction_trace_ptr>), first_provider_policy>;
可以看到这其实是一个插件的接口,具体可以参看method_decl,我们回头看transaction_async,通过get_method的方式将transaction信息异步发送至producer_plugin,那么get_method又是什么呢:
/**
* 获取对传入类型声明的方法的引用,第一次使用的时候将会重构这个方法,该方法也会绑定两个插件
*/
template<typename MethodDecl>
auto get_method() -> std::enable_if_t<is_method_decl<MethodDecl>::value, typename MethodDecl::method_type&>
{
using method_type = typename MethodDecl::method_type;
auto key = std::type_index(typeid(MethodDecl));
auto itr = methods.find(key);
if(itr != methods.end()) {
return *method_type::get_method(itr->second);
} else {
methods.emplace(std::make_pair(key, method_type::make_unique()));
return *method_type::get_method(methods.at(key));
}
}
读到这里我们大概都会猜想的到,既然是两个插件之间的通信,想必producer_plugin中也有transaction_async相关的使用,果不其然,在producer_plugin我们可以找得到transaction_async及其使用的地方:
//接收来自chain_plugin中的transaction的句柄
incoming::methods::transaction_async::method_type::handle _incoming_transaction_async_provider;
//在producer_plugin插件初始化的时候就绑定了_incoming_transaction_async_provider和方法,类似于回调的方式,当有get_method执行的时候,on_incoming_transaction_async也将会执行
my->_incoming_transaction_async_provider = app().get_method<incoming::methods::transaction_async>().register_provider([this](const packed_transaction_ptr& trx, bool persist_until_expired, next_function<transaction_trace_ptr> next) -> void {
return my->on_incoming_transaction_async(trx, persist_until_expired, next );
});
在以前的文章中提到,节点生产区块实在start_block中执行的,我们不再赘述,下面完整的(默克尔树太长,忽略)打印其中一个区块的信息,新入门eos开发的读者朋友们也可以参考下一个区块中到底包含有哪些信息:
{
"id": "0000000000000000000000000000000000000000000000000000000000000000",
"block_num": 58447,
"header": {
"timestamp": "2018-09-15T07:28:49.500",
"producer": "eosio",
"confirmed": 0,
"previous": "0000e44e252e319484583568da419e4179a9d956198e933927f4b7806bb8a373",
"transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"action_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"schedule_version": 0,
"header_extensions": [],
"producer_signature": "SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne"
},
"dpos_proposed_irreversible_blocknum": 58447,
"dpos_irreversible_blocknum": 58446,
"bft_irreversible_blocknum": 0,
"pending_schedule_lib_num": 0,
"pending_schedule_hash": "828135c21a947b15cdbf4941ba09e1c9e0a80e88a157b0989e9b476b71a21c6b",
"pending_schedule": {
"version": 0,
"producers": []
},
"active_schedule": {
"version": 0,
"producers": [{
"producer_name": "eosio",
"block_signing_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
}]
},
"blockroot_merkle": {
//默克尔树省略
},
"producer_to_last_produced": [["eosio",
58447]],
"producer_to_last_implied_irb": [["eosio",
58446]],
"block_signing_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"confirm_count": [],
"confirmations": [],
"block": {
"timestamp": "2018-09-15T07:28:49.500",
"producer": "eosio",
"confirmed": 1,
"previous": "0000e44e252e319484583568da419e4179a9d956198e933927f4b7806bb8a373",
"transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"action_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
"schedule_version": 0,
"header_extensions": [],
"producer_signature": "SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne",
"transactions": [],
"block_extensions": []
},
"validated": false,
"in_current_chain": true
}
在我们的chain_plugin执行完push_transaction之后,controller.cpp中也对应着push_transaction,当没有transaction信息到来的时候,下面的内容不会执行,而当有交易信息的时候,则将会交易信息写入到pending中(注意,我这里加了部分日志打印来确认):
if (!trx->implicit) {
transaction_receipt::status_enum s = (trx_context.delay == fc::seconds(0))
? transaction_receipt::executed
: transaction_receipt::delayed;
trace->receipt = push_receipt(trx->packed_trx, s, trx_context.billed_cpu_time_us, trace->net_usage);
pending->_pending_block_state->trxs.emplace_back(trx);
strPending = fc::json::to_string(*pending->_pending_block_state);
dlog("contorller push_transaction pending state step3:${state}", ("state", strPending));
}
在这些执行完成之后,我们可以看到pending的打印中将会多出transaction的相关信息,如下:
"transactions": [{
"status": "executed",
"cpu_usage_us": 953,
"net_usage_words": 25,
"trx": [1,
{
"signatures": ["SIG_K1_KVpVk3PeWTXqGmExT6Lf7TbbgmJsPXcmmF63UZrTjFxf9Q8mqnKtLrU2CcBeZH3KU6qps7g73HxPDrAsUHZcic9NUp7E6f"],
"compression": "none",
"packed_context_free_data": "",
"packed_trx": "cfb49c5b4de4d5cd608f00000000010000000000ea305500409e9a2264b89a010000000000ea305500000000a8ed3232660000000000ea305500000819ab9cb1ca01000000010003e2f5c375717113f8cde854b8fabf0f8db01c02b9e197e13b8cf83100728f0b390100000001000000010003e2f5c375717113f8cde854b8fabf0f8db01c02b9e197e13b8cf83100728f0b390100000000"
}]
本文主要结合日志打印来分析交易信息是如何通过push_action写入到区块中的,以命令行创建用户为例,拆分为八步来讨论两个插件之间的异步交互,chain_plugin中的信息是如何发送至producer_plugin中的。