rippled
Handler.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/rpc/handlers/Handlers.h>
21 #include <ripple/rpc/handlers/Version.h>
22 #include <ripple/rpc/impl/Handler.h>
23 #include <ripple/rpc/impl/RPCHelpers.h>
24 
25 namespace ripple {
26 namespace RPC {
27 namespace {
28 
30 template <typename Function>
31 Handler::Method<Json::Value>
32 byRef(Function const& f)
33 {
34  return [f](JsonContext& context, Json::Value& result) {
35  result = f(context);
36  if (result.type() != Json::objectValue)
37  {
38  assert(false);
39  result = RPC::makeObjectValue(result);
40  }
41 
42  return Status();
43  };
44 }
45 
46 template <class Object, class HandlerImpl>
47 Status
48 handle(JsonContext& context, Object& object)
49 {
50  HandlerImpl handler(context);
51 
52  auto status = handler.check();
53  if (status)
54  status.inject(object);
55  else
56  handler.writeResult(object);
57  return status;
58 };
59 
60 Handler const handlerArray[]{
61  // Some handlers not specified here are added to the table via addHandler()
62  // Request-response methods
63  {"account_info", byRef(&doAccountInfo), Role::USER, NO_CONDITION},
64  {"account_currencies",
65  byRef(&doAccountCurrencies),
66  Role::USER,
67  NO_CONDITION},
68  {"account_lines", byRef(&doAccountLines), Role::USER, NO_CONDITION},
69  {"account_channels", byRef(&doAccountChannels), Role::USER, NO_CONDITION},
70  {"account_nfts", byRef(&doAccountNFTs), Role::USER, NO_CONDITION},
71  {"account_objects", byRef(&doAccountObjects), Role::USER, NO_CONDITION},
72  {"account_offers", byRef(&doAccountOffers), Role::USER, NO_CONDITION},
73  {"account_tx", byRef(&doAccountTxJson), Role::USER, NO_CONDITION},
74  {"blacklist", byRef(&doBlackList), Role::ADMIN, NO_CONDITION},
75  {"book_offers", byRef(&doBookOffers), Role::USER, NO_CONDITION},
76  {"can_delete", byRef(&doCanDelete), Role::ADMIN, NO_CONDITION},
77  {"channel_authorize", byRef(&doChannelAuthorize), Role::USER, NO_CONDITION},
78  {"channel_verify", byRef(&doChannelVerify), Role::USER, NO_CONDITION},
79  {"connect", byRef(&doConnect), Role::ADMIN, NO_CONDITION},
80  {"consensus_info", byRef(&doConsensusInfo), Role::ADMIN, NO_CONDITION},
81  {"deposit_authorized",
82  byRef(&doDepositAuthorized),
83  Role::USER,
84  NO_CONDITION},
85  {"download_shard", byRef(&doDownloadShard), Role::ADMIN, NO_CONDITION},
86 #ifdef RIPPLED_REPORTING
87  {"gateway_balances", byRef(&doGatewayBalances), Role::ADMIN, NO_CONDITION},
88 #else
89  {"gateway_balances", byRef(&doGatewayBalances), Role::USER, NO_CONDITION},
90 #endif
91  {"get_counts", byRef(&doGetCounts), Role::ADMIN, NO_CONDITION},
92  {"feature", byRef(&doFeature), Role::ADMIN, NO_CONDITION},
93  {"fee", byRef(&doFee), Role::USER, NEEDS_CURRENT_LEDGER},
94  {"fetch_info", byRef(&doFetchInfo), Role::ADMIN, NO_CONDITION},
95  {"ledger_accept",
96  byRef(&doLedgerAccept),
99  {"ledger_cleaner",
100  byRef(&doLedgerCleaner),
101  Role::ADMIN,
103  {"ledger_closed", byRef(&doLedgerClosed), Role::USER, NEEDS_CLOSED_LEDGER},
104  {"ledger_current",
105  byRef(&doLedgerCurrent),
106  Role::USER,
108  {"ledger_data", byRef(&doLedgerData), Role::USER, NO_CONDITION},
109  {"ledger_entry", byRef(&doLedgerEntry), Role::USER, NO_CONDITION},
110  {"ledger_header", byRef(&doLedgerHeader), Role::USER, NO_CONDITION},
111  {"ledger_request", byRef(&doLedgerRequest), Role::ADMIN, NO_CONDITION},
112  {"log_level", byRef(&doLogLevel), Role::ADMIN, NO_CONDITION},
113  {"logrotate", byRef(&doLogRotate), Role::ADMIN, NO_CONDITION},
114  {"manifest", byRef(&doManifest), Role::USER, NO_CONDITION},
115  {"nft_buy_offers", byRef(&doNFTBuyOffers), Role::USER, NO_CONDITION},
116  {"nft_sell_offers", byRef(&doNFTSellOffers), Role::USER, NO_CONDITION},
117  {"node_to_shard", byRef(&doNodeToShard), Role::ADMIN, NO_CONDITION},
118  {"noripple_check", byRef(&doNoRippleCheck), Role::USER, NO_CONDITION},
119  {"owner_info", byRef(&doOwnerInfo), Role::USER, NEEDS_CURRENT_LEDGER},
120  {"peers", byRef(&doPeers), Role::ADMIN, NO_CONDITION},
121  {"path_find", byRef(&doPathFind), Role::USER, NEEDS_CURRENT_LEDGER},
122  {"ping", byRef(&doPing), Role::USER, NO_CONDITION},
123  {"print", byRef(&doPrint), Role::ADMIN, NO_CONDITION},
124  // { "profile", byRef (&doProfile), Role::USER,
125  // NEEDS_CURRENT_LEDGER },
126  {"random", byRef(&doRandom), Role::USER, NO_CONDITION},
127  {"peer_reservations_add",
128  byRef(&doPeerReservationsAdd),
129  Role::ADMIN,
130  NO_CONDITION},
131  {"peer_reservations_del",
132  byRef(&doPeerReservationsDel),
133  Role::ADMIN,
134  NO_CONDITION},
135  {"peer_reservations_list",
136  byRef(&doPeerReservationsList),
137  Role::ADMIN,
138  NO_CONDITION},
139  {"ripple_path_find", byRef(&doRipplePathFind), Role::USER, NO_CONDITION},
140  {"sign", byRef(&doSign), Role::USER, NO_CONDITION},
141  {"sign_for", byRef(&doSignFor), Role::USER, NO_CONDITION},
142  {"submit", byRef(&doSubmit), Role::USER, NEEDS_CURRENT_LEDGER},
143  {"submit_multisigned",
144  byRef(&doSubmitMultiSigned),
145  Role::USER,
147  {"server_info", byRef(&doServerInfo), Role::USER, NO_CONDITION},
148  {"server_state", byRef(&doServerState), Role::USER, NO_CONDITION},
149  {"crawl_shards", byRef(&doCrawlShards), Role::ADMIN, NO_CONDITION},
150  {"stop", byRef(&doStop), Role::ADMIN, NO_CONDITION},
151  {"transaction_entry", byRef(&doTransactionEntry), Role::USER, NO_CONDITION},
152  {"tx", byRef(&doTxJson), Role::USER, NEEDS_NETWORK_CONNECTION},
153  {"tx_history", byRef(&doTxHistory), Role::USER, NO_CONDITION},
154  {"tx_reduce_relay", byRef(&doTxReduceRelay), Role::USER, NO_CONDITION},
155  {"unl_list", byRef(&doUnlList), Role::ADMIN, NO_CONDITION},
156  {"validation_create",
157  byRef(&doValidationCreate),
158  Role::ADMIN,
159  NO_CONDITION},
160  {"validators", byRef(&doValidators), Role::ADMIN, NO_CONDITION},
161  {"validator_list_sites",
162  byRef(&doValidatorListSites),
163  Role::ADMIN,
164  NO_CONDITION},
165  {"validator_info", byRef(&doValidatorInfo), Role::ADMIN, NO_CONDITION},
166  {"wallet_propose", byRef(&doWalletPropose), Role::ADMIN, NO_CONDITION},
167  // Evented methods
168  {"subscribe", byRef(&doSubscribe), Role::USER, NO_CONDITION},
169  {"unsubscribe", byRef(&doUnsubscribe), Role::USER, NO_CONDITION},
170 };
171 
172 class HandlerTable
173 {
174 private:
175  template <std::size_t N>
176  explicit HandlerTable(const Handler (&entries)[N])
177  {
178  for (std::size_t i = 0; i < N; ++i)
179  {
180  auto const& entry = entries[i];
181  assert(table_.find(entry.name_) == table_.end());
182  table_[entry.name_] = entry;
183  }
184 
185  // This is where the new-style handlers are added.
186  addHandler<LedgerHandler>();
187  addHandler<VersionHandler>();
188  }
189 
190 public:
191  static HandlerTable const&
192  instance()
193  {
194  static HandlerTable const handlerTable(handlerArray);
195  return handlerTable;
196  }
197 
198  Handler const*
199  getHandler(unsigned version, bool betaEnabled, std::string const& name)
200  const
201  {
202  if (version < RPC::apiMinimumSupportedVersion ||
203  version > (betaEnabled ? RPC::apiBetaVersion
205  return nullptr;
206  auto i = table_.find(name);
207  return i == table_.end() ? nullptr : &i->second;
208  }
209 
211  getHandlerNames() const
212  {
214  ret.reserve(table_.size());
215  for (auto const& i : table_)
216  ret.push_back(i.second.name_);
217  return ret;
218  }
219 
220 private:
222 
223  template <class HandlerImpl>
224  void
225  addHandler()
226  {
227  assert(table_.find(HandlerImpl::name()) == table_.end());
228 
229  Handler h;
230  h.name_ = HandlerImpl::name();
231  h.valueMethod_ = &handle<Json::Value, HandlerImpl>;
232  h.role_ = HandlerImpl::role();
233  h.condition_ = HandlerImpl::condition();
234 
235  table_[HandlerImpl::name()] = h;
236  }
237 };
238 
239 } // namespace
240 
241 Handler const*
242 getHandler(unsigned version, bool betaEnabled, std::string const& name)
243 {
244  return HandlerTable::instance().getHandler(version, betaEnabled, name);
245 }
246 
249 {
250  return HandlerTable::instance().getHandlerNames();
251 };
252 
253 } // namespace RPC
254 } // namespace ripple
ripple::doAccountNFTs
Json::Value doAccountNFTs(RPC::JsonContext &context)
General RPC command that can retrieve objects in the account root.
Definition: AccountObjects.cpp:52
ripple::doFeature
Json::Value doFeature(RPC::JsonContext &context)
Definition: Feature1.cpp:35
ripple::doLedgerAccept
Json::Value doLedgerAccept(RPC::JsonContext &)
Definition: LedgerAccept.cpp:35
ripple::doUnlList
Json::Value doUnlList(RPC::JsonContext &)
Definition: UnlList.cpp:30
ripple::doGetCounts
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:161
ripple::doLedgerEntry
Json::Value doLedgerEntry(RPC::JsonContext &)
Definition: LedgerEntry.cpp:41
ripple::doLedgerCurrent
Json::Value doLedgerCurrent(RPC::JsonContext &)
Definition: LedgerCurrent.cpp:30
ripple::doAccountCurrencies
Json::Value doAccountCurrencies(RPC::JsonContext &context)
Definition: AccountCurrenciesHandler.cpp:32
std::string
STL class.
ripple::doValidationCreate
Json::Value doValidationCreate(RPC::JsonContext &)
Definition: ValidationCreate.cpp:45
ripple::doOwnerInfo
Json::Value doOwnerInfo(RPC::JsonContext &)
Definition: OwnerInfo.cpp:34
ripple::doPathFind
Json::Value doPathFind(RPC::JsonContext &)
Definition: PathFind.cpp:33
ripple::doDownloadShard
Json::Value doDownloadShard(RPC::JsonContext &context)
RPC command that downloads and import shard archives.
Definition: DownloadShard.cpp:49
ripple::doAccountInfo
Json::Value doAccountInfo(RPC::JsonContext &context)
Definition: AccountInfo.cpp:53
ripple::doRipplePathFind
Json::Value doRipplePathFind(RPC::JsonContext &)
Definition: RipplePathFind.cpp:32
ripple::doSignFor
Json::Value doSignFor(RPC::JsonContext &)
Definition: SignFor.cpp:35
ripple::RPC::getHandler
Handler const * getHandler(unsigned version, bool betaEnabled, std::string const &name)
Definition: Handler.cpp:242
ripple::doAccountObjects
Json::Value doAccountObjects(RPC::JsonContext &context)
Definition: AccountObjects.cpp:169
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
ripple::doValidators
Json::Value doValidators(RPC::JsonContext &)
Definition: Validators.cpp:29
ripple::doValidatorListSites
Json::Value doValidatorListSites(RPC::JsonContext &)
Definition: ValidatorListSites.cpp:29
ripple::doTxReduceRelay
Json::Value doTxReduceRelay(RPC::JsonContext &)
Definition: TxReduceRelay.cpp:28
ripple::doChannelVerify
Json::Value doChannelVerify(RPC::JsonContext &)
Definition: PayChanClaim.cpp:98
ripple::RPC::NEEDS_NETWORK_CONNECTION
@ NEEDS_NETWORK_CONNECTION
Definition: Handler.h:41
ripple::doPrint
Json::Value doPrint(RPC::JsonContext &)
Definition: Print.cpp:29
ripple::doGatewayBalances
Json::Value doGatewayBalances(RPC::JsonContext &context)
Definition: GatewayBalances.cpp:53
ripple::doManifest
Json::Value doManifest(RPC::JsonContext &)
Definition: rpc/handlers/Manifest.cpp:30
ripple::doPeers
Json::Value doPeers(RPC::JsonContext &)
Definition: Peers.cpp:32
ripple::doSubmitMultiSigned
Json::Value doSubmitMultiSigned(RPC::JsonContext &)
Definition: SubmitMultiSigned.cpp:34
ripple::RPC::apiBetaVersion
constexpr unsigned int apiBetaVersion
Definition: RPCHelpers.h:244
std::vector::push_back
T push_back(T... args)
ripple::doNFTBuyOffers
Json::Value doNFTBuyOffers(RPC::JsonContext &)
Definition: NFTOffers.cpp:167
ripple::doCrawlShards
Json::Value doCrawlShards(RPC::JsonContext &context)
RPC command that reports stored shards by nodes.
Definition: CrawlShards.cpp:44
ripple::doTxHistory
Json::Value doTxHistory(RPC::JsonContext &)
Definition: TxHistory.cpp:42
ripple::doLogLevel
Json::Value doLogLevel(RPC::JsonContext &)
Definition: LogLevel.cpp:32
ripple::doSubmit
Json::Value doSubmit(RPC::JsonContext &)
Definition: Submit.cpp:48
ripple::Role::ADMIN
@ ADMIN
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::doPing
Json::Value doPing(RPC::JsonContext &)
Definition: Ping.cpp:32
ripple::doLedgerRequest
Json::Value doLedgerRequest(RPC::JsonContext &)
Definition: LedgerRequest.cpp:38
ripple::doNFTSellOffers
Json::Value doNFTSellOffers(RPC::JsonContext &)
Definition: NFTOffers.cpp:153
ripple::Role::USER
@ USER
ripple::doChannelAuthorize
Json::Value doChannelAuthorize(RPC::JsonContext &)
Definition: PayChanClaim.cpp:44
ripple::RPC::NEEDS_CURRENT_LEDGER
@ NEEDS_CURRENT_LEDGER
Definition: Handler.h:42
ripple::doRandom
Json::Value doRandom(RPC::JsonContext &)
Definition: Random.cpp:39
ripple::doValidatorInfo
Json::Value doValidatorInfo(RPC::JsonContext &)
Definition: ValidatorInfo.cpp:30
ripple::doStop
Json::Value doStop(RPC::JsonContext &)
Definition: Stop.cpp:33
ripple::doWalletPropose
Json::Value doWalletPropose(RPC::JsonContext &)
Definition: WalletPropose.cpp:67
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:40
ripple::doCanDelete
Json::Value doCanDelete(RPC::JsonContext &context)
Definition: CanDelete.cpp:35
ripple::doConsensusInfo
Json::Value doConsensusInfo(RPC::JsonContext &context)
Definition: ConsensusInfo.cpp:31
ripple::doLedgerData
Json::Value doLedgerData(RPC::JsonContext &)
Definition: LedgerData.cpp:46
ripple::doAccountOffers
Json::Value doAccountOffers(RPC::JsonContext &context)
Definition: AccountOffers.cpp:57
ripple::doLogRotate
Json::Value doLogRotate(RPC::JsonContext &)
Definition: LogRotate.cpp:28
ripple::doNoRippleCheck
Json::Value doNoRippleCheck(RPC::JsonContext &)
Definition: NoRippleCheck.cpp:61
ripple::RPC::NEEDS_CLOSED_LEDGER
@ NEEDS_CLOSED_LEDGER
Definition: Handler.h:43
std::map
STL class.
ripple::doLedgerClosed
Json::Value doLedgerClosed(RPC::JsonContext &)
Definition: LedgerClosed.cpp:29
ripple::doBookOffers
Json::Value doBookOffers(RPC::JsonContext &context)
Definition: BookOffers.cpp:35
std::experimental::filesystem::status
T status(T... args)
ripple::doPeerReservationsList
Json::Value doPeerReservationsList(RPC::JsonContext &)
Definition: Reservations.cpp:121
ripple::NodeStore::Status
Status
Return codes from Backend operations.
Definition: nodestore/Types.h:44
ripple::doTransactionEntry
Json::Value doTransactionEntry(RPC::JsonContext &)
Definition: TransactionEntry.cpp:36
ripple::doUnsubscribe
Json::Value doUnsubscribe(RPC::JsonContext &)
Definition: Unsubscribe.cpp:32
ripple::RPC::apiMinimumSupportedVersion
constexpr unsigned int apiMinimumSupportedVersion
Definition: RPCHelpers.h:242
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::doBlackList
Json::Value doBlackList(RPC::JsonContext &context)
Definition: BlackList.cpp:28
ripple::doAccountTxJson
Json::Value doAccountTxJson(RPC::JsonContext &context)
Definition: AccountTx.cpp:544
ripple::doSign
Json::Value doSign(RPC::JsonContext &)
Definition: SignHandler.cpp:33
ripple::doPeerReservationsDel
Json::Value doPeerReservationsDel(RPC::JsonContext &)
Definition: Reservations.cpp:91
ripple::doTxJson
Json::Value doTxJson(RPC::JsonContext &)
Definition: Tx.cpp:402
ripple::RPC::apiMaximumSupportedVersion
constexpr unsigned int apiMaximumSupportedVersion
Definition: RPCHelpers.h:243
ripple::doNodeToShard
Json::Value doNodeToShard(RPC::JsonContext &)
Definition: NodeToShard.cpp:33
ripple::RPC::makeObjectValue
Json::Value makeObjectValue(Value const &value, Json::StaticString const &field=jss::message)
Return a Json::objectValue with a single entry.
Definition: Handler.h:63
std::size_t
ripple::doAccountChannels
Json::Value doAccountChannels(RPC::JsonContext &context)
Definition: AccountChannels.cpp:68
ripple::doServerInfo
Json::Value doServerInfo(RPC::JsonContext &)
Definition: ServerInfo.cpp:33
ripple::doLedgerHeader
Json::Value doLedgerHeader(RPC::JsonContext &)
Definition: LedgerHeader.cpp:33
ripple::doFetchInfo
Json::Value doFetchInfo(RPC::JsonContext &context)
Definition: FetchInfo.cpp:31
ripple::doServerState
Json::Value doServerState(RPC::JsonContext &)
Definition: ServerState.cpp:31
ripple::doConnect
Json::Value doConnect(RPC::JsonContext &context)
Definition: Connect.cpp:38
ripple::doLedgerCleaner
Json::Value doLedgerCleaner(RPC::JsonContext &)
Definition: LedgerCleanerHandler.cpp:29
ripple::RPC::getHandlerNames
std::vector< char const * > getHandlerNames()
Return names of all methods.
Definition: Handler.cpp:248
ripple::doDepositAuthorized
Json::Value doDepositAuthorized(RPC::JsonContext &context)
Definition: DepositAuthorized.cpp:37
ripple::doPeerReservationsAdd
Json::Value doPeerReservationsAdd(RPC::JsonContext &)
Definition: Reservations.cpp:35
ripple::doAccountLines
Json::Value doAccountLines(RPC::JsonContext &context)
Definition: AccountLines.cpp:88
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::doFee
Json::Value doFee(RPC::JsonContext &context)
Definition: Fee1.cpp:31
ripple::doSubscribe
Json::Value doSubscribe(RPC::JsonContext &)
Definition: Subscribe.cpp:37