mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-27 09:00:32 +00:00
Support ipv6 for peer and RPC comms:
Fixes: RIPD-1574 Alias beast address classes to the asio equivalents. Adjust users of address classes accordingly. Fix resolver class so that it can support ipv6 addresses. Make unit tests use ipv6 localhost network. Extend endpoint peer message to support string endpoint representations while also supporting the existing fields (both are optional/repeated types). Expand test for Livecache and Endpoint. Workaround some false positive ipaddr tests on windows (asio bug?) Replaced usage of address::from_string(deprecated) with free function make_address. Identified a remaining use of v4 address type and replaced with the more appropriate IPEndpoint type (rpc_ip cmdline option). Add CLI flag for using ipv4 with unit tests. Release Notes ------------- The optional rpc_port command line flag is deprecated. The rpc_ip parameter now works as documented and accepts ip and port combined.
This commit is contained in:
@@ -21,10 +21,18 @@
|
||||
#include <ripple/peerfinder/impl/Livecache.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <ripple/beast/clock/manual_clock.h>
|
||||
#include <test/beast/IPEndpointCommon.h>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
namespace ripple {
|
||||
namespace PeerFinder {
|
||||
|
||||
bool operator== (Endpoint const& a, Endpoint const& b)
|
||||
{
|
||||
return (a.hops == b.hops &&
|
||||
a.address == b.address);
|
||||
}
|
||||
|
||||
class Livecache_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
@@ -32,38 +40,178 @@ public:
|
||||
|
||||
// Add the address as an endpoint
|
||||
template <class C>
|
||||
void add (std::uint32_t index, std::uint16_t port, C& c)
|
||||
inline void add (beast::IP::Endpoint ep, C& c, int hops = 0)
|
||||
{
|
||||
Endpoint ep;
|
||||
ep.hops = 0;
|
||||
ep.address = beast::IP::Endpoint (
|
||||
beast::IP::AddressV4 (index), port);
|
||||
c.insert (ep);
|
||||
Endpoint cep {ep, hops};
|
||||
c.insert (cep);
|
||||
}
|
||||
|
||||
void testFetch ()
|
||||
void testBasicInsert ()
|
||||
{
|
||||
testcase ("Basic Insert");
|
||||
Livecache <> c (m_clock, beast::Journal());
|
||||
BEAST_EXPECT(c.empty());
|
||||
|
||||
for (auto i = 0; i < 10; ++i)
|
||||
add(beast::IP::randomEP(true), c);
|
||||
|
||||
BEAST_EXPECT(! c.empty());
|
||||
BEAST_EXPECT(c.size() == 10);
|
||||
|
||||
for (auto i = 0; i < 10; ++i)
|
||||
add(beast::IP::randomEP(false), c);
|
||||
|
||||
BEAST_EXPECT(! c.empty());
|
||||
BEAST_EXPECT(c.size() == 20);
|
||||
}
|
||||
|
||||
void testInsertUpdate ()
|
||||
{
|
||||
testcase ("Insert/Update");
|
||||
Livecache <> c (m_clock, beast::Journal());
|
||||
|
||||
add (1, 1, c);
|
||||
add (2, 1, c);
|
||||
add (3, 1, c);
|
||||
add (4, 1, c);
|
||||
add (4, 2, c);
|
||||
add (4, 3, c);
|
||||
add (5, 1, c);
|
||||
add (6, 1, c);
|
||||
add (6, 2, c);
|
||||
add (7, 1, c);
|
||||
auto ep1 = Endpoint {beast::IP::randomEP(), 2};
|
||||
c.insert(ep1);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// third position list will contain the entry
|
||||
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
|
||||
|
||||
// VFALCO TODO!
|
||||
auto ep2 = Endpoint {ep1.address, 4};
|
||||
// this will not change the entry has higher hops
|
||||
c.insert(ep2);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// still in third position list
|
||||
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
|
||||
|
||||
pass();
|
||||
auto ep3 = Endpoint {ep1.address, 2};
|
||||
// this will not change the entry has the same hops as existing
|
||||
c.insert(ep3);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// still in third position list
|
||||
BEAST_EXPECT((c.hops.begin()+2)->begin()->hops == 2);
|
||||
|
||||
auto ep4 = Endpoint {ep1.address, 1};
|
||||
c.insert(ep4);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// now at second position list
|
||||
BEAST_EXPECT((c.hops.begin()+1)->begin()->hops == 1);
|
||||
}
|
||||
|
||||
void testExpire ()
|
||||
{
|
||||
testcase ("Expire");
|
||||
using namespace std::chrono_literals;
|
||||
Livecache <> c (m_clock, beast::Journal());
|
||||
|
||||
auto ep1 = Endpoint {beast::IP::randomEP(), 1};
|
||||
c.insert(ep1);
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
c.expire();
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// verify that advancing to 1 sec before expiration
|
||||
// leaves our entry intact
|
||||
m_clock.advance(Tuning::liveCacheSecondsToLive - 1s);
|
||||
c.expire();
|
||||
BEAST_EXPECT(c.size() == 1);
|
||||
// now advance to the point of expiration
|
||||
m_clock.advance(1s);
|
||||
c.expire();
|
||||
BEAST_EXPECT(c.empty());
|
||||
}
|
||||
|
||||
void testHistogram ()
|
||||
{
|
||||
testcase ("Histogram");
|
||||
constexpr auto num_eps = 40;
|
||||
Livecache <> c (m_clock, beast::Journal());
|
||||
for (auto i = 0; i < num_eps; ++i)
|
||||
add(
|
||||
beast::IP::randomEP(true),
|
||||
c,
|
||||
ripple::rand_int(0, static_cast<int>(Tuning::maxHops + 1)));
|
||||
auto h = c.hops.histogram();
|
||||
if(! BEAST_EXPECT(! h.empty()))
|
||||
return;
|
||||
std::vector <std::string> v;
|
||||
boost::split (v, h, boost::algorithm::is_any_of (","));
|
||||
auto sum = 0;
|
||||
for (auto const& n : v)
|
||||
{
|
||||
auto val = boost::lexical_cast<int>(boost::trim_copy(n));
|
||||
sum += val;
|
||||
BEAST_EXPECT(val >= 0);
|
||||
}
|
||||
BEAST_EXPECT(sum == num_eps);
|
||||
}
|
||||
|
||||
|
||||
void testShuffle ()
|
||||
{
|
||||
testcase ("Shuffle");
|
||||
Livecache <> c (m_clock, beast::Journal());
|
||||
for (auto i = 0; i < 100; ++i)
|
||||
add(
|
||||
beast::IP::randomEP(true),
|
||||
c,
|
||||
ripple::rand_int(0, static_cast<int>(Tuning::maxHops + 1)));
|
||||
|
||||
using at_hop = std::vector <ripple::PeerFinder::Endpoint>;
|
||||
using all_hops = std::array <at_hop, 1 + Tuning::maxHops + 1>;
|
||||
|
||||
auto cmp_EP = [](Endpoint const& a, Endpoint const& b) {
|
||||
return (b.hops < a.hops || (b.hops == a.hops && b.address < a.address));
|
||||
};
|
||||
all_hops before;
|
||||
all_hops before_sorted;
|
||||
for (auto i = std::make_pair(0, c.hops.begin());
|
||||
i.second != c.hops.end(); ++i.first, ++i.second)
|
||||
{
|
||||
std::copy ((*i.second).begin(), (*i.second).end(),
|
||||
std::back_inserter (before[i.first]));
|
||||
std::copy ((*i.second).begin(), (*i.second).end(),
|
||||
std::back_inserter (before_sorted[i.first]));
|
||||
std::sort(
|
||||
before_sorted[i.first].begin(),
|
||||
before_sorted[i.first].end(),
|
||||
cmp_EP);
|
||||
}
|
||||
|
||||
c.hops.shuffle();
|
||||
|
||||
all_hops after;
|
||||
all_hops after_sorted;
|
||||
for (auto i = std::make_pair(0, c.hops.begin());
|
||||
i.second != c.hops.end(); ++i.first, ++i.second)
|
||||
{
|
||||
std::copy ((*i.second).begin(), (*i.second).end(),
|
||||
std::back_inserter (after[i.first]));
|
||||
std::copy ((*i.second).begin(), (*i.second).end(),
|
||||
std::back_inserter (after_sorted[i.first]));
|
||||
std::sort(
|
||||
after_sorted[i.first].begin(),
|
||||
after_sorted[i.first].end(),
|
||||
cmp_EP);
|
||||
}
|
||||
|
||||
// each hop bucket should contain the same items
|
||||
// before and after sort, albeit in different order
|
||||
bool all_match = true;
|
||||
for (auto i = 0; i < before.size(); ++i)
|
||||
{
|
||||
BEAST_EXPECT(before[i].size() == after[i].size());
|
||||
all_match = all_match && (before[i] == after[i]);
|
||||
BEAST_EXPECT(before_sorted[i] == after_sorted[i]);
|
||||
}
|
||||
BEAST_EXPECT(! all_match);
|
||||
}
|
||||
|
||||
void run () override
|
||||
{
|
||||
testFetch ();
|
||||
testBasicInsert ();
|
||||
testInsertUpdate ();
|
||||
testExpire ();
|
||||
testHistogram ();
|
||||
testShuffle ();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user