transaction.hpp 3.4 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#pragma once
6 7 8 9 10
#include <eosiolib/transaction.h>
#include <eosiolib/action.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/types.hpp>
#include <eosiolib/serialize.hpp>
11
#include <vector>
12

P
Pravin 已提交
13
namespace eosio {
14 15

   /**
16
    * @defgroup transactioncppapi Transaction C++ API
17
    * @ingroup transactionapi
18
    * @brief Type-safe C++ wrappers for transaction C API
19 20 21
    *
    * @note There are some methods from the @ref transactioncapi that can be used directly from C++
    *
22
    * @{
23 24
    */

25
   class transaction_header {
26
   public:
27 28 29
      transaction_header( time exp = now() + 60 )
         :expiration(exp)
      { eosio::print("now=", now(), " exp=", expiration, "\n"); }
30

31 32
      time            expiration;
      uint16_t        ref_block_num;
D
Daniel Larimer 已提交
33
      uint32_t        ref_block_prefix;
34
      unsigned_int    net_usage_words = 0UL; /// number of 8 byte words this transaction can serialize into after compressions
35 36
      unsigned_int    kcpu_usage = 0UL; /// number of CPU usage units to bill transaction for
      unsigned_int    delay_sec = 0UL; /// number of CPU usage units to bill transaction for
37

38
      EOSLIB_SERIALIZE( transaction_header, (expiration)(ref_block_num)(ref_block_prefix)(net_usage_words)(kcpu_usage)(delay_sec) )
39 40 41 42
   };

   class transaction : public transaction_header {
   public:
43
      transaction(time exp = now() + 60) : transaction_header( exp ) {}
44 45 46 47 48 49

      void send(uint64_t sender_id, account_name payer) const {
         auto serialize = pack(*this);
         send_deferred(sender_id, payer, serialize.data(), serialize.size());
      }

D
Daniel Larimer 已提交
50
      vector<action>  context_free_actions;
51
      vector<action>  actions;
52

53
      EOSLIB_SERIALIZE_DERIVED( transaction, transaction_header, (context_free_actions)(actions) )
54 55
   };

56 57 58
   struct onerror {
      uint128_t    sender_id;
      transaction  sent_trx;
59

60 61 62
      static onerror from_current_action() {
         return unpack_action_data<onerror>();
      }
63

64
      EOSLIB_SERIALIZE( onerror, (sender_id)(sent_trx) )
65
   };
66

67 68 69 70 71 72
   /**
    * Retrieve the indicated action from the active transaction.
    * @param type - 0 for context free action, 1 for action
    * @param index - the index of the requested action
    * @return the indicated action
    */
73
   inline action get_action( uint32_t type, uint32_t index ) {
74
      auto size = ::get_action(type, index, nullptr, 0);
75
      eosio_assert( size > 0, "get_action size failed" );
76 77 78
      char buf[size];
      auto size2 = ::get_action(type, index, &buf[0], static_cast<size_t>(size) );
      eosio_assert( size == size2, "get_action failed" );
K
Kevin Heifner 已提交
79
      return eosio::unpack<eosio::action>(&buf[0], static_cast<size_t>(size));
80 81
   }

82 83 84 85 86
   inline void check_auth(const bytes& trx_packed, const vector<permission_level>& permissions) {
      auto perm_packed = pack(permissions);
      ::check_auth( trx_packed.data(), trx_packed.size(), perm_packed.data(), perm_packed.size() );
   }

87 88 89 90 91
   inline void check_auth(const char *serialized_transaction, size_t size, const vector<permission_level>& permissions) {
      auto perm_packed = pack(permissions);
      ::check_auth( serialized_transaction, size, perm_packed.data(), perm_packed.size() );
   }

92 93 94 95 96 97
   inline void check_auth(const transaction& trx, const vector<permission_level>& permissions) {
      auto trx_packed = pack(trx);
      check_auth( trx_packed, permissions );
      //return res > 0;
   }

98
   ///@} transactioncpp api
99 100

} // namespace eos