10000 Instrument proposal, validation and transaction messages by Tapanito · Pull Request #5348 · XRPLF/rippled · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

Instrument proposal, validation and transaction messages #5348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions src/test/overlay/traffic_count_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2025 Ripple Labs Inc.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <xrpld/overlay/Message.h>
#include <xrpld/overlay/detail/TrafficCount.h>

#include <xrpl/beast/unit_test.h>
#include <xrpl/protocol/messages.h>

namespace ripple {

namespace test {

class traffic_count_test : public beast::unit_test::suite
{
public:
traffic_count_test() = default;

void
testCategorize()
{
testcase("categorize");
protocol::TMPing message;
message.set_type(protocol::TMPing::ptPING);

// a known message is categorized to a proper category
auto const known =
TrafficCount::categorize(message, protocol::mtPING, false);
BEAST_EXPECT(known == TrafficCount::category::base);

// an unknown message type is categorized as unknown
auto const unknown = TrafficCount::categorize(
message, static_cast<protocol::MessageType>(99), false);
BEAST_EXPECT(unknown == TrafficCount::category::unknown);
}

struct TestCase
{
std::string name;
int size;
bool inbound;
int messageCount;
std::uint64_t expectedBytesIn;
std::uint64_t expectedBytesOut;
std::uint64_t expectedMessagesIn;
std::uint64_t expectedMessagesOut;
};

void
testAddCount()
{
auto run = [&](TestCase const& tc) {
testcase(tc.name);
TrafficCount m_traffic;

auto const counts = m_traffic.getCounts();
std::for_each(counts.begin(), counts.end(), [&](auto const& pair) {
for (auto i = 0; i < tc.messageCount; ++i)
m_traffic.addCount(pair.first, tc.inbound, tc.size);
});

auto const counts_new = m_traffic.getCounts();
std::for_each(
counts_new.begin(), counts_new.end(), [&](auto const& pair) {
BEAST_EXPECT(
pair.second.bytesIn.load() == tc.expectedBytesIn);
BEAST_EXPECT(
pair.second.bytesOut.load() == tc.expectedBytesOut);
BEAST_EXPECT(
pair.second.messagesIn.load() == tc.expectedMessagesIn);
BEAST_EXPECT(
pair.second.messagesOut.load() ==
tc.expectedMessagesOut);
});
};

auto const testcases = {
TestCase{
.name = "zero-counts",
.size = 0,
.inbound = false,
.messageCount = 0,
.expectedBytesIn = 0,
.expectedBytesOut = 0,
.expectedMessagesIn = 0,
.expectedMessagesOut = 0,
},
TestCase{
.name = "inbound-counts",
.size = 10,
.inbound = true,
.messageCount = 10,
.expectedBytesIn = 100,
.expectedBytesOut = 0,
.expectedMessagesIn = 10,
.expectedMessagesOut = 0,
},
TestCase{
.name = "outbound-counts",
.size = 10,
.inbound = false,
.messageCount = 10,
.expectedBytesIn = 0,
.expectedBytesOut = 100,
.expectedMessagesIn = 0,
.expectedMessagesOut = 10,
},
};

for (auto const& tc : testcases)
run(tc);
}

void
testToString()
{
testcase("category-to-string");

// known category returns known string value
BEAST_EXPECT(
TrafficCount::to_string(TrafficCount::category::total) == "total");

// return "unknown" for unknown categories
BEAST_EXPECT(
TrafficCount::to_string(
static_cast<TrafficCount::category>(1000)) == "unknown");
}

void
run() override
{
testCategorize();
testAddCount();
testToString();
}
};

BEAST_DEFINE_TESTSUITE(traffic_count, overlay, ripple);

} // namespace test
} // namespace ripple
2 changes: 1 addition & 1 deletion src/xrpld/overlay/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Message : public std::enable_shared_from_this<Message>
*/
Message(
::google::protobuf::Message const& message,
int type,
protocol::MessageType type,
std::optional<PublicKey> const& validator = {});

/** Retrieve the size of the packed but uncompressed message data. */
Expand Down
2 changes: 1 addition & 1 deletion src/xrpld/overlay/detail/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace ripple {

Message::Message(
::google::protobuf::Message const& message,
int type,
protocol::MessageType type,
std::optional<PublicKey> const& validator)
: category_(TrafficCount::categorize(message, type, false))
, validatorKey_(validator)
Expand Down
41 changes: 20 additions & 21 deletions src/xrpld/overlay/detail/OverlayImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#include <boost/algorithm/string/predicate.hpp>

#include "xrpld/overlay/detail/TrafficCount.h"

namespace ripple {

namespace CrawlOptions {
Expand Down Expand Up @@ -145,13 +147,11 @@
std::bind(&OverlayImpl::collect_metrics, this),
collector,
[counts = m_traffic.getCounts(), collector]() {
std::vector<TrafficGauges> ret;
ret.reserve(counts.size());
std::unordered_map<TrafficCount::category, TrafficGauges> ret;

for (size_t i = 0; i < counts.size(); ++i)
{
ret.push_back(TrafficGauges(counts[i].name, collector));
}
for (auto const& pair : counts)
ret.emplace(
pair.first, TrafficGauges(pair.second.name, collector));

return ret;
}())
Expand Down Expand Up @@ -580,17 +580,14 @@
{
beast::PropertyStream::Set set("traffic", stream);
auto const stats = m_traffic.getCounts();
for (auto const& i : stats)
for (auto const& pair : stats)
{
if (i)
{
beast::PropertyStream::Map item(set);
item["category"] = i.name;
item["bytes_in"] = std::to_string(i.bytesIn.load());
item["messages_in"] = std::to_string(i.messagesIn.load());
item["bytes_out"] = std::to_string(i.bytesOut.load());
item["messages_out"] = std::to_string(i.messagesOut.load());
}
beast::PropertyStream::Map item(set);
item["category"] = pair.second.name;
item["bytes_in"] = std::to_string(pair.second.bytesIn.load());
item["messages_in"] = std::to_string(pair.second.messagesIn.load());
item["bytes_out"] = std::to_string(pair.second.bytesOut.load());
item["messages_out"] = std::to_string(pair.second.messagesOut.load());
}
}

Expand Down Expand Up @@ -690,14 +687,16 @@
}

void
OverlayImpl::reportTraffic(
TrafficCount::category cat,
bool isInbound,
int number)
OverlayImpl::reportInboundTraffic(TrafficCount::category cat, int size)

Check warning on line 690 in src/xrpld/overlay/detail/OverlayImpl.cpp

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.cpp#L690

Added line #L690 was not covered by tests
{
m_traffic.addCount(cat, isInbound, number);
m_traffic.addCount(cat, true, size);

Check warning on line 692 in src/xrpld/overlay/detail/OverlayImpl.cpp

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.cpp#L692

Added line #L692 was not covered by tests
}

void
OverlayImpl::reportOutboundTraffic(TrafficCount::category cat, int size)

Check warning on line 696 in src/xrpld/overlay/detail/OverlayImpl.cpp

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.cpp#L696

Added line #L696 was not covered by tests
{
m_traffic.addCount(cat, false, size);

Check warning on line 698 in src/xrpld/overlay/detail/OverlayImpl.cpp

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.cpp#L698

Added line #L698 was not covered by tests
}
/** The number of active peers on the network
Active peers are only those peers that have completed the handshake
and are running the Ripple protocol.
Expand Down
38 changes: 28 additions & 10 deletions src/xrpld/overlay/detail/OverlayImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@
makePrefix(std::uint32_t id);

void
reportTraffic(TrafficCount::category cat, bool isInbound, int bytes);
reportInboundTraffic(TrafficCount::category cat, int bytes);

void
reportOutboundTraffic(TrafficCount::category cat, int bytes);

void
incJqTransOverflow() override
Expand Down Expand Up @@ -561,14 +564,16 @@
struct TrafficGauges
{
TrafficGauges(
char const* name,
std::string const& name,
beast::insight::Collector::ptr const& collector)
: bytesIn(collector->make_gauge(name, "Bytes_In"))
: name(name)
, bytesIn(collector->make_gauge(name, "Bytes_In"))
, bytesOut(collector->make_gauge(name, "Bytes_Out"))
, messagesIn(collector->make_gauge(name, "Messages_In"))
, messagesOut(collector->make_gauge(name, "Messages_Out"))
{
}
std::string const name;
beast::insight::Gauge bytesIn;
beast::insight::Gauge bytesOut;
beast::insight::Gauge messagesIn;
Expand All @@ -581,7 +586,8 @@
Stats(
Handler const& handler,
beast::insight::Collector::ptr const& collector,
std::vector<TrafficGauges>&& trafficGauges_)
std::unordered_map<TrafficCount::category, TrafficGauges>&&
trafficGauges_)
: peerDisconnects(
collector->make_gauge("Overlay", "Peer_Disconnects"))
, trafficGauges(std::move(trafficGauges_))
Expand All @@ -590,7 +596,7 @@
}

beast::insight::Gauge peerDisconnects;
std::vector<TrafficGauges> trafficGauges;
std::unordered_map<TrafficCount::category, TrafficGauges> trafficGauges;
beast::insight::Hook hook;
};

Expand All @@ -607,13 +613,25 @@
counts.size() == m_stats.trafficGauges.size(),
"ripple::OverlayImpl::collect_metrics : counts size do match");

for (std::size_t i = 0; i < counts.size(); ++i)
for (auto const& [key, value] : counts)
{
m_stats.trafficGauges[i].bytesIn = counts[i].bytesIn;
m_stats.trafficGauges[i].bytesOut = counts[i].bytesOut;
m_stats.trafficGauges[i].messagesIn = counts[i].messagesIn;
m_stats.trafficGauges[i].messagesOut = counts[i].messagesOut;
auto it = m_stats.trafficGauges.find(key);
if (it == m_stats.trafficGauges.end())
continue;

Check warning on line 620 in src/xrpld/overlay/detail/OverlayImpl.h

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.h#L620

Added line #L620 was not covered by tests

auto& gauge = it->second;

Check warning on line 622 in src/xrpld/overlay/detail/OverlayImpl.h

View check run for this annotation

Codecov / codecov/patch

src/xrpld/overlay/detail/OverlayImpl.h#L622

Added line #L622 was not covered by tests

XRPL_ASSERT(
gauge.name == value.name,
"ripple::OverlayImpl::collect_metrics : gauge and counter "
"match");

gauge.bytesIn = value.bytesIn;
gauge.bytesOut = value.bytesOut;
gauge.messagesIn = value.messagesIn;
gauge.messagesOut = value.messagesOut;
}

m_stats.peerDisconnects = getPeerDisconnect();
}
};
Expand Down
Loading
Loading
0