chain_api_plugin.cpp 3.3 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
D
Daniel Larimer 已提交
5
#include <eosio/chain_api_plugin/chain_api_plugin.hpp>
6
#include <eosio/chain/exceptions.hpp>
7 8 9

#include <fc/io/json.hpp>

P
Pravin 已提交
10
namespace eosio {
11

12 13
static appbase::abstract_plugin& _chain_api_plugin = app().register_plugin<chain_api_plugin>();

P
Pravin 已提交
14
using namespace eosio;
15 16 17

class chain_api_plugin_impl {
public:
18
   chain_api_plugin_impl(controller& db)
19 20
      : db(db) {}

21
   controller& db;
22 23 24
};


N
Nathan Hourt 已提交
25
chain_api_plugin::chain_api_plugin(){}
26 27 28 29 30
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&) {}

31
#define CALL(api_name, api_handle, api_namespace, call_name, http_response_code) \
N
Nathan Hourt 已提交
32 33
{std::string("/v1/" #api_name "/" #call_name), \
   [this, api_handle](string, string body, url_response_callback cb) mutable { \
N
Nathan Hourt 已提交
34 35 36
          try { \
             if (body.empty()) body = "{}"; \
             auto result = api_handle.call_name(fc::json::from_string(body).as<api_namespace::call_name ## _params>()); \
37
             cb(http_response_code, fc::json::to_string(result)); \
38
          } catch (chain::tx_missing_sigs& e) { \
39
             error_results results{401, "UnAuthorized", e}; \
40 41
             cb(401, fc::json::to_string(results)); \
          } catch (chain::tx_duplicate& e) { \
42
             error_results results{409, "Conflict", e}; \
43 44
             cb(409, fc::json::to_string(results)); \
          } catch (chain::transaction_exception& e) { \
45
             error_results results{400, "Bad Request", e}; \
46 47
             cb(400, fc::json::to_string(results)); \
          } catch (fc::eof_exception& e) { \
48
             error_results results{400, "Bad Request", e}; \
49
             cb(400, fc::json::to_string(results)); \
N
Nathan Hourt 已提交
50
             elog("Unable to parse arguments: ${args}", ("args", body)); \
N
Nathan Hourt 已提交
51
          } catch (fc::exception& e) { \
52
             error_results results{500, "Internal Service Error", e}; \
53
             cb(500, fc::json::to_string(results)); \
N
Nathan Hourt 已提交
54 55 56 57
             elog("Exception encountered while processing ${call}: ${e}", ("call", #api_name "." #call_name)("e", e)); \
          } \
       }}

58 59
#define CHAIN_RO_CALL(call_name, http_response_code) CALL(chain, ro_api, chain_apis::read_only, call_name, http_response_code)
#define CHAIN_RW_CALL(call_name, http_response_code) CALL(chain, rw_api, chain_apis::read_write, call_name, http_response_code)
N
Nathan Hourt 已提交
60

61
void chain_api_plugin::plugin_startup() {
62
   ilog( "starting chain_api_plugin" );
N
Nathan Hourt 已提交
63
   my.reset(new chain_api_plugin_impl(app().get_plugin<chain_plugin>().chain()));
N
Nathan Hourt 已提交
64
   auto ro_api = app().get_plugin<chain_plugin>().get_read_only_api();
N
Nathan Hourt 已提交
65
   auto rw_api = app().get_plugin<chain_plugin>().get_read_write_api();
N
Nathan Hourt 已提交
66

67
   app().get_plugin<http_plugin>().add_api({
68 69 70 71 72
      CHAIN_RO_CALL(get_info, 200),
      CHAIN_RO_CALL(get_block, 200),
      CHAIN_RO_CALL(get_account, 200),
      CHAIN_RO_CALL(get_code, 200),
      CHAIN_RO_CALL(get_table_rows, 200),
73 74
      CHAIN_RO_CALL(get_currency_balance, 200),
      CHAIN_RO_CALL(get_currency_stats, 200),
75 76 77 78 79 80
      CHAIN_RO_CALL(abi_json_to_bin, 200),
      CHAIN_RO_CALL(abi_bin_to_json, 200),
      CHAIN_RO_CALL(get_required_keys, 200),
      CHAIN_RW_CALL(push_block, 202),
      CHAIN_RW_CALL(push_transaction, 202),
      CHAIN_RW_CALL(push_transactions, 202)
81 82 83 84 85 86
   });
}

void chain_api_plugin::plugin_shutdown() {}

}