forked from jmzkChain/jmzk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_api_plugin.cpp
115 lines (96 loc) · 5.18 KB
/
net_api_plugin.cpp
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
/**
* @file
* @copyright defined in evt/LICENSE.txt
*/
#include <evt/chain/exceptions.hpp>
#include <evt/chain/transaction.hpp>
#include <evt/net_api_plugin/net_api_plugin.hpp>
#include <fc/io/json.hpp>
#include <fc/variant.hpp>
#include <chrono>
namespace evt { namespace detail {
struct net_api_plugin_empty {};
}} // namespace evt::detail
FC_REFLECT(evt::detail::net_api_plugin_empty, );
namespace evt {
static appbase::abstract_plugin& _net_api_plugin = app().register_plugin<net_api_plugin>();
using namespace evt;
#define CALL(api_name, api_handle, call_name, INVOKE, http_response_code) \
{ \
std::string("/v1/" #api_name "/" #call_name), \
[&api_handle](string, string body, url_response_callback cb) mutable { \
try { \
if(body.empty()) \
body = "{}"; \
INVOKE \
cb(http_response_code, fc::json::to_string(result)); \
} \
catch (...) { \
http_plugin::handle_exception(#api_name, #call_name, body, cb); \
} \
} \
}
#define INVOKE_R_R(api_handle, call_name, in_param) \
auto result = api_handle.call_name(fc::json::from_string(body).as<in_param>());
#define INVOKE_R_R_R_R(api_handle, call_name, in_param0, in_param1, in_param2) \
const auto& vs = fc::json::json::from_string(body).as<fc::variants>(); \
auto result = api_handle.call_name(vs.at(0).as<in_param0>(), vs.at(1).as<in_param1>(), vs.at(2).as<in_param2>());
#define INVOKE_R_V(api_handle, call_name) \
auto result = api_handle.call_name();
#define INVOKE_V_R(api_handle, call_name, in_param) \
api_handle.call_name(fc::json::from_string(body).as<in_param>()); \
evt::detail::net_api_plugin_empty result;
#define INVOKE_V_R_R(api_handle, call_name, in_param0, in_param1) \
const auto& vs = fc::json::json::from_string(body).as<fc::variants>(); \
api_handle.call_name(vs.at(0).as<in_param0>(), vs.at(1).as<in_param1>()); \
evt::detail::net_api_plugin_empty result;
#define INVOKE_V_V(api_handle, call_name) \
api_handle.call_name(); \
evt::detail::net_api_plugin_empty result;
void
net_api_plugin::plugin_startup() {
ilog("starting net_api_plugin");
// lifetime of plugin is lifetime of application
auto& net_mgr = app().get_plugin<net_plugin>();
app().get_plugin<http_plugin>().add_api({
// CALL(net, net_mgr, set_timeout,
// INVOKE_V_R(net_mgr, set_timeout, int64_t), 200),
// CALL(net, net_mgr, sign_transaction,
// INVOKE_R_R_R_R(net_mgr, sign_transaction, chain::signed_transaction, flat_set<public_key_type>, chain::chain_id_type), 201),
CALL(net, net_mgr, connect,
INVOKE_R_R(net_mgr, connect, std::string), 201),
CALL(net, net_mgr, disconnect,
INVOKE_R_R(net_mgr, disconnect, std::string), 201),
CALL(net, net_mgr, status,
INVOKE_R_R(net_mgr, status, std::string), 201),
CALL(net, net_mgr, connections,
INVOKE_R_V(net_mgr, connections), 201),
// CALL(net, net_mgr, open,
// INVOKE_V_R(net_mgr, open, std::string), 200),
});
}
void
net_api_plugin::plugin_initialize(const variables_map& options) {
try {
const auto& _http_plugin = app().get_plugin<http_plugin>();
if(!_http_plugin.is_on_loopback()) {
wlog("\n"
"**********SECURITY WARNING**********\n"
"* *\n"
"* -- Net API -- *\n"
"* - EXPOSED to the LOCAL NETWORK - *\n"
"* - USE ONLY ON SECURE NETWORKS! - *\n"
"* *\n"
"************************************\n");
}
}
FC_LOG_AND_RETHROW()
}
#undef INVOKE_R_R
#undef INVOKE_R_R_R_R
#undef INVOKE_R_V
#undef INVOKE_V_R
#undef INVOKE_V_R_R
#undef INVOKE_V_V
#undef CALL
} // namespace evt