20 #include <ripple/app/rdb/Wallet.h>
21 #include <boost/format.hpp>
29 return std::make_unique<DatabaseCon>(
37 return std::make_unique<DatabaseCon>(
43 soci::session& session,
49 std::string const sql =
"SELECT RawData FROM " + dbTable +
";";
50 soci::blob sociRawData(session);
51 soci::statement st = (session.prepare << sql, soci::into(sociRawData));
56 convert(sociRawData, serialized);
61 JLOG(j.
warn()) <<
"Unverifiable manifest in db";
69 JLOG(j.
warn()) <<
"Malformed manifest in database";
76 soci::session& session,
83 soci::blob rawData(session);
85 session <<
"INSERT INTO " << dbTable <<
" (RawData) VALUES (:rawData);",
91 soci::session& session,
97 soci::transaction tr(session);
98 session <<
"DELETE FROM " << dbTable;
99 for (
auto const& v : map)
103 if (!v.second.revoked() && !isTrusted(v.second.masterKey))
105 JLOG(j.
info()) <<
"Untrusted manifest in cache not saved to db";
117 soci::transaction tr(session);
118 saveManifest(session,
"ValidatorManifests", serialized);
125 session <<
"DELETE FROM NodeIdentity;";
133 boost::optional<std::string> pubKO, priKO;
136 <<
"SELECT PublicKey, PrivateKey FROM NodeIdentity;",
142 auto const sk = parseBase58<SecretKey>(
144 auto const pk = parseBase58<PublicKey>(
157 boost::format(
"INSERT INTO NodeIdentity (PublicKey,PrivateKey) "
158 "VALUES ('%s','%s');") %
162 return {newpublicKey, newsecretKey};
171 boost::optional<std::string> valPubKey, valDesc;
177 <<
"SELECT PublicKey, Description FROM PeerReservations;",
178 soci::into(valPubKey),
179 soci::into(valDesc));
183 if (!valPubKey || !valDesc)
189 auto const optNodeId =
193 JLOG(j.
warn()) <<
"load: not a public key: " << valPubKey;
204 soci::session& session,
208 session <<
"INSERT INTO PeerReservations (PublicKey, Description) "
209 "VALUES (:nodeId, :desc) "
210 "ON CONFLICT (PublicKey) DO UPDATE SET "
211 "Description=excluded.Description",
213 soci::use(description);
219 session <<
"DELETE FROM PeerReservations WHERE PublicKey = :nodeId",
226 soci::transaction tr(session);
228 "SELECT count(*) FROM sqlite_master "
229 "WHERE type='table' AND name='FeatureVotes'";
231 boost::optional<int> featureVotesCount;
232 session << sql, soci::into(featureVotesCount);
233 bool exists =
static_cast<bool>(*featureVotesCount);
238 session <<
"CREATE TABLE FeatureVotes ( "
239 "AmendmentHash CHARACTER(64) NOT NULL, "
240 "AmendmentName TEXT, "
241 "Veto INTEGER NOT NULL );";
249 soci::session& session,
251 boost::optional<std::string> amendment_hash,
252 boost::optional<std::string> amendment_name,
253 boost::optional<AmendmentVote> vote)>
const& callback)
256 auto intToVote = [](boost::optional<int>
const& dbVote)
257 -> boost::optional<AmendmentVote> {
258 return safe_cast<AmendmentVote>(dbVote.value_or(1));
261 soci::transaction tr(session);
263 "SELECT AmendmentHash, AmendmentName, Veto FROM "
264 "( SELECT AmendmentHash, AmendmentName, Veto, RANK() OVER "
265 "( PARTITION BY AmendmentHash ORDER BY ROWID DESC ) "
266 "as rnk FROM FeatureVotes ) WHERE rnk = 1";
268 boost::optional<std::string> amendment_hash;
269 boost::optional<std::string> amendment_name;
270 boost::optional<int> vote_to_veto;
272 (session.prepare << sql,
273 soci::into(amendment_hash),
274 soci::into(amendment_name),
275 soci::into(vote_to_veto));
279 callback(amendment_hash, amendment_name, intToVote(vote_to_veto));
285 soci::session& session,
290 soci::transaction tr(session);
292 "INSERT INTO FeatureVotes (AmendmentHash, AmendmentName, Veto) VALUES "
295 sql +=
"', '" + name;
constexpr auto WalletDBName
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
bool createFeatureVotes(soci::session &session)
createFeatureVotes Creates the FeatureVote table if it does not exist.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
void addValidatorManifest(soci::session &session, std::string const &serialized)
addValidatorManifest Saves the manifest of a validator to the database.
std::unique_ptr< DatabaseCon > makeTestWalletDB(DatabaseCon::Setup const &setup, std::string const &dbname)
makeTestWalletDB Opens a test wallet database with an arbitrary name.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
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 insertPeerReservation(soci::session &session, PublicKey const &nodeId, std::string const &description)
insertPeerReservation Adds an entry to the peer reservation table.
void deletePeerReservation(soci::session &session, PublicKey const &nodeId)
deletePeerReservation Deletes an entry from the peer reservation table.
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.
A generic endpoint for log messages.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
std::unordered_set< PeerReservation, beast::uhash<>, KeyEqual > getPeerReservationTable(soci::session &session, beast::Journal j)
getPeerReservationTable Returns the peer reservation table.
Remembers manifests with the highest sequence number.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void voteAmendment(soci::session &session, uint256 const &amendment, std::string const &name, AmendmentVote vote)
voteAmendment Set the veto value for a particular amendment.
std::unique_ptr< DatabaseCon > makeWalletDB(DatabaseCon::Setup const &setup)
makeWalletDB Opens the wallet database and returns it.
static void saveManifest(soci::session &session, std::string const &dbTable, std::string const &serialized)
std::pair< PublicKey, SecretKey > getNodeIdentity(Application &app, boost::program_options::variables_map const &cmdline)
The cryptographic credentials identifying this server instance.
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
std::optional< Manifest > deserializeManifest(Slice s, beast::Journal journal)
Constructs Manifest from serialized string.
constexpr std::array< char const *, 6 > WalletDBInit
ManifestDisposition applyManifest(Manifest m)
Add manifest to cache.
void clearNodeIdentity(soci::session &session)
Delete any saved public/private key associated with this node.
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.