This repository was archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathabi_serializer.cpp
300 lines (261 loc) · 12.3 KB
/
abi_serializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <eos/types/abi_serializer.hpp>
#include <fc/io/raw.hpp>
#include <boost/algorithm/string/predicate.hpp>
using namespace boost;
namespace eosio { namespace types {
using boost::algorithm::ends_with;
using std::string;
template <typename T>
inline fc::variant variant_from_stream(fc::datastream<const char*>& stream) {
T temp;
fc::raw::unpack( stream, temp );
return fc::variant(temp);
}
template <typename T>
auto pack_unpack() {
return std::make_pair<abi_serializer::unpack_function, abi_serializer::pack_function>(
[]( fc::datastream<const char*>& stream, bool is_array) -> fc::variant {
if( is_array )
return variant_from_stream<vector<T>>(stream);
return variant_from_stream<T>(stream);
},
[]( const fc::variant& var, fc::datastream<char*>& ds, bool is_array ){
if( is_array )
fc::raw::pack( ds, var.as<vector<T>>() );
else
fc::raw::pack( ds, var.as<T>());
}
);
}
abi_serializer::abi_serializer( const abi& abi ) {
configure_built_in_types();
set_abi(abi);
}
void abi_serializer::configure_built_in_types() {
//public_key.hpp
built_in_types.emplace("public_key", pack_unpack<public_key>());
//asset.hpp
built_in_types.emplace("asset", pack_unpack<asset>());
built_in_types.emplace("price", pack_unpack<price>());
//native.hpp
built_in_types.emplace("string", pack_unpack<string>());
built_in_types.emplace("time", pack_unpack<time>());
built_in_types.emplace("signature", pack_unpack<signature>());
built_in_types.emplace("checksum", pack_unpack<checksum>());
built_in_types.emplace("field_name", pack_unpack<field_name>());
built_in_types.emplace("fixed_string32",pack_unpack<fixed_string32>());
built_in_types.emplace("fixed_string16",pack_unpack<fixed_string16>());
built_in_types.emplace("type_name", pack_unpack<type_name>());
built_in_types.emplace("bytes", pack_unpack<bytes>());
built_in_types.emplace("uint8", pack_unpack<uint8>());
built_in_types.emplace("uint16", pack_unpack<uint16>());
built_in_types.emplace("uint32", pack_unpack<uint32>());
built_in_types.emplace("uint64", pack_unpack<uint64>());
built_in_types.emplace("uint128", pack_unpack<uint128>());
built_in_types.emplace("uint256", pack_unpack<uint256>());
built_in_types.emplace("int8", pack_unpack<int8_t>());
built_in_types.emplace("int16", pack_unpack<int16_t>());
built_in_types.emplace("int32", pack_unpack<int32_t>());
built_in_types.emplace("int64", pack_unpack<int64_t>());
//built_in_types.emplace("int128", pack_unpack<int128>());
//built_in_types.emplace("int256", pack_unpack<int256>());
//built_in_types.emplace("uint128_t", pack_unpack<uint128_t>());
built_in_types.emplace("name", pack_unpack<name>());
built_in_types.emplace("field", pack_unpack<field>());
built_in_types.emplace("struct_t", pack_unpack<struct_t>());
built_in_types.emplace("fields", pack_unpack<fields>());
//generated.hpp
built_in_types.emplace("account_name", pack_unpack<account_name>());
built_in_types.emplace("permission_name", pack_unpack<permission_name>());
built_in_types.emplace("func_name", pack_unpack<func_name>());
built_in_types.emplace("message_name", pack_unpack<message_name>());
//built_in_types.emplace("type_name", pack_unpack<type_name>());
built_in_types.emplace("account_permission", pack_unpack<account_permission>());
built_in_types.emplace("message", pack_unpack<message>());
built_in_types.emplace("account_permission_weight",pack_unpack<account_permission_weight>());
built_in_types.emplace("transaction", pack_unpack<transaction>());
built_in_types.emplace("signed_transaction", pack_unpack<signed_transaction>());
built_in_types.emplace("key_permission_weight", pack_unpack<key_permission_weight>());
built_in_types.emplace("authority", pack_unpack<authority>());
built_in_types.emplace("blockchain_configuration", pack_unpack<blockchain_configuration>());
built_in_types.emplace("type_def", pack_unpack<type_def>());
built_in_types.emplace("action", pack_unpack<action>());
built_in_types.emplace("table", pack_unpack<table>());
built_in_types.emplace("abi", pack_unpack<abi>());
}
void abi_serializer::set_abi(const abi& abi) {
typedefs.clear();
structs.clear();
actions.clear();
tables.clear();
for( const auto& st : abi.structs )
structs[st.name] = st;
for( const auto& td : abi.types ) {
FC_ASSERT(is_type(td.type), "invalid type", ("type",td.type));
typedefs[td.new_type_name] = td.type;
}
for( const auto& a : abi.actions )
actions[a.action_name] = a.type;
for( const auto& t : abi.tables )
tables[t.table_name] = t.type;
/**
* The ABI vector may contain duplicates which would make it
* an invalid ABI
*/
FC_ASSERT( typedefs.size() == abi.types.size() );
FC_ASSERT( structs.size() == abi.structs.size() );
FC_ASSERT( actions.size() == abi.actions.size() );
FC_ASSERT( tables.size() == abi.tables.size() );
}
bool abi_serializer::is_builtin_type(const type_name& type)const {
return built_in_types.find(type) != built_in_types.end();
}
bool abi_serializer::is_integer(const type_name& type) const {
string stype = type;
return boost::starts_with(stype, "uint") || boost::starts_with(stype, "int");
}
int abi_serializer::get_integer_size(const type_name& type) const {
string stype = type;
FC_ASSERT( is_integer(type), "${stype} is not an integer type", ("stype",stype));
if( boost::starts_with(stype, "uint") ) {
return boost::lexical_cast<int>(stype.substr(4));
} else {
return boost::lexical_cast<int>(stype.substr(3));
}
}
bool abi_serializer::is_struct(const type_name& type)const {
return structs.find(resolve_type(type)) != structs.end();
}
bool abi_serializer::is_array(const type_name& type)const {
return ends_with(string(type), "[]");
}
type_name abi_serializer::array_type(const type_name& type)const {
if( !is_array(type) ) return type;
return type_name(string(type).substr(0, type.size()-2));
}
bool abi_serializer::is_type(const type_name& rtype)const {
auto type = array_type(rtype);
if( built_in_types.find(type) != built_in_types.end() ) return true;
if( typedefs.find(type) != typedefs.end() ) return is_type(typedefs.find(type)->second);
if( structs.find(type) != structs.end() ) return true;
return false;
}
const struct_t& abi_serializer::get_struct(const type_name& type)const {
auto itr = structs.find(resolve_type(type) );
FC_ASSERT( itr != structs.end(), "Unknown struct ${type}", ("type",type) );
return itr->second;
}
void abi_serializer::validate()const {
for( const auto& t : typedefs ) { try {
vector<type_name> types_seen{t.first, t.second};
auto itr = typedefs.find(t.second);
while( itr != typedefs.end() ) {
FC_ASSERT( find(types_seen.begin(), types_seen.end(), itr->second) == types_seen.end(), "Circular reference in type ${type}", ("type",t.first) );
types_seen.emplace_back(itr->second);
itr = typedefs.find(itr->second);
}
} FC_CAPTURE_AND_RETHROW( (t) ) }
for( const auto& t : typedefs ) { try {
FC_ASSERT(is_type(t.second), "", ("type",t.second) );
} FC_CAPTURE_AND_RETHROW( (t) ) }
for( const auto& s : structs ) { try {
if( s.second.base != type_name() ) {
struct_t current = s.second;
vector<type_name> types_seen{current.name};
while( current.base != type_name() ) {
const auto& base = get_struct(current.base); //<-- force struct to inherit from another struct
FC_ASSERT( find(types_seen.begin(), types_seen.end(), base.name) == types_seen.end(), "Circular reference in struct ${type}", ("type",s.second.name) );
types_seen.emplace_back(base.name);
current = base;
}
}
for( const auto& field : s.second.fields ) { try {
FC_ASSERT(is_type(field.type) );
} FC_CAPTURE_AND_RETHROW( (field) ) }
} FC_CAPTURE_AND_RETHROW( (s) ) }
for( const auto& a : actions ) { try {
FC_ASSERT(is_type(a.second), "", ("type",a.second) );
} FC_CAPTURE_AND_RETHROW( (a) ) }
for( const auto& t : tables ) { try {
FC_ASSERT(is_type(t.second), "", ("type",t.second) );
} FC_CAPTURE_AND_RETHROW( (t) ) }
}
type_name abi_serializer::resolve_type(const type_name& type)const {
auto itr = typedefs.find(type);
if( itr != typedefs.end() )
return resolve_type(itr->second);
return type;
}
void abi_serializer::binary_to_variant(const type_name& type, fc::datastream<const char *>& stream,
fc::mutable_variant_object& obj)const {
const auto& st = get_struct(type);
if( st.base != type_name() ) {
binary_to_variant(resolve_type(st.base), stream, obj);
}
for( const auto& field : st.fields ) {
obj( field.name, binary_to_variant(resolve_type(field.type), stream) );
}
}
fc::variant abi_serializer::binary_to_variant(const type_name& type, fc::datastream<const char *>& stream)const
{
type_name rtype = resolve_type(type);
auto btype = built_in_types.find(array_type(rtype) );
if( btype != built_in_types.end() ) {
return btype->second.first(stream, is_array(rtype));
}
fc::mutable_variant_object mvo;
binary_to_variant(rtype, stream, mvo);
return fc::variant( std::move(mvo) );
}
fc::variant abi_serializer::binary_to_variant(const type_name& type, const bytes& binary)const{
fc::datastream<const char*> ds( binary.data(), binary.size() );
return binary_to_variant(type, ds);
}
void abi_serializer::variant_to_binary(const type_name& type, const fc::variant& var, fc::datastream<char *>& ds)const
{ try {
auto rtype = resolve_type(type);
auto btype = built_in_types.find(array_type(rtype));
if( btype != built_in_types.end() ) {
btype->second.second(var, ds, is_array(rtype));
} else {
const auto& st = get_struct(rtype);
const auto& vo = var.get_object();
if( st.base != type_name() ) {
variant_to_binary(resolve_type(st.base), var, ds);
}
for( const auto& field : st.fields ) {
if( vo.contains( string(field.name).c_str() ) ) {
variant_to_binary(field.type, vo[field.name], ds);
}
else {
/// TODO: default construct field and write it out
FC_ASSERT( !"missing field in variant object", "Missing '${f}' in variant object", ("f",field.name) );
}
}
}
} FC_CAPTURE_AND_RETHROW( (type)(var) ) }
bytes abi_serializer::variant_to_binary(const type_name& type, const fc::variant& var)const {
if( !is_type(type) ) {
return var.as<bytes>();
}
bytes temp( 1024*1024 );
fc::datastream<char*> ds(temp.data(), temp.size() );
variant_to_binary(type, var, ds);
temp.resize(ds.tellp());
return temp;
}
type_name abi_serializer::get_action_type(name action)const {
auto itr = actions.find(action);
if( itr != actions.end() ) return itr->second;
return type_name();
}
type_name abi_serializer::get_table_type(name action)const {
auto itr = tables.find(action);
if( itr != tables.end() ) return itr->second;
return type_name();
}
} }