xrpld
Loading...
Searching...
No Matches
CanDelete.cpp
1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/main/Application.h>
3#include <xrpld/app/misc/SHAMapStore.h>
4#include <xrpld/rpc/Context.h>
5
6#include <xrpl/basics/StringUtilities.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/beast/core/LexicalCast.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/protocol/ErrorCodes.h>
11#include <xrpl/protocol/jss.h>
12
13#include <cstdint>
14#include <limits>
15#include <string>
16
17namespace xrpl {
18
19// can_delete [<ledgerid>|<ledgerhash>|now|always|never]
20json::Value
22{
23 if (!context.app.getSHAMapStore().advisoryDelete())
25
27
28 if (context.params.isMember(jss::can_delete))
29 {
30 json::Value const canDelete = context.params.get(jss::can_delete, 0);
31 std::uint32_t canDeleteSeq = 0;
32
33 if (canDelete.isUInt())
34 {
35 canDeleteSeq = canDelete.asUInt();
36 }
37 else
38 {
39 std::string canDeleteStr = canDelete.asString();
40 canDeleteStr = toLower(canDeleteStr);
41
42 if (canDeleteStr.find_first_not_of("0123456789") == std::string::npos)
43 {
44 canDeleteSeq = beast::lexicalCast<std::uint32_t>(canDeleteStr);
45 }
46 else if (canDeleteStr == "never")
47 {
48 canDeleteSeq = 0;
49 }
50 else if (canDeleteStr == "always")
51 {
53 }
54 else if (canDeleteStr == "now")
55 {
56 canDeleteSeq = context.app.getSHAMapStore().getLastRotated();
57 if (canDeleteSeq == 0u)
59 }
60 else if (uint256 lh; lh.parseHex(canDeleteStr))
61 {
62 auto ledger = context.ledgerMaster.getLedgerByHash(lh);
63
64 if (!ledger)
65 return rpc::makeError(RpcLgrNotFound, "ledgerNotFound");
66
67 canDeleteSeq = ledger->header().seq;
68 }
69 else
70 {
72 }
73 }
74
75 ret[jss::can_delete] = context.app.getSHAMapStore().setCanDelete(canDeleteSeq);
76 }
77 else
78 {
79 ret[jss::can_delete] = context.app.getSHAMapStore().getCanDelete();
80 }
81
82 return ret;
83}
84
85} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
Value get(UInt index, Value const &defaultValue) const
If the array contains at least index+1 elements, returns the element value, otherwise returns default...
bool isUInt() const
UInt asUInt() const
std::string asString() const
Returns the unquoted string value.
bool isMember(char const *key) const
Return true if the object has a member named key.
std::shared_ptr< Ledger const > getLedgerByHash(uint256 const &hash)
virtual LedgerIndex getCanDelete()=0
Highest ledger that may be deleted.
virtual bool advisoryDelete() const =0
Whether advisory delete is enabled.
virtual LedgerIndex getLastRotated()=0
Maximum ledger that has been deleted, or will be deleted if currently in the act of online deletion.
virtual LedgerIndex setCanDelete(LedgerIndex canDelete)=0
Highest ledger that may be deleted.
virtual SHAMapStore & getSHAMapStore()=0
T find_first_not_of(T... args)
T max(T... args)
constexpr Out lexicalCast(In in, Out defaultValue=Out())
Convert from one type to another.
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
json::Value makeError(ErrorCodeI code)
Returns a new json object that reflects the error code.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcLgrNotFound
Definition ErrorCodes.h:55
@ RpcNotReady
Definition ErrorCodes.h:43
@ RpcNotEnabled
Definition ErrorCodes.h:42
@ RpcInvalidParams
Definition ErrorCodes.h:67
std::string toLower(std::string str)
Fold ASCII upper case letters to lower case.
json::Value doCanDelete(rpc::JsonContext &context)
Definition CanDelete.cpp:21
BaseUInt< 256 > uint256
Definition base_uint.h:580
Application & app
Definition Context.h:29
LedgerMaster & ledgerMaster
Definition Context.h:32
json::Value params
Definition Context.h:51