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