1#include <xrpl/core/ServiceRegistry.h>
2#include <xrpl/ledger/AmendmentTable.h>
3#include <xrpl/protocol/Feature.h>
4#include <xrpl/protocol/STValidation.h>
5#include <xrpl/protocol/TxFlags.h>
6#include <xrpl/protocol/jss.h>
7#include <xrpl/server/Wallet.h>
9#include <boost/algorithm/string.hpp>
10#include <boost/format.hpp>
11#include <boost/range/adaptor/transformed.hpp>
12#include <boost/regex.hpp>
22 static boost::regex
const re1(
25 "([abcdefABCDEF0-9]{64})"
29 boost::regex_constants::optimize);
33 for (
auto const& line : section.
lines())
37 if (!boost::regex_match(line,
match, re1))
38 Throw<std::runtime_error>(
"Invalid entry '" + line +
"' in [" + section.
name() +
"]");
42 if (!
id.parseHex(
match[1]))
43 Throw<std::runtime_error>(
44 "Invalid amendment ID '" +
match[1] +
"' in [" + section.
name() +
"]");
100 newRecordedVotes.reserve(allTrusted.
size());
105 for (
auto& trusted : allTrusted)
116 newRecordedVotes[trusted];
146 using namespace std::chrono_literals;
149 auto const newTimeout = closeTime + expiresAfter;
153 for (
auto const& val : valSet)
157 if (
auto const iter =
recordedVotes_.find(val->getSignerPublic());
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 "
166 << choices.size() <<
" amendment votes: "
167 << boost::algorithm::join(
168 iter->second.upVotes |
169 boost::adaptors::transformed(to_string<256, void>),
180 iter->second.upVotes.clear();
181 JLOG(j.
debug()) <<
"recordVotes: Validation from trusted " << pkHuman
182 <<
" has no amendment votes.";
187 JLOG(j.
debug()) <<
"recordVotes: Ignoring validation from untrusted " << pkHuman;
195 [&closeTime, newTimeout, &j](
decltype(
recordedVotes_)::value_type& votes) {
196 auto const pkHuman = toBase58(TokenType::NodePublic, votes.first);
197 if (!votes.second.timeout)
200 votes.second.upVotes.empty(),
201 "xrpl::TrustedVotes::recordVotes : received no "
203 JLOG(j.debug()) <<
"recordVotes: Have not received any "
204 "amendment votes from "
205 << pkHuman <<
" since last timeout or startup";
207 else if (closeTime > votes.second.timeout)
209 JLOG(j.debug()) <<
"recordVotes: Timeout: Clearing votes from " << pkHuman;
210 votes.second.timeout.reset();
211 votes.second.upVotes.clear();
213 else if (votes.second.timeout != newTimeout)
216 votes.second.timeout < newTimeout,
217 "xrpl::TrustedVotes::recordVotes : votes not "
219 using namespace std::chrono;
220 auto const age = duration_cast<minutes>(newTimeout - *votes.second.timeout);
221 JLOG(j.debug()) <<
"recordVotes: Using " << age.count()
222 <<
"min old cached votes from " << pkHuman;
235 for (
auto& validatorVotes : recordedVotes_)
238 validatorVotes.second.timeout || validatorVotes.second.upVotes.empty(),
239 "xrpl::TrustedVotes::getVotes : valid votes");
240 if (validatorVotes.second.timeout)
242 for (
uint256 const& amendment : validatorVotes.second.upVotes)
265 bool enabled =
false;
268 bool supported =
false;
283 int trustedValidations_ = 0;
294 auto [trustedCount, newVotes] = trustedVotes.
getVotes(rules, lock);
296 trustedValidations_ = trustedCount;
297 votes_.
swap(newVotes);
309 auto const& it = votes_.
find(amendment);
311 if (it == votes_.
end())
316 if (trustedValidations_ == 1)
317 return it->second >= threshold_;
319 return it->second > threshold_;
325 auto const& it = votes_.
find(amendment);
327 if (it == votes_.
end())
336 return trustedValidations_;
421 veto(
uint256 const& amendment)
override;
423 unVeto(
uint256 const& amendment)
override;
426 enable(
uint256 const& amendment)
override;
429 isEnabled(
uint256 const& amendment)
const override;
431 isSupported(
uint256 const& amendment)
const override;
434 hasUnsupportedEnabled()
const override;
437 firstUnsupportedExpected()
const override;
440 getJson(
bool isAdmin)
const override;
445 needValidatedLedger(
LedgerIndex seq)
const override;
460 getDesired()
const override;
473AmendmentTableImpl::AmendmentTableImpl(
481 , majorityTime_(majorityTime)
482 , unsupportedEnabled_(false)
484 , db_(registry.getWalletDB())
489 bool const featureVotesExist = [
this]() {
495 for (
auto const& [name, amendment, votebehavior] : supported)
501 switch (votebehavior)
516 JLOG(
j_.
debug()) <<
"Amendment " << amendment <<
" (" << s.
name
517 <<
") is supported and will be "
519 <<
" voted by default if not enabled on the ledger.";
526 if (featureVotesExist)
528 JLOG(
j_.
warn()) <<
"[amendments] section in config file ignored"
529 " in favor of data in db/wallet.db.";
534 detect_conflict.
insert(a.first);
542 if (featureVotesExist)
544 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config file ignored"
545 " in favor of data in db/wallet.db.";
550 if (detect_conflict.
count(a.first) == 0)
556 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config has amendment " <<
'('
557 << a.first <<
", " << a.second
558 <<
") both [veto_amendments] and [amendments].";
567 [&](boost::optional<std::string> amendment_hash,
568 boost::optional<std::string> amendment_name,
569 boost::optional<AmendmentVote> vote) {
571 if (!amendment_hash || !amendment_name || !vote)
574 Throw<std::runtime_error>(
"Invalid FeatureVotes row in wallet.db");
576 if (!amend_hash.
parseHex(*amendment_hash))
578 Throw<std::runtime_error>(
579 "Invalid amendment ID '" + *amendment_hash +
" in wallet.db");
584 if (
auto s =
get(amend_hash, lock))
586 JLOG(
j_.
info()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash
587 <<
"} is downvoted.";
588 if (!amendment_name->empty())
589 s->name = *amendment_name;
599 JLOG(
j_.
debug()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash
601 if (!amendment_name->empty())
602 s.
name = *amendment_name;
643 if (name == e.second.name)
658 "xrpl::AmendmentTableImpl::persistVote : valid vote input");
702 JLOG(
j_.
error()) <<
"Unsupported amendment " << amendment <<
" activated.";
752 (enabled.
count(e.first) == 0))
754 amendments.push_back(e.first);
755 JLOG(
j_.
info()) <<
"Voting for amendment " << e.second.name;
760 if (!amendments.empty())
761 std::sort(amendments.begin(), amendments.end());
782 << enabledAmendments.
size() <<
", " << majorityAmendments.
size() <<
", "
792 JLOG(
j_.
debug()) <<
"Counted votes from " << vote->trustedValidations()
793 <<
" valid trusted validations, threshold is: " << vote->threshold();
802 if (enabledAmendments.
contains(entry.first))
804 JLOG(
j_.
trace()) << entry.first <<
": amendment already enabled";
809 bool const hasValMajority = vote->passes(entry.first);
812 auto const it = majorityAmendments.
find(entry.first);
813 if (it != majorityAmendments.
end())
818 bool const hasLedgerMajority = majorityTime.has_value();
820 auto const logStr = [&entry, &vote]() {
822 ss << entry.first <<
" (" << entry.second.name <<
") has " << vote->votes(entry.first)
827 if (hasValMajority && !hasLedgerMajority && entry.second.vote ==
AmendmentVote::up)
831 JLOG(
j_.
debug()) << logStr <<
": amendment got majority";
834 else if (!hasValMajority && hasLedgerMajority)
837 JLOG(
j_.
debug()) << logStr <<
": amendment lost majority";
841 hasLedgerMajority && ((*majorityTime +
majorityTime_) <= closeTime) &&
845 JLOG(
j_.
debug()) << logStr <<
": amendment majority held";
846 actions[entry.first] = 0;
849 else if (hasValMajority && hasLedgerMajority)
851 JLOG(
j_.
debug()) << logStr <<
": amendment holding majority, waiting to be enabled";
853 else if (!hasValMajority)
855 JLOG(
j_.
debug()) << logStr <<
": amendment does not have majority";
881 for (
auto& e : enabled)
893 for (
auto const& [hash, time] : majority)
902 JLOG(
j_.
info()) <<
"Unsupported amendment " << hash <<
" reached majority at "
928 v[jss::name] = fs.
name;
934 v[jss::vetoed] =
"Obsolete";
942 auto const votesTotal =
lastVote_->trustedValidations();
943 auto const votesNeeded =
lastVote_->threshold();
944 auto const votesFor =
lastVote_->votes(
id);
946 v[jss::count] = votesFor;
947 v[jss::validations] = votesTotal;
950 v[jss::threshold] = votesNeeded;
997 registry, majorityTime, supported, enabled, vetoed, journal);
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.
Service registry for dependency injection.
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.
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(ServiceRegistry ®istry, 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.
void voteAmendment(soci::session &session, uint256 const &amendment, std::string const &name, AmendmentVote vote)
voteAmendment Set the veto value for a particular amendment.
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)