rippled
NodeToShard.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2021 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/main/Application.h>
21 #include <ripple/app/misc/NetworkOPs.h>
22 #include <ripple/json/json_value.h>
23 #include <ripple/net/RPCErr.h>
24 #include <ripple/nodestore/DatabaseShard.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/rpc/Context.h>
28 
29 namespace ripple {
30 
31 // node_to_shard [status|start|stop]
34 {
35  if (context.app.config().reporting())
37 
38  // Shard store must be enabled
39  auto const shardStore = context.app.getShardStore();
40  if (!shardStore)
42 
43  if (!context.params.isMember(jss::action))
44  return RPC::missing_field_error(jss::action);
45 
46  // Obtain and normalize the action to perform
47  auto const action = [&context] {
48  auto value = context.params[jss::action].asString();
49  boost::to_lower(value);
50 
51  return value;
52  }();
53 
54  // Vector of allowed actions
55  std::vector<std::string> const allowedActions = {"status", "start", "stop"};
56 
57  // Validate the action
58  if (std::find(allowedActions.begin(), allowedActions.end(), action) ==
59  allowedActions.end())
60  return RPC::invalid_field_error(jss::action);
61 
62  // Perform the action
63  if (action == "status")
64  {
65  // Get the status of the database import
66  return shardStore->getDatabaseImportStatus();
67  }
68  else if (action == "start")
69  {
70  // Kick off an import
71  return shardStore->startNodeToShard();
72  }
73  else if (action == "stop")
74  {
75  // Halt an import
76  return shardStore->stopNodeToShard();
77  }
78  else
79  {
80  // Shouldn't happen
81  assert(false);
82  return rpcError(rpcINTERNAL);
83  }
84 }
85 
86 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
std::vector< std::string >
std::find
T find(T... args)
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
ripple::Application::config
virtual Config & config()=0
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::rpcNOT_ENABLED
@ rpcNOT_ENABLED
Definition: ErrorCodes.h:59
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::rpcINTERNAL
@ rpcINTERNAL
Definition: ErrorCodes.h:130
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::vector::begin
T begin(T... args)
ripple::doNodeToShard
Json::Value doNodeToShard(RPC::JsonContext &)
Definition: NodeToShard.cpp:33
std::vector::end
T end(T... args)
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::RPC::invalid_field_error
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:304
ripple::RPC::make_error
Json::Value make_error(error_code_i code)
Returns a new json object that reflects the error code.
Definition: ErrorCodes.cpp:178
Json::Value
Represents a JSON value.
Definition: json_value.h:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469