1. 程式人生 > >EOS原始碼備忘-Push Transaction機制

EOS原始碼備忘-Push Transaction機制

這裡我們討論EOS Push Transaction 的邏輯,這塊EOS與Eosforce實現有一些區別,我們會著重點出。 關於wasm相關的內容我們會有一片專門的文件分析。

我們這裡通常將Transaction譯做交易,其實這裡應該是事務的意思。

1. Transaction與Action

在EOS中Transaction與Action是最重要的幾個型別, 在EOS中,所有的鏈上行為都是Action,Transaction是一系列Action組成的事務。

EOS中使用繼承體系劃分trx與action結構,關係圖如下:

transaction_header <- transaction <- signed_transaction <- deferred_transaction

                        |

                packed_transaction

1.1 Action

我們這裡先看一下Action的宣告:

// 許可權結構structpermission_level{account_name    actor;      permission_name permission;  };  ...structaction{account_name              account;      action_name                name;// 執行所需的許可權vector  authorization;      bytes                      data;    ...// 打包成二進位制templateTdata_as()const{        ...      }  };

Action沒有什麼特別的內容,但要注意:

!> 在EOS中一個transaction中包含很多個action,而在Eosforce中一個trx只能包括一個action。

1.2 Transaction

下面我們分析一下transaction,這裡簡寫為trx。

首先看下

/**

    *  The transaction header contains the fixed-sized data

    *  associated with each transaction. It is separated from

    *  the transaction body to facilitate partial parsing of

    *  transactions without requiring dynamic memory allocation.

    *

    *  All transactions have an expiration time after which they

    *  may no longer be included in the blockchain. Once a block

    *  with a block_header::timestamp greater than expiration is

    *  deemed irreversible, then a user can safely trust the transaction

    *  will never be included.

    *

    *  Each region is an independent blockchain, it is included as routing

    *  information for inter-blockchain communication. A contract in this

    *  region might generate or authorize a transaction intended for a foreign

    *  region.

    */structtransaction_header{time_point_sec        expiration;///< trx超時時間uint16_tref_block_num      =0U;// 包含trx的block num 注意這個值是後2^16個塊中uint32_tref_block_prefix    =0UL;// blockid的低32位fc::unsigned_int      max_net_usage_words =0UL;// 網路資源上限uint8_tmax_cpu_usage_ms    =0;// cpu資源上限fc::unsigned_int      delay_sec          =0UL;/// 延遲交易的延遲時間/**

      * @return the absolute block number given the relative ref_block_num

      * 計算ref_block_num

      */block_num_typeget_ref_blocknum( block_num_type head_blocknum )const{return((head_blocknum/0xffff)*0xffff) head_blocknum%0xffff;      }voidset_reference_block(constblock_id_type& reference_block );boolverify_reference_block(constblock_id_type& reference_block )const;voidvalidate()const;  };

transaction_header包含一個trx中固定長度的資料,這裡之所以要單獨提出來主要是為了優化。

transaction視為交易體資料,這裡主要是儲存這個trx包含的action。

  /**

    *  A transaction consits of a set of messages which must all be applied or

    *  all are rejected. These messages have access to data within the given

    *  read and write scopes.

    */

  // 在EOS中一個交易中 action要麼全部執行,要麼都不執行

  struct transaction : public transaction_header {

      vector<action>        context_free_actions;

      vector<action>        actions;

      extensions_type        transaction_extensions;

      // 獲取trx id

      transaction_id_type        id()const;

      digest_type                sig_digest( const chain_id_type& chain_id, const vector<bytes>& cfd = vector<bytes>() )const;

    ...

  };

注意這裡的context_free_actions,這裡指上下文無關的Action,具體資訊可以參見這裡: https://medium.com/@bytemaster/eosio-development-update-272198df22c1 和 https://github.com/EOSIO/eos/issues/1387。 如果一個Action執行時只依賴與transaction的資料,而不依賴與鏈上的狀態,這樣的action可以併發的執行。

另外一個值得注意的是trx id:

transaction_id_type transaction::id()const{  digest_type::encoder enc;  fc::raw::pack( enc, *this);returnenc.result();}

!> Eosforce不同

在Eosforce中為了新增手續費資訊,trx與EOS結構不同,主要是增加了fee, 在transaction中:

  struct transaction : public transaction_header {

      vector<action>        context_free_actions;

      vector<action>        actions;

      extensions_type        transaction_extensions;

      asset                  fee; // EOSForce 增加的手續費,在客戶端push trx時需要寫入

      transaction_id_type        id()const;

      digest_type                sig_digest( const chain_id_type& chain_id, const vector<bytes>& cfd = vector<bytes>() )const;

      flat_set<public_key_type>  get_signature_keys( const vector<signature_type>& signatures,

                                                    const chain_id_type& chain_id,

                                                    const vector<bytes>& cfd = vector<bytes>(),

                                                    bool allow_duplicate_keys = false,

                                                    bool use_cache = true )const;

      uint32_t total_actions()const { return context_free_actions.size() actions.size(); }

      account_name first_authorizor()const {

        for( const auto& a : actions ) {

            for( const auto& u : a.authorization )

              return u.actor;

        }

        return account_name();

      }

  };

在 https://eosforce.github.io/Documentation/#/zh-cn/eosforce_client_develop_guild 這篇文件裡也有說明。

這裡計算trx id時完全使用trx的資料,這意味著,如果是兩個trx資料完全一致,特別的他們在一個區塊中,那麼這兩個trx的id就會是一樣的。

1.3 signed_transaction

一個trx簽名之後會得到一個signed_transaction,

structsigned_transaction:publictransaction  {      ...vector    signatures;// 簽名vector            context_free_data;// 上下文無關的action所使用的資料// 簽名constsignature_type&sign(constprivate_key_type& key,constchain_id_type& chain_id);signature_typesign(constprivate_key_type& key,constchain_id_type& chain_id)const;      flat_set get_signature_keys(constchain_id_type& chain_id,boolallow_duplicate_keys =false,booluse_cache =true)const;  };

signed_transaction包含簽名資料和上下文無關的action所使用的資料,

這裡要談一下context_free_data,可以參見 https://github.com/EOSIO/eos/commit/a41b4d56b5cbfd0346de34b0e03819f72e834041 ,之前我們看過context_free_actions, 在上下文無關的action中可以去從context_free_data獲取資料,可以參見在api_tests.cpp中的測試用例:

...      {// back to normal actionactionact1(pl, da);        signed_transaction trx;        trx.context_free_actions.push_back(act);        trx.context_free_data.emplace_back(fc::raw::pack(100));// verify payload matches context free datatrx.context_free_data.emplace_back(fc::raw::pack(200));        trx.actions.push_back(act1);// attempt to access non context free apifor(uint32_ti =200; i <=211; i) {            trx.context_free_actions.clear();            trx.context_free_data.clear();            cfa.payload = i;            cfa.cfd_idx =1;actioncfa_act({}, cfa);            trx.context_free_actions.emplace_back(cfa_act);            trx.signatures.clear();            set_transaction_headers(trx);            sigs = trx.sign(get_private_key(N(testapi),"active"), control->get_chain_id());            BOOST_CHECK_EXCEPTION(push_transaction(trx), unaccessible_api,                [](constfc::exception& e) {returnexpect_assert_message(e,"only context free api's can be used in this context");                }            );        }...

這裡可以作為context_free_action的一個例子,在test_api.cpp中的合約會呼叫void test_action::test_cf_action()函式:

// 這個是測試`context_free_action`的action

void test_action::test_cf_action() {

  eosio::action act = eosio::get_action( 0, 0 );

  cf_action cfa = act.data_as<cf_action>();

  if ( cfa.payload == 100 ) {

      // verify read of get_context_free_data, also verifies system api access

      // 測試在合約中通過 get_context_free_data 獲取 context_free_data

      int size = get_context_free_data( cfa.cfd_idx, nullptr, 0 );

      eosio_assert( size > 0, "size determination failed" );

      eosio::bytes cfd( static_cast<size_t>(size) );

      size = get_context_free_data( cfa.cfd_idx, &cfd[0], static_cast<size_t>(size) );

      eosio_assert(static_cast<size_t>(size) == cfd.size(), "get_context_free_data failed" );

      uint32_t v = eosio::unpack<uint32_t>( &cfd[0], cfd.size() );

      eosio_assert( v == cfa.payload, "invalid value" );

      // 以下是測試一些功能

      // verify crypto api access

      checksum256 hash;

      char test[] = "test";

      ...

      // verify context_free_system_api

      eosio_assert( true, "verify eosio_assert can be called" );

      // 下面是測試一些在上下文無關action中不能使用的功能

  } else if ( cfa.payload == 200 ) {

      // attempt to access non context free api, privileged_api

      is_privileged(act.name);

      eosio_assert( false, "privileged_api should not be allowed" );

  } else if ( cfa.payload == 201 ) {

      // attempt to access non context free api, producer_api

      get_active_producers( nullptr, 0 );

      eosio_assert( false, "producer_api should not be allowed" );

 

    ...

 

  } else if ( cfa.payload == 211 ) {

      send_deferred( N(testapi), N(testapi), "hello", 6 );

      eosio_assert( false, "transaction_api should not be allowed" );

  }

}

接下來我們來看一看packed_transaction,通過這個類我們可以將trx打包,這樣可以最大的節省空間,關於它的功能,會在下面使用的提到。

2. Transaction的接收和轉發流程

瞭解Transaction類定義之後,我們先來看一下trx在EOS系統中的接收和轉發流程,確定發起trx的入口, 在EOS中,大部分trx都是由使用者所操縱的客戶端發向同步節點,再通過同步網路傳送給超級節點,超級節點會把trx打包進塊,這裡我們梳理一下這裡的邏輯,

首先,關於客戶端提交trx的流程,可以參見 https://eosforce.github.io/Documentation/#/zh-cn/eosforce_client_develop_guild , 我們這裡從node的角度看是怎麼處理收到的trx的。

對於一個節點,trx可能是其他節點同步過來的,也可能是客戶端通過api請求的,我們先看看api:

EOS中通過http_plugin外掛響應http請求,這裡我們只看處理邏輯,在chain_api_plugin.cpp中註冊的這兩個:

voidchain_api_plugin::plugin_startup() {  ilog("starting chain_api_plugin");  my.reset(newchain_api_plugin_impl(app().get_plugin().chain()));autoro_api = app().get_plugin().get_read_only_api();autorw_api = app().get_plugin().get_read_write_api();  app().get_plugin().add_api({      ...      CHAIN_RW_CALL_ASYNC(push_transaction, chain_apis::read_write::push_transaction_results,202),      CHAIN_RW_CALL_ASYNC(push_transactions, chain_apis::read_write::push_transactions_results,202)  });}

最終實際呼叫的是這裡:

// 呼叫流程 push_transactions -> push_recurse -> push_transactionvoidread_write::push_transaction(constread_write::push_transaction_params& params, next_function next) {try{autopretty_input =std::make_shared();autoresolver = make_resolver(this, abi_serializer_max_time);try{// 這裡在使用 packed_transaction 解包abi_serializer::from_variant(params, *pretty_input, resolver, abi_serializer_max_time);      } EOS_RETHROW_EXCEPTIONS(chain::packed_transaction_type_exception,"Invalid packed transaction")// 這裡呼叫 incoming::methods::transaction_async 函式app().get_method()(pretty_input,true, [this, next](constfc::static_variant& result) ->void{        ...// 返回返回值, 略去});  }catch( boost::interprocess::bad_alloc& ) {      raise(SIGUSR1);  } CATCH_AND_CALL(next);}

注意這裡的 persist_until_expired 引數,我們在 EOS原始碼備忘-Block Produce機制 這篇文件中分析過。 incoming::methods::transaction_async註冊的是on_incoming_transaction_async函式:

my->_incoming_transaction_async_provider = app().get_method().register_provider([this](constpacked_transaction_ptr& trx,boolpersist_until_expired, next_function next) ->void{returnmy->on_incoming_transaction_async(trx, persist_until_expired, next );  });

on_incoming_transaction_async如下:

voidon_incoming_transaction_async(constpacked_transaction_ptr& trx,boolpersist_until_expired, next_function next){        chain::controller& chain = app().get_plugin().chain();if(!chain.pending_block_state()) {            _pending_incoming_transactions.emplace_back(trx, persist_until_expired, next);return;        }autoblock_time = chain.pending_block_state()->header.timestamp.to_time_point();// 返回結果的回撥autosend_response = [this, &trx, &next](constfc::static_variant& response) {            next(response);if(response.contains()) {              _transaction_ack_channel.publish(std::pair(response.get(), trx));            }else{              _transaction_ack_channel.publish(std::pair(nullptr, trx));            }        };autoid = trx->id();// 超時時間檢查if( fc::time_point(trx->expiration()) < block_time ) {            send_response(std::static_pointer_cast(std::make_shared(FC_LOG_MESSAGE(error,"expired transaction ${id}", ("id", id)) )));return;        }// 檢查是否是已處理過的trxif( chain.is_known_unexpired_transaction(id) ) {            send_response(std::static_pointer_cast(std::make_shared(FC_LOG_MESSAGE(error,"duplicate transaction ${id}", ("id", id)) )));return;        }// 看看是否超過最大的執行時間了autodeadline = fc::time_point::now() fc::milliseconds(_max_transaction_time_ms);booldeadline_is_subjective =false;if(_max_transaction_time_ms <0|| (_pending_block_mode == pending_block_mode::producing && block_time < deadline) ) {            deadline_is_subjective =true;            deadline = block_time;        }try{// 這裡直接呼叫`push_transaction`來執行trxautotrace = chain.push_transaction(std::make_shared(*trx), deadline);if(trace->except) {if(failure_is_subjective(*trace->except, deadline_is_subjective)) {                  _pending_incoming_transactions.emplace_back(trx, persist_until_expired, next);              }else{autoe_ptr = trace->except->dynamic_copy_exception();                  send_response(e_ptr);              }            }else{if(persist_until_expired) {// if this trx didnt fail/soft-fail and the persist flag is set, store its ID so that we can// ensure its applied to all future speculative blocks as well._persistent_transactions.insert(transaction_id_with_expiry{trx->id(), trx->expiration()});              }              send_response(trace);            }        }catch(constguard_exception& e ) {            app().get_plugin().handle_guard_exception(e);        }catch( boost::interprocess::bad_alloc& ) {            raise(SIGUSR1);        } CATCH_AND_CALL(send_response);      }

注意上面的is_known_unexpired_transaction,程式碼如下:

boolcontroller::is_known_unexpired_transaction(consttransaction_id_type& id)const{returndb().find(id);}

與之對應的是這個函式:

voidtransaction_context::record_transaction(consttransaction_id_type& id, fc::time_point_sec expire ) {try{          control.db().create([&](transaction_object& transaction) {              transaction.trx_id = id;              transaction.expiration = expire;          });      }catch(constboost::interprocess::bad_alloc& ) {throw;      }catch( ... ) {          EOS_ASSERT(false, tx_duplicate,"duplicate transaction ${id}", ("id", id ) );      }  }/// record_transaction

在push_transaction中會呼叫到,記錄trx已經被處理過了。

下面我們來看看send_response這個回撥:

autosend_response = [this, &trx, &next](constfc::static_variant& response) {            next(response);if(response.contains()) {              _transaction_ack_channel.publish(std::pair(response.get(), trx));            }else{              _transaction_ack_channel.publish(std::pair(nullptr, trx));            }        };

在執行之後會呼叫send_response,這裡是將結果傳送到_transaction_ack_channel中,對於_transaction_ack_channel, 這個實際對應的是下面這個型別:

namespacecompat {namespacechannels {usingtransaction_ack      =            channel_decl>;      }  }

在EOS中在net_plugin註冊響應這個channel的函式:

      my->incoming_transaction_ack_subscription =

            app().get_channel<channels::transaction_ack>().subscribe(

                  boost::bind(&net_plugin_impl::transaction_ack, my.get(), _1));

處理的函式如下:

voidnet_plugin_impl::transaction_ack(conststd::pair& results) {      transaction_id_type id = results.second->id();if(results.first) {        fc_ilog(logger,"signaled NACK, trx-id = ${id} : ${why}",("id", id)("why", results.first->to_detail_string()));        dispatcher->rejected_transaction(id);      }else{        fc_ilog(logger,"signaled ACK, trx-id = ${id}",("id", id));        dispatcher->bcast_transaction(*results.second);      }  }

這裡會將執行正常的廣播給其他節點,這其中會發送給超級節點打包入塊,打包過程可以參見 https://eosforce.github.io/Documentation/#/zh-cn/code/block_produce 。

3. push_transaction程式碼分析

這裡我們來分析下push_transaction的過程,作為執行trx的入口,這個函式在EOS中非常重要,另一方面,這裡EOS與Eosforce有一定區別,這裡會具體介紹。

TODO 需要一個流程圖,不過部落格還不支援

3.1 transaction_metadata

我們先來看下push_transaction的transaction_metadata引數, 這個引數統一了各種不同型別,不同行為的trx:

/**

*  This data structure should store context-free cached data about a transaction such as

*  packed/unpacked/compressed and recovered keys

*/

class transaction_metadata {

  public:

      transaction_id_type                                        id;  // trx ID

      transaction_id_type                                        signed_id; // signed trx ID

      signed_transaction                                        trx;

      packed_transaction                                        packed_trx;

      optional<pair<chain_id_type, flat_set<public_key_type>>>  signing_keys;

      bool                                                      accepted = false; // 標註是否呼叫了accepted訊號,確保只調用一次

      bool                                                      implicit = false; // 是否忽略檢查

      bool                                                      scheduled = false; // 是否是延遲trx

      explicit transaction_metadata( const signed_transaction& t, packed_transaction::compression_type c = packed_transaction::none )

      :trx(t),packed_trx(t, c) {

        id = trx.id();

        //raw_packed = fc::raw::pack( static_cast<const transaction&>(trx) );

        signed_id = digest_type::hash(packed_trx);

      }

      explicit transaction_metadata( const packed_transaction& ptrx )

      :trx( ptrx.get_signed_transaction() ), packed_trx(ptrx) {

        id = trx.id();

        //raw_packed = fc::raw::pack( static_cast<const transaction&>(trx) );

        signed_id = digest_type::hash(packed_trx);

      }

      const flat_set<public_key_type>& recover_keys( const chain_id_type& chain_id );

      uint32_t total_actions()const { return trx.context_free_actions.size() trx.actions.size(); }

};

using transaction_metadata_ptr = std::shared_ptr<transaction_metadata>;

先看一下implicit,這個引數指示下面的邏輯是否要忽略對於trx的各種檢查,一般用於系統內部的trx, 對於EOS,主要是處理on_block_transaction(可以參見出塊文件),在start_block呼叫:

...autoonbtrx =std::make_shared( get_on_block_transaction() );            onbtrx->implicit =true;// on_block trx 會被無條件接受autoreset_in_trx_requiring_checks = fc::make_scoped_exit([old_value=in_trx_requiring_checks,this](){                  in_trx_requiring_checks = old_value;              });            in_trx_requiring_checks =true;// 修改in_trx_requiring_checks變數達到不將trx寫入區塊,一些系統的trx沒有必要寫入區塊。push_transaction( onbtrx, fc::time_point::maximum(), self.get_global_properties().configuration.min_transaction_cpu_usage,true);...

!> Eosforce不同之處

而對於EOSForce中,除了on_block action之外,onfee合約也是被設定為implicit==true的,onfee合約是eosforce的系統合約,設計用來收取交易的手續費。

3.2 push_transaction函式

下面我們逐行分析下程式碼,EOS中push_transaction程式碼如下:

/**

    *  This is the entry point for new transactions to the block state. It will check authorization and

    *  determine whether to execute it now or to delay it. Lastly it inserts a transaction receipt into

    *  the pending block.

    */transaction_trace_ptrpush_transaction(consttransaction_metadata_ptr& trx,                                          fc::time_point deadline,uint32_tbilled_cpu_time_us,boolexplicit_billed_cpu_time =false){// deadline必須不為空// deadline是trx執行時間的一個大上限,為了防止某些trx執行時間過長導致出塊失敗等問題,// 這裡必須有一個嚴格的上限,一旦超過上限,交易會立即失敗。EOS_ASSERT(deadline != fc::time_point(), transaction_exception,"deadline cannot be uninitialized");      transaction_trace_ptr trace;// trace主要用來儲存執行中的一些錯誤資訊。try{// trx_context是執行trx的上下文狀態,下面會專門說明transaction_contexttrx_context(self, trx->trx, trx->id);if((bool)subjective_cpu_leeway && pending->_block_status == controller::block_status::incomplete) {            trx_context.leeway = *subjective_cpu_leeway;        }// 設定資料trx_context.deadline = deadline;        trx_context.explicit_billed_cpu_time = explicit_billed_cpu_time;        trx_context.billed_cpu_time_us = billed_cpu_time_us;        trace = trx_context.trace;try{if( trx->implicit ) {// 如果是implicit的就沒有必要做下面的一些檢查和記錄,這裡的檢查主要是資源方面的trx_context.init_for_implicit_trx();              trx_context.can_subjectively_fail =false;            }else{// 如果是重放並且不是重放過程中接到的新交易,則不去使用`record_transaction`記錄boolskip_recording = replay_head_time && (time_point(trx->trx.expiration) <= *replay_head_time);// 一些trx_context的初始化操作trx_context.init_for_input_trx( trx->packed_trx.get_unprunable_size(),                                              trx->packed_trx.get_prunable_size(),                                              trx->trx.signatures.size(),                                              skip_recording);            }if( trx_context.can_subjectively_fail && pending->_block_status == controller::block_status::incomplete ) {              check_actor_list( trx_context.bill_to_accounts );// Assumes bill_to_accounts is the set of actors authorizing the transaction}            trx_context.delay = fc::seconds(trx->trx.delay_sec);if( !self.skip_auth_check() && !trx->implicit ) {// 檢測交易所需要的許可權authorization.check_authorization(                      trx->trx.actions,                      trx->recover_keys( chain_id ),                      {},                      trx_context.delay,                      [](){}/*std::bind(&transaction_context::add_cpu_usage_and_check_time, &trx_context,

                                std::placeholders::_1)*/,false);            }// 執行,注意這時trx_context包括所有資訊和狀態trx_context.exec();            trx_context.finalize();// Automatically rounds up network and CPU usage in trace and bills payers if successfulautorestore = make_block_restore_point();if(!trx->implicit) {// 如果是非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);            }else{// 注意,這裡implicit類的交易是不會進入區塊的,只會計入資源消耗// 因為這類的trx無條件執行,所以不需要另行記錄。transaction_receipt_header r;              r.status = transaction_receipt::executed;              r.cpu_usage_us = trx_context.billed_cpu_time_us;              r.net_usage_words = trace->net_usage /8;              trace->receipt = r;            }// 這裡會將執行過的action寫入待出塊狀態的_actions之中fc::move_append(pending->_actions, move(trx_context.executed));// call the accept signal but only once for this transaction// 為這個交易呼叫accept訊號,保證只調用一次if(!trx->accepted) {              trx->accepted =true;              emit( self.accepted_transaction, trx);            }// 觸發applied_transaction訊號emit(self.applied_transaction, trace);if( read_mode != db_read_mode::SPECULATIVE && pending->_block_status == controller::block_status::incomplete ) {//this may happen automatically in destructor, but I prefere make it more explicittrx_context.undo();            }else{              restore.cancel();              trx_context.squash();            }// implicit的trx壓根沒有在unapplied_transactions中if(!trx->implicit) {              unapplied_transactions.erase( trx->signed_id );            }returntrace;        }catch(constfc::exception& e) {            trace->except = e;            trace->except_ptr =std::current_exception();        }// 注意這裡,如果成功的話上面就返回了這裡是失敗的情況// failure_is_subjective 表明if(!failure_is_subjective(*trace->except)) {            unapplied_transactions.erase( trx->signed_id );        }        emit( self.accepted_transaction, trx );        emit( self.applied_transaction, trace );returntrace;      } FC_CAPTURE_AND_RETHROW((trace))  }/// push_transaction

上面註釋中闡述了大致的流程,下面仔細分析一下:

首先是trx_context,這個物件的類宣告如下:

classtransaction_context{...// 省略voiddispatch_action( action_trace& trace,constaction& a, account_name receiver,boolcontext_free =false,uint32_trecurse_depth =0);inlinevoiddispatch_action( action_trace& trace,constaction& a,boolcontext_free =false){            dispatch_action(trace, a, a.account, context_free);        };voidschedule_transaction();voidrecord_transaction(consttransaction_id_type& id, fc::time_point_sec expire );voidvalidate_cpu_usage_to_bill(int64_tu,boolcheck_minimum =true)const;public:        controller&                  control;// controller類的引用constsigned_transaction&    trx;// 要執行的trxtransaction_id_type          id;        optional  undo_session;        transaction_trace_ptr        trace;// 記錄錯誤的tracefc::time_point                start;// 起始時刻fc::time_point                published;// publish的時刻vector        executed;// 執行完成的actionflat_set        bill_to_accounts;          flat_set        validate_ram_usage;/// the maximum number of virtual CPU instructions of the transaction that can be safely billed to the billable accountsuint64_tinitial_max_billable_cpu =0;        fc::microseconds              delay;boolis_input          =false;boolapply_context_free =true;boolcan_subjectively_fail =true;        fc::time_point                deadline = fc::time_point::maximum();        fc::microseconds              leeway = fc::microseconds(3000);int64_tbilled_cpu_time_us =0;boolexplicit_billed_cpu_time =false;private:boolis_initialized =false;uint64_tnet_limit =0;boolnet_limit_due_to_block =true;boolnet_limit_due_to_greylist =false;uint64_teager_net_limit =0;uint64_t&                    net_usage;/// reference to trace->net_usageboolcpu_limit_due_to_greylist =false;        fc::microseconds              initial_objective_duration_limit;        fc::microseconds              objective_duration_limit;        fc::time_point                _deadline = fc::time_point::maximum();int64_tdeadline_exception_code = block_cpu_usage_exceeded::code_value;int64_tbilling_timer_exception_code = block_cpu_usage_exceeded::code_value;        fc::time_point                pseudo_start;        fc::microseconds              billed_time;        fc::microseconds              billing_timer_duration_limit;  };

我們先看一下init_for_input_trx:

voidtransaction_context::init_for_input_trx(uint64_tpacked_trx_unprunable_size,// 這個是指trx打包後完整的大小uint64_tpacked_trx_prunable_size,// 這個指trx額外資訊的大小uint32_tnum_signatures,// 這個引數沒用上boolskip_recording )// 是否要跳過記錄{// 根據cfg和trx初始化資源constauto& cfg = control.get_global_properties().configuration;// 利用packed_trx_unprunable_size和packed_trx_prunable_size 計算net資源消耗uint64_tdiscounted_size_for_pruned_data = packed_trx_prunable_size;if( cfg.context_free_discount_net_usage_den >0&& cfg.context_free_discount_net_usage_num < cfg.context_free_discount_net_usage_den )      {        discounted_size_for_pruned_data *= cfg.context_free_discount_net_usage_num;        discounted_size_for_pruned_data =  ( discounted_size_for_pruned_data cfg.context_free_discount_net_usage_den -1)                                                                                    / cfg.context_free_discount_net_usage_den;// rounds up}uint64_tinitial_net_usage =static_cast(cfg.base_per_transaction_net_usage)                                    packed_trx_unprunable_size discounted_size_for_pruned_data;// 對於delay trx需要額外的net資源if( trx.delay_sec.value >0) {// If delayed, also charge ahead of time for the additional net usage needed to retire the delayed transaction// whether that be by successfully executing, soft failure, hard failure, or expiration.initial_net_usage =static_cast(cfg.base_per_transaction_net_usage)                              static_cast(config::transaction_id_net_usage);      }// 初始化一些資訊published = control.pending_block_time();      is_input =true;if(!control.skip_trx_checks()) {        control.validate_expiration(trx);        control.validate_tapos(trx);        control.validate_referenced_accounts(trx);      }      init( initial_net_usage);// 這裡呼叫init函式, 在這個函式中會處理cpu資源和ram資源if(!skip_recording)// 將trx新增入記錄中record_transaction( id, trx.expiration );/// checks for dupes}

這裡會先計算net,再在init函式中處理其他資源:

voidtransaction_context::init(uint64_tinitial_net_usage)  {      EOS_ASSERT( !is_initialized, transaction_exception,"cannot initialize twice");conststaticint64_tlarge_number_no_overflow =std::numeric_limits::max()/2;constauto& cfg = control.get_global_properties().configuration;auto& rl = control.get_mutable_resource_limits_manager();      net_limit = rl.get_block_net_limit();      objective_duration_limit = fc::microseconds( rl.get_block_cpu_limit() );      _deadline = start objective_duration_limit;// Possibly lower net_limit to the maximum net usage a transaction is allowed to be billedif( cfg.max_transaction_net_usage <= net_limit ) {        net_limit = cfg.max_transaction_net_usage;        net_limit_due_to_block =false;      }// Possibly lower objective_duration_limit to the maximum cpu usage a transaction is allowed to be billedif( cfg.max_transaction_cpu_usage <= objective_duration_limit.count() ) {        objective_duration_limit = fc::microseconds(cfg.max_transaction_cpu_usage);        billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;        _deadline = start objective_duration_limit;      }// Possibly lower net_limit to optional limit set in the transaction headeruint64_ttrx_specified_net_usage_limit =static_cast(trx.max_net_usage_words.value) *8;if( trx_specified_net_usage_limit >0&& trx_specified_net_usage_limit <= net_limit ) {        net_limit = trx_specified_net_usage_limit;        net_limit_due_to_block =false;      }// Possibly lower objective_duration_limit to optional limit set in transaction headerif( trx.max_cpu_usage_ms >0) {autotrx_specified_cpu_usage_limit = fc::milliseconds(trx.max_cpu_usage_ms);if( trx_specified_cpu_usage_limit <= objective_duration_limit ) {            objective_duration_limit = trx_specified_cpu_usage_limit;            billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;            _deadline = start objective_duration_limit;        }      }      initial_objective_duration_limit = objective_duration_limit;if( billed_cpu_time_us >0)// could also call on explicit_billed_cpu_time but it would be redundantvalidate_cpu_usage_to_bill( billed_cpu_time_us,false);// Fail early if the amount to be billed is too high// Record accounts to be billed for network and CPU usagefor(constauto& act : trx.actions ) {for(constauto& auth : act.authorization ) {            bill_to_accounts.insert( auth.actor );        }      }      validate_ram_usage.reserve( bill_to_accounts.size() );// Update usage values of accounts to reflect new timerl.update_account_usage( bill_to_accounts, block_timestamp_type(control.pending_block_time()).slot );// Calculate the highest network usage and CPU time that all of the billed accounts can afford to be billedint64_taccount_net_limit =0;int64_taccount_cpu_limit =0;boolgreylisted_net =false, greylisted_cpu =false;std::tie( account_net_limit, account_cpu_limit, greylisted_net, greylisted_cpu) = max_bandwidth_billed_accounts_can_pay();      net_limit_due_to_greylist |= greylisted_net;      cpu_limit_due_to_greylist |= greylisted_cpu;      eager_net_limit = net_limit;// Possible lower eager_net_limit to what the billed accounts can pay plus some (objective) leewayautonew_eager_net_limit =std::min( eager_net_limit,static_cast(account_net_limit cfg.net_usage_leeway) );if( new_eager_net_limit < eager_net_limit ) {        eager_net_limit = new_eager_net_limit;        net_limit_due_to_block =false;      }// Possibly limit deadline if the duration accounts can be billed for ( a subjective leeway) does not exceed current deltaif( (fc::microseconds(account_cpu_limit) leeway) <= (_deadline - start) ) {        _deadline = start fc::microseconds(account_cpu_limit) leeway;        billing_timer_exception_code = leeway_deadline_exception::code_value;      }      billing_timer_duration_limit = _deadline - start;// Check if deadline is limited by caller-set deadline (only change deadline if billed_cpu_time_us is not set)if( explicit_billed_cpu_time || deadline < _deadline ) {        _deadline = deadline;        deadline_exception_code = deadline_exception::code_value;      }else{        deadline_exception_code = billing_timer_exception_code;      }      eager_net_limit = (eager_net_limit/8)*8;// Round down to nearest multiple of word size (8 bytes) so check_net_usage can be efficientif( initial_net_usage >0)        add_net_usage( initial_net_usage );// Fail early if current net usage is already greater than the calculated limitchecktime();// Fail early if deadline has already been exceededis_initialized =true;  }

以上就是transaction_context初始化過程,這裡主要是處理資源消耗。

下面是exec函式,這個函式很簡單:

voidtransaction_context::exec() {      EOS_ASSERT( is_initialized, transaction_exception,"must first initialize");// 呼叫`dispatch_action`,這裡並沒有對上下文無關trx進行特別的操作,只是引數不同if( apply_context_free ) {for(constauto& act : trx.context_free_actions ) {            trace->action_traces.emplace_back();            dispatch_action( trace->action_traces.back(), act,true);        }      }if( delay == fc::microseconds() ) {for(constauto& act : trx.actions ) {            trace->action_traces.emplace_back();            dispatch_action( trace->action_traces.back(), act );        }      }else{// 對於延遲交易,這裡特別處理schedule_transaction();      }  }

主要執行在dispatch_action中,這裡會根據action不同分別觸發對應的呼叫:

voidtransaction_context::dispatch_action( action_trace& trace,constaction& a, account_name receiver,boolcontext_free,uint32_trecurse_depth ) {// 構建apply_context執行action, apply_context的分析在下節進行apply_contextacontext( control, *this, a, recurse_depth );      acontext.context_free = context_free;      acontext.receiver    = receiver;try{        acontext.exec();      }catch( ... ) {        trace = move(acontext.trace);throw;      }// 彙總結果到tracetrace = move(acontext.trace);  }

對於延遲交易,執行schedule_transaction:

voidtransaction_context::schedule_transaction() {// 因為交易延遲執行,會消耗額外的net和ram資源// Charge ahead of time for the additional net usage needed to retire the delayed transaction// whether that be by successfully executing, soft failure, hard failure, or expiration.if( trx.delay_sec.value ==0) {// Do not double bill. Only charge if we have not already charged for the delay.constauto& cfg = control.get_global_properties().configuration;        add_net_usage(static_cast(cfg.base_per_transaction_net_usage)                        static_cast(config::transaction_id_net_usage) );// Will exit early if net usage cannot be payed.}autofirst_auth = trx.first_authorizor();// 將延遲交易寫入節點執行時狀態資料庫中,到時會從這裡查找出來執行uint32_ttrx_size =0;constauto& cgto = control.db().create( [&](auto& gto ) {        gto.trx_id      = id;        gto.payer      = first_auth;        gto.sender      = account_name();/// delayed transactions have no sendergto.sender_id  = transaction_id_to_sender_id( gto.trx_id );        gto.published  = control.pending_block_time();        gto.delay_until = gto.published delay;        gto.expiration  = gto.delay_until fc::seconds(control.get_global_properties().configuration.deferred_trx_expiration_window);        trx_size = gto.set( trx );      });// 因為要寫記憶體記錄,所以也消耗了一定的ramadd_ram_usage( cgto.payer, (config::billable_size_v trx_size) );  }

呼叫完exec之後會呼叫transaction_context::finalize():

// 這裡主要是處理資源消耗voidtransaction_context::finalize() {      EOS_ASSERT( is_initialized, transaction_exception,"must first initialize");if( is_input ) {auto& am = control.get_mutable_authorization_manager();for(constauto& act : trx.actions ) {for(constauto& auth : act.authorization ) {              am.update_permission_usage( am.get_permission(auth) );            }        }      }auto& rl = control.get_mutable_resource_limits_manager();for(autoa : validate_ram_usage ) {        rl.verify_account_ram_usage( a );      }// Calculate the new highest network usage and CPU time that all of the billed accounts can afford to be billedint64_taccount_net_limit =0;int64_taccount_cpu_limit =0;boolgreylisted_net =false, greylisted_cpu =false;std::tie( account_net_limit, account_cpu_limit, greylisted_net, greylisted_cpu) = max_bandwidth_billed_accounts_can_pay();      net_limit_due_to_greylist |= greylisted_net;      cpu_limit_due_to_greylist |= greylisted_cpu;// Possibly lower net_limit to what the billed accounts can payif(static_cast(account_net_limit) <= net_limit ) {//NOTE:net_limit may possibly not be objective anymore due to net greylisting, but it should still be no greater than the truly objective net_limitnet_limit =static_cast(account_net_limit);        net_limit_due_to_block =false;      }// Possibly lower objective_duration_limit to what the billed accounts can payif( account_cpu_limit <= objective_duration_limit.count() ) {//NOTE:objective_duration_limit may possibly not be objective anymore due to cpu greylisting, but it should still be no greater than the truly objective objective_duration_limitobjective_duration_limit = fc::microseconds(account_cpu_limit);        billing_timer_exception_code = tx_cpu_usage_exceeded::code_value;      }      net_usage = ((net_usage 7)/8)*8;// Round up to nearest multiple of word size (8 bytes)eager_net_limit = net_limit;      check_net_usage();autonow = fc::time_point::now();      trace->elapsed = now - start;      update_billed_cpu_time( now );      validate_cpu_usage_to_bill( billed_cpu_time_us );      rl.add_transaction_usage( bill_to_accounts,static_cast(billed_cpu_time_us), net_usage,                                block_timestamp_type(control.pending_block_time()).slot );// Should never fail}

接下來make_block_restore_point,這裡添加了一個檢查點:

// The returned scoped_exit should not exceed the lifetime of the pending which existed when make_block_restore_point was called.fc::scoped_exit> make_block_restore_point() {autoorig_block_transactions_size = pending->_pending_block_state->block->transactions.size();autoorig_state_transactions_size = pending->_pending_block_state->trxs.size();autoorig_state_actions_size      = pending->_actions.size();std::function callback = [this,                                        orig_block_transactions_size,                                        orig_state_transactions_size,                                        orig_state_actions_size]()      {        pending->_pending_block_state->block->transactions.resize(orig_block_transactions_size);        pending->_pending_block_state->trxs.resize(orig_state_transactions_size);        pending->_actions.resize(orig_state_actions_size);      };returnfc::make_scoped_exit(std::move(callback) );   }

而後對於不是implicit的交易會呼叫push_receipt,這裡會將trx寫入區塊資料中,這也意味著implicit為true的交易雖然執行了,但不會在區塊中。

/**

    *  Adds the transaction receipt to the pending block and returns it.

    */templateconsttransaction_receipt&push_receipt(constT& trx, transaction_receipt_header::status_enum status,uint64_tcpu_usage_us,uint64_tnet_usage ){uint64_tnet_usage_words = net_usage /8;      EOS_ASSERT( net_usage_words*8== net_usage, transaction_exception,"net_usage is not divisible by 8");      pending->_pending_block_state->block->transactions.emplace_back( trx );      transaction_receipt& r = pending->_pending_block_state->block->transactions.back();      r.cpu_usage_us        = cpu_usage_us;      r.net_usage_words      = net_usage_words;      r.status              = status;returnr;  }

上面的邏輯很大程度上和implicit為true時的邏輯重複,估計以後會重構。

接下來值得注意的是這裡:

if( read_mode != db_read_mode::SPECULATIVE && pending->_block_status == controller::block_status::incomplete ) {//this may happen automatically in destructor, but I prefere make it more explicittrx_context.undo();            }else{              restore.cancel();              trx_context.squash();            }

TODO trx_context.undo

這裡呼叫database::session對應的函式,

!> Eosforce不同之處

以上是EOS的流程,這裡我們再來看看Eosforce的不同之處,Eosforce與EOS一個明顯的不同是Eosforce採用了基於手續費的資源模型, 這種模型意味著,如果一個交易在超級節點打包進塊時失敗了,此時也要收取手續費,否則會造成潛在的攻擊風險,所以Eosforce中,執行失敗的交易也會寫入區塊中,這樣每次執行時會呼叫對應onfee。 另一方面, Eosforce雖然使用手續費,但是還是區分cpu,net,ram資源,並且在大的限制上依然進行檢查。 後續Eosforce會完成新的資源模型,這裡會有所改動。

Eosforce中的push_transaction函式如下:

transaction_trace_ptrpush_transaction(consttransaction_metadata_ptr& trx,                                          fc::time_point deadline,uint32_tbilled_cpu_time_us,boolexplicit_billed_cpu_time =false){      EOS_ASSERT(deadline != fc::time_point(), transaction_exception,"deadline cannot be uninitialized");// eosforce暫時沒有開放延遲交易和上下文無關交易EOS_ASSERT(trx->trx.delay_sec.value ==0UL, transaction_exception,"delay,transaction failed");      EOS_ASSERT(trx->trx.context_free_actions.size()==0, transaction_exception,"context free actions size should be zero!");// 在eosforce中,為了安全性,對於特定一些交易進行了額外的驗證,主要是考慮到,系統會將執行錯誤的交易寫入區塊// 此時就要先驗證下交易內容,特別是大小上有沒有超出限制,否則將會帶來安全問題。check_action(trx->trx.actions);      transaction_trace_ptr trace;try{// 一樣的程式碼 略去...try{// 一樣的程式碼 略去...// 處理手續費EOS_ASSERT(trx->trx.fee == txfee.get_required_fee(trx->trx), transaction_exception,"set tx fee failed");              EOS_ASSERT(txfee.check_transaction(trx->trx) ==true, transaction_exception,"transaction include actor more than one");try{// 這裡會執行onfee合約,也是通過`push_transaction`實現的autoonftrx =std::make_shared( get_on_fee_transaction(trx->trx.fee, trx->trx.actions[0].authorization[0].actor) );                  onftrx->implicit =true;autoonftrace = push_transaction( onftrx, fc::time_point::maximum(), config::default_min_transaction_cpu_usage,true);// 這裡如果執行失敗直接丟擲異常,不會執行下面的東西if( onftrace->except )throw*onftrace->except;              }catch(constfc::exception &e) {                  EOS_ASSERT(false, transaction_exception,"on fee transaction failed, exception: ${e}", ("e", e));              }catch( ... ) {                  EOS_ASSERT(false, transaction_exception,"on fee transaction failed, but shouldn't enough asset to pay for transaction fee");              }            }// 注意這一層try catch,因為eos中出錯的交易會被拋棄,所以eos的異常會被直接丟擲到外層// 而在eosforce中出錯的交易會進入區塊// 但是要注意,這裡如果這裡並不是在超級節點出塊時呼叫,雖然也會執行下面的邏輯,但是不會被轉發給超級節點。try{if(explicit_billed_cpu_time && billed_cpu_time_us ==0){// 在eosforce中 因為超級節點打包區塊時失敗的交易也會被寫入區塊中,// 而很多交易失敗的原因不是交易本身有問題,而是在執行交易時,資源上限被觸發,導致交易被直接判定為失敗,// 這時寫入區塊的交易的cpu消耗是0, 這裡是需要失敗的,否則重跑區塊時會出現不同步的情況EOS_ASSERT(false, transaction_exception,"billed_cpu_time_us is 0");              }              trx_context.exec();              trx_context.finalize();// Automatically rounds up network and CPU usage in trace and bills payers if successful}catch(constfc::exception &e) {              trace->except = e;              trace->except_ptr =std::current_exception();// eosforce加了一些日誌if(head->block_num !=1) {                elog("---trnasction exe failed--------trace: ${trace}", ("trace", trace));              }            }autorestore = make_block_restore_point();if(!trx->implicit) {// 這裡不太好的地方是,對於出錯的交易也被標為`executed`(嚴格說也確實是executed),後續eosforce將會重構這裡transaction_receipt::status_enum s = (trx_context.delay == fc::seconds(0))                                                    ? transaction_receipt::executed                                                    :