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

Commit

Permalink
Progress #14: Add get_block call
Browse files Browse the repository at this point in the history
Add get_block call to chain read_only api
  • Loading branch information
nathanielhourt committed Apr 25, 2017
1 parent ead686b commit 5e61e56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugins/chain_api_plugin/chain_api_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void chain_api_plugin::plugin_initialize(const variables_map&) {}
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::eof_exception) { \
cb(400, "Invalid arguments"); \
elog("Unable to parse arguments: ${args}", ("args", body)); \
} catch (fc::exception& e) { \
cb(500, e.what()); \
elog("Exception encountered while processing ${call}: ${e}", ("call", #api_name "." #call_name)("e", e)); \
Expand All @@ -39,7 +42,8 @@ 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({
CALL(chain, ro_api, chain_apis::read_only, get_info)
CALL(chain, ro_api, chain_apis::read_only, get_info),
CALL(chain, ro_api, chain_apis::read_only, get_block)
});
}

Expand Down
14 changes: 14 additions & 0 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,19 @@ read_only::get_info_results read_only::get_info(const read_only::get_info_params
};
}

read_only::get_block_results read_only::get_block(const read_only::get_block_params& params) const {
try {
if (auto block = db.fetch_block_by_id(fc::json::from_string(params.block_num_or_id).as<chain::block_id_type>()))
return *block;
} catch (fc::bad_cast_exception) {/* do nothing */}
try {
if (auto block = db.fetch_block_by_number(fc::to_uint64(params.block_num_or_id)))
return *block;
} catch (fc::bad_cast_exception) {/* do nothing */}

FC_THROW_EXCEPTION(chain::unknown_block_exception,
"Could not find block: ${block}", ("block", params.block_num_or_id));
}

} // namespace chain_apis
} // namespace eos
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class read_only {
double participation_rate;
};
get_info_results get_info(const get_info_params&) const;

struct get_block_params {
string block_num_or_id;
};
using get_block_results = chain::signed_block;
get_block_results get_block(const get_block_params& params) const;
};
} // namespace chain_apis

Expand Down Expand Up @@ -65,3 +71,4 @@ 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))
FC_REFLECT(eos::chain_apis::read_only::get_block_params, (block_num_or_id))

0 comments on commit 5e61e56

Please sign in to comment.