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