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