Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
request_handler.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
6
7namespace {
8 using namespace sysio::trace_api;
9
10 std::string to_iso8601_datetime( const fc::time_point& t) {
11 return (std::string)t + "Z";
12 }
13
14 fc::variants process_authorizations(const std::vector<authorization_trace_v0>& authorizations, const yield_function& yield ) {
15 fc::variants result;
16 result.reserve(authorizations.size());
17 for ( const auto& a: authorizations) {
18 yield();
19
20 result.emplace_back(fc::mutable_variant_object()
21 ("account", a.account.to_string())
22 ("permission", a.permission.to_string())
23 );
24 }
25
26 return result;
27
28 }
29
30 template<typename ActionTrace>
31 fc::variants process_actions(const std::vector<ActionTrace>& actions, const data_handler_function & data_handler, const yield_function& yield ) {
32 fc::variants result;
33 result.reserve(actions.size());
34 // create a vector of indices to sort based on actions to avoid copies
35 std::vector<int> indices(actions.size());
36 std::iota(indices.begin(), indices.end(), 0);
37 std::sort(indices.begin(), indices.end(), [&actions](const int& lhs, const int& rhs) -> bool {
38 return actions.at(lhs).global_sequence < actions.at(rhs).global_sequence;
39 });
40 for ( int index : indices) {
41 yield();
42
43 const auto& a = actions.at(index);
44 auto common_mvo = fc::mutable_variant_object();
45
46 common_mvo("global_sequence", a.global_sequence)
47 ("receiver", a.receiver.to_string())
48 ("account", a.account.to_string())
49 ("action", a.action.to_string())
50 ("authorization", process_authorizations(a.authorization, yield))
51 ("data", fc::to_hex(a.data.data(), a.data.size()));
52
53 auto action_variant = fc::mutable_variant_object();
54 if constexpr(std::is_same_v<ActionTrace, action_trace_v0>){
55 action_variant(std::move(common_mvo));
56 auto [params, return_data] = data_handler(a, yield);
57 if (!params.is_null()) {
58 action_variant("params", params);
59 }
60 }
61 else if constexpr(std::is_same_v<ActionTrace, action_trace_v1>){
62 action_variant(std::move(common_mvo));
63 action_variant("return_value", fc::to_hex(a.return_value.data(),a.return_value.size())) ;
64 auto [params, return_data] = data_handler(a, yield);
65 if (!params.is_null()) {
66 action_variant("params", params);
67 }
68 if(return_data.has_value()){
69 action_variant("return_data", *return_data);
70 }
71 }
72 result.emplace_back( std::move(action_variant) );
73 }
74 return result;
75 }
76
77 template<typename TransactionTrace>
78 fc::variants process_transactions(const std::vector<TransactionTrace>& transactions, const data_handler_function& data_handler, const yield_function& yield ) {
79 fc::variants result;
80 result.reserve(transactions.size());
81 for ( const auto& t: transactions) {
82 yield();
83
84 if constexpr(std::is_same_v<TransactionTrace, transaction_trace_v0>){
85 result.emplace_back(
87 ("id", t.id.str())
88 ("actions", process_actions<action_trace_v0>(t.actions, data_handler, yield)));
89 } else {
90 auto common_mvo = fc::mutable_variant_object();
91 common_mvo("status", t.status)
92 ("cpu_usage_us", t.cpu_usage_us)
93 ("net_usage_words", t.net_usage_words)
94 ("signatures", t.signatures)
95 ("transaction_header", t.trx_header);
96 if constexpr(std::is_same_v<TransactionTrace, transaction_trace_v1>){
97 result.emplace_back(
99 ("id", t.id.str())
100 ("actions", process_actions<action_trace_v0>(t.actions, data_handler, yield))
101 (std::move(common_mvo)));
102 }
103 else if constexpr(std::is_same_v<TransactionTrace, transaction_trace_v2>){
104 result.emplace_back(
106 ("id", t.id.str())
107 ("actions", process_actions<action_trace_v1>(std::get<std::vector<action_trace_v1>>(t.actions), data_handler, yield))
108 (std::move(common_mvo)));
109 }
110 else if constexpr(std::is_same_v<TransactionTrace, transaction_trace_v3>){
111 result.emplace_back(
113 ("id", t.id.str())
114 ("block_num", t.block_num)
115 ("block_time", t.block_time)
116 ("producer_block_id", t.producer_block_id)
117 ("actions", process_actions<action_trace_v1>(std::get<std::vector<action_trace_v1>>(t.actions), data_handler, yield))
118 (std::move(common_mvo))
119 );
120 }
121 }
122 }
123
124 return result;
125 }
126}
127
128namespace sysio::trace_api::detail {
129 fc::variant response_formatter::process_block( const data_log_entry& trace, bool irreversible, const data_handler_function& data_handler, const yield_function& yield ) {
130 auto common_mvo = std::visit([&](auto&& arg) -> fc::mutable_variant_object {
132 ("id", arg.id.str())
133 ("number", arg.number )
134 ("previous_id", arg.previous_id.str())
135 ("status", irreversible ? "irreversible" : "pending" )
136 ("timestamp", to_iso8601_datetime(arg.timestamp))
137 ("producer", arg.producer.to_string());}, trace);
138 if (std::holds_alternative<block_trace_v0> (trace)){
139 auto& block_trace = std::get<block_trace_v0>(trace);
141 (std::move(common_mvo))
142 ("transactions", process_transactions<transaction_trace_v0>(block_trace.transactions, data_handler, yield ));
143 }else if(std::holds_alternative<block_trace_v1>(trace)){
144 auto& block_trace = std::get<block_trace_v1>(trace);
146 (std::move(common_mvo))
147 ("transaction_mroot", block_trace.transaction_mroot)
148 ("action_mroot", block_trace.action_mroot)
149 ("schedule_version", block_trace.schedule_version)
150 ("transactions", process_transactions<transaction_trace_v1>( block_trace.transactions_v1, data_handler, yield )) ;
151 }else if(std::holds_alternative<block_trace_v2>(trace)){
152 auto& block_trace = std::get<block_trace_v2>(trace);
153 auto transactions = std::visit([&](auto&& arg){
154 return process_transactions(arg, data_handler, yield);
155 }, block_trace.transactions);
157 (std::move(common_mvo))
158 ("transaction_mroot", block_trace.transaction_mroot)
159 ("action_mroot", block_trace.action_mroot)
160 ("schedule_version", block_trace.schedule_version)
161 ("transactions", transactions) ;
162 }else{
164 }
165 }
166}
An order-preserving dictionary of variants.
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)
thread_local yield_t yield
Definition yield.hpp:52
std::vector< fc::variant > variants
Definition variant.hpp:173
fc::string to_hex(const char *d, uint32_t s)
Definition hex.cpp:17
std::variant< block_trace_v0, block_trace_v1, block_trace_v2 > data_log_entry
Definition data_log.hpp:9
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
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
account_query_db::get_accounts_by_authorizers_params params