Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
action.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace sysio { namespace chain {
7
12
13 inline bool operator== (const permission_level& lhs, const permission_level& rhs) {
14 return std::tie(lhs.actor, lhs.permission) == std::tie(rhs.actor, rhs.permission);
15 }
16
17 inline bool operator!= (const permission_level& lhs, const permission_level& rhs) {
18 return std::tie(lhs.actor, lhs.permission) != std::tie(rhs.actor, rhs.permission);
19 }
20
21 inline bool operator< (const permission_level& lhs, const permission_level& rhs) {
22 return std::tie(lhs.actor, lhs.permission) < std::tie(rhs.actor, rhs.permission);
23 }
24
25 inline bool operator<= (const permission_level& lhs, const permission_level& rhs) {
26 return std::tie(lhs.actor, lhs.permission) <= std::tie(rhs.actor, rhs.permission);
27 }
28
29 inline bool operator> (const permission_level& lhs, const permission_level& rhs) {
30 return std::tie(lhs.actor, lhs.permission) > std::tie(rhs.actor, rhs.permission);
31 }
32
33 inline bool operator>= (const permission_level& lhs, const permission_level& rhs) {
34 return std::tie(lhs.actor, lhs.permission) >= std::tie(rhs.actor, rhs.permission);
35 }
36
56 struct action_base {
60
61 action_base() = default;
62
64 : account(acnt), name(act), authorization(auth) {}
66 : account(acnt), name(act), authorization(std::move(auth)) {}
67 };
68
69 struct action : public action_base {
71
72 action() = default;
73
74 template<typename T, std::enable_if_t<std::is_base_of<bytes, T>::value, int> = 1>
76 : action_base( T::get_account(), T::get_name(), std::move(auth) ) {
77 data.assign(value.data(), value.data() + value.size());
78 }
79
80 template<typename T, std::enable_if_t<!std::is_base_of<bytes, T>::value, int> = 1>
82 : action_base( T::get_account(), T::get_name(), std::move(auth) ) {
83 data = fc::raw::pack(value);
84 }
85
87 : action_base(account, name, std::move(auth)), data(data) {
88 }
89
90 template<typename T>
91 T data_as()const {
92 SYS_ASSERT( account == T::get_account(), action_type_exception, "account is not consistent with action struct" );
93 SYS_ASSERT( name == T::get_name(), action_type_exception, "action name is not consistent with action struct" );
94 return fc::raw::unpack<T>(data);
95 }
96 };
97
98 template <typename Hasher>
99 auto generate_action_digest(Hasher&& hash, const action& act, const vector<char>& action_output) {
100 using hash_type = decltype(hash(nullptr, 0));
101 hash_type hashes[2];
102 const action_base* base = &act;
103 const auto action_base_size = fc::raw::pack_size(*base);
104 const auto action_input_size = fc::raw::pack_size(act.data);
105 const auto action_output_size = fc::raw::pack_size(action_output);
106 const auto rhs_size = action_input_size + action_output_size;
107 std::vector<char> buff;
108 buff.reserve(std::max(action_base_size, rhs_size));
109 {
110 buff.resize(action_base_size);
111 fc::datastream<char*> ds(buff.data(), action_base_size);
112 fc::raw::pack(ds, *base);
113 hashes[0] = hash(buff.data(), action_base_size);
114 }
115 {
116 buff.resize(rhs_size);
117 fc::datastream<char*> ds(buff.data(), rhs_size);
118 fc::raw::pack(ds, act.data);
119 fc::raw::pack(ds, action_output);
120 hashes[1] = hash(buff.data(), rhs_size);
121 }
122 auto hashes_size = fc::raw::pack_size(hashes[0]) + fc::raw::pack_size(hashes[1]);
123 buff.resize(hashes_size); // may cause reallocation but in practice will be unlikely
124 fc::datastream<char*> ds(buff.data(), hashes_size);
125 fc::raw::pack(ds, hashes[0]);
126 fc::raw::pack(ds, hashes[1]);
127 return hash(buff.data(), hashes_size);
128 }
129
133
134} }
135
136FC_REFLECT( sysio::chain::permission_level, (actor)(permission) )
137FC_REFLECT( sysio::chain::action_base, (account)(name)(authorization) )
std::string name
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
size_t pack_size(const T &v)
Definition raw.hpp:671
Definition name.hpp:106
bool operator<(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:21
bool operator<=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:25
bool operator==(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:13
bool operator!=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:17
bool operator>(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:29
auto generate_action_digest(Hasher &&hash, const action &act, const vector< char > &action_output)
Definition action.hpp:99
sysio::chain::action_name action_name
bool operator>=(const permission_level &lhs, const permission_level &rhs)
Definition action.hpp:33
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
void get_account(const string &accountName, const string &coresym, bool json_format)
Definition main.cpp:2211
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition reflect.hpp:298
vector< permission_level > authorization
Definition action.hpp:59
action_base(account_name acnt, action_name act, vector< permission_level > &&auth)
Definition action.hpp:65
action_base(account_name acnt, action_name act, const vector< permission_level > &auth)
Definition action.hpp:63
T data_as() const
Definition action.hpp:91
action(vector< permission_level > auth, account_name account, action_name name, const bytes &data)
Definition action.hpp:86
action(vector< permission_level > auth, const T &value)
Definition action.hpp:75
Immutable except for fc::from_variant.
Definition name.hpp:43
permission_name permission
Definition action.hpp:10