Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
producer_api_plugin.cpp
Go to the documentation of this file.
3
4#include <fc/variant.hpp>
5#include <fc/io/json.hpp>
6
7#include <chrono>
8
9namespace sysio { namespace detail {
11 std::string result;
12 };
13}}
14
16
17namespace sysio {
18
19static appbase::abstract_plugin& _producer_api_plugin = app().register_plugin<producer_api_plugin>();
20
21using namespace sysio;
22
23struct async_result_visitor : public fc::visitor<fc::variant> {
24 template<typename T>
25 fc::variant operator()(const T& v) const {
26 return fc::variant(v);
27 }
28};
29
30#define CALL_WITH_400(api_name, api_handle, call_name, INVOKE, http_response_code) \
31{std::string("/v1/" #api_name "/" #call_name), \
32 [&api_handle](string, string body, url_response_callback cb) mutable { \
33 try { \
34 INVOKE \
35 cb(http_response_code, fc::variant(result)); \
36 } catch (...) { \
37 http_plugin::handle_exception(#api_name, #call_name, body, cb); \
38 } \
39 }}
40
41#define CALL_ASYNC(api_name, api_handle, call_name, call_result, INVOKE, http_response_code) \
42{std::string("/v1/" #api_name "/" #call_name), \
43 [&api_handle](string, string body, url_response_callback cb) mutable { \
44 if (body.empty()) body = "{}"; \
45 auto next = [cb, body](const std::variant<fc::exception_ptr, call_result>& result){\
46 if (std::holds_alternative<fc::exception_ptr>(result)) {\
47 try {\
48 std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception();\
49 } catch (...) {\
50 http_plugin::handle_exception(#api_name, #call_name, body, cb);\
51 }\
52 } else {\
53 cb(http_response_code, std::visit(async_result_visitor(), result));\
54 }\
55 };\
56 INVOKE\
57 }\
58}
59
60#define INVOKE_R_R(api_handle, call_name, in_param) \
61 auto params = parse_params<in_param, http_params_types::params_required>(body);\
62 auto result = api_handle.call_name(std::move(params));
63
64#define INVOKE_R_R_II(api_handle, call_name, in_param) \
65 auto params = parse_params<in_param, http_params_types::possible_no_params>(body);\
66 auto result = api_handle.call_name(std::move(params));
67
68#define INVOKE_R_V(api_handle, call_name) \
69 body = parse_params<std::string, http_params_types::no_params>(body); \
70 auto result = api_handle.call_name();
71
72#define INVOKE_R_V_ASYNC(api_handle, call_name)\
73 api_handle.call_name(next);
74
75#define INVOKE_V_R(api_handle, call_name, in_param) \
76 auto params = parse_params<in_param, http_params_types::params_required>(body);\
77 api_handle.call_name(std::move(params)); \
78 sysio::detail::producer_api_plugin_response result{"ok"};
79
80#define INVOKE_V_V(api_handle, call_name) \
81 body = parse_params<std::string, http_params_types::no_params>(body); \
82 api_handle.call_name(); \
83 sysio::detail::producer_api_plugin_response result{"ok"};
84
85
87 ilog("starting producer_api_plugin");
88 // lifetime of plugin is lifetime of application
89 auto& producer = app().get_plugin<producer_plugin>();
90
91 app().get_plugin<http_plugin>().add_api({
92 CALL_WITH_400(producer, producer, pause,
93 INVOKE_V_V(producer, pause), 201),
94 CALL_WITH_400(producer, producer, resume,
95 INVOKE_V_V(producer, resume), 201),
96 CALL_WITH_400(producer, producer, paused,
97 INVOKE_R_V(producer, paused), 201),
98 CALL_WITH_400(producer, producer, get_runtime_options,
99 INVOKE_R_V(producer, get_runtime_options), 201),
100 CALL_WITH_400(producer, producer, update_runtime_options,
101 INVOKE_V_R(producer, update_runtime_options, producer_plugin::runtime_options), 201),
102 CALL_WITH_400(producer, producer, add_greylist_accounts,
103 INVOKE_V_R(producer, add_greylist_accounts, producer_plugin::greylist_params), 201),
104 CALL_WITH_400(producer, producer, remove_greylist_accounts,
105 INVOKE_V_R(producer, remove_greylist_accounts, producer_plugin::greylist_params), 201),
106 CALL_WITH_400(producer, producer, get_greylist,
107 INVOKE_R_V(producer, get_greylist), 201),
108 CALL_WITH_400(producer, producer, get_whitelist_blacklist,
109 INVOKE_R_V(producer, get_whitelist_blacklist), 201),
110 CALL_WITH_400(producer, producer, set_whitelist_blacklist,
111 INVOKE_V_R(producer, set_whitelist_blacklist, producer_plugin::whitelist_blacklist), 201),
112 CALL_WITH_400(producer, producer, get_integrity_hash,
113 INVOKE_R_V(producer, get_integrity_hash), 201),
114 CALL_ASYNC(producer, producer, create_snapshot, producer_plugin::snapshot_information,
115 INVOKE_R_V_ASYNC(producer, create_snapshot), 201),
116 CALL_WITH_400(producer, producer, get_scheduled_protocol_feature_activations,
117 INVOKE_R_V(producer, get_scheduled_protocol_feature_activations), 201),
118 CALL_WITH_400(producer, producer, schedule_protocol_feature_activations,
119 INVOKE_V_R(producer, schedule_protocol_feature_activations, producer_plugin::scheduled_protocol_feature_activations), 201),
120 CALL_WITH_400(producer, producer, get_supported_protocol_features,
121 INVOKE_R_R_II(producer, get_supported_protocol_features,
123 CALL_WITH_400(producer, producer, get_account_ram_corrections,
124 INVOKE_R_R(producer, get_account_ram_corrections, producer_plugin::get_account_ram_corrections_params), 201),
126}
127
128void producer_api_plugin::plugin_initialize(const variables_map& options) {
129 try {
130 const auto& _http_plugin = app().get_plugin<http_plugin>();
131 if( !_http_plugin.is_on_loopback()) {
132 wlog( "\n"
133 "**********SECURITY WARNING**********\n"
134 "* *\n"
135 "* -- Producer API -- *\n"
136 "* - EXPOSED to the LOCAL NETWORK - *\n"
137 "* - USE ONLY ON SECURE NETWORKS! - *\n"
138 "* *\n"
139 "************************************\n" );
140
141 }
143}
144
145
146#undef INVOKE_R_R
147#undef INVOKE_R_V
148#undef INVOKE_V_R
149#undef INVOKE_V_V
150#undef CALL
151
152}
#define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code, params_type)
abstract_plugin & get_plugin(const string &name) const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
void plugin_initialize(const variables_map &vm)
#define INVOKE_R_V(api_handle, call_name)
#define FC_LOG_AND_RETHROW()
#define wlog(FORMAT,...)
Definition logger.hpp:124
#define ilog(FORMAT,...)
Definition logger.hpp:118
application & app()
#define INVOKE_V_R(api_handle, call_name, in_param)
#define INVOKE_V_V(api_handle, call_name)
#define INVOKE_R_R(api_handle, call_name, in_param)
#define T(meth, val, expected)
#define INVOKE_R_R_II(api_handle, call_name, in_param)
#define INVOKE_R_V_ASYNC(api_handle, call_name)
#define CALL_ASYNC(api_name, api_handle, call_name, call_result, INVOKE, http_response_code)
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
static constexpr int medium_high
fc::variant operator()(const T &v) const
void pause()