1#include <xrpl/ledger/AmendmentTable.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/UnorderedContainers.h>
5#include <xrpl/basics/base_uint.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/config/BasicConfig.h>
11#include <xrpl/core/ServiceRegistry.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/ledger/View.h>
14#include <xrpl/protocol/Feature.h>
15#include <xrpl/protocol/Protocol.h>
16#include <xrpl/protocol/PublicKey.h>
17#include <xrpl/protocol/Rules.h>
18#include <xrpl/protocol/SField.h>
19#include <xrpl/protocol/STValidation.h>
20#include <xrpl/protocol/SystemParameters.h>
21#include <xrpl/protocol/TxFlags.h>
22#include <xrpl/protocol/jss.h>
23#include <xrpl/protocol/tokens.h>
24#include <xrpl/server/Wallet.h>
26#include <boost/algorithm/string/join.hpp>
27#include <boost/optional/optional.hpp>
28#include <boost/range/adaptor/transformed.hpp>
29#include <boost/regex/v5/regbase.hpp>
30#include <boost/regex/v5/regex.hpp>
31#include <boost/regex/v5/regex_match.hpp>
49static std::vector<std::pair<uint256, std::string>>
52 static boost::regex
const kRe1(
55 "([abcdefABCDEF0-9]{64})"
59 boost::regex_constants::optimize);
63 for (
auto const& line : section.
lines())
67 if (!boost::regex_match(line, match, kRe1))
72 if (!
id.parseHex(match[1]))
75 "Invalid amendment ID '" + match[1] +
"' in [" + section.
name() +
"]");
132 newRecordedVotes.reserve(allTrusted.
size());
137 for (
auto& trusted : allTrusted)
148 newRecordedVotes[trusted];
178 using namespace std::chrono_literals;
181 auto const newTimeout = closeTime + kExpiresAfter;
185 for (
auto const& val : valSet)
189 if (
auto const iter =
recordedVotes_.find(val->getSignerPublic());
192 iter->second.timeout = newTimeout;
193 if (val->isFieldPresent(sfAmendments))
195 auto const& choices = val->getFieldV256(sfAmendments);
196 iter->second.upVotes.assign(choices.begin(), choices.end());
197 JLOG(j.
debug()) <<
"recordVotes: Validation from trusted " << pkHuman <<
" has "
198 << choices.size() <<
" amendment votes: "
199 << boost::algorithm::join(
200 iter->second.upVotes |
212 iter->second.upVotes.clear();
213 JLOG(j.
debug()) <<
"recordVotes: Validation from trusted " << pkHuman
214 <<
" has no amendment votes.";
219 JLOG(j.
debug()) <<
"recordVotes: Ignoring validation from untrusted " << pkHuman;
226 [&closeTime, newTimeout, &j](
decltype(
recordedVotes_)::value_type& votes) {
228 if (!votes.second.timeout)
231 votes.second.upVotes.empty(),
232 "xrpl::TrustedVotes::recordVotes : received no "
234 JLOG(j.
debug()) <<
"recordVotes: Have not received any "
235 "amendment votes from "
236 << pkHuman <<
" since last timeout or startup";
238 else if (closeTime > votes.second.timeout)
240 JLOG(j.
debug()) <<
"recordVotes: Timeout: Clearing votes from " << pkHuman;
241 votes.second.timeout.reset();
242 votes.second.upVotes.clear();
244 else if (votes.second.timeout != newTimeout)
247 votes.second.timeout < newTimeout,
248 "xrpl::TrustedVotes::recordVotes : votes not "
252 JLOG(j.
debug()) <<
"recordVotes: Using " << age.count()
253 <<
"min old cached votes from " << pkHuman;
269 validatorVotes.second.timeout || validatorVotes.second.upVotes.empty(),
270 "xrpl::TrustedVotes::getVotes : valid votes");
271 if (validatorVotes.second.timeout)
273 for (
uint256 const& amendment : validatorVotes.second.upVotes)
278 return {available, ret};
325 auto [trustedCount, newVotes] = trustedVotes.
getVotes(rules,
lock);
340 auto const& it =
votes_.find(amendment);
356 auto const& it =
votes_.find(amendment);
516 bool const featureVotesExist = [
this]() {
517 auto db =
db_.checkoutDb();
522 for (
auto const& [name, amendment, votebehavior] : supported)
528 switch (votebehavior)
543 JLOG(
j_.debug()) <<
"Amendment " << amendment <<
" (" << s.
name
544 <<
") is supported and will be "
546 <<
" voted by default if not enabled on the ledger.";
553 if (featureVotesExist)
555 JLOG(
j_.warn()) <<
"[amendments] section in config file ignored"
556 " in favor of data in db/wallet.db.";
561 detectConflict.
insert(a.first);
568 if (featureVotesExist)
570 JLOG(
j_.warn()) <<
"[veto_amendments] section in config file ignored"
571 " in favor of data in db/wallet.db.";
576 if (!detectConflict.
contains(a.first))
582 JLOG(
j_.warn()) <<
"[veto_amendments] section in config has amendment " <<
'('
583 << a.first <<
", " << a.second
584 <<
") both [veto_amendments] and [amendments].";
589 auto db =
db_.checkoutDb();
592 [&](boost::optional<std::string> amendmentHash,
593 boost::optional<std::string> amendmentName,
594 boost::optional<AmendmentVote> vote) {
596 if (!amendmentHash || !amendmentName || !vote)
601 if (!amendHash.
parseHex(*amendmentHash))
604 "Invalid amendment ID '" + *amendmentHash +
" in wallet.db");
609 if (
auto s =
get(amendHash,
lock))
611 JLOG(
j_.info()) <<
"Amendment {" << *amendmentName <<
", " << amendHash
612 <<
"} is downvoted.";
613 if (!amendmentName->empty())
614 s->name = *amendmentName;
624 JLOG(
j_.debug()) <<
"Amendment {" << *amendmentName <<
", " << amendHash
626 if (!amendmentName->empty())
627 s.
name = *amendmentName;
668 if (name == e.second.name)
683 "xrpl::AmendmentTableImpl::persistVote : valid vote input");
684 auto db =
db_.checkoutDb();
727 JLOG(
j_.error()) <<
"Unsupported amendment " << amendment <<
" activated.";
739 return (s !=
nullptr) && s->
enabled;
779 amendments.push_back(e.first);
780 JLOG(
j_.info()) <<
"Voting for amendment " << e.second.name;
785 if (!amendments.empty())
807 << enabledAmendments.
size() <<
", " << majorityAmendments.
size() <<
", "
817 JLOG(
j_.debug()) <<
"Counted votes from " << vote->trustedValidations()
818 <<
" valid trusted validations, threshold is: " << vote->threshold();
827 if (enabledAmendments.
contains(entry.first))
829 JLOG(
j_.trace()) << entry.first <<
": amendment already enabled";
834 bool const hasValMajority = vote->passes(entry.first);
837 auto const it = majorityAmendments.
find(entry.first);
838 if (it != majorityAmendments.
end())
843 bool const hasLedgerMajority = majorityTime.
has_value();
845 auto const logStr = [&entry, &vote]() {
847 ss << entry.first <<
" (" << entry.second.name <<
") has " << vote->votes(entry.first)
852 if (hasValMajority && !hasLedgerMajority && entry.second.vote ==
AmendmentVote::Up)
856 JLOG(
j_.debug()) << logStr <<
": amendment got majority";
857 actions[entry.first] = tfGotMajority;
859 else if (!hasValMajority && hasLedgerMajority)
862 JLOG(
j_.debug()) << logStr <<
": amendment lost majority";
863 actions[entry.first] = tfLostMajority;
866 hasLedgerMajority && ((*majorityTime +
majorityTime_) <= closeTime) &&
870 JLOG(
j_.debug()) << logStr <<
": amendment majority held";
871 actions[entry.first] = 0;
874 else if (hasValMajority && hasLedgerMajority)
876 JLOG(
j_.debug()) << logStr <<
": amendment holding majority, waiting to be enabled";
878 else if (!hasValMajority)
880 JLOG(
j_.debug()) << logStr <<
": amendment does not have majority";
906 for (
auto& e : enabled)
918 for (
auto const& [
hash,
time] : majority)
927 JLOG(
j_.info()) <<
"Unsupported amendment " <<
hash <<
" reached majority at "
953 v[jss::name] = fs.
name;
960 v[jss::vetoed] =
"Obsolete";
971 auto const votesTotal =
lastVote_->trustedValidations();
972 auto const votesNeeded =
lastVote_->threshold();
973 auto const votesFor =
lastVote_->votes(
id);
975 v[jss::count] = votesFor;
976 v[jss::validations] = votesTotal;
978 if (votesNeeded != 0)
979 v[jss::threshold] = votesNeeded;
1030 registry, majorityTime, supported, enabled, vetoed, journal);
A generic endpoint for log messages.
int trustedValidations() const
AmendmentSet(Rules const &rules, TrustedVotes const &trustedVotes, std::scoped_lock< std::mutex > const &lock)
int votes(uint256 const &amendment) const
hash_map< uint256, int > votes_
bool passes(uint256 const &amendment) const
bool isSupported(uint256 const &amendment) const override
std::optional< NetClock::time_point > firstUnsupportedExpected() const override
std::optional< NetClock::time_point > firstUnsupportedExpected_
json::Value getJson(bool isAdmin) const override
AmendmentState & add(uint256 const &amendment, std::scoped_lock< std::mutex > const &lock)
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
std::uint32_t lastUpdateSeq_
AmendmentTableImpl(ServiceRegistry ®istry, std::chrono::seconds majorityTime, std::vector< FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
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_
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
AmendmentState * get(uint256 const &amendment, std::scoped_lock< std::mutex > const &lock)
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_
void injectJson(json::Value &v, uint256 const &amendment, AmendmentState const &state, bool isAdmin, std::scoped_lock< std::mutex > const &lock) const
The amendment table stores the list of enabled and potential amendments.
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
std::chrono::time_point< NetClock > time_point
std::chrono::duration< rep, period > duration
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
std::pair< int, hash_map< uint256, int > > getVotes(Rules const &rules, std::scoped_lock< std::mutex > const &lock) const
hash_map< PublicKey, UpvotesAndTimeout > recordedVotes_
void recordVotes(Rules const &rules, std::vector< std::shared_ptr< STValidation > > const &valSet, NetClock::time_point const closeTime, beast::Journal j, std::scoped_lock< std::mutex > const &lock)
TrustedVotes(TrustedVotes const &rhs)=delete
void trustChanged(hash_set< PublicKey > const &allTrusted, std::scoped_lock< std::mutex > const &lock)
T duration_cast(T... args)
T emplace_back(T... args)
@ Object
object value (collection of name/value pairs).
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::unique_ptr< AmendmentTable > makeAmendmentTable(ServiceRegistry ®istry, std::chrono::seconds majorityTime, std::vector< AmendmentTable::FeatureInfo > const &supported, Section const &enabled, Section const &vetoed, beast::Journal journal)
std::uint32_t LedgerIndex
A ledger index.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
std::unordered_set< Value, Hash, Pred, Allocator > hash_set
void readAmendments(soci::session &session, std::function< void(boost::optional< std::string > amendmentHash, boost::optional< std::string > amendmentName, boost::optional< AmendmentVote > vote)> const &callback)
readAmendments Reads all amendments from the FeatureVotes table.
constexpr std::ratio< 80, 100 > kAmendmentMajorityCalcThreshold
The minimum amount of support an amendment should have.
std::string to_string(BaseUInt< Bits, Tag > const &a)
bool createFeatureVotes(soci::session &session)
createFeatureVotes Creates the FeatureVote table if it does not exist.
std::map< uint256, NetClock::time_point > majorityAmendments_t
std::unordered_map< Key, Value, Hash, Pred, Allocator > hash_map
bool isAdmin(Port const &port, json::Value const ¶ms, beast::IP::Address const &remoteIp)
void voteAmendment(soci::session &session, uint256 const &amendment, std::string const &name, AmendmentVote vote)
voteAmendment Set the veto value for a particular amendment.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
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)