Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
chain_api_plugin.cpp
Go to the documentation of this file.
3
4#include <fc/io/json.hpp>
5
6namespace sysio {
7
8static appbase::abstract_plugin& _chain_api_plugin = app().register_plugin<chain_api_plugin>();
9
10using namespace sysio;
11
19
20
21chain_api_plugin::chain_api_plugin(){}
23
24void chain_api_plugin::set_program_options(options_description&, options_description&) {}
25void chain_api_plugin::plugin_initialize(const variables_map&) {}
26
27struct async_result_visitor : public fc::visitor<fc::variant> {
28 template<typename T>
29 fc::variant operator()(const T& v) const {
30 return fc::variant(v);
31 }
32};
33
34// Only want a simple 'Invalid transaction id' if unable to parse the body
35template<>
38 if (body.empty()) {
39 SYS_THROW(chain::invalid_http_request, "A Request body is required");
40 }
41
42 try {
44 if( v.id == transaction_id_type() ) throw false;
45 return v;
46 } catch( ... ) {
47 SYS_THROW(chain::invalid_http_request, "Invalid transaction id");
48 }
49}
50
51#define CALL_WITH_400(api_name, api_handle, api_namespace, call_name, http_response_code, params_type) \
52{std::string("/v1/" #api_name "/" #call_name), \
53 [api_handle](string, string body, url_response_callback cb) mutable { \
54 api_handle.validate(); \
55 try { \
56 auto params = parse_params<api_namespace::call_name ## _params, params_type>(body);\
57 fc::variant result( api_handle.call_name( std::move(params) ) ); \
58 cb(http_response_code, std::move(result)); \
59 } catch (...) { \
60 http_plugin::handle_exception(#api_name, #call_name, body, cb); \
61 } \
62 }}
63
64#define CALL_ASYNC_WITH_400(api_name, api_handle, api_namespace, call_name, call_result, http_response_code, params_type) \
65{std::string("/v1/" #api_name "/" #call_name), \
66 [api_handle](string, string body, url_response_callback cb) mutable { \
67 api_handle.validate(); \
68 try { \
69 auto params = parse_params<api_namespace::call_name ## _params, params_type>(body);\
70 api_handle.call_name( std::move(params),\
71 [cb, body](const std::variant<fc::exception_ptr, call_result>& result){\
72 if (std::holds_alternative<fc::exception_ptr>(result)) {\
73 try {\
74 std::get<fc::exception_ptr>(result)->dynamic_rethrow_exception();\
75 } catch (...) {\
76 http_plugin::handle_exception(#api_name, #call_name, body, cb);\
77 }\
78 } else {\
79 cb(http_response_code, std::visit(async_result_visitor(), result));\
80 }\
81 });\
82 } catch (...) { \
83 http_plugin::handle_exception(#api_name, #call_name, body, cb); \
84 } \
85 }\
86}
87
88#define CHAIN_RO_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)
89#define CHAIN_RW_CALL(call_name, http_response_code, params_type) CALL_WITH_400(chain, rw_api, chain_apis::read_write, call_name, http_response_code, params_type)
90#define CHAIN_RO_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, ro_api, chain_apis::read_only, call_name, call_result, http_response_code, params_type)
91#define CHAIN_RW_CALL_ASYNC(call_name, call_result, http_response_code, params_type) CALL_ASYNC_WITH_400(chain, rw_api, chain_apis::read_write, call_name, call_result, http_response_code, params_type)
92
93#define CHAIN_RO_CALL_WITH_400(call_name, http_response_code, params_type) CALL_WITH_400(chain, ro_api, chain_apis::read_only, call_name, http_response_code, params_type)
94
96 ilog( "starting chain_api_plugin" );
97 my.reset(new chain_api_plugin_impl(app().get_plugin<chain_plugin>().chain()));
98 auto& chain = app().get_plugin<chain_plugin>();
99 auto ro_api = chain.get_read_only_api();
100 auto rw_api = chain.get_read_write_api();
101
102 auto& _http_plugin = app().get_plugin<http_plugin>();
103 ro_api.set_shorten_abi_errors( !_http_plugin.verbose_errors() );
104
105 _http_plugin.add_api( {
107 _http_plugin.add_api({
108 CHAIN_RO_CALL(get_activated_protocol_features, 200, http_params_types::possible_no_params),
111 CHAIN_RO_CALL(get_block_header_state, 200, http_params_types::params_required),
116 CHAIN_RO_CALL(get_raw_code_and_abi, 200, http_params_types::params_required),
119 CHAIN_RO_CALL(get_table_by_scope, 200, http_params_types::params_required),
120 CHAIN_RO_CALL(get_currency_balance, 200, http_params_types::params_required),
121 CHAIN_RO_CALL(get_currency_stats, 200, http_params_types::params_required),
123 CHAIN_RO_CALL(get_producer_schedule, 200, http_params_types::no_params),
124 CHAIN_RO_CALL(get_scheduled_transactions, 200, http_params_types::params_required),
127 CHAIN_RO_CALL(get_required_keys, 200, http_params_types::params_required),
128 CHAIN_RO_CALL(get_transaction_id, 200, http_params_types::params_required),
135 });
136
137 if (chain.account_queries_enabled()) {
138 _http_plugin.add_async_api({
139 CHAIN_RO_CALL_WITH_400(get_accounts_by_authorizers, 200, http_params_types::params_required),
140 });
141 }
142
143 if (chain.transaction_finality_status_enabled()) {
144 _http_plugin.add_async_api({
146 });
147 }
148}
149
151
152}
#define SYS_THROW(exc_type, FORMAT,...)
#define CHAIN_RO_CALL_ASYNC(call_name, call_result, http_response_code, params_type)
#define CHAIN_RW_CALL_ASYNC(call_name, call_result, http_response_code, params_type)
#define CHAIN_RO_CALL_WITH_400(call_name, http_response_code, params_type)
#define CHAIN_RO_CALL(call_name, http_response_code, params_type)
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
T as() const
Definition variant.hpp:327
virtual void set_program_options(options_description &, options_description &) override
void plugin_initialize(const variables_map &)
chain_apis::read_only get_read_only_api() const
#define ilog(FORMAT,...)
Definition logger.hpp:118
application & app()
chain_apis::read_only::get_transaction_status_params parse_params< chain_apis::read_only::get_transaction_status_params, http_params_types::params_required >(const std::string &body)
#define T(meth, val, expected)
void get_account(const string &accountName, const string &coresym, bool json_format)
Definition main.cpp:2211
sysio::chain_apis::read_only::get_info_results get_info()
Definition main.cpp:287
void send_transaction(signed_transaction &trx, packed_transaction::compression_type compression=packed_transaction::compression_type::none)
Definition main.cpp:641
fc::variant push_transaction(signed_transaction &trx, packed_transaction::compression_type compression=packed_transaction::compression_type::none)
Definition main.cpp:325
static constexpr int medium_high
fc::variant operator()(const T &v) const