xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
overlay
detail
PeerReservationTable.cpp
1
#include <xrpl/core/PeerReservationTable.h>
2
3
#include <xrpl/json/json_value.h>
4
#include <xrpl/protocol/PublicKey.h>
5
#include <xrpl/protocol/jss.h>
6
#include <xrpl/protocol/tokens.h>
7
#include <xrpl/server/Wallet.h>
8
9
#include <
algorithm
>
10
#include <
iterator
>
11
#include <
mutex
>
12
#include <
optional
>
13
#include <
string
>
14
#include <
vector
>
15
16
namespace
xrpl
{
17
18
auto
19
PeerReservation::toJson
() const ->
json
::Value
20
{
21
json::Value
result{
json::ValueType::Object
};
22
result[jss::node] =
toBase58
(
TokenType::NodePublic
,
nodeId
);
23
if
(!
description
.empty())
24
{
25
result[jss::description] =
description
;
26
}
27
return
result;
28
}
29
30
auto
31
PeerReservationTable::list
() const ->
std
::vector<
PeerReservation
>
32
{
33
std::vector<PeerReservation>
list
;
34
{
35
std::scoped_lock
const
lock(
mutex_
);
36
list
.reserve(
table_
.size());
37
std::ranges::copy
(
table_
,
std::back_inserter
(
list
));
38
}
39
std::sort
(
list
.begin(),
list
.end());
// NOLINT(modernize-use-ranges)
40
return
list
;
41
}
42
43
// See `include/xrpl/rdb/DBInit.h` for the `CREATE TABLE` statement.
44
// It is unfortunate that we do not get to define a function for it.
45
46
// We choose a `bool` return type to fit in with the error handling scheme
47
// of other functions called from `ApplicationImp::setup`, but we always
48
// return "no error" (`true`) because we can always return an empty table.
49
bool
50
PeerReservationTable::load
(
DatabaseCon
& connection)
51
{
52
std::scoped_lock
const
lock(
mutex_
);
53
54
connection_
= &connection;
55
auto
db = connection.
checkoutDb
();
56
auto
table =
getPeerReservationTable
(*db,
journal_
);
57
table_
.insert(table.begin(), table.end());
58
59
return
true
;
60
}
61
62
std::optional<PeerReservation>
63
PeerReservationTable::insertOrAssign
(
PeerReservation
const
& reservation)
64
{
65
std::optional<PeerReservation>
previous;
66
67
std::scoped_lock
const
lock(
mutex_
);
68
69
auto
hint =
table_
.find(reservation);
70
if
(hint !=
table_
.end())
71
{
72
// The node already has a reservation. Remove it.
73
// `std::unordered_set` does not have an `insertOrAssign` method,
74
// and sadly makes it impossible for us to implement one efficiently:
75
// https://stackoverflow.com/q/49651835/618906
76
// Regardless, we don't expect this function to be called often, or
77
// for the table to be very large, so this less-than-ideal
78
// remove-then-insert is acceptable in order to present a better API.
79
previous = *hint;
80
// We should pick an adjacent location for the insertion hint.
81
// Decrementing may be illegal if the found reservation is at the
82
// beginning. Incrementing is always legal; at worst we'll point to
83
// the end.
84
auto
const
deleteme = hint;
85
++hint;
86
table_
.erase(deleteme);
87
}
88
table_
.insert(hint, reservation);
89
90
auto
db =
connection_
->checkoutDb();
91
insertPeerReservation
(*db, reservation.
nodeId
, reservation.
description
);
92
93
return
previous;
94
}
95
96
std::optional<PeerReservation>
97
PeerReservationTable::erase
(
PublicKey
const
& nodeId)
98
{
99
std::optional<PeerReservation>
previous;
100
101
std::scoped_lock
const
lock(
mutex_
);
102
103
auto
const
it =
table_
.find({.nodeId = nodeId});
104
if
(it !=
table_
.end())
105
{
106
previous = *it;
107
table_
.erase(it);
108
auto
db =
connection_
->checkoutDb();
109
deletePeerReservation
(*db, nodeId);
110
}
111
112
return
previous;
113
}
114
115
}
// namespace xrpl
algorithm
std::back_inserter
T back_inserter(T... args)
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::DatabaseCon
Definition
DatabaseCon.h:74
xrpl::DatabaseCon::checkoutDb
LockedSociSession checkoutDb()
Definition
DatabaseCon.h:177
xrpl::PeerReservationTable::list
std::vector< PeerReservation > list() const
Definition
PeerReservationTable.cpp:31
xrpl::PeerReservationTable::insertOrAssign
std::optional< PeerReservation > insertOrAssign(PeerReservation const &reservation)
Definition
PeerReservationTable.cpp:63
xrpl::PeerReservationTable::mutex_
std::mutex mutex_
Definition
PeerReservationTable.h:97
xrpl::PeerReservationTable::erase
std::optional< PeerReservation > erase(PublicKey const &nodeId)
Definition
PeerReservationTable.cpp:97
xrpl::PeerReservationTable::journal_
beast::Journal journal_
Definition
PeerReservationTable.h:96
xrpl::PeerReservationTable::connection_
DatabaseCon * connection_
Definition
PeerReservationTable.h:98
xrpl::PeerReservationTable::table_
std::unordered_set< PeerReservation, beast::Uhash<>, KeyEqual > table_
Definition
PeerReservationTable.h:99
xrpl::PeerReservationTable::load
bool load(DatabaseCon &connection)
Definition
PeerReservationTable.cpp:50
xrpl::PublicKey
A public key.
Definition
PublicKey.h:53
std::ranges::copy
T copy(T... args)
iterator
mutex
json
JSON (JavaScript Object Notation).
Definition
json_errors.h:5
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition
AccountID.cpp:95
xrpl::deletePeerReservation
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
Definition
Wallet.cpp:234
xrpl::insertPeerReservation
void insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
Definition
Wallet.cpp:220
xrpl::getPeerReservationTable
std::unordered_set< PeerReservation, beast::Uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
Definition
Wallet.cpp:185
xrpl::TokenType::NodePublic
@ NodePublic
Definition
tokens.h:21
optional
std::scoped_lock
std::sort
T sort(T... args)
string
xrpl::PeerReservation
Definition
PeerReservationTable.h:21
xrpl::PeerReservation::nodeId
PublicKey nodeId
Definition
PeerReservationTable.h:23
xrpl::PeerReservation::toJson
auto toJson() const -> json::Value
Definition
PeerReservationTable.cpp:19
xrpl::PeerReservation::description
std::string description
Definition
PeerReservationTable.h:24
vector
Generated by
1.17.0