rippled
CanDelete.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/ledger/LedgerMaster.h>
21 #include <ripple/app/main/Application.h>
22 #include <ripple/app/misc/NetworkOPs.h>
23 #include <ripple/app/misc/SHAMapStore.h>
24 #include <ripple/beast/core/LexicalCast.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/jss.h>
27 #include <ripple/rpc/Context.h>
28 #include <boost/algorithm/string/case_conv.hpp>
29 #include <boost/format.hpp>
30 
31 namespace ripple {
32 
33 // can_delete [<ledgerid>|<ledgerhash>|now|always|never]
36 {
37  if (context.app.config().reporting())
39 
40  if (!context.app.getSHAMapStore().advisoryDelete())
42 
44 
45  if (context.params.isMember(jss::can_delete))
46  {
47  Json::Value canDelete = context.params.get(jss::can_delete, 0);
48  std::uint32_t canDeleteSeq = 0;
49 
50  if (canDelete.isUInt())
51  {
52  canDeleteSeq = canDelete.asUInt();
53  }
54  else
55  {
56  std::string canDeleteStr = canDelete.asString();
57  boost::to_lower(canDeleteStr);
58 
59  if (canDeleteStr.find_first_not_of("0123456789") ==
60  std::string::npos)
61  {
62  canDeleteSeq = beast::lexicalCast<std::uint32_t>(canDeleteStr);
63  }
64  else if (canDeleteStr == "never")
65  {
66  canDeleteSeq = 0;
67  }
68  else if (canDeleteStr == "always")
69  {
71  }
72  else if (canDeleteStr == "now")
73  {
74  canDeleteSeq = context.app.getSHAMapStore().getLastRotated();
75  if (!canDeleteSeq)
77  }
78  else if (uint256 lh; lh.parseHex(canDeleteStr))
79  {
80  auto ledger = context.ledgerMaster.getLedgerByHash(lh);
81 
82  if (!ledger)
83  return RPC::make_error(rpcLGR_NOT_FOUND, "ledgerNotFound");
84 
85  canDeleteSeq = ledger->info().seq;
86  }
87  else
88  {
90  }
91  }
92 
93  ret[jss::can_delete] =
94  context.app.getSHAMapStore().setCanDelete(canDeleteSeq);
95  }
96  else
97  {
98  ret[jss::can_delete] = context.app.getSHAMapStore().getCanDelete();
99  }
100 
101  return ret;
102 }
103 
104 } // namespace ripple
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
ripple::rpcINVALID_PARAMS
@ rpcINVALID_PARAMS
Definition: ErrorCodes.h:84
Json::Value::get
Value get(UInt index, const Value &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
Definition: json_value.cpp:834
ripple::rpcNOT_READY
@ rpcNOT_READY
Definition: ErrorCodes.h:60
ripple::RPC::Context::ledgerMaster
LedgerMaster & ledgerMaster
Definition: Context.h:45
ripple::LedgerMaster::getLedgerByHash
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
Definition: LedgerMaster.cpp:1854
std::string::find_first_not_of
T find_first_not_of(T... args)
ripple::rpcLGR_NOT_FOUND
@ rpcLGR_NOT_FOUND
Definition: ErrorCodes.h:72
ripple::rpcREPORTING_UNSUPPORTED
@ rpcREPORTING_UNSUPPORTED
Definition: ErrorCodes.h:141
ripple::SHAMapStore::getLastRotated
virtual LedgerIndex getLastRotated()=0
Maximum ledger that has been deleted, or will be deleted if currently in the act of online deletion.
ripple::base_uint< 256 >
ripple::SHAMapStore::advisoryDelete
virtual bool advisoryDelete() const =0
Whether advisory delete is enabled.
ripple::Config::reporting
bool reporting() const
Definition: Config.h:337
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::SHAMapStore::setCanDelete
virtual LedgerIndex setCanDelete(LedgerIndex canDelete)=0
Highest ledger that may be deleted.
ripple::Application::config
virtual Config & config()=0
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::doCanDelete
Json::Value doCanDelete(RPC::JsonContext &context)
Definition: CanDelete.cpp:35
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
std::uint32_t
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
Json::Value::isUInt
bool isUInt() const
Definition: json_value.cpp:985
ripple::SHAMapStore::getCanDelete
virtual LedgerIndex getCanDelete()=0
Highest ledger that may be deleted.
ripple::Application::getSHAMapStore
virtual SHAMapStore & getSHAMapStore()=0
std::numeric_limits::max
T max(T... args)
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:496
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
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