1#include <xrpld/app/misc/AmendmentTable.h>
2#include <xrpld/core/ConfigSections.h>
4#include <xrpl/core/ServiceRegistry.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/STValidation.h>
7#include <xrpl/protocol/TxFlags.h>
8#include <xrpl/protocol/jss.h>
9#include <xrpl/server/Wallet.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(
471 , majorityTime_(majorityTime)
472 , unsupportedEnabled_(false)
474 , db_(registry.getWalletDB())
479 bool const featureVotesExist = [
this]() {
485 for (
auto const& [name, amendment, votebehavior] : supported)
491 switch (votebehavior)
506 JLOG(
j_.
debug()) <<
"Amendment " << amendment <<
" (" << s.
name <<
") is supported and will be "
508 <<
" voted by default if not enabled on the ledger.";
515 if (featureVotesExist)
517 JLOG(
j_.
warn()) <<
"[amendments] section in config file ignored"
518 " in favor of data in db/wallet.db.";
523 detect_conflict.
insert(a.first);
531 if (featureVotesExist)
533 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config file ignored"
534 " in favor of data in db/wallet.db.";
539 if (detect_conflict.
count(a.first) == 0)
545 JLOG(
j_.
warn()) <<
"[veto_amendments] section in config has amendment " <<
'(' << a.first <<
", "
546 << a.second <<
") both [veto_amendments] and [amendments].";
555 [&](boost::optional<std::string> amendment_hash,
556 boost::optional<std::string> amendment_name,
557 boost::optional<AmendmentVote> vote) {
559 if (!amendment_hash || !amendment_name || !vote)
562 Throw<std::runtime_error>(
"Invalid FeatureVotes row in wallet.db");
564 if (!amend_hash.
parseHex(*amendment_hash))
566 Throw<std::runtime_error>(
"Invalid amendment ID '" + *amendment_hash +
" in wallet.db");
571 if (
auto s =
get(amend_hash, lock))
573 JLOG(
j_.
info()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash <<
"} is downvoted.";
574 if (!amendment_name->empty())
575 s->name = *amendment_name;
585 JLOG(
j_.
debug()) <<
"Amendment {" << *amendment_name <<
", " << amend_hash <<
"} is upvoted.";
586 if (!amendment_name->empty())
587 s.
name = *amendment_name;
628 if (name == e.second.name)
682 JLOG(
j_.
error()) <<
"Unsupported amendment " << amendment <<
" activated.";
733 amendments.push_back(e.first);
734 JLOG(
j_.
info()) <<
"Voting for amendment " << e.second.name;
739 if (!amendments.empty())
740 std::sort(amendments.begin(), amendments.end());
761 << majorityAmendments.
size() <<
", " << valSet.size();
770 JLOG(
j_.
debug()) <<
"Counted votes from " << vote->trustedValidations()
771 <<
" valid trusted validations, threshold is: " << vote->threshold();
780 if (enabledAmendments.
contains(entry.first))
782 JLOG(
j_.
trace()) << entry.first <<
": amendment already enabled";
787 bool const hasValMajority = vote->passes(entry.first);
790 auto const it = majorityAmendments.
find(entry.first);
791 if (it != majorityAmendments.
end())
796 bool const hasLedgerMajority = majorityTime.has_value();
798 auto const logStr = [&entry, &vote]() {
800 ss << entry.first <<
" (" << entry.second.name <<
") has " << vote->votes(entry.first) <<
" votes";
804 if (hasValMajority && !hasLedgerMajority && entry.second.vote ==
AmendmentVote::up)
808 JLOG(
j_.
debug()) << logStr <<
": amendment got majority";
811 else if (!hasValMajority && hasLedgerMajority)
814 JLOG(
j_.
debug()) << logStr <<
": amendment lost majority";
818 hasLedgerMajority && ((*majorityTime +
majorityTime_) <= closeTime) &&
822 JLOG(
j_.
debug()) << logStr <<
": amendment majority held";
823 actions[entry.first] = 0;
826 else if (hasValMajority && hasLedgerMajority)
828 JLOG(
j_.
debug()) << logStr <<
": amendment holding majority, waiting to be enabled";
830 else if (!hasValMajority)
832 JLOG(
j_.
debug()) << logStr <<
": amendment does not have majority";
858 for (
auto& e : enabled)
870 for (
auto const& [hash, time] : majority)
879 JLOG(
j_.
info()) <<
"Unsupported amendment " << hash <<
" reached majority at " <<
to_string(time);
904 v[jss::name] = fs.
name;
910 v[jss::vetoed] =
"Obsolete";
918 auto const votesTotal =
lastVote_->trustedValidations();
919 auto const votesNeeded =
lastVote_->threshold();
920 auto const votesFor =
lastVote_->votes(
id);
922 v[jss::count] = votesFor;
923 v[jss::validations] = votesTotal;
926 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.
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)