xrpld
Loading...
Searching...
No Matches
State.cpp
1#include <xrpl/server/State.h>
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/config/BasicConfig.h>
5#include <xrpl/protocol/Protocol.h>
6#include <xrpl/rdb/SociDB.h>
7
8#include <boost/optional/optional.hpp> // IWYU pragma: keep
9
10#include <soci/into.h>
11#include <soci/session.h>
12#include <soci/use.h>
13
14#include <cstdint>
15#include <stdexcept>
16#include <string>
17
18namespace xrpl {
19
20void
21initStateDB(soci::session& session, BasicConfig const& config, std::string const& dbName)
22{
23 open(session, config, dbName);
24
25 session << "PRAGMA synchronous=FULL;";
26
27 session << "CREATE TABLE IF NOT EXISTS DbState ("
28 " Key INTEGER PRIMARY KEY,"
29 " WritableDb TEXT,"
30 " ArchiveDb TEXT,"
31 " LastRotatedLedger INTEGER"
32 ");";
33
34 session << "CREATE TABLE IF NOT EXISTS CanDelete ("
35 " Key INTEGER PRIMARY KEY,"
36 " CanDeleteSeq INTEGER"
37 ");";
38
39 std::int64_t count = 0;
40 {
41 // SOCI requires boost::optional (not std::optional) as the parameter.
42 boost::optional<std::int64_t> countO;
43 session << "SELECT COUNT(Key) FROM DbState WHERE Key = 1;", soci::into(countO);
44 if (!countO)
45 Throw<std::runtime_error>("Failed to fetch Key Count from DbState.");
46 count = *countO;
47 }
48
49 if (count == 0)
50 {
51 session << "INSERT INTO DbState VALUES (1, '', '', 0);";
52 }
53
54 {
55 // SOCI requires boost::optional (not std::optional) as the parameter.
56 boost::optional<std::int64_t> countO;
57 session << "SELECT COUNT(Key) FROM CanDelete WHERE Key = 1;", soci::into(countO);
58 if (!countO)
59 Throw<std::runtime_error>("Failed to fetch Key Count from CanDelete.");
60 count = *countO;
61 }
62
63 if (count == 0)
64 {
65 session << "INSERT INTO CanDelete VALUES (1, 0);";
66 }
67}
68
70getCanDelete(soci::session& session)
71{
72 LedgerIndex seq = 0;
73 session << "SELECT CanDeleteSeq FROM CanDelete WHERE Key = 1;", soci::into(seq);
74 ;
75 return seq;
76}
77
79setCanDelete(soci::session& session, LedgerIndex canDelete)
80{
81 session << "UPDATE CanDelete SET CanDeleteSeq = :canDelete WHERE Key = 1;",
82 soci::use(canDelete);
83 return canDelete;
84}
85
86SavedState
87getSavedState(soci::session& session)
88{
89 SavedState state;
90 session << "SELECT WritableDb, ArchiveDb, LastRotatedLedger"
91 " FROM DbState WHERE Key = 1;",
92 soci::into(state.writableDb), soci::into(state.archiveDb), soci::into(state.lastRotated);
93
94 return state;
95}
96
97void
98setSavedState(soci::session& session, SavedState const& state)
99{
100 session << "UPDATE DbState"
101 " SET WritableDb = :writableDb,"
102 " ArchiveDb = :archiveDb,"
103 " LastRotatedLedger = :lastRotated"
104 " WHERE Key = 1;",
105 soci::use(state.writableDb), soci::use(state.archiveDb), soci::use(state.lastRotated);
106}
107
108void
109setLastRotated(soci::session& session, LedgerIndex seq)
110{
111 session << "UPDATE DbState SET LastRotatedLedger = :seq"
112 " WHERE Key = 1;",
113 soci::use(seq);
114}
115
116} // namespace xrpl
Holds unparsed configuration information.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void initStateDB(soci::session &session, BasicConfig const &config, std::string const &dbName)
initStateDB Opens a session with the State database.
Definition State.cpp:21
void setSavedState(soci::session &session, SavedState const &state)
setSavedState Saves the given state.
Definition State.cpp:98
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:259
void setLastRotated(soci::session &session, LedgerIndex seq)
setLastRotated Updates the last rotated ledger sequence.
Definition State.cpp:109
SavedState getSavedState(soci::session &session)
getSavedState Returns the saved state.
Definition State.cpp:87
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.
Definition SociDB.cpp:92
LedgerIndex setCanDelete(soci::session &session, LedgerIndex canDelete)
setCanDelete Updates the ledger sequence which can be deleted.
Definition State.cpp:79
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
LedgerIndex getCanDelete(soci::session &session)
getCanDelete Returns the ledger sequence which can be deleted.
Definition State.cpp:70
std::string writableDb
Definition State.h:13
LedgerIndex lastRotated
Definition State.h:15
std::string archiveDb
Definition State.h:14