1#include <test/jtx/Env.h>
2#include <test/jtx/PeerStub.h>
3#include <test/jtx/envconfig.h>
5#include <xrpld/app/main/Application.h>
6#include <xrpld/overlay/Message.h>
7#include <xrpld/overlay/Peer.h>
8#include <xrpld/overlay/ReduceRelayCommon.h>
9#include <xrpld/overlay/Slot.h>
10#include <xrpld/overlay/Squelch.h>
11#include <xrpld/overlay/detail/Handshake.h>
13#include <xrpl/basics/base_uint.h>
14#include <xrpl/basics/random.h>
15#include <xrpl/beast/net/IPAddress.h>
16#include <xrpl/beast/unit_test/suite.h>
17#include <xrpl/beast/utility/Journal.h>
18#include <xrpl/protocol/KeyType.h>
19#include <xrpl/protocol/PublicKey.h>
20#include <xrpl/protocol/SecretKey.h>
22#include <boost/asio/ip/address.hpp>
49using namespace std::chrono;
152 protocol::MessageType type = protocol::mtVALIDATION) = 0;
182 auto const sp =
peer_.lock();
185 peer->onMessage(m, f);
200 auto const p =
peer_.lock();
207 auto p =
peer_.lock();
229 protocol::TMValidation v;
230 v.set_validation(
"validation");
282 for (
auto const id : peers)
284 assert(
links_.contains(
id));
298 for (
auto const& link : v)
337 auto const it =
links_.find(
id);
338 assert(it !=
links_.end());
339 it->second->up(
true);
345 auto const it =
links_.find(
id);
346 assert(it !=
links_.end());
347 it->second->up(
false);
439 return res ? *res : 0;
448 protocol::MessageType type = protocol::mtVALIDATION)
override
458 slots_.deletePeer(
id,
true);
492 auto const it =
peers_.find(
id);
493 assert(it !=
peers_.end());
517 for (
auto& [
id, _] :
peers_)
544 return selected.contains(peer);
551 assert(!selected.empty());
552 return *selected.begin();
573 if (
auto const it =
peers_.find(
id); it !=
peers_.end())
579 if (
auto const it =
peers_.find(
id); it !=
peers_.end())
604 auto const peer =
overlay_.addPeer();
625 auto const peer =
overlay_.addPeer();
634 auto id =
overlay_.deleteLastPeer();
691 squelch.clear_validatorpubkey();
708 for (
auto const v : s)
718 bool resetClock =
true)
729 for (
int m = 0; m < nMessages; ++m)
759 for (
auto& [_, v] : peers)
786 for (
auto& [k, v] : peers)
787 std::cout << k <<
":" << (int)std::get<reduce_relay::PeerState>(v) <<
" ";
801 bool const res =
static_cast<bool>(
duration);
806 auto const sp = peerPtr.
lock();
849 bool squelched =
false;
863 str <<
" selected: ";
864 for (
auto const s : selected)
868 std::cout << (double)reduce_relay::epoch<milliseconds>(now).count() / 1000.
869 <<
" random, squelched, validator: " <<
validator.id()
874 countingState ==
false &&
875 selected.size() ==
env_.app().config().vpReduceRelaySquelchMaxSelectedPeers);
882 auto const update = [&](
EventType event) {
884 events[event].validator =
validator.id();
886 events[event].peer = link.
peerId();
888 events[event].time = now;
892 events[event].isSelected =
900 auto const r =
randInt(0, 1000);
910 bool const allCounting =
network_.allCounting(event.peer);
913 if (event.isSelected)
914 sendSquelch(v, peerPtr, {});
915 event.handled =
true;
922 bool const handled = (!
event.isSelected && !
event.handled) ||
923 (event.isSelected && (event.handled || allCounting));
924 BEAST_EXPECT(handled);
926 event.isSelected =
false;
927 event.handledCnt += handled;
928 event.handled =
false;
929 network_.onDisconnectPeer(event.peer);
944 bool mustHandle =
false;
945 if (event.state ==
State::On && BEAST_EXPECT(event.key))
947 event.isSelected =
network_.overlay().isSelected(*event.key, event.peer);
948 auto peers =
network_.overlay().getPeers(*event.key);
950 std::get<3>(peers[event.peer]);
951 mustHandle =
event.isSelected &&
955 peers.contains(event.peer);
958 event.handled =
true;
959 if (mustHandle && v == event.key)
966 (!event.handled && !mustHandle);
967 BEAST_EXPECT(handled);
974 BEAST_EXPECT(handled);
976 event.isSelected =
false;
977 event.handledCnt += handled;
978 event.handled =
false;
979 network_.enableLink(event.validator, event.peer,
true);
986 BEAST_EXPECT(down.handledCnt >= down.cnt - 1);
988 BEAST_EXPECT(disconnected.cnt == disconnected.handledCnt);
991 std::cout <<
"link down count: " << down.cnt <<
"/" << down.handledCnt
992 <<
" peer disconnect count: " << disconnected.cnt <<
"/"
993 << disconnected.handledCnt;
1001 BEAST_EXPECT(countingState == isCountingState);
1002 return countingState == isCountingState;
1031 doTest(
"Peer Unsquelched Too Soon",
log, [
this](
bool log) {
1069 kMaxPeers -
env_.app().config().vpReduceRelaySquelchMaxSelectedPeers);
1077 auto const selected =
network_.overlay().getSelected(
network_.validator(0));
1078 BEAST_EXPECT(selected.size() ==
env_.app().config().vpReduceRelaySquelchMaxSelectedPeers);
1079 BEAST_EXPECT(n == 1);
1082 return n == 1 && res;
1094 bool resetClock =
true)
1096 bool squelched =
false;
1103 BEAST_EXPECT(
false);
1111 return !squelched && res;
1135 doTest(
"Selected Peer Disconnects",
log, [
this](
bool log) {
1144 kMaxPeers -
env_.app().config().vpReduceRelaySquelchMaxSelectedPeers);
1156 doTest(
"Selected Peer Stops Relaying",
log, [
this](
bool log) {
1161 network_.overlay().deleteIdlePeers(
1166 kMaxPeers -
env_.app().config().vpReduceRelaySquelchMaxSelectedPeers);
1177 doTest(
"Squelched Peer Disconnects",
log, [
this](
bool log) {
1182 return std::get<reduce_relay::PeerState>(it.second) ==
1185 assert(it != peers.end());
1189 BEAST_EXPECT(unsquelched == 0);
1197 doTest(
"Test Config - squelch enabled (legacy)",
log, [&](
bool log) {
1209 doTest(
"Test Config - squelch disabled (legacy)",
log, [&](
bool log) {
1222 toLoad = R
"xrpldConfig(
1230 doTest(
"Test Config - squelch enabled",
log, [&](
bool log) {
1235vp_base_squelch_enable=1
1242 doTest(
"Test Config - squelch disabled",
log, [&](
bool log) {
1247vp_base_squelch_enable=0
1254 doTest(
"Test Config - legacy and new",
log, [&](
bool log) {
1259vp_base_squelch_enable=0
1264 auto const expectedError =
1265 "Invalid reduce_relay"
1266 " cannot specify both vp_base_squelch_enable and vp_enable "
1268 "vp_enable was deprecated and replaced by "
1269 "vp_base_squelch_enable";
1280 BEAST_EXPECT(error == expectedError);
1283 doTest(
"Test Config - max selected peers",
log, [&](
bool log) {
1295 toLoad = R"xrpldConfig(
1297vp_base_squelch_max_selected_peers=6
1305 toLoad = R"xrpldConfig(
1307vp_base_squelch_max_selected_peers=2
1311 auto const expectedError =
1312 "Invalid reduce_relay"
1313 " vp_base_squelch_max_selected_peers must be "
1314 "greater than or equal to 3";
1324 BEAST_EXPECT(error == expectedError);
1329 testBaseSquelchReady(
bool log)
1331 doTest(
"BaseSquelchReady", log, [&](
bool log) {
1333 auto const createSlots =
1335 env_.app().config().vpReduceRelayBaseSquelchEnable = baseSquelchEnabled;
1340 BEAST_EXPECT(!createSlots(
false).baseSquelchReady());
1344 BEAST_EXPECT(!createSlots(
true).baseSquelchReady());
1349 BEAST_EXPECT(createSlots(
true).baseSquelchReady());
1353 BEAST_EXPECT(!createSlots(
false).baseSquelchReady());
1358 testInternalHashRouter(
bool log)
1360 doTest(
"Duplicate Message", log, [&](
bool log) {
1363 std::int16_t
const nMessages = 5;
1364 for (
int i = 0; i < nMessages; i++)
1367 network_.overlay().updateSlotAndSquelch(
1374 BEAST_EXPECT(std::get<1>(peers[0]) == (nMessages - 1));
1376 UInt256 const key(nMessages - 1);
1377 network_.overlay().updateSlotAndSquelch(
1381 BEAST_EXPECT(std::get<1>(peers[0]) == (nMessages - 1));
1384 network_.overlay().updateSlotAndSquelch(
1388 BEAST_EXPECT(std::get<1>(peers[0]) == nMessages);
1394 Handler() =
default;
1401 unsquelch(PublicKey
const&, Peer::ID)
const override
1404 mutable int maxDuration{0};
1408 testRandomSquelch(
bool l)
1410 doTest(
"Random Squelch", l, [&](
bool l) {
1414 auto const run = [&](
int npeers) {
1415 handler.maxDuration = 0;
1416 reduce_relay::Slots<ManualClock> slots(env_.app(), handler, env_.app().config());
1423 for (
int peer = 0; peer < npeers; peer++)
1427 std::uint64_t
const mid = (m * 1000) + peer;
1429 slots.updateSlotAndSquelch(
1430 message,
validator, peer, protocol::MessageType::mtVALIDATION);
1437 using namespace reduce_relay;
1442 handler.maxDuration >= kMinUnsquelchExpire.count() &&
1443 handler.maxDuration <= kMaxUnsquelchExpireDefault.count());
1446 handler.maxDuration >= kMinUnsquelchExpire.count() &&
1447 handler.maxDuration <= kMaxUnsquelchExpireDefault.count());
1456 handler.maxDuration >= kMinUnsquelchExpire.count() &&
1457 handler.maxDuration <= kMaxUnsquelchExpirePeers.count());
1459 if (handler.maxDuration <= kMaxUnsquelchExpireDefault.count())
1461 log <<
makeReason(
"warning: squelch duration is low", __FILE__, __LINE__)
1468 handler.maxDuration >= kMinUnsquelchExpire.count() &&
1469 handler.maxDuration <= kMaxUnsquelchExpirePeers.count());
1470 if (handler.maxDuration <= kMaxUnsquelchExpireDefault.count())
1472 log <<
makeReason(
"warning: squelch duration is low", __FILE__, __LINE__)
1480 testHandshake(
bool log)
1482 doTest(
"Handshake", log, [&](
bool log) {
1483 auto setEnv = [&](
bool enable) {
1485 std::stringstream str;
1486 str <<
"[reduce_relay]\n"
1487 <<
"vp_enable=" << enable <<
"\n"
1488 <<
"[compression]\n"
1490 c.loadFromString(str.
str());
1491 env_.app().config().vpReduceRelayBaseSquelchEnable =
1492 c.vpReduceRelayBaseSquelchEnable;
1494 env_.app().config().compression = c.compression;
1496 auto const handshake = [&](
int outboundEnable,
int inboundEnable) {
1499 setEnv(outboundEnable);
1502 env_.app().config().compression,
1504 env_.app().config().txReduceRelayEnable,
1505 env_.app().config().vpReduceRelayBaseSquelchEnable);
1507 httpRequest.version(request.version());
1508 httpRequest.base() = request.base();
1511 auto const peerEnabled = inboundEnable && outboundEnable;
1514 auto const inboundEnabled =
1516 BEAST_EXPECT(!(peerEnabled ^ inboundEnabled));
1518 setEnv(inboundEnable);
1520 true, httpRequest, addr, addr,
UInt256{1}, 1, {1, 0}, env_.app());
1523 auto const outboundEnabled =
1525 BEAST_EXPECT(!(peerEnabled ^ outboundEnabled));
1540 cfg->vpReduceRelayBaseSquelchEnable =
true;
1541 cfg->vpReduceRelaySquelchMaxSelectedPeers = 6;
1544 , network_(env_.app())
1551 bool const log =
false;
1553 testInitialRound(log);
1554 testPeerUnsquelchedTooSoon(log);
1555 testPeerUnsquelched(log);
1557 testSquelchedPeerDisconnects(log);
1558 testSelectedPeerDisconnects(log);
1559 testSelectedPeerStopsRelaying(log);
1560 testInternalHashRouter(log);
1561 testRandomSquelch(log);
1563 testBaseSquelchReady(log);
1578 bool const log =
false;
T back_inserter(T... args)
A generic endpoint for log messages.
LogOs< char > log
Logging output stream.
TestcaseT testcase
Memberspace for declaring test cases.
std::size_t vpReduceRelaySquelchMaxSelectedPeers
////////////////// !
bool vpReduceRelayBaseSquelchEnable
void loadFromString(std::string const &fileContents)
Load the config from the contents of the string.
std::uint32_t ID
Uniquely identifies a peer.
std::uint8_t const * data() const noexcept
static std::size_t size() noexcept
Service registry for dependency injection.
An immutable linear range of bytes.
Slot is associated with a specific validator via validator's public key.
Slots is a container for validator's Slot and handles Slot update when a message is received from a v...
Maintains squelching of relaying messages from validators.
Simulate link from a validator to a peer directly connected to the server.
std::pair< milliseconds, milliseconds > Latency
Link(Validator &validator, PeerSPtr peer, Latency latency={milliseconds(5), milliseconds(15)})
void send(MessageSPtr const &m, SquelchCB f)
static void randAdvance(milliseconds min, milliseconds max)
static duration randDuration(milliseconds min, milliseconds max)
static bool const is_steady
static void advance(duration d) noexcept
std::chrono::time_point< ManualClock > time_point
static void reset() noexcept
static time_point now() noexcept
std::chrono::duration< std::uint32_t, period > duration
void onDisconnectPeer(Peer::ID peer)
static void forRand(std::uint32_t min, std::uint32_t max, std::function< void(std::uint32_t)> f)
bool isSelected(Peer::ID id)
Is peer in Selected state in any of the slots.
void propagate(LinkIterCB link, std::uint16_t nValidators=kMaxValidators, std::uint32_t nMessages=kMaxMessages, bool purge=true, bool resetClock=true)
std::vector< Validator > validators_
Network(Application &app)
bool allCounting(Peer::ID peer)
Check if there are peers to unsquelch - peer is in Selected state in any of the slots and there are p...
void enableLink(std::uint16_t validatorId, Peer::ID peer, bool enable)
Validator & validator(std::uint16_t v)
std::set< ID > getSelected(PublicKey const &validator)
std::uint16_t getNumPeers() const
void updateSlotAndSquelch(UInt256 const &key, PublicKey const &validator, Peer::ID id, SquelchCB f, protocol::MessageType type=protocol::mtVALIDATION) override
PeerSPtr addPeer(bool useCache=true)
bool isSelected(PublicKey const &validator, Peer::ID peer)
ID getSelectedPeer(PublicKey const &validator)
ServiceRegistry & registry_
std::unordered_map< ID, std::tuple< reduce_relay::PeerState, std::uint16_t, std::uint32_t, std::uint32_t > > getPeers(PublicKey const &validator)
std::optional< Peer::ID > deleteLastPeer()
std::unordered_map< Peer::ID, PeerSPtr > Peers
void deleteIdlePeers(UnsquelchCB f) override
std::uint16_t inState(PublicKey const &validator, reduce_relay::PeerState state)
OverlaySim(Application &app)
bool isCountingState(PublicKey const &validator)
~OverlaySim() override=default
void deletePeer(ID id, UnsquelchCB f) override
reduce_relay::Slots< ManualClock > slots_
void deletePeer(Peer::ID id, bool useCache=true)
void unsquelch(PublicKey const &validator, Peer::ID id) const override
Unsquelch handler.
void squelch(PublicKey const &validator, Peer::ID id, std::uint32_t squelchDuration) const override
Squelch handler.
Simulate server's OverlayImpl.
virtual void deleteIdlePeers(UnsquelchCB)=0
virtual void updateSlotAndSquelch(UInt256 const &key, PublicKey const &validator, Peer::ID id, SquelchCB f, protocol::MessageType type=protocol::mtVALIDATION)=0
virtual ~Overlay()=default
virtual void deletePeer(Peer::ID, UnsquelchCB)=0
Simulate two entities - peer directly connected to the server (via squelch in PeerSim) and PeerImp (v...
virtual void onMessage(protocol::TMSquelch const &squelch)=0
virtual void onMessage(MessageSPtr const &m, SquelchCB f)=0
void send(protocol::TMSquelch const &squelch)
~PeerSim() override=default
void onMessage(protocol::TMSquelch const &squelch) override
Remote Peer (Directly connected Peer).
reduce_relay::Squelch< ManualClock > squelch_
void onMessage(MessageSPtr const &m, SquelchCB f) override
Local Peer (PeerImp).
PeerSim(Overlay &overlay, beast::Journal journal)
void send(std::shared_ptr< Message > const &) override
void addPeer(PeerSPtr peer)
Validator & operator=(Validator &&)=default
void send(SquelchCB f)
Send to all peers.
void forLinks(LinkIterCB f, bool simulateSlow=false)
Validator(Validator const &)=default
Validator & operator=(Validator const &)=default
void send(std::vector< Peer::ID > peers, SquelchCB f)
Send to specific peers.
void deletePeer(Peer::ID id)
std::unordered_map< Peer::ID, LinkSPtr > Links
Validator(Validator &&)=default
void linkDown(Peer::ID id)
void forLinks(std::vector< Peer::ID > peers, LinkIterCB f)
void testRandom(bool log)
void run() override
Runs the suite.
void printPeers(std::string const &msg, std::uint16_t validator=0)
void doTest(std::string const &msg, bool log, std::function< void(bool)> f)
bool propagateNoSquelch(bool log, std::uint16_t nMessages, bool countingState, bool purge=true, bool resetClock=true)
Send fewer message so that squelch event is not generated.
bool propagateAndSquelch(bool log, bool purge=true, bool resetClock=true)
Propagate enough messages to generate one squelch event.
static Peer::ID sendSquelch(PublicKey const &validator, PeerWPtr const &peerPtr, std::optional< std::uint32_t > duration)
Send squelch (if duration is set) or unsquelch (if duration not set).
void testConfig(bool log)
void random(bool log)
Randomly brings the link between a validator and a peer down.
void testSquelchedPeerDisconnects(bool log)
Squelched peer disconnects.
void testPeerUnsquelchedTooSoon(bool log)
Receiving message from squelched peer too soon should not change the slot's state to Counting.
reduce_relay::Slot< ManualClock > Slot
void testInitialRound(bool log)
Initial counting round: three peers receive message "faster" then others.
void testSelectedPeerStopsRelaying(bool log)
Selected peer stops relaying.
void testSelectedPeerDisconnects(bool log)
Selected peer disconnects.
bool checkCounting(PublicKey const &validator, bool isCountingState)
void testPeerUnsquelched(bool log)
Receiving message from squelched peer should change the slot's state to Counting.
void testNewPeer(bool log)
Receiving a message from new peer should change the slot's state to Counting.
boost::asio::ip::address Address
std::string makeReason(String const &reason, char const *file, int line)
static constexpr uint16_t kMaxMessageThreshold
static constexpr auto kWaitOnBootup
static constexpr auto kIdled
std::unique_ptr< Config > envconfig()
creates and initializes a default configuration for jtx::Env
std::unique_ptr< Config > validator(std::unique_ptr< Config >, std::string const &)
adjust configuration with params needed to be a validator
static constexpr std::uint32_t kMaxMessages
std::function< void(Link &, MessageSPtr)> LinkIterCB
BEAST_DEFINE_TESTSUITE(AMMClawback, app, xrpl)
std::shared_ptr< Peer > PeerSPtr
static constexpr std::uint32_t kMaxPeers
BEAST_DEFINE_TESTSUITE_MANUAL(AMMCalc, app, xrpl)
std::shared_ptr< Message > MessageSPtr
static constexpr std::uint32_t kMaxValidators
std::shared_ptr< Link > LinkSPtr
std::weak_ptr< Peer > PeerWPtr
std::function< void(PublicKey const &, PeerWPtr const &)> UnsquelchCB
std::function< void(PublicKey const &, PeerWPtr const &, std::uint32_t)> SquelchCB
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
HttpResponseType makeResponse(bool crawlPublic, HttpRequestType const &req, beast::ip::Address publicIp, beast::ip::Address remoteIp, UInt256 const &sharedValue, std::optional< std::uint32_t > networkID, ProtocolVersion protocol, Application &app)
Make http response.
auto makeRequest(bool crawlPublic, bool comprEnabled, bool ledgerReplayEnabled, bool txReduceRelayEnabled, bool vpReduceRelayEnabled) -> RequestType
Make outbound http request.
boost::beast::http::request< boost::beast::http::dynamic_body > HttpRequestType
int run(int argc, char *const *argv)
bool peerFeatureEnabled(Headers const &request, std::string const &feature, std::string value, bool config)
Check if a feature should be enabled for a peer.
constexpr Number squelch(Number const &x, Number const &limit) noexcept
static constexpr char kFeatureVprr[]
T dynamic_pointer_cast(T... args)
time_point< ManualClock > time
std::optional< PublicKey > key