提交 ead686b6 编写于 作者: N Nathan Hourt

Progress #14: Restructure API

The beginnings of the chain API is now much closer to the design
described in #14
上级 2e08c14f
......@@ -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<api_namespace::call_name ## _params>()); \
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<chain_plugin>().get_read_only_api();
app().get_plugin<http_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)
});
}
......
......@@ -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
......@@ -7,29 +7,61 @@ namespace eos {
using std::unique_ptr;
using namespace appbase;
class chain_plugin : public plugin<chain_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<chain_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<class chain_plugin_impl> 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<class chain_plugin_impl> 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))
......@@ -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 )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册