rippled
Loading...
Searching...
No Matches
PeerReservationTable.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2019 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/rdb/RelationalDatabase.h>
21#include <xrpld/app/rdb/Wallet.h>
22#include <xrpld/overlay/PeerReservationTable.h>
23
24#include <xrpl/json/json_value.h>
25#include <xrpl/protocol/PublicKey.h>
26#include <xrpl/protocol/jss.h>
27
28#include <algorithm>
29#include <iterator>
30#include <mutex>
31#include <string>
32#include <vector>
33
34namespace ripple {
35
36auto
38{
40 result[jss::node] = toBase58(TokenType::NodePublic, nodeId);
41 if (!description.empty())
42 {
43 result[jss::description] = description;
44 }
45 return result;
46}
47
48auto
50{
52 {
54 list.reserve(table_.size());
56 }
57 std::sort(list.begin(), list.end());
58 return list;
59}
60
61// See `ripple/app/main/DBInit.cpp` for the `CREATE TABLE` statement.
62// It is unfortunate that we do not get to define a function for it.
63
64// We choose a `bool` return type to fit in with the error handling scheme
65// of other functions called from `ApplicationImp::setup`, but we always
66// return "no error" (`true`) because we can always return an empty table.
67bool
69{
71
72 connection_ = &connection;
73 auto db = connection.checkoutDb();
74 auto table = getPeerReservationTable(*db, journal_);
75 table_.insert(table.begin(), table.end());
76
77 return true;
78}
79
82{
84
86
87 auto hint = table_.find(reservation);
88 if (hint != table_.end())
89 {
90 // The node already has a reservation. Remove it.
91 // `std::unordered_set` does not have an `insert_or_assign` method,
92 // and sadly makes it impossible for us to implement one efficiently:
93 // https://stackoverflow.com/q/49651835/618906
94 // Regardless, we don't expect this function to be called often, or
95 // for the table to be very large, so this less-than-ideal
96 // remove-then-insert is acceptable in order to present a better API.
97 previous = *hint;
98 // We should pick an adjacent location for the insertion hint.
99 // Decrementing may be illegal if the found reservation is at the
100 // beginning. Incrementing is always legal; at worst we'll point to
101 // the end.
102 auto const deleteme = hint;
103 ++hint;
104 table_.erase(deleteme);
105 }
106 table_.insert(hint, reservation);
107
108 auto db = connection_->checkoutDb();
109 insertPeerReservation(*db, reservation.nodeId, reservation.description);
110
111 return previous;
112}
113
116{
118
120
121 auto const it = table_.find({nodeId});
122 if (it != table_.end())
123 {
124 previous = *it;
125 table_.erase(it);
126 auto db = connection_->checkoutDb();
127 deletePeerReservation(*db, nodeId);
128 }
129
130 return previous;
131}
132
133} // namespace ripple
T back_inserter(T... args)
Represents a JSON value.
Definition json_value.h:149
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:61
T copy(T... args)
T empty(T... args)
JSON (JavaScript Object Notation).
Definition json_errors.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::unordered_set< PeerReservation, beast::uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
Definition Wallet.cpp:170
void insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
Definition Wallet.cpp:207
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
Definition Wallet.cpp:221
STL namespace.
T sort(T... args)
auto toJson() const -> Json::Value