eos智能合约执行流程

eos智能合约执行

1. 执行流程

controller::push_transaction()  // 事务

  -> transaction_context::exec()  // 事务

    -> transaction_context::dispatch_action() // 通过便利transaction中的各个action来分发执行

      -> apply_context::exec() // action

        -> apply_context::exec_one() // action 执行具体的智能合约

          -> controller::get_wasm_interface()->apply() // 进入虚拟机开始执行对应智能合约

            -> wasm_interface_impl::get_instantiated_module()->apply() // 加载智能合约并执行

              -> wavm_instantiated_module::apply() // 具体模块开始接收调用

                -> wavm_instantiated_module::call() // 开始执行具体函数

                  -> Runtime::invokeFunction() // 进入到wasm运行时库开始执行具体函数

2. 分析exec_one

/**

 * IMPORTENT:执行具体action

 */

void apply_context::exec_one(action_trace &trace)

{

   auto start = fc::time_point::now();

 

   ...

 

   const auto &cfg = control.get_global_properties().configuration;

   try

   {

      try

      {

         /**

          * 接收者信息,智能合约账号?应该是对应为eosio

          */

         const auto &a = control.get_account(receiver);

         privileged = a.privileged;

         /**

          * IMPORTENT:智能合约查找apply_handler,Native是原生api

          */

         auto native = control.find_apply_handler(receiver, act.account, act.name);

         if (native)

         {

           ......

           ...内置API调用

            (*native)(*this);

         }

 

         /**

          * IMPORTENT:智能合约,a.code?这部分是代码?智能合约账号对应code就是合约代码

          */

         if (a.code.size() > 0 && !(act.account == config::system_account_name && act.name == N(setcode) &&

                                    receiver == config::system_account_name))

         {

            ......

            try

            {

               /**

                * IMPORTENT:执行智能合约

                */

               control.get_wasm_interface().apply(a.code_version, a.code, *this);

            }

            catch (const wasm_exit &)

            {

            }

         }

      }

      FC_RETHROW_EXCEPTIONS(warn, "pending console output: ${console}", ("console", _pending_console_output.str()))

   }

   catch (fc::exception &e)

   {

     ...

     throw;

   }

   ...

}

猜你喜欢

转载自www.cnblogs.com/SkyMouse/p/10105738.html
今日推荐