EOS合约开发第十一章-合约执行上下文

当前请求执行的上下文环境主要包含以下几类:

1. action数据

read_action_data获取了action调用的完整数据

get_action获取action的方法名称

action_data_size获取action调用完整数据的大小

2. 上下文free data

get_context_free_data获取action执行请求时指定的free data

3. transaction相关

transaction_size

4. 节点相关

get_active_producers获取激活的产快节点

5. 权限验证相关

require_auth验证账户是否在请求的签名中

require_recipient

二、读取合约执行上下文信息

1. 入口参数

receiver:接受该请求的合约账户

code:合约名称

action:合约方法

extern "C" {

   /// The apply method implements the dispatch of events to this contract
   void apply( uint64_t receiver, uint64_t code, uint64_t action ) {
      print(" receiver: ", name{receiver});
      print(" code: ", name{code});
      print(" action: ", name{action});
   }
} // extern "C"

2. 合约调用参数

   /**
    *  Copy up to @ref len bytes of current action data to the specified location
    *  @brief Copy current action data to the specified location
    *  @param msg - a pointer where up to @ref len bytes of the current action data will be copied
    *  @param len - len of the current action data to be copied, 0 to report required size
    *  @return the number of bytes copied to msg, or number of bytes that can be copied if len==0 passed
    */
   uint32_t read_action_data( void* msg, uint32_t len );

   /**
    * Get the length of the current action's data field
    * This method is useful for dynamically sized actions
    * @brief Get the length of current action's data field
    * @return the length of the current action's data field
    */
   uint32_t action_data_size();

例如:


 
#pragma pack(push,1)
   struct dummy_action {
     char a;
     uint64_t b;
     uint32_t c;
   };
#pragma pack(pop)

......

req_size = action_data_size();
read_action_data(buffer, 100);
dummy_action* dummy = reinterpret_cast<dummy_action*>(buffer);

通过以下方式可以直接获取参数:

struct dummy_action {
 char a;
 uint64_t b;
 uint32_t c;
};

.....

dummy_action dummy = unpack_action_data<dummy_action>();

3. 读取当前的活跃产快节点


 
    *  Example:
    *  @code
    *  account_name producers[21];
    *  uint32_t bytes_populated = get_active_producers(producers, sizeof(account_name)*21);
    *  @endcode
    */

   uint32_t get_active_producers( account_name* producers, uint32_t datalen );

例如:

     account_name producers[21];
     uint32_t byte_prods = get_active_producers(producers, sizeof(account_name) * 21);
     uint32_t num_prods = byte_prods / sizeof(account_name);

     for(int i = 0;i < num_prods;i ++) {
       print(" producer: ", name{producers[i]});
     }

4. 获取时间


 
   /**
    *  Returns the time in microseconds from 1970 of the current block
    *  @brief Get time of the current block (i.e. the block including this action)
    *  @return time in microseconds from 1970 of the current block
    */
   uint64_t  current_time();

   /**
    *  Returns the time in seconds from 1970 of the block including this action
    *  @brief Get time (rounded down to the nearest second) of the current block (i.e. the block including this action)
    *  @return time in seconds from 1970 of the current block
    */
   uint32_t  now();  
   
   /**
    *  Returns the time in microseconds from 1970 of the publication_time
    *  @brief Get the publication time
    *  @return the time in microseconds from 1970 of the publication_time
    */
   uint64_t  publication_time();

例如:

   //@abi action gettime
   void gettime() {
     uint64_t t_now = now();
     uint64_t t_time = current_time();
     uint64_t t_pub_time = publication_time();

     print(" now : ", t_now);
     print(" current time: ", t_time);
     print(" publication time: ", t_pub_time);
   }

 

5. 检查授权

 

   /**
    *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found
    *  @brief Verify specified account exists in the set of provided auths
    *  @param name - name of the account to be verified
    */
   void require_auth( account_name name );
   bool has_auth( account_name name );

   /**
    *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found
    *  @brief Verify specified account exists in the set of provided auths
    *  @param name - name of the account to be verified
    *  @param permission - permission level to be verified
    */
   void require_auth2( account_name name, permission_name permission );

 

 

 

 

 

 

 

阅读更多

更多精彩内容