EOS 源码解析 区块回滚对交易的影响

    在主网上玩耍的小伙伴们确定遇到过区块回滚致使本身的交易没有上链。这种状况让有些人误觉得区块回滚会丢弃交易。 其实区块回滚并非致使交易没上链的主要缘由, 主要缘由是交易过时了才致使交易被丢弃。node

流程描述:

每一个交易是会被广播到全网每一个节点上面的( ps: 固然传播过程当中过时的话,当我没说哈 ),假如出块节点 A 打包了 trx a, 但此时出块节点 B 没接受到 A 的打包块,他也开始打包了,那么他也包含了该 trx a 并会将他打包( ps: 固然也有例外状况,那就是 出块节点 B 接收到 trx a 时,他就过时了,因此还没打包就丢弃他,或者还没传递到出块节点 B, 但会抵达下个节点 C D E, 但状况类似,就不另外说明了 )。但若是 a 的过时时间设置太短,致使出块节点 B 打包时发现他过时了,就会丢弃他。 这即是交易没上链的缘由。

源码解析:

咱们来看看区块生产时是如何丢弃过时交易的。区块生产的流程 区块的分叉处理能够看下我以前的文章。这里生产区块以及回滚的细节就不赘述了。

区块打包时,会将 pending block 里已经执行成功了的 trx 另外存起来, 并初始化 pending block。 等到打包的时候会再去执行一次这些 trx , 咦,为何要从新执行一遍浪费资源,由于这些 trx 都是在区块打包以前执行的,鬼知道过时了没有 =,=。微信

如下是交易被残忍丢弃的过程:app

// controller.cpp
// 将 pending block 的 trx 保存到 unapplied_transaction
void abort_block() {
if( pending ) {this

if ( read_mode == db_read_mode::SPECULATIVE ) {
     for( const auto& t : pending->_pending_block_state->trxs )
        unapplied_transactions[t->signed_id] = t;
  }
  pending.reset();

}
}code

// producer_plugin.cpp
producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool &last_block) {
// ...图片

try {ci

// ...

  // 将 pending block 里面的 trx 保存了 unapplied_transaction
  chain.abort_block();
  // 初始化 pending block
  chain.start_block(block_time, blocks_to_confirm);

} FC_LOG_AND_DROP();资源

const auto& pbs = chain.pending_block_state();
if (pbs) {开发

// ...


  // _persistent_transactions 是指经过该节点的 http 端口 push_transaction 推送过来的 trx
  // remove all persisted transactions that have now expired
  auto& persisted_by_id = _persistent_transactions.get<by_id>();
  auto& persisted_by_expiry = _persistent_transactions.get<by_expiry>();
  if (!persisted_by_expiry.empty()) {
     int num_expired_persistent = 0;
     int orig_count = _persistent_transactions.size();

     // 丢弃过时交易
     while(!persisted_by_expiry.empty() && persisted_by_expiry.begin()->expiry <= pbs->header.timestamp.to_time_point()) {
        auto const& txid = persisted_by_expiry.begin()->trx_id;
        if (_pending_block_mode == pending_block_mode::producing) {
           fc_dlog(_trx_trace_log, "[TRX_TRACE] Block ${block_num} for producer ${prod} is EXPIRING PERSISTED tx: ${txid}",
                   ("block_num", chain.head_block_num() + 1)
                   ("prod", chain.pending_block_state()->header.producer)
                   ("txid", txid));
        } else {
           fc_dlog(_trx_trace_log, "[TRX_TRACE] Speculative execution is EXPIRING PERSISTED tx: ${txid}",
                   ("txid", txid));
        }

        persisted_by_expiry.erase(persisted_by_expiry.begin());
        num_expired_persistent++;
     }

     fc_dlog(_log, "Processed ${n} persisted transactions, Expired ${expired}",
            ("n", orig_count)
            ("expired", num_expired_persistent));
  }

  try {
     size_t orig_pending_txn_size = _pending_incoming_transactions.size();

     // Processing unapplied transactions...
     // 当节点不是 出块节点而且 也没有经过该节点推送的 trx, 则 unapplied_transaction 无心义
     // 由于你不须要打包区块, 也没有 trx 须要广播出去。
     if (_producers.empty() && persisted_by_id.empty()) {
        // if this node can never produce and has no persisted transactions,
        // there is no need for unapplied transactions they can be dropped
        chain.drop_all_unapplied_transactions();
     } else {
        std::vector<transaction_metadata_ptr> apply_trxs;
        { // derive appliable transactions from unapplied_transactions and drop droppable transactions
           auto unapplied_trxs = chain.get_unapplied_transactions();
           apply_trxs.reserve(unapplied_trxs.size());

           auto calculate_transaction_category = [&](const transaction_metadata_ptr& trx) {
              if (trx->packed_trx.expiration() < pbs->header.timestamp.to_time_point()) {
                 return tx_category::EXPIRED;
              } else if (persisted_by_id.find(trx->id) != persisted_by_id.end()) {
                 return tx_category::PERSISTED;
              } else {
                 return tx_category::UNEXPIRED_UNPERSISTED;
              }
           };


           // 将没过时的放进 apply_trxs, 过时的丢弃掉。
           for (auto& trx: unapplied_trxs) {
              auto category = calculate_transaction_category(trx);
              if (category == tx_category::EXPIRED || (category == tx_category::UNEXPIRED_UNPERSISTED && _producers.empty())) {
                 if (!_producers.empty()) {
                    fc_dlog(_trx_trace_log, "[TRX_TRACE] Node with producers configured is dropping an EXPIRED transaction that was PREVIOUSLY ACCEPTED : ${txid}",
                           ("txid", trx->id));
                 }
                 chain.drop_unapplied_transaction(trx);
              } else if (category == tx_category::PERSISTED || (category == tx_category::UNEXPIRED_UNPERSISTED && _pending_block_mode == pending_block_mode::producing)) {
                 apply_trxs.emplace_back(std::move(trx));
              }
           }
        }

        if (!apply_trxs.empty()) {
           // 执行 trx, 成功的 emplace_back 进 pending block
        }
     }

     // ...
     // 执行 deffered transaction
     // 和执行 初始化 pending block 时推送进来的 transcation ( 由于初始化时,pending block 不能存 trx, 因此先另外存起来)

}rem

return start_block_result::failed;
}

总结:

回滚并不会丢弃 trx, 只会致使 trx 延后打包,以至于 trx 可能过时被丢弃。
设置过时时间时,时间跨度应该足够 2 个 BP 出完块,这样即便 B 没接收到 A 的区块,但 trx 不会由于过时而被 B 丢弃,固然还要大体估算你的 trx 广播到出块节点的时间。

有任何疑问或者想交流的朋友能够加 EOS LIVE 小助手,备注 eos开发者拉您进 EOS LIVE DAPP 开发者社区微信群哦。
图片描述

原文连接:https://eos.live/detail/18931

相关文章
相关标签/搜索