1#include <xrpld/app/main/Application.h>
2#include <xrpld/app/misc/AmendmentTable.h>
3#include <xrpld/app/rdb/Wallet.h>
4#include <xrpld/core/ConfigSections.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/STValidation.h>
8#include <xrpl/protocol/TxFlags.h>
9#include <xrpl/protocol/jss.h>
11#include <boost/algorithm/string.hpp>
12#include <boost/format.hpp>
13#include <boost/range/adaptor/transformed.hpp>
14#include <boost/regex.hpp>
24 static boost::regex
const re1(
27 "([abcdefABCDEF0-9]{64})"
31 boost::regex_constants::optimize);
35 for (
auto const& line : section.
lines())
39 if (!boost::regex_match(line,
match, re1))
40 Throw<std::runtime_error>(
"Invalid entry '" + line +
"' in [" + section.
name() +
"]");
44 if (!
id.parseHex(
match[1]))
45 Throw<std::runtime_error>(
"Invalid amendment ID '" +
match[1] +
"' in [" + section.
name() +
"]");
101 newRecordedVotes.reserve(allTrusted.
size());
106 for (
auto& trusted : allTrusted)
117 newRecordedVotes[trusted];
147 using namespace std::chrono_literals;
150 auto const newTimeout = closeTime + expiresAfter;
154 for (
auto const& val : valSet)
160 iter->second.timeout = newTimeout;
161 if (val->isFieldPresent(sfAmendments))
163 auto const& choices = val->getFieldV256(sfAmendments);
164 iter->second.upVotes.assign(choices.begin(), choices.end());
165 JLOG(j.
debug()) <<
"recordVotes: Validation from trusted " << pkHuman <<
" has " << choices.size()
166 <<
" amendment votes: "
167 << boost::algorithm::join(
168 iter->second.upVotes | boost::adaptors::transformed(to_string<256, void>),
179 iter->second.upVotes.clear();
180 JLOG(j.
debug()) <<
"recordVotes: Validation from trusted " << pkHuman <<
" has no amendment votes.";
185 JLOG(j.
debug()) <<
"recordVotes: Ignoring validation from untrusted " << pkHuman;
193 [&closeTime, newTimeout, &j](
decltype(
recordedVotes_)::value_type& votes) {
194 auto const pkHuman = toBase58(TokenType::NodePublic, votes.first);
195 if (!votes.second.timeout)
198 votes.second.upVotes.empty(),
199 "xrpl::TrustedVotes::recordVotes : received no "
201 JLOG(j.debug()) <<
"recordVotes: Have not received any "
202 "amendment votes from "
203 << pkHuman <<
" since last timeout or startup";
205 else if (closeTime > votes.second.timeout)
207 JLOG(j.debug()) <<
"recordVotes: Timeout: Clearing votes from " << pkHuman;
208 votes.second.timeout.reset();
209 votes.second.upVotes.clear();
211 else if (votes.second.timeout != newTimeout)
214 votes.second.timeout < newTimeout,
215 "xrpl::TrustedVotes::recordVotes : votes not "
217 using namespace std::chrono;
218 auto const age = duration_cast<minutes>(newTimeout - *votes.second.timeout);
219 JLOG(j.debug()) <<
"recordVotes: Using " << age.count() <<
"min old cached votes from " << pkHuman;
232 for (
auto& validatorVotes : recordedVotes_)
235 validatorVotes.second.timeout || validatorVotes.second.upVotes.empty(),
236 "xrpl::TrustedVotes::getVotes : valid votes");
237 if (validatorVotes.second.timeout)
239 for (
uint256 const& amendment : validatorVotes.second.upVotes)
262 bool enabled =
false;
265 bool supported =
false;
280 int trustedValidations_ = 0;
288 auto [trustedCount, newVotes] = trustedVotes.
getVotes(rules, lock);
290 trustedValidations_ = trustedCount;
291 votes_.
swap(newVotes);
302 auto const& it = votes_.
find(amendment);
304 if (it == votes_.
end())
309 if (trustedValidations_ == 1)
310 return it->second >= threshold_;
312 return it->second > threshold_;
318 auto const& it = votes_.
find(amendment);
320 if (it == votes_.
end())
329 return trustedValidations_;
414 veto(
uint256 const& amendment)
override;
416 unVeto(
uint256 const& amendment)
override;
419 enable(
uint256 const& amendment)
override;
422 isEnabled(
uint256 const& amendment)
const override;
424 isSupported(
uint256 const& amendment)
const override;
427 hasUnsupportedEnabled()
const override;
430 firstUnsupportedExpected()
const override;
433 getJson(
bool isAdmin)
const override;
438 needValidatedLedger(
LedgerIndex seq)
const override;
450 getDesired()
const override;
463AmendmentTableImpl::AmendmentTableImpl(
470 : lastUpdateSeq_(0), majorityTime_(majorityTime), unsupportedEnabled_(false), j_(journal), db_(app.getWalletDB())
475 bool const featureVotesExist = [
this]() {
481 for (
auto const& [name, amendment, votebehavior] : supported)
487 switch (votebehavior)
502 JLOG(
j_.
debug()) <<
"Amendment " << amendment <<
" (" << s.
name <<
") is supported and will be "
504 <<
" voted by default if not enabled on the ledger.";
511 if (featureVotesExist)
513 JLOG(
j_.
warn()) <<
"[amendments] section in config file ignored"
514 " in favor of data in db/wallet.db.";
519 detect_conflict.
insert(a.first);
527 if (featureVotesExist)
529 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config file ignored"
530 " in favor of data in db/wallet.db.";
535 if (detect_conflict.
count(a.first) == 0)
541 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config has amendment " <<
'(' << a.first <<
", "
542 << a.second <<
") both [veto_amendments] and [amendments].";
551 [&](boost::optional<std::string> amendment_hash,
552 boost::optional<std::string> amendment_name,
553 boost::optional<AmendmentVote> vote) {
555 if (!amendment_hash || !amendment_name || !vote)
558 Throw<std::runtime_error>(
"Invalid FeatureVotes row in wallet.db");
560 if (!amend_hash.
parseHex(*amendment_hash))
562 Throw<std::runtime_error>(
"Invalid amendment ID '" + *amendment_hash +
" in wallet.db");
567 if (
auto s =
get(amend_hash, lock))
569 JLOG(
j_.
info()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash <<
"} is downvoted.";
570 if (!amendment_name->empty())
571 s->name = *amendment_name;
581 JLOG(
j_.
debug()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash <<
"} is upvoted.";
582 if (!amendment_name->empty())
583 s.
name = *amendment_name;
624 if (name == e.second.name)
678 JLOG(
j_.
error()) <<
"Unsupported amendment " << amendment <<
" activated.";
729 amendments.push_back(e.first);
730 JLOG(
j_.
info()) <<
"Voting for amendment " << e.second.name;
735 if (!amendments.empty())
736 std::sort(amendments.begin(), amendments.end());
757 << majorityAmendments.
size() <<
", " << valSet.size();
766 JLOG(
j_.
debug()) <<
"Counted votes from " << vote->trustedValidations()
767 <<
" valid trusted validations, threshold is: " << vote->threshold();
776 if (enabledAmendments.
contains(entry.first))
778 JLOG(
j_.
trace()) << entry.first <<
": amendment already enabled";
783 bool const hasValMajority = vote->passes(entry.first);
786 auto const it = majorityAmendments.
find(entry.first);
787 if (it != majorityAmendments.
end())
792 bool const hasLedgerMajority = majorityTime.has_value();
794 auto const logStr = [&entry, &vote]() {
796 ss << entry.first <<
" (" << entry.second.name <<
") has " << vote->votes(entry.first) <<
" votes";
800 if (hasValMajority && !hasLedgerMajority && entry.second.vote ==
AmendmentVote::up)
804 JLOG(
j_.
debug()) << logStr <<
": amendment got majority";
807 else if (!hasValMajority && hasLedgerMajority)
810 JLOG(
j_.
debug()) << logStr <<
": amendment lost majority";
814 hasLedgerMajority && ((*majorityTime +
majorityTime_) <= closeTime) &&
818 JLOG(
j_.
debug()) << logStr <<
": amendment majority held";
819 actions[entry.first] = 0;
822 else if (hasValMajority && hasLedgerMajority)
824 JLOG(
j_.
debug()) << logStr <<
": amendment holding majority, waiting to be enabled";
826 else if (!hasValMajority)
828 JLOG(
j_.
debug()) << logStr <<
": amendment does not have majority";
854 for (
auto& e : enabled)
866 for (
auto const& [hash, time] : majority)
875 JLOG(
j_.
info()) <<
"Unsupported amendment " << hash <<
" reached majority at " <<
to_string(time);
900 v[jss::name] = fs.
name;
906 v[jss::vetoed] =
"Obsolete";
914 auto const votesTotal =
lastVote_->trustedValidations();
915 auto const votesNeeded =
lastVote_->threshold();
916 auto const votesFor =
lastVote_->votes(
id);
918 v[jss::count] = votesFor;
919 v[jss::validations] = votesTotal;
922 v[jss::threshold] = votesNeeded;
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
The status of all amendments requested in a given window.
int trustedValidations() const
int votes(uint256 const &amendment) const
hash_map< uint256, int > votes_
AmendmentSet(Rules const &rules, TrustedVotes const &trustedVotes, std::lock_guard< std::mutex > const &lock)
bool passes(uint256 const &amendment) const
Track the list of "amendments".
Json::Value getJson(bool isAdmin) const override
bool isSupported(uint256 const &amendment) const override
std::optional< NetClock::time_point > firstUnsupportedExpected() const override
std::optional< NetClock::time_point > firstUnsupportedExpected_
void injectJson(Json::Value &v, uint256 const &amendment, AmendmentState const &state, bool isAdmin, std::lock_guard< std::mutex > const &lock) const
TrustedVotes previousTrustedVotes_
std::vector< uint256 > doValidation(std::set< uint256 > const &enabledAmendments) const override
bool veto(uint256 const &amendment) override
bool hasUnsupportedEnabled() const override
returns true if one or more amendments on the network have been enabled that this server does not sup...
void persistVote(uint256 const &amendment, std::string const &name, AmendmentVote vote) const
AmendmentState & add(uint256 const &amendment, std::lock_guard< std::mutex > const &lock)
std::uint32_t lastUpdateSeq_
std::unique_ptr< AmendmentSet > lastVote_
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 isEnabled(uint256 const &amendment) const override
std::chrono::seconds const majorityTime_
AmendmentState * get(uint256 const &amendment, std::lock_guard< std::mutex > const &lock)
bool enable(uint256 const &amendment) override
bool unVeto(uint256 const &amendment) override
uint256 find(std::string const &name) const override
std::vector< uint256 > getDesired() const override
void trustChanged(hash_set< PublicKey > const &allTrusted) override
void doValidatedLedger(LedgerIndex seq, std::set< uint256 > const &enabled, majorityAmendments_t const &majority) override
bool needValidatedLedger(LedgerIndex seq) const override
Called to determine whether the amendment logic needs to process a new validated ledger.
hash_map< uint256, AmendmentState > amendmentMap_
The amendment table stores the list of enabled and potential amendments.
LockedSociSession checkoutDb()
Rules controlling protocol behavior.
Holds a collection of configuration values.
std::string const & name() const
Returns the name of this section.
std::vector< std::string > const & lines() const
Returns all the lines in the section.
TrustedVotes records the most recent votes from trusted validators.
TrustedVotes & operator=(TrustedVotes const &rhs)=delete
hash_map< PublicKey, UpvotesAndTimeout > recordedVotes_
std::pair< int, hash_map< uint256, int > > getVotes(Rules const &rules, std::lock_guard< std::mutex > const &lock) const
void trustChanged(hash_set< PublicKey > const &allTrusted, std::lock_guard< std::mutex > const &lock)
void recordVotes(Rules const &rules, std::vector< std::shared_ptr< STValidation > > const &valSet, NetClock::time_point const closeTime, beast::Journal j, std::lock_guard< std::mutex > const &lock)
TrustedVotes(TrustedVotes const &rhs)=delete
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
@ objectValue
object value (collection of name/value pairs).
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.
Json::Value getJson(LedgerFill const &fill)
Return a new Json::Value representing the ledger with given options.
std::string to_string(base_uint< Bits, Tag > const &a)
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::unique_ptr< AmendmentTable > make_AmendmentTable(Application &app, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
bool isAdmin(Port const &port, Json::Value const ¶ms, beast::IP::Address const &remoteIp)
constexpr std::uint32_t tfLostMajority
constexpr std::uint32_t tfGotMajority
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.
bool createFeatureVotes(soci::session &session)
createFeatureVotes Creates the FeatureVote table if it does not exist.
constexpr std::ratio< 80, 100 > amendmentMajorityCalcThreshold
The minimum amount of support an amendment should have.
static std::vector< std::pair< uint256, std::string > > parseSection(Section const §ion)
Current state of an amendment.
std::string name
The name of this amendment, possibly empty.
AmendmentVote vote
If an amendment is down-voted, a server will not vote to enable it.
bool supported
Indicates an amendment that this server has code support for.
bool enabled
Indicates that the amendment has been enabled.
std::vector< uint256 > upVotes
std::optional< NetClock::time_point > timeout
An unseated timeout indicates that either.
T time_since_epoch(T... args)