1#include <xrpl/server/Wallet.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/beast/hash/uhash.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/core/PeerReservationTable.h>
10#include <xrpl/protocol/KeyType.h>
11#include <xrpl/protocol/PublicKey.h>
12#include <xrpl/protocol/SecretKey.h>
13#include <xrpl/protocol/tokens.h>
14#include <xrpl/rdb/DBInit.h>
15#include <xrpl/rdb/DatabaseCon.h>
16#include <xrpl/rdb/SociDB.h>
17#include <xrpl/server/Manifest.h>
19#include <boost/format/free_funcs.hpp>
20#include <boost/optional/optional.hpp>
24#include <soci/session.h>
25#include <soci/statement.h>
26#include <soci/transaction.h>
38std::unique_ptr<DatabaseCon>
56 soci::session& session,
62 std::string const sql =
"SELECT RawData FROM " + dbTable +
";";
63 soci::blob sociRawData(session);
64 soci::statement st = (session.prepare << sql, soci::into(sociRawData));
69 convert(sociRawData, serialized);
74 JLOG(j.
warn()) <<
"Unverifiable manifest in db";
82 JLOG(j.
warn()) <<
"Malformed manifest in database";
93 soci::blob rawData(session);
95 session <<
"INSERT INTO " << dbTable <<
" (RawData) VALUES (:rawData);", soci::use(rawData);
100 soci::session& session,
106 soci::transaction tr(session);
107 session <<
"DELETE FROM " << dbTable;
108 for (
auto const& v : map)
112 if (!v.second.revoked() && !isTrusted(v.second.masterKey))
114 JLOG(j.
info()) <<
"Untrusted manifest in cache not saved to db";
126 soci::transaction tr(session);
127 saveManifest(session,
"ValidatorManifests", serialized);
134 session <<
"DELETE FROM NodeIdentity;";
142 boost::optional<std::string> pubKO, priKO;
144 (session.prepare <<
"SELECT PublicKey, PrivateKey FROM NodeIdentity;",
164 "INSERT INTO NodeIdentity (PublicKey,PrivateKey) "
165 "VALUES ('%s','%s');") %
169 return {newpublicKey, newsecretKey};
178 boost::optional<std::string> valPubKey, valDesc;
183 (session.prepare <<
"SELECT PublicKey, Description FROM PeerReservations;",
184 soci::into(valPubKey),
185 soci::into(valDesc));
189 if (!valPubKey || !valDesc)
198 JLOG(j.
warn()) <<
"load: not a public key: " << valPubKey;
209 soci::session& session,
214 session <<
"INSERT INTO PeerReservations (PublicKey, Description) "
215 "VALUES (:nodeId, :desc) "
216 "ON CONFLICT (PublicKey) DO UPDATE SET "
217 "Description=excluded.Description",
218 soci::use(sNodeId), soci::use(description);
225 session <<
"DELETE FROM PeerReservations WHERE PublicKey = :nodeId", soci::use(sNodeId);
231 soci::transaction tr(session);
233 "SELECT count(*) FROM sqlite_master "
234 "WHERE type='table' AND name='FeatureVotes'";
236 boost::optional<int> featureVotesCount;
237 session << sql, soci::into(featureVotesCount);
238 bool const exists =
static_cast<bool>(*featureVotesCount);
243 session <<
"CREATE TABLE FeatureVotes ( "
244 "AmendmentHash CHARACTER(64) NOT NULL, "
245 "AmendmentName TEXT, "
246 "Veto INTEGER NOT NULL );";
254 soci::session& session,
256 boost::optional<std::string> amendmentHash,
257 boost::optional<std::string> amendmentName,
258 boost::optional<AmendmentVote> vote)>
const& callback)
261 auto intToVote = [](boost::optional<int>
const& dbVote) -> boost::optional<AmendmentVote> {
265 soci::transaction
const tr(session);
267 "SELECT AmendmentHash, AmendmentName, Veto FROM "
268 "( SELECT AmendmentHash, AmendmentName, Veto, RANK() OVER "
269 "( PARTITION BY AmendmentHash ORDER BY ROWID DESC ) "
270 "as rnk FROM FeatureVotes ) WHERE rnk = 1";
272 boost::optional<std::string> amendmentHash;
273 boost::optional<std::string> amendmentName;
274 boost::optional<int> voteToVeto;
276 (session.prepare << sql,
277 soci::into(amendmentHash),
278 soci::into(amendmentName),
279 soci::into(voteToVeto));
283 callback(amendmentHash, amendmentName, intToVote(voteToVeto));
289 soci::session& session,
294 soci::transaction tr(session);
296 "INSERT INTO FeatureVotes (AmendmentHash, AmendmentName, Veto) VALUES "
299 sql +=
"', '" + name;
A generic endpoint for log messages.
Remembers manifests with the highest sequence number.
ManifestDisposition applyManifest(Manifest m)
Add manifest to cache.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
void saveManifests(soci::session &session, std::string const &dbTable, std::function< bool(PublicKey const &)> const &isTrusted, hash_map< PublicKey, Manifest > const &map, beast::Journal j)
saveManifests Saves all given manifests to the database.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
static void saveManifest(soci::session &session, std::string const &dbTable, std::string const &serialized)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
std::pair< PublicKey, SecretKey > getNodeIdentity(soci::session &session)
Returns a stable public and private key for this node.
void insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
void readAmendments(soci::session &session, std::function< void(boost::optional< std::string > amendmentHash, boost::optional< std::string > amendmentName, boost::optional< AmendmentVote > vote)> const &callback)
readAmendments Reads all amendments from the FeatureVotes table.
constexpr auto kWalletDbName
std::unordered_set< PeerReservation, beast::Uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
std::string to_string(BaseUInt< Bits, Tag > const &a)
void addValidatorManifest(soci::session &session, std::string const &serialized)
addValidatorManifest Saves the manifest of a validator to the database.
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
constexpr std::array< char const *, 6 > kWalletDbInit
std::unique_ptr< DatabaseCon > makeWalletDB(DatabaseCon::Setup const &setup, beast::Journal j)
makeWalletDB Opens the wallet database and returns it.
bool createFeatureVotes(soci::session &session)
createFeatureVotes Creates the FeatureVote table if it does not exist.
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map
void clearNodeIdentity(soci::session &session)
Delete any saved public/private key associated with this node.
void getManifests(soci::session &session, std::string const &dbTable, ManifestCache &cache, beast::Journal j)
getManifests Loads a manifest from the wallet database and stores it in the cache.
std::unique_ptr< DatabaseCon > makeTestWalletDB(DatabaseCon::Setup const &setup, std::string const &dbname, beast::Journal j)
makeTestWalletDB Opens a test wallet database with an arbitrary name.
void voteAmendment(soci::session &session, uint256 const &amendment, std::string const &name, AmendmentVote vote)
voteAmendment Set the veto value for a particular amendment.
void convert(soci::blob &from, std::vector< std::uint8_t > &to)