Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
request_handler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fc/variant.hpp>
7
8namespace sysio::trace_api {
9 using data_handler_function = std::function<std::tuple<fc::variant, std::optional<fc::variant>>( const std::variant<action_trace_v0, action_trace_v1> & action_trace_t, const yield_function&)>;
10
11 namespace detail {
13 public:
14 static fc::variant process_block( const data_log_entry& trace, bool irreversible, const data_handler_function& data_handler, const yield_function& yield );
15 };
16 }
17
18 template<typename LogfileProvider, typename DataHandlerProvider>
20 public:
21 request_handler(LogfileProvider&& logfile_provider, DataHandlerProvider&& data_handler_provider, log_handler log)
22 :logfile_provider(std::move(logfile_provider))
23 ,data_handler_provider(std::move(data_handler_provider))
24 ,_log(log)
25 {
26 _log("Constructed request_handler");
27 }
28
40 fc::variant get_block_trace( uint32_t block_height, const yield_function& yield = {}) {
41 auto data = logfile_provider.get_block(block_height, yield);
42 if (!data) {
43 _log("No block found at block height " + std::to_string(block_height) );
44 return {};
45 }
46
47 yield();
48
49 auto data_handler = [this](const auto& action, const yield_function& yield) -> std::tuple<fc::variant, std::optional<fc::variant>> {
50 return std::visit([&](const auto& action_trace_t) {
51 return data_handler_provider.serialize_to_variant(action_trace_t, yield);
52 }, action);
53 };
54
55 return detail::response_formatter::process_block(std::get<0>(*data), std::get<1>(*data), data_handler, yield);
56 }
57
71 _log("get_transaction_trace called" );
72 fc::variant result = {};
73 // extract the transaction trace from the block trace
74 auto resp = get_block_trace(block_height, yield);
75 if (!resp.is_null()) {
76 auto& b_mvo = resp.get_object();
77 if (b_mvo.contains("transactions")) {
78 auto& transactions = b_mvo["transactions"];
79 std::string input_id = trxid.str();
80 for (uint32_t i = 0; i < transactions.size(); ++i) {
81 if (transactions[i].is_null()) continue;
82 auto& t_mvo = transactions[i].get_object();
83 if (t_mvo.contains("id")) {
84 const auto& t_id = t_mvo["id"].get_string();
85 if (t_id == input_id) {
86 result = transactions[i];
87 break;
88 }
89 }
90 }
91 if( result.is_null() )
92 _log("Exhausted all " + std::to_string(transactions.size()) + " transactions in block " + b_mvo["number"].as_string() + " without finding trxid " + trxid.str());
93 }
94 }
95 return result;
96 }
97
98 private:
99 LogfileProvider logfile_provider;
100 DataHandlerProvider data_handler_provider;
101 log_handler _log;
102 };
103
104
105}
string str() const
Definition sha256.cpp:26
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
static fc::variant process_block(const data_log_entry &trace, bool irreversible, const data_handler_function &data_handler, const yield_function &yield)
request_handler(LogfileProvider &&logfile_provider, DataHandlerProvider &&data_handler_provider, log_handler log)
fc::variant get_block_trace(uint32_t block_height, const yield_function &yield={})
fc::variant get_transaction_trace(chain::transaction_id_type trxid, uint32_t block_height, const yield_function &yield={})
Definition name.hpp:106
fc::optional_delegate< void()> yield_function
Definition common.hpp:13
std::variant< block_trace_v0, block_trace_v1, block_trace_v2 > data_log_entry
Definition data_log.hpp:9
fc::optional_delegate< void(const std::string &)> log_handler
Definition common.hpp:43
std::function< std::tuple< fc::variant, std::optional< fc::variant > >(const std::variant< action_trace_v0, action_trace_v1 > &action_trace_t, const yield_function &)> data_handler_function
unsigned int uint32_t
Definition stdint.h:126