-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_plugin.cpp
More file actions
208 lines (172 loc) · 7.2 KB
/
Copy pathinfo_plugin.cpp
File metadata and controls
208 lines (172 loc) · 7.2 KB
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
/**
* @file
* @copyright defined in eos/LICENSE
*/
#include <eosio/info_plugin/info_plugin.hpp>
#include <eosio/http_plugin/http_plugin.hpp>
#include <eosio/chain_plugin/chain_plugin.hpp>
#include <eosio/chain/permission_object.hpp>
#include <eosio/chain/permission_link_object.hpp>
#include <fc/io/json.hpp>
namespace eosio {
using namespace info_apis;
using namespace chain;
static appbase::abstract_plugin& _info_plugin = app().register_plugin<info_plugin>();
class info_plugin_impl {
public:
chain_plugin *chain_plug;
fc::microseconds abi_serializer_max_time_ms;
};
info_plugin::info_plugin():my(new info_plugin_impl()){}
info_plugin::~info_plugin(){}
void info_plugin::set_program_options(options_description&, options_description& cfg) {
//cfg.add_options()
// ("abi-serializer-max-time-ms", bpo::value<uint32_t>()->default_value(config::default_abi_serializer_max_time_ms),
// "Override default maximum ABI serialization time allowed in ms")
// ;
}
controller& info_plugin::chain() { return my->chain_plug->chain();}
const controller& info_plugin::chain() const { return my->chain_plug->chain();}
namespace info_apis {
permission_info::get_permission_results permission_info::get_permission( const get_permission_params& params )const {
get_permission_results result;
result.account_name = params.account_name;
const auto& d = db.db();
const auto& permissions = d.get_index<permission_index,by_owner>();
auto perm = permissions.lower_bound( boost::make_tuple( params.account_name ) );
while( perm != permissions.end() && perm->owner == params.account_name ) {
/// TODO: lookup perm->parent name
name parent;
// Don't lookup parent if null
if( perm->parent._id ) {
const auto* p = d.find<permission_object,by_id>( perm->parent );
if( p ) {
EOS_ASSERT(perm->owner == p->owner, invalid_parent_permission, "Invalid parent permission");
parent = p->name;
}
}
vector<action> actions;
const auto& permission_links = d.get_index<permission_link_index, by_permission_name>();
auto link = permission_links.lower_bound(boost::make_tuple(perm->owner));
action act;
while(link != permission_links.end()) {
if(link->account != perm->owner){
break;
}
if (link->required_permission != perm->name) {
++link;
continue;
}
//elog("contract name: ${e}",("e",link->code));
//elog("action name: ${e}",("e",link->message_type));
act.contract_name = link->code;
act.action_name = link->message_type;
actions.push_back(act);
++link;
}
//elog("actions: ${e}",("e",actions));
result.permissions.push_back( permission{ perm->name, parent, perm->auth.to_authority(), actions} );
//elog("result.permissions: ${e}",("e",result.permissions));
++perm;
}
return result;
}
template<typename Api>
struct resolver_factory {
static auto make(const Api* api, const fc::microseconds& max_serialization_time) {
return [api, max_serialization_time](const account_name &name) -> optional<abi_serializer> {
const auto* accnt = api->db.db().template find<account_object, by_name>(name);
if (accnt != nullptr) {
abi_def abi;
if (abi_serializer::to_abi(accnt->abi, abi)) {
return abi_serializer(abi, max_serialization_time);
}
}
return optional<abi_serializer>();
};
}
};
template<typename Api>
auto make_resolver(const Api* api, const fc::microseconds& max_serialization_time) {
return resolver_factory<Api>::make(api, max_serialization_time);
}
template<typename I>
std::string itoh(I n, size_t hlen = sizeof(I)<<1) {
static const char* digits = "0123456789abcdef";
std::string r(hlen, '0');
for(size_t i = 0, j = (hlen - 1) * 4 ; i < hlen; ++i, j -= 4)
r[i] = digits[(n>>j) & 0x0f];
return r;
}
uint64_t block_info::get_ref_block_prefix(string block_num_or_id) const {
signed_block_ptr block;
optional<uint64_t> block_num;
EOS_ASSERT( !block_num_or_id.empty() && block_num_or_id.size() <= 64,
chain::block_id_type_exception,
"Invalid Block number or ID, must be greater than 0 and less than 64 characters"
);
try {
block_num = fc::to_uint64(block_num_or_id);
} catch( ... ) {}
if( block_num.valid() ) {
block = db.fetch_block_by_number( *block_num );
} else {
try {
block = db.fetch_block_by_id( fc::variant(block_num_or_id).as<block_id_type>() );
} EOS_RETHROW_EXCEPTIONS(chain::block_id_type_exception, "Invalid block ID: ${block_num_or_id}", ("block_num_or_id", block_num_or_id))
}
EOS_ASSERT( block, unknown_block_exception, "Could not find block: ${block}", ("block", block_num_or_id));
uint32_t ref_block_prefix = block->id()._hash[1];
return ref_block_prefix;
}
info_apis::get_block_info_results block_info::get_block_info(const block_info::get_block_info_params&) const {
const auto& rm = db.get_resource_limits_manager();
return {
db.head_block_time(),
db.last_irreversible_block_num(),
get_ref_block_prefix(fc::to_string(db.last_irreversible_block_num())),
};
}
}
fc::microseconds info_plugin::get_abi_serializer_max_time() const {
return my->abi_serializer_max_time_ms;
}
void info_plugin::plugin_initialize(const variables_map& options) {
try {
//if(options.count("abi-serializer-max-time-ms")){
//my->abi_serializer_max_time_ms = fc::microseconds(options.at("abi-serializer-max-time-ms").as<uint32_t>() * 1000);
//}
my->abi_serializer_max_time_ms = fc::microseconds(2000*1000);
}catch(...){
}
my->chain_plug = app().find_plugin<chain_plugin>();
EOS_ASSERT(my->chain_plug, chain::missing_chain_plugin_exception, "");
//FC_LOG_AND_RETHROW();
}
#define CALL(api_name, api_handle, api_namespace, call_name) \
{std::string("/v1/" #api_name "/" #call_name), \
[api_handle](string, string body, url_response_callback cb) mutable { \
try { \
if (body.empty()) body = "{}"; \
auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
cb(200, fc::json::to_string(result)); \
} catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
}}
#define PERMISSION_CALL(call_name) CALL(info, permission_api, permission_info, call_name)
#define BLOCK_CALL(call_name) CALL(info, block_api, block_info, call_name)
void info_plugin::plugin_startup() {
// Make the magic happen
auto permission_api = get_permission_info_api();
auto block_api = get_block_info_api();
ilog( "starting info_plugin" );
app().get_plugin<http_plugin>().add_api({
PERMISSION_CALL(get_permission),
BLOCK_CALL(get_block_info)
});
}
void info_plugin::plugin_shutdown() {
// OK, that's enough magic
}
}