From ead686b609ffe6868e07eb7765a325171cc9edf2 Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Tue, 25 Apr 2017 16:20:12 -0500 Subject: [PATCH] Progress #14: Restructure API The beginnings of the chain API is now much closer to the design described in #14 --- plugins/chain_api_plugin/chain_api_plugin.cpp | 30 ++++----- plugins/chain_plugin/chain_plugin.cpp | 15 +++++ .../include/eos/chain_plugin/chain_plugin.hpp | 66 ++++++++++++++----- plugins/p2p_plugin/CMakeLists.txt | 1 + 4 files changed, 80 insertions(+), 32 deletions(-) diff --git a/plugins/chain_api_plugin/chain_api_plugin.cpp b/plugins/chain_api_plugin/chain_api_plugin.cpp index bcb81b9ec..35dc61180 100644 --- a/plugins/chain_api_plugin/chain_api_plugin.cpp +++ b/plugins/chain_api_plugin/chain_api_plugin.cpp @@ -12,20 +12,6 @@ public: chain_api_plugin_impl(database& db) : db(db) {} - void get_info(url_response_callback& cb) { - fc::mutable_variant_object response; - - response["head_block_num"] = db.head_block_num(); - response["head_block_id"] = db.head_block_id(); - response["head_block_time"] = db.head_block_time(); - response["head_block_producer"] = db.head_block_producer(); - response["recent_slots"] = std::bitset<64>(db.get_dynamic_global_properties().recent_slots_filled).to_string(); - response["participation_rate"] = - __builtin_popcountll(db.get_dynamic_global_properties().recent_slots_filled) / 64.0; - - cb(200, fc::json::to_string(response)); - } - database& db; }; @@ -37,9 +23,23 @@ chain_api_plugin::~chain_api_plugin(){} void chain_api_plugin::set_program_options(options_description&, options_description&) {} void chain_api_plugin::plugin_initialize(const variables_map&) {} +#define CALL(api_name, api_handle, api_namespace, call_name) \ +{std::string("/v1/" #api_name "/" #call_name), [this, api_handle](string, string body, url_response_callback cb) { \ + try { \ + if (body.empty()) body = "{}"; \ + auto result = api_handle.call_name(fc::json::from_string(body).as()); \ + cb(200, fc::json::to_string(result)); \ + } catch (fc::exception& e) { \ + cb(500, e.what()); \ + elog("Exception encountered while processing ${call}: ${e}", ("call", #api_name "." #call_name)("e", e)); \ + } \ + }} + void chain_api_plugin::plugin_startup() { + auto ro_api = app().get_plugin().get_read_only_api(); + app().get_plugin().add_api({ - {std::string("/v1/chain/get_info"), [this](string, string, url_response_callback cb) {my->get_info(cb);}} + CALL(chain, ro_api, chain_apis::read_only, get_info) }); } diff --git a/plugins/chain_plugin/chain_plugin.cpp b/plugins/chain_plugin/chain_plugin.cpp index a7d166fa4..d038f92bb 100644 --- a/plugins/chain_plugin/chain_plugin.cpp +++ b/plugins/chain_plugin/chain_plugin.cpp @@ -30,6 +30,7 @@ chain_plugin::chain_plugin() chain_plugin::~chain_plugin(){} database& chain_plugin::db() { return my->db; } +const chain::database& chain_plugin::db() const { return my->db; } void chain_plugin::set_program_options(options_description& cli, options_description& cfg) { @@ -145,4 +146,18 @@ bool chain_plugin::block_is_on_preferred_chain(const chain::block_id_type& block return db().get_block_id_for_num(chain::block_header::num_from_id(block_id)) == block_id; } +namespace chain_apis { + +read_only::get_info_results read_only::get_info(const read_only::get_info_params&) const { + return { + db.head_block_num(), + db.head_block_id(), + db.head_block_time(), + db.head_block_producer(), + std::bitset<64>(db.get_dynamic_global_properties().recent_slots_filled).to_string(), + __builtin_popcountll(db.get_dynamic_global_properties().recent_slots_filled) / 64.0 + }; } + +} // namespace chain_apis +} // namespace eos diff --git a/plugins/chain_plugin/include/eos/chain_plugin/chain_plugin.hpp b/plugins/chain_plugin/include/eos/chain_plugin/chain_plugin.hpp index 2aea23291..e1b8f6465 100644 --- a/plugins/chain_plugin/include/eos/chain_plugin/chain_plugin.hpp +++ b/plugins/chain_plugin/include/eos/chain_plugin/chain_plugin.hpp @@ -7,29 +7,61 @@ namespace eos { using std::unique_ptr; using namespace appbase; - class chain_plugin : public plugin - { - public: - APPBASE_PLUGIN_REQUIRES() +namespace chain_apis { +struct empty{}; - chain_plugin(); - virtual ~chain_plugin(); +class read_only { + const database& db; - virtual void set_program_options(options_description& cli, options_description& cfg) override; +public: + read_only(const database& db) + : db(db) {} - void plugin_initialize(const variables_map& options); - void plugin_startup(); - void plugin_shutdown(); + using get_info_params = empty; + struct get_info_results { + uint32_t head_block_num; + chain::block_id_type head_block_id; + fc::time_point_sec head_block_time; + chain::producer_id_type head_block_producer; + string recent_slots; + double participation_rate; + }; + get_info_results get_info(const get_info_params&) const; +}; +} // namespace chain_apis - bool accept_block(const chain::signed_block& block, bool currently_syncing); - void accept_transaction(const chain::signed_transaction& trx); +class chain_plugin : public plugin { +public: + APPBASE_PLUGIN_REQUIRES() - bool block_is_on_preferred_chain(const chain::block_id_type& block_id); + chain_plugin(); + virtual ~chain_plugin(); - database& db(); + virtual void set_program_options(options_description& cli, options_description& cfg) override; - private: - unique_ptr my; - }; + void plugin_initialize(const variables_map& options); + void plugin_startup(); + void plugin_shutdown(); + + chain_apis::read_only get_read_only_api() const { + return chain_apis::read_only(db()); + } + + bool accept_block(const chain::signed_block& block, bool currently_syncing); + void accept_transaction(const chain::signed_transaction& trx); + + bool block_is_on_preferred_chain(const chain::block_id_type& block_id); + + database& db(); + const database& db() const; + +private: + unique_ptr my; +}; } + +FC_REFLECT(eos::chain_apis::empty, ) +FC_REFLECT(eos::chain_apis::read_only::get_info_results, + (head_block_num)(head_block_id)(head_block_time)(head_block_producer) + (recent_slots)(participation_rate)) diff --git a/plugins/p2p_plugin/CMakeLists.txt b/plugins/p2p_plugin/CMakeLists.txt index ddbb362dc..54150174b 100644 --- a/plugins/p2p_plugin/CMakeLists.txt +++ b/plugins/p2p_plugin/CMakeLists.txt @@ -2,6 +2,7 @@ file(GLOB HEADERS "include/eos/p2p_plugin/*.hpp") add_library( p2p_plugin p2p_plugin.cpp + ${HEADERS} ) target_link_libraries( p2p_plugin chain_plugin appbase eos_chain eos_utilities eos_net ) -- GitLab