EOS源码解析 eosio帐号默认合约

EOS 系统帐号的建立

     当EOS启动时,程序会去调用create_native_account 建立eosio等帐号,可是建立通常帐号时,却不是调用create_native_account,那此时cleos create account是如何建立帐号的呢, 其实它也是调用eosio智能合约的newaccount,但eosio默认的智能合约是由代码提早约定好的,而不是eosio.system智能合约。bash

eosio的默认合约是由eos_contract_abi.cppeos_contract_abi.cpp这两个文件约定好的。app

eos_contract_abi.cpp 是构建智能合约的abi信息的。

// TODO add ricardian contracts
   eos_abi.actions.push_back( action_def{name("newaccount"), "newaccount",""} );
   eos_abi.actions.push_back( action_def{name("setcode"), "setcode",""} );
   eos_abi.actions.push_back( action_def{name("setabi"), "setabi",""} );
   eos_abi.actions.push_back( action_def{name("updateauth"), "updateauth",""} );
   eos_abi.actions.push_back( action_def{name("deleteauth"), "deleteauth",""} );
   eos_abi.actions.push_back( action_def{name("linkauth"), "linkauth",""} );
   eos_abi.actions.push_back( action_def{name("unlinkauth"), "unlinkauth",""} );
   eos_abi.actions.push_back( action_def{name("canceldelay"), "canceldelay",""} );
   eos_abi.actions.push_back( action_def{name("onerror"), "onerror",""} );
   eos_abi.actions.push_back( action_def{name("onblock"), "onblock",""} );
复制代码

eos_contract.cpp 是实现了abi定义的action方法。

/**
 *  This method is called assuming precondition_system_newaccount succeeds a
 */
void apply_eosio_newaccount(apply_context& context) {
   auto create = context.act.data_as<newaccount>();
   try {
   context.require_authorization(create.creator);
//   context.require_write_lock( config::eosio_auth_scope );
   auto& authorization = context.control.get_mutable_authorization_manager();
  //判断公钥是否合法。
   EOS_ASSERT( validate(create.owner), action_validate_exception, "Invalid owner authority");
   EOS_ASSERT( validate(create.active), action_validate_exception, "Invalid active authority");

   auto& db = context.db;

   auto name_str = name(create.name).to_string();
  //判断account name的合法性
   EOS_ASSERT( !create.name.empty(), action_validate_exception, "account name cannot be empty" );
   EOS_ASSERT( name_str.size() <= 12, action_validate_exception, "account names can only be 12 chars long" );

   // Check if the creator is privileged
   //只有eosio才能建立eosio.为前缀的帐号。
   const auto &creator = db.get<account_object, by_name>(create.creator);
   if( !creator.privileged ) {
      EOS_ASSERT( name_str.find( "eosio." ) != 0, action_validate_exception,
                  "only privileged accounts can have names that start with 'eosio.'" );
   }
  //判断用户名是否存在。
   auto existing_account = db.find<account_object, by_name>(create.name);
   EOS_ASSERT(existing_account == nullptr, account_name_exists_exception,
              "Cannot create account named ${name}, as that name is already taken",
              ("name", create.name));

   const auto& new_account = db.create<account_object>([&](auto& a) {
      a.name = create.name;
      a.creation_date = context.control.pending_block_time();
   });

   db.create<account_sequence_object>([&](auto& a) {
      a.name = create.name;
   });

   for( const auto& auth : { create.owner, create.active } ){
      validate_authority_precondition( context, auth );
   }

   const auto& owner_permission  = authorization.create_permission( create.name, config::owner_name, 0,
                                                                    std::move(create.owner) );
   const auto& active_permission = authorization.create_permission( create.name, config::active_name, owner_permission.id,
                                                                    std::move(create.active) );

   context.control.get_mutable_resource_limits_manager().initialize_account(create.name);

   int64_t ram_delta = config::overhead_per_account_ram_bytes;
   ram_delta += 2*config::billable_size_v<permission_object>;
   ram_delta += owner_permission.auth.get_billable_size();
   ram_delta += active_permission.auth.get_billable_size();

   context.trx_context.add_ram_usage(create.name, ram_delta);

} FC_CAPTURE_AND_RETHROW( (create) ) }
复制代码

本章经过newaccount来作示例。post

咱们再来找找EOS 是在哪里给eosio设定了默认合约。直接全局搜索eosio_contract.ui

能够发如今建立eosio帐号的时候,便给他设置了abi信息,可是action的wasm是如何导入的呢。this

找找eosio_contract.cpp的方法在哪里引用到,会在controller.cpp找到,由于他是经过宏去链接字符,因此全局搜索字符没法找到。编码

#define SET_APP_HANDLER( receiver, contract, action) \
   set_apply_handler( #receiver, #contract, #action, &BOOST_PP_CAT(apply_, BOOST_PP_CAT(contract, BOOST_PP_CAT(_,action) ) ) )

   SET_APP_HANDLER( eosio, eosio, newaccount );
   SET_APP_HANDLER( eosio, eosio, setcode );
   SET_APP_HANDLER( eosio, eosio, setabi );
   SET_APP_HANDLER( eosio, eosio, updateauth );
   SET_APP_HANDLER( eosio, eosio, deleteauth );
   SET_APP_HANDLER( eosio, eosio, linkauth );
   SET_APP_HANDLER( eosio, eosio, unlinkauth );
/*
   SET_APP_HANDLER( eosio, eosio, postrecovery );
   SET_APP_HANDLER( eosio, eosio, passrecovery );
   SET_APP_HANDLER( eosio, eosio, vetorecovery );
*/

   SET_APP_HANDLER( eosio, eosio, canceldelay );




 void set_apply_handler( account_name receiver, account_name contract, action_name action, apply_handler v ) {
      apply_handlers[receiver][make_pair(contract,action)] = v;
   }

const apply_handler* controller::find_apply_handler( account_name receiver, account_name scope, action_name act ) const
{
   auto native_handler_scope = my->apply_handlers.find( receiver );
   if( native_handler_scope != my->apply_handlers.end() ) {
      auto handler = native_handler_scope->second.find( make_pair( scope, act ) );
      if( handler != native_handler_scope->second.end() )
         return &handler->second;
   }
   return nullptr;
}
复制代码

按照这样的顺序能够得知,eosio的默认合约是经过find_apply_handler来调用的。找找哪里调用了find_apply_handler。spa

action_trace apply_context::exec_one()
{
   auto start = fc::time_point::now();

   const auto& cfg = control.get_global_properties().configuration;
   try {
      const auto& a = control.get_account( receiver );
      privileged = a.privileged;
      //寻找eosio默认智能合约方法句柄
      auto native = control.find_apply_handler( receiver, act.account, act.name );
      if( native ) {
         if( trx_context.can_subjectively_fail && control.is_producing_block()) {
            control.check_contract_list( receiver );
            control.check_action_list( act.account, act.name );
         }
         (*native)( *this );
      }

      if( a.code.size() > 0
          && !(act.account == config::system_account_name && act.name == N( setcode ) &&
               receiver == config::system_account_name)) {
         if( trx_context.can_subjectively_fail && control.is_producing_block()) {
            control.check_contract_list( receiver );
            control.check_action_list( act.account, act.name );
         }
         try {
            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()))

   action_receipt r;
   r.receiver         = receiver;
   r.act_digest       = digest_type::hash(act);
   r.global_sequence  = next_global_sequence();
   r.recv_sequence    = next_recv_sequence( receiver );

   const auto& account_sequence = db.get<account_sequence_object, by_name>(act.account);
   r.code_sequence    = account_sequence.code_sequence;
   r.abi_sequence     = account_sequence.abi_sequence;

   for( const auto& auth : act.authorization ) {
      r.auth_sequence[auth.actor] = next_auth_sequence( auth.actor );
   }

   action_trace t(r);
   t.trx_id = trx_context.id;
   t.act = act;
   t.console = _pending_console_output.str();

   trx_context.executed.emplace_back( move(r) );

   if ( control.contracts_console() ) {
      print_debug(receiver, t);
   }

   reset_console();

   t.elapsed = fc::time_point::now() - start;
   return t;
}
复制代码

EOS 在push action的时候会先去找找find_apply_handler有没有该action,有的话经过它找到方法句柄来执行。没有的话直接经过wasm去执行action。ssr

eosio帐号的默认合约的调用过程已经解析完毕了。debug

结论

     eosio 的初始合约是经过硬编码部署的,实际上也不是经过虚拟机运行的。从代码上也能够看出,即便部署了 eosio.system 智能合约,eosio 的默认合约仍是会执行。code

相关文章
相关标签/搜索