Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::trace_api_rpc_plugin_impl Struct Reference
Inheritance diagram for sysio::trace_api_rpc_plugin_impl:
Collaboration diagram for sysio::trace_api_rpc_plugin_impl:

Public Types

using request_handler_t = request_handler<shared_store_provider<store_provider>, abi_data_handler::shared_provider>
 

Public Member Functions

 trace_api_rpc_plugin_impl (const std::shared_ptr< trace_api_common_impl > &common)
 
void plugin_initialize (const appbase::variables_map &options)
 
fc::time_point calc_deadline (const fc::microseconds &max_serialization_time)
 
void plugin_startup ()
 
void plugin_shutdown ()
 

Static Public Member Functions

static void set_program_options (appbase::options_description &cli, appbase::options_description &cfg)
 

Public Attributes

std::shared_ptr< trace_api_common_implcommon
 
std::shared_ptr< request_handler_treq_handler
 

Detailed Description

Interface with the RPC process

Definition at line 179 of file trace_api_plugin.cpp.

Member Typedef Documentation

◆ request_handler_t

Constructor & Destructor Documentation

◆ trace_api_rpc_plugin_impl()

sysio::trace_api_rpc_plugin_impl::trace_api_rpc_plugin_impl ( const std::shared_ptr< trace_api_common_impl > & common)
inline

Definition at line 181 of file trace_api_plugin.cpp.

182 :common(common) {}
std::shared_ptr< trace_api_common_impl > common

Member Function Documentation

◆ calc_deadline()

fc::time_point sysio::trace_api_rpc_plugin_impl::calc_deadline ( const fc::microseconds & max_serialization_time)
inline

Definition at line 236 of file trace_api_plugin.cpp.

236 {
238 if( max_serialization_time > fc::microseconds::maximum() - deadline.time_since_epoch() ) {
239 deadline = fc::time_point::maximum();
240 } else {
241 deadline += max_serialization_time;
242 }
243 return deadline;
244 }
static constexpr microseconds maximum()
Definition time.hpp:14
static time_point now()
Definition time.cpp:14
static constexpr time_point maximum()
Definition time.hpp:46
Here is the call graph for this function:

◆ plugin_initialize()

void sysio::trace_api_rpc_plugin_impl::plugin_initialize ( const appbase::variables_map & options)
inline

Definition at line 201 of file trace_api_plugin.cpp.

201 {
202 ilog("initializing trace api rpc plugin");
203 std::shared_ptr<abi_data_handler> data_handler = std::make_shared<abi_data_handler>([](const exception_with_context& e){
204 log_exception(e, fc::log_level::debug);
205 });
206
207 if( options.count("trace-rpc-abi") ) {
208 SYS_ASSERT(options.count("trace-no-abis") == 0, chain::plugin_config_exception,
209 "Trace API is configured with ABIs however trace-no-abis is set");
210 const std::vector<std::string> key_value_pairs = options["trace-rpc-abi"].as<std::vector<std::string>>();
211 for (const auto& entry : key_value_pairs) {
212 try {
213 auto kv = parse_kv_pairs(entry);
214 auto account = chain::name(kv.first);
215 auto abi = abi_def_from_file(kv.second, app().data_dir());
216 data_handler->add_abi(account, abi);
217 } catch (...) {
218 elog("Malformed trace-rpc-abi provider: \"${val}\"", ("val", entry));
219 throw;
220 }
221 }
222 } else {
223 SYS_ASSERT(options.count("trace-no-abis") != 0, chain::plugin_config_exception,
224 "Trace API is not configured with ABIs and trace-no-abis is not set");
225 }
226
227 req_handler = std::make_shared<request_handler_t>(
228 shared_store_provider<store_provider>(common->store),
230 [](const std::string& msg ) {
231 fc_dlog( _log, msg );
232 }
233 );
234 }
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
#define ilog(FORMAT,...)
Definition logger.hpp:118
#define elog(FORMAT,...)
Definition logger.hpp:130
application & app()
std::pair< std::string, std::string > parse_kv_pairs(const std::string &input)
chain::abi_def abi_def_from_file(const std::string &file_name, const fc::path &data_dir)
std::tuple< const std::exception_ptr &, char const *, uint64_t, char const * > exception_with_context
Definition common.hpp:40
std::shared_ptr< request_handler_t > req_handler
Here is the call graph for this function:

◆ plugin_shutdown()

void sysio::trace_api_rpc_plugin_impl::plugin_shutdown ( )
inline

Definition at line 349 of file trace_api_plugin.cpp.

349 {
350 }

◆ plugin_startup()

void sysio::trace_api_rpc_plugin_impl::plugin_startup ( )
inline

Definition at line 246 of file trace_api_plugin.cpp.

246 {
247 auto& http = app().get_plugin<http_plugin>();
248 fc::microseconds max_response_time = http.get_max_response_time();
249
250 http.add_async_handler("/v1/trace_api/get_block",
251 [wthis=weak_from_this(), max_response_time](std::string, std::string body, url_response_callback cb)
252 {
253 auto that = wthis.lock();
254 if (!that) {
255 return;
256 }
257
258 auto block_number = ([&body]() -> std::optional<uint32_t> {
259 if (body.empty()) {
260 return {};
261 }
262
263 try {
264 auto input = fc::json::from_string(body);
265 auto block_num = input.get_object()["block_num"].as_uint64();
266 if (block_num > std::numeric_limits<uint32_t>::max()) {
267 return {};
268 }
269 return block_num;
270 } catch (...) {
271 return {};
272 }
273 })();
274
275 if (!block_number) {
276 error_results results{400, "Bad or missing block_num"};
277 cb( 400, fc::variant( results ));
278 return;
279 }
280
281 try {
282
283 const auto deadline = that->calc_deadline( max_response_time );
284 auto resp = that->req_handler->get_block_trace(*block_number, [deadline]() { FC_CHECK_DEADLINE(deadline); });
285 if (resp.is_null()) {
286 error_results results{404, "Trace API: block trace missing"};
287 cb( 404, fc::variant( results ));
288 } else {
289 cb( 200, std::move(resp) );
290 }
291 } catch (...) {
292 http_plugin::handle_exception("trace_api", "get_block", body, cb);
293 }
294 });
295
296
297 http.add_async_handler("/v1/trace_api/get_transaction_trace",
298 [wthis=weak_from_this(), max_response_time, this](std::string, std::string body, url_response_callback cb)
299 {
300 auto that = wthis.lock();
301 if (!that) {
302 return;
303 }
304
305 auto trx_id = ([&body]() -> std::optional<transaction_id_type> {
306 if (body.empty()) {
307 return {};
308 }
309 try {
310 auto input = fc::json::from_string(body);
311 auto trxid = input.get_object()["id"].as_string();
312 if (trxid.size() < 8 || trxid.size() > 64) {
313 return {};
314 }
315 return transaction_id_type(trxid);
316 } catch (...) {
317 return {};
318 }
319 })();
320
321 if (!trx_id) {
322 error_results results{400, "Bad or missing transaction ID"};
323 cb( 400, fc::variant( results ));
324 return;
325 }
326
327 try {
328 const auto deadline = that->calc_deadline( max_response_time );
329 // search for the block that contains the transaction
330 get_block_n blk_num = common->store->get_trx_block_number(*trx_id, common->minimum_irreversible_history_blocks, [deadline]() { FC_CHECK_DEADLINE(deadline); });
331 if (!blk_num.has_value()){
332 error_results results{404, "Trace API: transaction id missing in the transaction id log files"};
333 cb( 404, fc::variant( results ));
334 } else {
335 auto resp = that->req_handler->get_transaction_trace(*trx_id, *blk_num, [deadline]() { FC_CHECK_DEADLINE(deadline); });
336 if (resp.is_null()) {
337 error_results results{404, "Trace API: transaction trace missing"};
338 cb( 404, fc::variant( results ));
339 } else {
340 cb( 200, std::move(resp) );
341 }
342 }
343 } catch (...) {
344 http_plugin::handle_exception("trace_api", "get_transaction", body, cb);
345 }
346 });
347 }
abstract_plugin & get_plugin(const string &name) const
static variant from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:442
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
static void handle_exception(const char *api_name, const char *call_name, const string &body, url_response_callback cb)
#define FC_CHECK_DEADLINE(DEADLINE,...)
checksum_type transaction_id_type
Definition types.hpp:236
std::optional< uint32_t > get_block_n
Definition common.hpp:51
std::function< void(int, fc::variant)> url_response_callback
A callback function provided to a URL handler to allow it to specify the HTTP response code and body.
Structure used to create JSON error responses.
Here is the call graph for this function:

◆ set_program_options()

static void sysio::trace_api_rpc_plugin_impl::set_program_options ( appbase::options_description & cli,
appbase::options_description & cfg )
inlinestatic

Definition at line 184 of file trace_api_plugin.cpp.

184 {
185 auto cfg_options = cfg.add_options();
186 cfg_options("trace-rpc-abi", bpo::value<vector<string>>()->composing(),
187 "ABIs used when decoding trace RPC responses.\n"
188 "There must be at least one ABI specified OR the flag trace-no-abis must be used.\n"
189 "ABIs are specified as \"Key=Value\" pairs in the form <account-name>=<abi-def>\n"
190 "Where <abi-def> can be:\n"
191 " an absolute path to a file containing a valid JSON-encoded ABI\n"
192 " a relative path from `data-dir` to a file containing a valid JSON-encoded ABI\n"
193 );
194 cfg_options("trace-no-abis",
195 "Use to indicate that the RPC responses will not use ABIs.\n"
196 "Failure to specify this option when there are no trace-rpc-abi configuations will result in an Error.\n"
197 "This option is mutually exclusive with trace-rpc-api"
198 );
199 }
Here is the caller graph for this function:

Member Data Documentation

◆ common

std::shared_ptr<trace_api_common_impl> sysio::trace_api_rpc_plugin_impl::common

Definition at line 352 of file trace_api_plugin.cpp.

◆ req_handler

std::shared_ptr<request_handler_t> sysio::trace_api_rpc_plugin_impl::req_handler

Definition at line 355 of file trace_api_plugin.cpp.


The documentation for this struct was generated from the following file: