chain_api_plugin.cpp 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#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:
12
   chain_api_plugin_impl(chain_controller& db)
13 14
      : db(db) {}

15
   chain_controller& db;
16 17 18
};


N
Nathan Hourt 已提交
19
chain_api_plugin::chain_api_plugin(){}
20 21 22 23 24
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&) {}

N
Nathan Hourt 已提交
25
#define CALL(api_name, api_handle, api_namespace, call_name) \
N
Nathan Hourt 已提交
26 27
{std::string("/v1/" #api_name "/" #call_name), \
   [this, api_handle](string, string body, url_response_callback cb) mutable { \
N
Nathan Hourt 已提交
28 29 30 31
          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)); \
N
Nathan Hourt 已提交
32 33 34
          } catch (fc::eof_exception) { \
             cb(400, "Invalid arguments"); \
             elog("Unable to parse arguments: ${args}", ("args", body)); \
N
Nathan Hourt 已提交
35
          } catch (fc::exception& e) { \
36
             cb(500, e.to_detail_string()); \
N
Nathan Hourt 已提交
37 38 39 40
             elog("Exception encountered while processing ${call}: ${e}", ("call", #api_name "." #call_name)("e", e)); \
          } \
       }}

N
Nathan Hourt 已提交
41
#define CHAIN_RO_CALL(call_name) CALL(chain, ro_api, chain_apis::read_only, call_name)
N
Nathan Hourt 已提交
42
#define CHAIN_RW_CALL(call_name) CALL(chain, rw_api, chain_apis::read_write, call_name)
N
Nathan Hourt 已提交
43

44
void chain_api_plugin::plugin_startup() {
45
   ilog( "starting chain_api_plugin" );
N
Nathan Hourt 已提交
46
   my.reset(new chain_api_plugin_impl(app().get_plugin<chain_plugin>().chain()));
N
Nathan Hourt 已提交
47
   auto ro_api = app().get_plugin<chain_plugin>().get_read_only_api();
N
Nathan Hourt 已提交
48
   auto rw_api = app().get_plugin<chain_plugin>().get_read_write_api();
N
Nathan Hourt 已提交
49

50
   app().get_plugin<http_plugin>().add_api({
N
Nathan Hourt 已提交
51
      CHAIN_RO_CALL(get_info),
N
Nathan Hourt 已提交
52
      CHAIN_RO_CALL(get_block),
53
      CHAIN_RO_CALL(get_account),
D
Daniel Larimer 已提交
54
      CHAIN_RO_CALL(get_table_rows_i64),
55
      CHAIN_RO_CALL(get_table_rows_i128i128_primary),
56
      CHAIN_RO_CALL(abi_json_to_bin),
N
Nathan Hourt 已提交
57 58
      CHAIN_RW_CALL(push_block),
      CHAIN_RW_CALL(push_transaction)
59 60 61 62 63 64
   });
}

void chain_api_plugin::plugin_shutdown() {}

}