20 #include <ripple/app/main/Application.h>
21 #include <ripple/app/misc/AmendmentTable.h>
22 #include <ripple/core/ConfigSections.h>
23 #include <ripple/core/DatabaseCon.h>
24 #include <ripple/protocol/Feature.h>
25 #include <ripple/protocol/STValidation.h>
26 #include <ripple/protocol/TxFlags.h>
27 #include <ripple/protocol/jss.h>
28 #include <boost/format.hpp>
29 #include <boost/regex.hpp>
38 static boost::regex
const re1(
41 "([abcdefABCDEF0-9]{64})"
45 boost::regex_constants::optimize);
49 for (
auto const& line : section.
lines())
53 if (!boost::regex_match(line, match, re1))
54 Throw<std::runtime_error>(
55 "Invalid entry '" + line +
"' in [" + section.
name() +
"]");
59 if (!
id.parseHex(match[1]))
60 Throw<std::runtime_error>(
61 "Invalid amendment ID '" + match[1] +
"' in [" +
62 section.
name() +
"]");
114 for (
auto const& val : valSet)
116 if (val->isTrusted())
124 [&](
auto const& amendment) { ++votes_[amendment]; });
149 auto const& it =
votes_.find(amendment);
169 auto const& it =
votes_.find(amendment);
249 bool vote_to_veto)
const;
321 , majorityTime_(majorityTime)
322 , unsupportedEnabled_(false)
324 , db_(app.getWalletDB())
329 bool const featureVotesExist = [
this]() {
331 soci::transaction tr(*db);
333 "SELECT count(*) FROM sqlite_master "
334 "WHERE type='table' AND name='FeatureVotes'";
336 boost::optional<int> featureVotesCount;
337 *db << sql, soci::into(featureVotesCount);
338 bool exists =
static_cast<bool>(*featureVotesCount);
343 *db <<
"CREATE TABLE FeatureVotes ( "
344 "AmendmentHash CHARACTER(64) NOT NULL, "
345 "AmendmentName TEXT, "
346 "Veto INTEGER NOT NULL );";
355 if (
auto s =
add(a.first, sl))
357 JLOG(
j_.
debug()) <<
"Amendment " << a.first <<
" is supported.";
359 if (!a.second.empty())
370 if (featureVotesExist)
372 JLOG(
j_.
warn()) <<
"[amendments] section in config file ignored"
373 " in favor of data in db/wallet.db.";
378 detect_conflict.
insert(a.first);
386 if (featureVotesExist)
389 <<
"[veto_amendments] section in config file ignored"
390 " in favor of data in db/wallet.db.";
395 if (detect_conflict.
count(a.first) == 0)
402 <<
"[veto_amendments] section in config has amendment "
403 <<
'(' << a.first <<
", " << a.second
404 <<
") both [veto_amendments] and [amendments].";
411 soci::transaction tr(*db);
413 "SELECT AmendmentHash, AmendmentName, Veto FROM FeatureVotes";
415 boost::optional<std::string> amendment_hash;
416 boost::optional<std::string> amendment_name;
417 boost::optional<int> vote_to_veto;
420 soci::into(amendment_hash),
421 soci::into(amendment_name),
422 soci::into(vote_to_veto));
427 if (!amend_hash.
parseHex(*amendment_hash))
429 Throw<std::runtime_error>(
430 "Invalid amendment ID '" + *amendment_hash +
" in wallet.db");
435 if (
auto s =
get(amend_hash, sl))
437 JLOG(
j_.
info()) <<
"Amendment {" << *amendment_name <<
", "
438 << amend_hash <<
"} is vetoed.";
439 if (!amendment_name->empty())
440 s->name = *amendment_name;
446 if (
auto s =
add(amend_hash, sl))
448 JLOG(
j_.
debug()) <<
"Amendment {" << *amendment_name <<
", "
449 << amend_hash <<
"} is un-vetoed.";
450 if (!amendment_name->empty())
451 s->name = *amendment_name;
498 if (name == e.second.name)
509 bool vote_to_veto)
const
512 soci::transaction tr(*db);
514 "INSERT INTO FeatureVotes (AmendmentHash, AmendmentName, Veto) VALUES "
517 sql +=
"', '" + name;
527 auto s =
add(amendment, sl);
540 auto s =
get(amendment, sl);
542 if (!s || !s->vetoed)
553 auto s =
add(amendment, sl);
562 JLOG(
j_.
error()) <<
"Unsupported amendment " << amendment
574 auto s =
get(amendment, sl);
575 return s && s->enabled;
582 auto s =
get(amendment, sl);
583 return s && s->supported;
612 if (e.second.supported && !e.second.vetoed &&
613 (enabled.
count(e.first) == 0))
615 amendments.push_back(e.first);
620 if (!amendments.empty())
621 std::sort(amendments.begin(), amendments.end());
642 <<
": " << enabledAmendments.
size() <<
", "
643 << majorityAmendments.
size() <<
", " << valSet.size();
645 auto vote = std::make_unique<AmendmentSet>(rules, valSet);
647 JLOG(
j_.
debug()) <<
"Received " << vote->trustedValidations()
648 <<
" trusted validations, threshold is: "
649 << vote->threshold();
662 bool const hasValMajority = vote->passes(entry.first);
665 auto const it = majorityAmendments.
find(entry.first);
666 if (it != majorityAmendments.
end())
667 majorityTime = it->second;
670 if (enabledAmendments.
count(entry.first) != 0)
672 JLOG(
j_.
debug()) << entry.first <<
": amendment already enabled";
676 !entry.second.vetoed)
679 JLOG(
j_.
debug()) << entry.first <<
": amendment got majority";
685 JLOG(
j_.
debug()) << entry.first <<
": amendment lost majority";
691 !entry.second.vetoed)
694 JLOG(
j_.
debug()) << entry.first <<
": amendment majority held";
695 actions[entry.first] = 0;
721 for (
auto& e : enabled)
733 for (
auto const& [hash, time] : majority)
735 auto s =
add(hash, sl);
742 JLOG(
j_.
info()) <<
"Unsupported amendment " << hash
743 <<
" reached majority at " <<
to_string(time);
760 v[jss::name] = fs.
name;
763 v[jss::vetoed] = fs.
vetoed;
768 auto const votesTotal =
lastVote_->trustedValidations();
769 auto const votesNeeded =
lastVote_->threshold();
770 auto const votesFor =
lastVote_->votes(
id);
772 v[jss::count] = votesFor;
773 v[jss::validations] = votesTotal;
776 v[jss::threshold] = votesNeeded;
806 auto a =
get(amendmentID, sl);
823 return std::make_unique<AmendmentTableImpl>(
824 app, majorityTime, supported, enabled, vetoed, journal);
Holds a collection of configuration values.
bool supported
Indicates an amendment that this server has code support for.
std::uint32_t lastUpdateSeq_
Current state of an amendment.
Stream trace() const
Severity stream access functions.
std::optional< NetClock::time_point > firstUnsupportedExpected_
std::vector< uint256 > getDesired() const override
std::vector< uint256 > doValidation(std::set< uint256 > const &enabledAmendments) const override
uint256 find(std::string const &name) const override
bool vetoed
If an amendment is vetoed, a server will not support it.
hash_map< uint256, int > votes_
bool unVeto(uint256 const &amendment) override
The status of all amendments requested in a given window.
const std::uint32_t tfLostMajority
const std::uint32_t tfGotMajority
Track the list of "amendments".
static std::vector< std::pair< uint256, std::string > > parseSection(Section const §ion)
bool passes(uint256 const &amendment) const
Integers of any length that is a multiple of 32-bits.
std::string const & name() const
Returns the name of this section.
T time_since_epoch(T... args)
void persistVote(uint256 const &amendment, std::string const &name, bool vote_to_veto) const
LockedSociSession checkoutDb()
@ objectValue
object value (collection of name/value pairs).
std::optional< NetClock::time_point > firstUnsupportedExpected() const override
constexpr std::ratio< 204, 256 > preFixAmendmentMajorityCalcThreshold
The minimum amount of support an amendment should have.
const uint256 fixAmendmentMajorityCalc
std::map< uint256, std::uint32_t > doVoting(Rules const &rules, NetClock::time_point closeTime, std::set< uint256 > const &enabledAmendments, majorityAmendments_t const &majorityAmendments, std::vector< std::shared_ptr< STValidation >> const &validations) override
bool isSupported(uint256 const &amendment) const override
AmendmentTableImpl(Application &app, std::chrono::seconds majorityTime, Section const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
std::unique_ptr< AmendmentTable > make_AmendmentTable(Application &app, std::chrono::seconds majorityTime, Section const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
std::vector< std::string > const & lines() const
Returns all the lines in the section.
A generic endpoint for log messages.
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
bool veto(uint256 const &amendment) override
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
hash_map< uint256, AmendmentState > amendmentMap_
std::unique_ptr< AmendmentSet > lastVote_
const std::chrono::seconds majorityTime_
bool enabled
Indicates that the amendment has been enabled.
AmendmentSet(Rules const &rules, std::vector< std::shared_ptr< STValidation >> const &valSet)
Json::Value getJson() const override
std::string name
The name of this amendment, possibly empty.
Rules controlling protocol behavior.
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
int trustedValidations() const
bool isEnabled(uint256 const &amendment) const override
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
void injectJson(Json::Value &v, uint256 const &amendment, AmendmentState const &state, std::lock_guard< std::mutex > const &sl) const
AmendmentState * add(uint256 const &amendment, std::lock_guard< std::mutex > const &sl)
The amendment table stores the list of enabled and potential amendments.
bool needValidatedLedger(LedgerIndex seq) const override
Called to determine whether the amendment logic needs to process a new validated ledger.
int votes(uint256 const &amendment) const
constexpr std::ratio< 80, 100 > postFixAmendmentMajorityCalcThreshold
void doValidatedLedger(LedgerIndex seq, std::set< uint256 > const &enabled, majorityAmendments_t const &majority) override
bool enable(uint256 const &amendment) override
const SF_VECTOR256 sfAmendments
AmendmentState * get(uint256 const &amendment, std::lock_guard< std::mutex > const &sl)
bool hasUnsupportedEnabled() const override
returns true if one or more amendments on the network have been enabled that this server does not sup...