20#include <xrpld/app/rdb/Wallet.h>
21#include <boost/format.hpp>
29 return std::make_unique<DatabaseCon>(
40 return std::make_unique<DatabaseCon>(
46 soci::session& session,
52 std::string const sql =
"SELECT RawData FROM " + dbTable +
";";
53 soci::blob sociRawData(session);
54 soci::statement st = (session.prepare << sql, soci::into(sociRawData));
59 convert(sociRawData, serialized);
64 JLOG(j.
warn()) <<
"Unverifiable manifest in db";
72 JLOG(j.
warn()) <<
"Malformed manifest in database";
79 soci::session& session,
86 soci::blob rawData(session);
88 session <<
"INSERT INTO " << dbTable <<
" (RawData) VALUES (:rawData);",
94 soci::session& session,
100 soci::transaction tr(session);
101 session <<
"DELETE FROM " << dbTable;
102 for (
auto const& v : map)
106 if (!v.second.revoked() && !isTrusted(v.second.masterKey))
108 JLOG(j.
info()) <<
"Untrusted manifest in cache not saved to db";
120 soci::transaction tr(session);
121 saveManifest(session,
"ValidatorManifests", serialized);
128 session <<
"DELETE FROM NodeIdentity;";
136 boost::optional<std::string> pubKO, priKO;
139 <<
"SELECT PublicKey, PrivateKey FROM NodeIdentity;",
145 auto const sk = parseBase58<SecretKey>(
147 auto const pk = parseBase58<PublicKey>(
160 boost::format(
"INSERT INTO NodeIdentity (PublicKey,PrivateKey) "
161 "VALUES ('%s','%s');") %
165 return {newpublicKey, newsecretKey};
174 boost::optional<std::string> valPubKey, valDesc;
180 <<
"SELECT PublicKey, Description FROM PeerReservations;",
181 soci::into(valPubKey),
182 soci::into(valDesc));
186 if (!valPubKey || !valDesc)
192 auto const optNodeId =
196 JLOG(j.
warn()) <<
"load: not a public key: " << valPubKey;
207 soci::session& session,
212 session <<
"INSERT INTO PeerReservations (PublicKey, Description) "
213 "VALUES (:nodeId, :desc) "
214 "ON CONFLICT (PublicKey) DO UPDATE SET "
215 "Description=excluded.Description",
216 soci::use(sNodeId), soci::use(description);
223 session <<
"DELETE FROM PeerReservations WHERE PublicKey = :nodeId",
230 soci::transaction tr(session);
232 "SELECT count(*) FROM sqlite_master "
233 "WHERE type='table' AND name='FeatureVotes'";
235 boost::optional<int> featureVotesCount;
236 session << sql, soci::into(featureVotesCount);
237 bool exists =
static_cast<bool>(*featureVotesCount);
242 session <<
"CREATE TABLE FeatureVotes ( "
243 "AmendmentHash CHARACTER(64) NOT NULL, "
244 "AmendmentName TEXT, "
245 "Veto INTEGER NOT NULL );";
253 soci::session& session,
255 boost::optional<std::string> amendment_hash,
256 boost::optional<std::string> amendment_name,
257 boost::optional<AmendmentVote> vote)>
const& callback)
260 auto intToVote = [](boost::optional<int>
const& dbVote)
261 -> boost::optional<AmendmentVote> {
262 return safe_cast<AmendmentVote>(dbVote.value_or(1));
265 soci::transaction 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> amendment_hash;
273 boost::optional<std::string> amendment_name;
274 boost::optional<int> vote_to_veto;
276 (session.prepare << sql,
277 soci::into(amendment_hash),
278 soci::into(amendment_name),
279 soci::into(vote_to_veto));
283 callback(amendment_hash, amendment_name, intToVote(vote_to_veto));
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::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
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::unordered_set< PeerReservation, beast::uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
void insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
void addValidatorManifest(soci::session &session, std::string const &serialized)
addValidatorManifest Saves the manifest of a validator to the database.
std::pair< PublicKey, SecretKey > getNodeIdentity(Application &app, boost::program_options::variables_map const &cmdline)
The cryptographic credentials identifying this server instance.
bool createFeatureVotes(soci::session &session)
createFeatureVotes Creates the FeatureVote table if it does not exist.
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 convert(soci::blob &from, std::vector< std::uint8_t > &to)
constexpr std::array< char const *, 6 > WalletDBInit
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
void readAmendments(soci::session &session, std::function< void(boost::optional< std::string > amendment_hash, boost::optional< std::string > amendment_name, boost::optional< AmendmentVote > vote)> const &callback)
readAmendments Reads all amendments from the FeatureVotes table.
void clearNodeIdentity(soci::session &session)
Delete any saved public/private key associated with this node.
std::unique_ptr< DatabaseCon > makeWalletDB(DatabaseCon::Setup const &setup, beast::Journal j)
makeWalletDB Opens the wallet database and returns it.
std::string to_string(base_uint< Bits, Tag > const &a)
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
void getManifests(soci::session &session, std::string const &dbTable, ManifestCache &mCache, beast::Journal j)
getManifests Loads a manifest from the wallet database and stores it in the cache.
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
static void saveManifest(soci::session &session, std::string const &dbTable, std::string const &serialized)
void voteAmendment(soci::session &session, uint256 const &amendment, std::string const &name, AmendmentVote vote)
voteAmendment Set the veto value for a particular amendment.
constexpr auto WalletDBName