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/base_uint.h>
7#include <xrpl/beast/core/LexicalCast.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/jss.h>
11
12#include <boost/algorithm/string/case_conv.hpp>
13
14#include <cstdint>
15#include <limits>
16#include <string>
17
18namespace xrpl {
19
20// can_delete [<ledgerid>|<ledgerhash>|now|always|never]
21json::Value
23{
24 if (!context.app.getSHAMapStore().advisoryDelete())
26
28
29 if (context.params.isMember(jss::can_delete))
30 {
31 json::Value const canDelete = context.params.get(jss::can_delete, 0);
32 std::uint32_t canDeleteSeq = 0;
33
34 if (canDelete.isUInt())
35 {
36 canDeleteSeq = canDelete.asUInt();
37 }
38 else
39 {
40 std::string canDeleteStr = canDelete.asString();
41 boost::to_lower(canDeleteStr);
42
43 if (canDeleteStr.find_first_not_of("0123456789") == std::string::npos)
44 {
45 canDeleteSeq = beast::lexicalCast<std::uint32_t>(canDeleteStr);
46 }
47 else if (canDeleteStr == "never")
48 {
49 canDeleteSeq = 0;
50 }
51 else if (canDeleteStr == "always")
52 {
54 }
55 else if (canDeleteStr == "now")
56 {
57 canDeleteSeq = context.app.getSHAMapStore().getLastRotated();
58 if (canDeleteSeq == 0u)
60 }
61 else if (uint256 lh; lh.parseHex(canDeleteStr))
62 {
63 auto ledger = context.ledgerMaster.getLedgerByHash(lh);
64
65 if (!ledger)
66 return RPC::makeError(RpcLgrNotFound, "ledgerNotFound");
67
68 canDeleteSeq = ledger->header().seq;
69 }
70 else
71 {
73 }
74 }
75
76 ret[jss::can_delete] = context.app.getSHAMapStore().setCanDelete(canDeleteSeq);
77 }
78 else
79 {
80 ret[jss::can_delete] = context.app.getSHAMapStore().getCanDelete();
81 }
82
83 return ret;
84}
85
86} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
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)
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:26
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:54
@ RpcNotReady
Definition ErrorCodes.h:42
@ RpcNotEnabled
Definition ErrorCodes.h:41
@ RpcInvalidParams
Definition ErrorCodes.h:66
json::Value doCanDelete(RPC::JsonContext &context)
Definition CanDelete.cpp:22
BaseUInt< 256 > uint256
Definition base_uint.h:562
Application & app
Definition Context.h:21
LedgerMaster & ledgerMaster
Definition Context.h:24
json::Value params
Definition Context.h:43