xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
overlay
traffic_attribution_test.cpp
1
#include <test/jtx/Env.h>
2
#include <test/jtx/envconfig.h>
3
#include <test/overlay/CapturePeer.h>
4
5
#include <xrpld/overlay/Cluster.h>
6
#include <xrpld/overlay/detail/TrafficCount.h>
7
8
#include <xrpl/beast/unit_test/suite.h>
9
#include <xrpl/json/JsonPropertyStream.h>
10
#include <xrpl/protocol/KeyType.h>
11
#include <xrpl/protocol/PublicKey.h>
12
#include <xrpl/protocol/SecretKey.h>
13
14
#include <xrpl.pb.h>
15
16
#include <
cstddef
>
17
#include <
cstdint
>
18
#include <
memory
>
19
#include <
string
>
20
21
namespace
xrpl::test
{
22
23
using namespace
jtx
;
24
34
class
traffic_attribution_test
:
public
beast::unit_test::Suite
35
{
36
// The assertions compare deltas, so any size works. A size other than 1
37
// keeps a byte count from passing as a message count.
38
static
constexpr
std::size_t
kFrameSize
= 37;
39
40
struct
Counts
41
{
42
std::uint64_t
clusterMessages
{0};
43
std::uint64_t
clusterBytes
{0};
44
std::uint64_t
unknownMessages
{0};
45
std::uint64_t
unknownBytes
{0};
46
};
47
56
static
Counts
57
read
(
Env
& env)
58
{
59
JsonPropertyStream
stream;
60
env.
app
().
getOverlay
().
writeOne
(stream);
61
62
// `TrafficCount::toString` names the categories, so a renamed counter
63
// cannot leave this lookup silently matching nothing.
64
auto
const
clusterName =
TrafficCount::toString
(
TrafficCount::Category::Cluster
);
65
auto
const
unknownName =
TrafficCount::toString
(
TrafficCount::Category::Unknown
);
66
67
Counts
counts;
68
for
(
auto
const
& entry : stream.top()[
"peers"
][
"traffic"
])
69
{
70
auto
const
name = entry[
"category"
].
asString
();
71
auto
const
messages =
std::stoull
(entry[
"messages_in"
].asString());
72
auto
const
bytes =
std::stoull
(entry[
"bytes_in"
].asString());
73
74
if
(name == clusterName)
75
{
76
counts.
clusterMessages
= messages;
77
counts.
clusterBytes
= bytes;
78
}
79
else
if
(name == unknownName)
80
{
81
counts.
unknownMessages
= messages;
82
counts.
unknownBytes
= bytes;
83
}
84
}
85
return
counts;
86
}
87
88
void
89
testInboundCluster
()
90
{
91
testcase
(
"inbound cluster attribution"
);
92
93
Env
env{*
this
,
envconfig
()};
94
95
// The outsider takes a fresh random key, which no `[cluster_nodes]`
96
// entry names. `cluster()` reads the cluster map on every message, so
97
// the member's key can be registered either side of its peer.
98
auto
const
outsider =
makeCapturePeer
(env);
99
PublicKey
const
memberKey =
randomKeyPair
(
KeyType::Ed25519
).first;
100
BEAST_EXPECT(env.
app
().
getCluster
().
update
(memberKey,
"test-member"
));
101
auto
const
member =
makeCapturePeer
(env, memberKey);
102
103
auto
const
message =
std::make_shared<protocol::TMCluster>
();
104
auto
const
before =
read
(env);
105
106
// Any peer can send this type, so a sender outside the cluster is
107
// counted as unknown, leaving the cluster counter to mean traffic
108
// between configured members.
109
outsider->onMessageBegin(protocol::mtCLUSTER, message,
kFrameSize
,
kFrameSize
,
false
);
110
auto
const
afterOutsider =
read
(env);
111
BEAST_EXPECT(afterOutsider.unknownMessages == before.unknownMessages + 1);
112
BEAST_EXPECT(afterOutsider.unknownBytes == before.unknownBytes +
kFrameSize
);
113
BEAST_EXPECT(afterOutsider.clusterMessages == before.clusterMessages);
114
BEAST_EXPECT(afterOutsider.clusterBytes == before.clusterBytes);
115
116
// The same frame from a configured member is counted as cluster
117
// traffic.
118
member->onMessageBegin(protocol::mtCLUSTER, message,
kFrameSize
,
kFrameSize
,
false
);
119
auto
const
afterMember =
read
(env);
120
BEAST_EXPECT(afterMember.clusterMessages == afterOutsider.clusterMessages + 1);
121
BEAST_EXPECT(afterMember.clusterBytes == afterOutsider.clusterBytes +
kFrameSize
);
122
BEAST_EXPECT(afterMember.unknownMessages == afterOutsider.unknownMessages);
123
BEAST_EXPECT(afterMember.unknownBytes == afterOutsider.unknownBytes);
124
}
125
126
public
:
127
void
128
run
()
override
129
{
130
testInboundCluster
();
131
}
132
};
133
134
BEAST_DEFINE_TESTSUITE
(traffic_attribution, overlay,
xrpl
);
135
136
}
// namespace xrpl::test
beast::PropertyStream::Source::writeOne
void writeOne(PropertyStream &stream)
Write only this Source to the stream.
Definition
beast_PropertyStream.cpp:209
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::testcase
TestcaseT testcase
Memberspace for declaring test cases.
Definition
suite.h:155
json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition
json_value.cpp:494
xrpl::Cluster::update
bool update(PublicKey const &identity, std::string name, std::uint32_t loadFee=0, NetClock::time_point reportTime=NetClock::time_point{})
Store information about the state of a cluster node.
Definition
Cluster.cpp:58
xrpl::JsonPropertyStream
A PropertyStream::Sink which produces a json::Value of type ValueType::Object.
Definition
JsonPropertyStream.h:15
xrpl::PublicKey
A public key.
Definition
PublicKey.h:53
xrpl::ServiceRegistry::getCluster
virtual Cluster & getCluster()=0
xrpl::ServiceRegistry::getOverlay
virtual Overlay & getOverlay()=0
xrpl::TrafficCount::Category::Cluster
@ Cluster
Definition
TrafficCount.h:75
xrpl::TrafficCount::Category::Unknown
@ Unknown
Definition
TrafficCount.h:184
xrpl::TrafficCount::toString
static std::string toString(Category cat)
Definition
TrafficCount.h:259
xrpl::test::jtx::Env
A transaction testing environment.
Definition
Env.h:161
xrpl::test::jtx::Env::app
Application & app()
Definition
Env.h:300
xrpl::test::traffic_attribution_test
Covers the traffic attribution in PeerImp::onMessageBegin.
Definition
traffic_attribution_test.cpp:35
xrpl::test::traffic_attribution_test::read
static Counts read(Env &env)
Reads the cluster and unknown counters the way an operator reads them, through the overlay's property...
Definition
traffic_attribution_test.cpp:57
xrpl::test::traffic_attribution_test::run
void run() override
Runs the suite.
Definition
traffic_attribution_test.cpp:128
xrpl::test::traffic_attribution_test::testInboundCluster
void testInboundCluster()
Definition
traffic_attribution_test.cpp:89
xrpl::test::traffic_attribution_test::kFrameSize
static constexpr std::size_t kFrameSize
Definition
traffic_attribution_test.cpp:38
cstddef
cstdint
std::uint64_t
std::make_shared
T make_shared(T... args)
memory
xrpl::test::jtx
Definition
Oracle_test.cpp:34
xrpl::test::jtx::envconfig
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
Definition
envconfig.h:38
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
xrpl::test::makeCapturePeer
std::shared_ptr< PeerType > makeCapturePeer(jtx::Env &env, std::optional< PublicKey > key=std::nullopt, HttpRequestType request={})
Build an active CapturePeer and register it with the overlay.
Definition
CapturePeer.h:209
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::KeyType::Ed25519
@ Ed25519
Definition
KeyType.h:10
xrpl::randomKeyPair
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
Definition
SecretKey.cpp:355
std::size_t
std::stoull
T stoull(T... args)
string
xrpl::test::traffic_attribution_test::Counts
Definition
traffic_attribution_test.cpp:41
xrpl::test::traffic_attribution_test::Counts::clusterMessages
std::uint64_t clusterMessages
Definition
traffic_attribution_test.cpp:42
xrpl::test::traffic_attribution_test::Counts::clusterBytes
std::uint64_t clusterBytes
Definition
traffic_attribution_test.cpp:43
xrpl::test::traffic_attribution_test::Counts::unknownBytes
std::uint64_t unknownBytes
Definition
traffic_attribution_test.cpp:45
xrpl::test::traffic_attribution_test::Counts::unknownMessages
std::uint64_t unknownMessages
Definition
traffic_attribution_test.cpp:44
Generated by
1.17.0