EOS 智能合约源代码解读 (12)system合约“native.hpp”

Posted thefist11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EOS 智能合约源代码解读 (12)system合约“native.hpp”相关的知识,希望对你有一定的参考价值。

1. native类

//system_contract合约类继承于native,
class [[eosio::contract("eosio.system")]] system_contract : public native 

//native合约类继承于eosio::contract 
class [[eosio::contract("eosio.system")]] native : public eosio::contract

2.

2.1

//权限等级权重
struct permission_level_weight {
  permission_level  permission;
  uint16_t          weight;//16位的无符整型类型的权重。

  EOSLIB_SERIALIZE( permission_level_weight, (permission)(weight) )
};

//permission_level:类型的对象permission, 通过一个账户名以及其权限名构建的,例如{"useraaaaaaaa","active"},这样的一个组合构成了一个权限对象。
 

//公钥权重
struct key_weight {
  eosio::public_key  key;//公钥对象
  uint16_t           weight;
  EOSLIB_SERIALIZE( key_weight, (key)(weight) )
};


// 等待权重
struct wait_weight {
  uint32_t           wait_sec;
  uint16_t           weight;

  EOSLIB_SERIALIZE( wait_weight, (wait_sec)(weight) )
};

//权力
//authority 指有权利的人。
//permission 指某项许可。所以某人需要拥有很多别人授权的许可,才能称之为有权利的人。
struct authority {
  uint32_t                              threshold = 0;//阈值
  std::vector<key_weight>               keys;//多个密钥
  std::vector<permission_level_weight>  accounts;//多个权限
  std::vector<wait_weight>              waits;//多个等待

  EOSLIB_SERIALIZE( authority, (threshold)(keys)(accounts)(waits) )
};

 
   /**区块头
    * Blockchain block header.
    *
    * A block header is defined by:
    * - a timestamp,
    * - the maker that created it,
    * - a confirmed flag default as zero,
    * - a link to previous block,
    * - a link to the transaction merkel root,
    * - a link to action root,
    * - a schedule version,
    * - and a makers' schedule.
    */
   struct block_header {
      checksum256                               previous;
      uint32_t                                  timestamp;
      text_name                                 maker;
      uint16_t                                  confirmed = 0;
      checksum256                               transaction_mroot;
      checksum256                               action_mroot;
      uint32_t                                  schedule_version = 0;
      std::optional<eossys::maker_schedule>   new_makers;

      // explicit serialization macro is not necessary, used here only to improve compilation time
      eosLIB_SERIALIZE(block_header, (previous)(timestamp)(maker)(confirmed)(transaction_mroot)(action_mroot)
                                     (schedule_version)(new_makers))
   };

2.2 abi_hash状态表

通过[[eosio::table(“abihash”), eosio::contract(“eosio.system”)]]的方式可以为合约定义一个状态表,而不再需要原始的typedef multi_index的方式了。这种方式适用于只有主键的情况,如果有多级索引,仍旧需要multi_index

   /**
    * abi_hash is the structure underlying the abihash table and consists of:
    * - `owner`: the account owner of the contract's abi
    * - `hash`: is the sha256 hash of the abi/binary
    */
   struct [[eossys::table("abihash"), eossys::contract("eossys.system")]] abi_hash {
      name              owner;     //账户名
      checksum256       hash;
      uint64_t primary_key()const { return owner.value; }

      eosLIB_SERIALIZE( abi_hash, (owner)(hash) )
   };

以上是关于EOS 智能合约源代码解读 (12)system合约“native.hpp”的主要内容,如果未能解决你的问题,请参考以下文章

EOS 智能合约源代码解读 (14)system合约“exchange_state.hpp”

EOS 智能合约源代码解读 boot合约

EOS 智能合约源代码解读 总体说明

EOS 智能合约源代码解读 合约开发示例

EOS 智能合约源代码解读 bios合约

EOS 智能合约源代码解读 合约之action