Skip to content

Commit

Permalink
Expose all amendments known by libxrpl (#5026)
Browse files Browse the repository at this point in the history
  • Loading branch information
godexsoft authored Jun 14, 2024
1 parent ae7ea33 commit 20d0549
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
6 changes: 6 additions & 0 deletions src/ripple/protocol/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/container/flat_map.hpp>
#include <array>
#include <bitset>
#include <map>
#include <optional>
#include <string>

Expand Down Expand Up @@ -67,6 +68,11 @@
namespace ripple {

enum class VoteBehavior : int { Obsolete = -1, DefaultNo = 0, DefaultYes };
enum class AmendmentSupport : int { Retired = -1, Supported = 0, Unsupported };

/** All amendments libxrpl knows about. */
std::map<std::string, AmendmentSupport> const&
allAmendments();

namespace detail {

Expand Down
26 changes: 26 additions & 0 deletions src/ripple/protocol/impl/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class FeatureCollections
// name, index, and uint256 feature identifier
boost::multi_index::multi_index_container<Feature, feature_indexing>
features;
std::map<std::string, AmendmentSupport> all;
std::map<std::string, VoteBehavior> supported;
std::size_t upVotes = 0;
std::size_t downVotes = 0;
Expand Down Expand Up @@ -179,6 +180,13 @@ class FeatureCollections
std::string
featureToName(uint256 const& f) const;

/** All amendments that are registered within the table. */
std::map<std::string, AmendmentSupport> const&
allAmendments() const
{
return all;
}

/** Amendments that this server supports.
Whether they are enabled depends on the Rules defined in the validated
ledger */
Expand Down Expand Up @@ -251,6 +259,14 @@ FeatureCollections::registerFeature(

features.emplace_back(name, f);

auto const getAmendmentSupport = [=]() {
if (vote == VoteBehavior::Obsolete)
return AmendmentSupport::Retired;
return support == Supported::yes ? AmendmentSupport::Supported
: AmendmentSupport::Unsupported;
};
all.emplace(name, getAmendmentSupport());

if (support == Supported::yes)
{
supported.emplace(name, vote);
Expand All @@ -266,6 +282,9 @@ FeatureCollections::registerFeature(
check(
supported.size() <= features.size(),
"More supported features than defined features");
check(
features.size() == all.size(),
"The 'all' features list is populated incorrectly");
return f;
}
else
Expand Down Expand Up @@ -313,6 +332,13 @@ static FeatureCollections featureCollections;

} // namespace

/** All amendments libxrpl knows of. */
std::map<std::string, AmendmentSupport> const&
allAmendments()
{
return featureCollections.allAmendments();
}

/** Amendments that this server supports.
Whether they are enabled depends on the Rules defined in the validated
ledger */
Expand Down
83 changes: 62 additions & 21 deletions src/test/rpc/Feature_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,75 @@ class Feature_test : public beast::unit_test::suite
{
testcase("internals");

std::map<std::string, VoteBehavior> const& supported =
ripple::detail::supportedAmendments();
auto const& supportedAmendments = ripple::detail::supportedAmendments();
auto const& allAmendments = ripple::allAmendments();

BEAST_EXPECT(
supported.size() ==
supportedAmendments.size() ==
ripple::detail::numDownVotedAmendments() +
ripple::detail::numUpVotedAmendments());
std::size_t up = 0, down = 0, obsolete = 0;
for (std::pair<std::string const, VoteBehavior> const& amendment :
supported)
{
switch (amendment.second)
std::size_t up = 0, down = 0, obsolete = 0;
for (auto const& [name, vote] : supportedAmendments)
{
case VoteBehavior::DefaultYes:
++up;
break;
case VoteBehavior::DefaultNo:
++down;
break;
case VoteBehavior::Obsolete:
++obsolete;
break;
default:
fail("Unknown VoteBehavior", __FILE__, __LINE__);
switch (vote)
{
case VoteBehavior::DefaultYes:
++up;
break;
case VoteBehavior::DefaultNo:
++down;
break;
case VoteBehavior::Obsolete:
++obsolete;
break;
default:
fail("Unknown VoteBehavior", __FILE__, __LINE__);
}

if (vote == VoteBehavior::Obsolete)
{
BEAST_EXPECT(
allAmendments.contains(name) &&
allAmendments.at(name) == AmendmentSupport::Retired);
}
else
{
BEAST_EXPECT(
allAmendments.contains(name) &&
allAmendments.at(name) == AmendmentSupport::Supported);
}
}
BEAST_EXPECT(
down + obsolete == ripple::detail::numDownVotedAmendments());
BEAST_EXPECT(up == ripple::detail::numUpVotedAmendments());
}
{
std::size_t supported = 0, unsupported = 0, retired = 0;
for (auto const& [name, support] : allAmendments)
{
switch (support)
{
case AmendmentSupport::Supported:
++supported;
BEAST_EXPECT(supportedAmendments.contains(name));
break;
case AmendmentSupport::Unsupported:
++unsupported;
break;
case AmendmentSupport::Retired:
++retired;
break;
default:
fail("Unknown AmendmentSupport", __FILE__, __LINE__);
}
}

BEAST_EXPECT(supported + retired == supportedAmendments.size());
BEAST_EXPECT(
allAmendments.size() - unsupported ==
supportedAmendments.size());
}
BEAST_EXPECT(
down + obsolete == ripple::detail::numDownVotedAmendments());
BEAST_EXPECT(up == ripple::detail::numUpVotedAmendments());
}

void
Expand Down

0 comments on commit 20d0549

Please sign in to comment.