This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve #12: Implement basic chain API
Only one call, but that call works!
- Loading branch information
1 parent
9f70932
commit fcb8198
Showing
8 changed files
with
129 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
file(GLOB HEADERS "include/eos/chain_plugin/*.hpp") | ||
add_library( chain_api_plugin | ||
chain_api_plugin.cpp | ||
${HEADERS} ) | ||
|
||
target_link_libraries( chain_api_plugin chain_plugin http_plugin appbase ) | ||
target_include_directories( chain_api_plugin PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ) | ||
|
||
install( TARGETS | ||
chain_api_plugin | ||
|
||
RUNTIME DESTINATION bin | ||
LIBRARY DESTINATION lib | ||
ARCHIVE DESTINATION lib | ||
) | ||
install( FILES ${HEADERS} DESTINATION "include/eos/chain_api_plugin" ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <eos/chain_api_plugin/chain_api_plugin.hpp> | ||
#include <eos/chain/exceptions.hpp> | ||
|
||
#include <fc/io/json.hpp> | ||
|
||
namespace eos { | ||
|
||
using namespace eos; | ||
|
||
class chain_api_plugin_impl { | ||
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; | ||
}; | ||
|
||
|
||
chain_api_plugin::chain_api_plugin() | ||
:my(new chain_api_plugin_impl(app().get_plugin<chain_plugin>().db())) {} | ||
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&) {} | ||
|
||
void chain_api_plugin::plugin_startup() { | ||
app().get_plugin<http_plugin>().add_api({ | ||
{std::string("/v1/chain/get_info"), [this](string, string, url_response_callback cb) {my->get_info(cb);}} | ||
}); | ||
} | ||
|
||
void chain_api_plugin::plugin_shutdown() {} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
plugins/chain_api_plugin/include/eos/chain_api_plugin/chain_api_plugin.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#include <eos/chain_plugin/chain_plugin.hpp> | ||
#include <eos/http_plugin/http_plugin.hpp> | ||
|
||
#include <appbase/application.hpp> | ||
#include <eos/chain/database.hpp> | ||
|
||
namespace eos { | ||
using eos::chain::database; | ||
using std::unique_ptr; | ||
using namespace appbase; | ||
|
||
class chain_api_plugin : public plugin<chain_api_plugin> { | ||
public: | ||
APPBASE_PLUGIN_REQUIRES((chain_plugin)(http_plugin)) | ||
|
||
chain_api_plugin(); | ||
virtual ~chain_api_plugin(); | ||
|
||
virtual void set_program_options(options_description&, options_description&) override; | ||
|
||
void plugin_initialize(const variables_map&); | ||
void plugin_startup(); | ||
void plugin_shutdown(); | ||
|
||
private: | ||
unique_ptr<class chain_api_plugin_impl> my; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@bytemaster Please review this. I made this change because before, the response body would always come back empty. The 404 response, circa line 133, was working so I removed the
http_ios.post
call to match, and now the normal response works. Is this correct?