chain_api_plugin.cpp 3.2 KB
Newer Older
1 2 3 4
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
5
#include <eos/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

P
Pravin 已提交
12
using namespace eosio;
13 14 15

class chain_api_plugin_impl {
public:
16
   chain_api_plugin_impl(chain_controller& db)
17 18
      : db(db) {}

19
   chain_controller& db;
20 21 22
};


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

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

56 57
#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 已提交
58

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

65
   app().get_plugin<http_plugin>().add_api({
66 67 68 69 70 71 72 73 74 75 76
      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),
      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)
77 78 79 80 81 82
   });
}

void chain_api_plugin::plugin_shutdown() {}

}