Merge pull request #411 from msgmaxim/fix_ip_update

Fix not updating ip addresses of snodes
This commit is contained in:
Maxim Shishmarev 2021-03-23 11:20:12 +11:00 committed by GitHub
commit 9e040e3cd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 5 deletions

View file

@ -146,8 +146,8 @@ get_snode_map_from_swarms(const all_swarms_t& swarms) {
return snode_map;
}
static all_swarms_t apply_ips(const all_swarms_t& swarms_to_keep,
const all_swarms_t& other_swarms) {
auto apply_ips(const all_swarms_t& swarms_to_keep,
const all_swarms_t& other_swarms) -> all_swarms_t {
all_swarms_t result_swarms = swarms_to_keep;
const auto other_snode_map = get_snode_map_from_swarms(other_swarms);
@ -158,10 +158,10 @@ static all_swarms_t apply_ips(const all_swarms_t& swarms_to_keep,
const auto other_snode_it =
other_snode_map.find(snode.sn_address());
if (other_snode_it != other_snode_map.end()) {
const auto& other_snode = other_snode_it->second;
const auto& new_ip = other_snode_it->second.ip();
// Keep swarms_to_keep but don't overwrite with default IPs
if (snode.ip() == "0.0.0.0") {
snode.set_ip(other_snode.ip());
if (new_ip != "0.0.0.0" && snode.ip() != new_ip) {
snode.set_ip(new_ip);
updates_count++;
}
}

View file

@ -39,6 +39,12 @@ void debug_print(std::ostream& os, const block_update_t& bu);
swarm_id_t get_swarm_by_pk(const std::vector<SwarmInfo>& all_swarms,
const user_pubkey_t& pk);
/// For every node in `swarms_to_keep`, this checks whether the node
/// exists in incoming `other_swarms` and has a new IP address.
/// If it does and the value is not "0.0.0.0", it updates the value for that node.
auto apply_ips(const all_swarms_t& swarms_to_keep,
const all_swarms_t& other_swarms) -> all_swarms_t;
struct SwarmEvents {
/// our (potentially new) swarm id

View file

@ -8,6 +8,7 @@ add_executable (Test
signature.cpp
rate_limiter.cpp
command_line.cpp
service_node.cpp
)
target_link_libraries(Test PRIVATE common storage pow utils crypto httpserver_lib)

View file

@ -0,0 +1,65 @@
#include <boost/test/unit_test.hpp>
#include <iostream>
#include "swarm.h"
BOOST_AUTO_TEST_SUITE(service_node_stuff)
static auto create_dummy_sn_record() -> sn_record_t {
const std::string address =
"0123456789012345678901234567890123456789012345678901";
const std::string pk_hex =
"330e73449f6656cfe7816fa00d850af1f45884eab9e404026ca51f54b045e385";
const std::string pk_x25519 =
"66ab11bed0e6219e1f3aea9b9e33f89cf636d5db203ed4efb9090cdb15902414";
const std::string pk_x25519_bin = "";
const std::string pk_ed25519 =
"a38418ae9af2fedb560f400953f91cefb91a7a7efc971edfa31744ce5c4e319a";
const std::string ip = "0.0.0.0";
auto sn = sn_record_t{8080, 8081, address, pk_hex,
pk_x25519, pk_x25519_bin, pk_ed25519, ip};
return sn;
}
static auto test_ip_update(const char* old_ip, const char* new_ip,
const char* expected_ip) -> void {
auto sn = create_dummy_sn_record();
sn.set_ip(old_ip);
oxen::SwarmInfo si{0, std::vector<sn_record_t>{sn}};
auto current = std::vector<oxen::SwarmInfo>{si};
sn.set_ip(new_ip);
oxen::SwarmInfo si2{0, std::vector<sn_record_t>{sn}};
auto incoming = std::vector<oxen::SwarmInfo>{si2};
auto new_records = apply_ips(current, incoming);
BOOST_CHECK_EQUAL(new_records[0].snodes[0].ip(), expected_ip);
}
BOOST_AUTO_TEST_CASE(updates_ip_address) {
auto sn = create_dummy_sn_record();
const auto default_ip = "0.0.0.0";
const auto ip1 = "1.1.1.1";
const auto ip2 = "1.2.3.4";
// Should update
test_ip_update(ip1, ip2, ip2);
// Should update
test_ip_update(default_ip, ip2, ip2);
// Should NOT update with default ip
test_ip_update(ip1, default_ip, ip1);
}
BOOST_AUTO_TEST_SUITE_END()