Example #1
0
bool RpcServer::processJsonRpcRequest(const HttpRequest& request, HttpResponse& response) {

  using namespace JsonRpc;

  response.addHeader("Content-Type", "application/json");

  JsonRpcRequest jsonRequest;
  JsonRpcResponse jsonResponse;

  try {
    logger(TRACE) << "JSON-RPC request: " << request.getBody();
    jsonRequest.parseRequest(request.getBody());
    jsonResponse.setId(jsonRequest.getId()); // copy id

    static std::unordered_map<std::string, JsonMemberMethod> jsonRpcHandlers = {
      { "getblockcount", makeMemberMethod(&RpcServer::on_getblockcount) },
      { "on_getblockhash", makeMemberMethod(&RpcServer::on_getblockhash) },
      { "getblocktemplate", makeMemberMethod(&RpcServer::on_getblocktemplate) },
      { "getcurrencyid", makeMemberMethod(&RpcServer::on_get_currency_id) },
      { "submitblock", makeMemberMethod(&RpcServer::on_submitblock) },
      { "getlastblockheader", makeMemberMethod(&RpcServer::on_get_last_block_header) },
      { "getblockheaderbyhash", makeMemberMethod(&RpcServer::on_get_block_header_by_hash) },
      { "getblockheaderbyheight", makeMemberMethod(&RpcServer::on_get_block_header_by_height) }
    };

    auto it = jsonRpcHandlers.find(jsonRequest.getMethod());
    if (it == jsonRpcHandlers.end()) {
      throw JsonRpcError(JsonRpc::errMethodNotFound);
    }

    if (jsonRequest.getMethod() != "getcurrencyid" && !checkCoreReady()) {
      throw JsonRpcError(CORE_RPC_ERROR_CODE_CORE_BUSY, "Core is busy");
    }

    it->second(this, jsonRequest, jsonResponse);

  } catch (const JsonRpcError& err) {
    jsonResponse.setError(err);
  } catch (const std::exception& e) {
    jsonResponse.setError(JsonRpcError(JsonRpc::errInternalError, e.what()));
  }

  response.setBody(jsonResponse.getBody());
  logger(TRACE) << "JSON-RPC response: " << jsonResponse.getBody();
  return true;
}
void wallet_rpc_server::processRequest(const CryptoNote::HttpRequest& request, CryptoNote::HttpResponse& response) {

  using namespace CryptoNote::JsonRpc;

  JsonRpcRequest jsonRequest;
  JsonRpcResponse jsonResponse;

  try {
    jsonRequest.parseRequest(request.getBody());
    jsonResponse.setId(jsonRequest.getId());

    static std::unordered_map<std::string, JsonMemberMethod> s_methods = {
      { "getbalance", makeMemberMethod(&wallet_rpc_server::on_getbalance) },
      { "transfer", makeMemberMethod(&wallet_rpc_server::on_transfer) },
      { "store", makeMemberMethod(&wallet_rpc_server::on_store) },
      { "get_messages", makeMemberMethod(&wallet_rpc_server::on_get_messages) },
      { "get_payments", makeMemberMethod(&wallet_rpc_server::on_get_payments) },
      { "get_transfers", makeMemberMethod(&wallet_rpc_server::on_get_transfers) },
      { "get_height", makeMemberMethod(&wallet_rpc_server::on_get_height) },
      { "reset", makeMemberMethod(&wallet_rpc_server::on_reset) }
    };

    auto it = s_methods.find(jsonRequest.getMethod());
    if (it == s_methods.end()) {
      throw JsonRpcError(errMethodNotFound);
    }

    it->second(this, jsonRequest, jsonResponse);

  } catch (const JsonRpcError& err) {
    jsonResponse.setError(err);
  } catch (const std::exception& e) {
    jsonResponse.setError(JsonRpcError(WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR, e.what()));
  }

  response.setBody(jsonResponse.getBody());
}
Example #3
0
IResponsePtr RpcMethods::Post(const std::string& req)
{
        JsonRpcRequest request;
        IResponsePtr response;
        string id = "";
        try{
            request.Deserialize(req);
            id = request.GetId();
            auto method = request.GetMethod();
            if(this->paths.find(method) == this->paths.end())
                response = make_shared<ErrorJsonRpcResponse>(-32601,"Method does not exist",id);
            else
                response = make_shared<ValidJsonRpcResponse>(this->paths[method]->Execute(request.GetParams()),id);
        }
        catch(const DataStoreException& ex)
        {
            response = make_shared<ErrorJsonRpcResponse>(-32603,ex.what(), id);
        }
        catch(const std::exception& ex)
        {
            response = make_shared<ErrorJsonRpcResponse>(-32600,ex.what(),id);
        }
        return response;
}
Example #4
0
void invokeJsonRpcCommand(HttpClient& httpClient, JsonRpcRequest& jsReq, JsonRpcResponse& jsRes) {
  HttpRequest httpReq;
  HttpResponse httpRes;

  httpReq.setUrl("/json_rpc");
  httpReq.setBody(jsReq.getBody());

  httpClient.request(httpReq, httpRes);

  if (httpRes.getStatus() != HttpResponse::STATUS_200) {
    throw std::runtime_error("JSON-RPC call failed, HTTP status = " + std::to_string(httpRes.getStatus()));
  }

  jsRes.parse(httpRes.getBody());

  JsonRpcError err;
  if (jsRes.getError(err)) {
    throw err;
  }
}