Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Ref #14: Add read_write chain API
Browse files Browse the repository at this point in the history
Create a new read-write chain API with push_block and push_transaction
calls
  • Loading branch information
nathanielhourt committed Apr 26, 2017
1 parent 20d9c9b commit e15b635
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
9 changes: 7 additions & 2 deletions plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void chain_api_plugin::set_program_options(options_description&, options_descrip
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) { \
{std::string("/v1/" #api_name "/" #call_name), \
[this, api_handle](string, string body, url_response_callback cb) mutable { \
try { \
if (body.empty()) body = "{}"; \
auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
Expand All @@ -39,13 +40,17 @@ void chain_api_plugin::plugin_initialize(const variables_map&) {}
}}

#define CHAIN_RO_CALL(call_name) CALL(chain, ro_api, chain_apis::read_only, call_name)
#define CHAIN_RW_CALL(call_name) CALL(chain, rw_api, chain_apis::read_write, call_name)

void chain_api_plugin::plugin_startup() {
auto ro_api = app().get_plugin<chain_plugin>().get_read_only_api();
auto rw_api = app().get_plugin<chain_plugin>().get_read_write_api();

app().get_plugin<http_plugin>().add_api({
CHAIN_RO_CALL(get_info),
CHAIN_RO_CALL(get_block)
CHAIN_RO_CALL(get_block),
CHAIN_RW_CALL(push_block),
CHAIN_RW_CALL(push_transaction)
});
}

Expand Down
8 changes: 8 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,13 @@ read_only::get_block_results read_only::get_block(const read_only::get_block_par
"Could not find block: ${block}", ("block", params.block_num_or_id));
}

read_write::push_block_results read_write::push_block(const read_write::push_block_params& params) {
db.push_block(params);
}

read_write::push_transaction_results read_write::push_transaction(const read_write::push_transaction_params& params) {
db.push_transaction(params);
}

} // namespace chain_apis
} // namespace eos
19 changes: 16 additions & 3 deletions plugins/chain_plugin/include/eos/chain_plugin/chain_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class read_only {
using get_block_results = chain::signed_block;
get_block_results get_block(const get_block_params& params) const;
};

class read_write {
database& db;
public:
read_write(database& db) : db(db) {}

using push_block_params = chain::signed_block;
using push_block_results = empty;
push_block_results push_block(const push_block_params& params);

using push_transaction_params = chain::signed_transaction;
using push_transaction_results = empty;
push_transaction_results push_transaction(const push_transaction_params& params);
};
} // namespace chain_apis

class chain_plugin : public plugin<chain_plugin> {
Expand All @@ -49,9 +63,8 @@ class chain_plugin : public plugin<chain_plugin> {
void plugin_startup();
void plugin_shutdown();

chain_apis::read_only get_read_only_api() const {
return chain_apis::read_only(db());
}
chain_apis::read_only get_read_only_api() const { return chain_apis::read_only(db()); }
chain_apis::read_write get_read_write_api() { return chain_apis::read_write(db()); }

bool accept_block(const chain::signed_block& block, bool currently_syncing);
void accept_transaction(const chain::signed_transaction& trx);
Expand Down

0 comments on commit e15b635

Please sign in to comment.