Skip to content
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

addrman_tests #177

Merged
merged 15 commits into from Feb 28, 2024
20 changes: 11 additions & 9 deletions src/policy/feerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,28 @@

CFeeRate::CFeeRate(const CAmount& nFeePaid, uint32_t num_bytes)
{
const int64_t nSize{num_bytes};

assert(num_bytes <= uint64_t(std::numeric_limits<int64_t>::max()));
int64_t nSize = int64_t(num_bytes);
if (nSize > 0) {
gto90 marked this conversation as resolved.
Show resolved Hide resolved
nSatoshisPerK = nFeePaid * 1000 / nSize;
} else {
nSatoshisPerK = 0;
}
}
}

CAmount CFeeRate::GetFee(uint32_t num_bytes) const
{
const int64_t nSize{num_bytes};

assert(num_bytes <= uint64_t(std::numeric_limits<int64_t>::max()));
gto90 marked this conversation as resolved.
Show resolved Hide resolved
int64_t nSize = int64_t(num_bytes);
CAmount nFee{static_cast<CAmount>(std::ceil(nSatoshisPerK * nSize / 1000.0))};

if (nFee == 0 && nSize != 0) {
if (nSatoshisPerK > 0) nFee = CAmount(1);
if (nSatoshisPerK < 0) nFee = CAmount(-1);
if (nSatoshisPerK > 0) {
nFee = CAmount(1);
}
if (nSatoshisPerK < 0) {
nFee = CAmount(-1);
}
}

return nFee;
}

Expand Down
74 changes: 37 additions & 37 deletions src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_CHECK_EQUAL(addr_null.ToString(), "[::]:0");

// Test: Does Addrman::Add work as expected.
CService addr1 = ResolveService("250.1.1.1", 12024);
CService addr1 = ResolveService("250.1.1.1", 8333);
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
BOOST_CHECK_EQUAL(addrman.size(), 1U);
CAddrInfo addr_ret1 = addrman.Select();
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:12024");
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333");

// Test: Does IP address deduplication work correctly.
// Expected dup IP should not be added.
CService addr1_dup = ResolveService("250.1.1.1", 12024);
CService addr1_dup = ResolveService("250.1.1.1", 8333);
BOOST_CHECK(!addrman.Add(CAddress(addr1_dup, NODE_NONE), source));
BOOST_CHECK_EQUAL(addrman.size(), 1U);

Expand All @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
// hash collisions may occur. But we can always be sure of at least one
// success.

CService addr2 = ResolveService("250.1.1.2", 12024);
CService addr2 = ResolveService("250.1.1.2", 8333);
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), source));
BOOST_CHECK(addrman.size() >= 1);

Expand All @@ -174,8 +174,8 @@ BOOST_AUTO_TEST_CASE(addrman_simple)

// Test: AddrMan::Add multiple addresses works as expected
std::vector<CAddress> vAddr;
vAddr.push_back(CAddress(ResolveService("250.1.1.3", 12024), NODE_NONE));
vAddr.push_back(CAddress(ResolveService("250.1.1.4", 12024), NODE_NONE));
vAddr.push_back(CAddress(ResolveService("250.1.1.3", 8333), NODE_NONE));
vAddr.push_back(CAddress(ResolveService("250.1.1.4", 8333), NODE_NONE));
BOOST_CHECK(addrman.Add(vAddr, source));
BOOST_CHECK(addrman.size() >= 1);
}
Expand All @@ -189,23 +189,23 @@ BOOST_AUTO_TEST_CASE(addrman_ports)
BOOST_CHECK_EQUAL(addrman.size(), 0U);

// Test 7; Addr with same IP but diff port does not replace existing addr.
CService addr1 = ResolveService("250.1.1.1", 12024);
CService addr1 = ResolveService("250.1.1.1", 8333);
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
BOOST_CHECK_EQUAL(addrman.size(), 1U);

CService addr1_port = ResolveService("250.1.1.1", 8334);
BOOST_CHECK(!addrman.Add(CAddress(addr1_port, NODE_NONE), source));
BOOST_CHECK_EQUAL(addrman.size(), 1U);
CAddrInfo addr_ret2 = addrman.Select();
BOOST_CHECK_EQUAL(addr_ret2.ToString(), "250.1.1.1:12024");
BOOST_CHECK_EQUAL(addr_ret2.ToString(), "250.1.1.1:8333");

// Test: Add same IP but diff port to tried table, it doesn't get added.
// Perhaps this is not ideal behavior but it is the current behavior.
addrman.Good(CAddress(addr1_port, NODE_NONE));
BOOST_CHECK_EQUAL(addrman.size(), 1U);
bool newOnly = true;
CAddrInfo addr_ret3 = addrman.Select(newOnly);
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:12024");
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333");
}


Expand All @@ -216,13 +216,13 @@ BOOST_AUTO_TEST_CASE(addrman_select)
CNetAddr source = ResolveIP("252.2.2.2");

// Test: Select from new with 1 addr in new.
CService addr1 = ResolveService("250.1.1.1", 12024);
CService addr1 = ResolveService("250.1.1.1", 8333);
BOOST_CHECK(addrman.Add(CAddress(addr1, NODE_NONE), source));
BOOST_CHECK_EQUAL(addrman.size(), 1U);

bool newOnly = true;
CAddrInfo addr_ret1 = addrman.Select(newOnly);
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:12024");
BOOST_CHECK_EQUAL(addr_ret1.ToString(), "250.1.1.1:8333");

// Test: move addr to tried, select from new expected nothing returned.
addrman.Good(CAddress(addr1, NODE_NONE));
Expand All @@ -231,30 +231,30 @@ BOOST_AUTO_TEST_CASE(addrman_select)
BOOST_CHECK_EQUAL(addr_ret2.ToString(), "[::]:0");

CAddrInfo addr_ret3 = addrman.Select();
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:12024");
BOOST_CHECK_EQUAL(addr_ret3.ToString(), "250.1.1.1:8333");

BOOST_CHECK_EQUAL(addrman.size(), 1U);


// Add three addresses to new table.
CService addr2 = ResolveService("250.3.1.1", 12024);
CService addr2 = ResolveService("250.3.1.1", 8333);
CService addr3 = ResolveService("250.3.2.2", 9999);
CService addr4 = ResolveService("250.3.3.3", 9999);

BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333)));
BOOST_CHECK(addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333)));
BOOST_CHECK(addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333)));

// Add three addresses to tried table.
CService addr5 = ResolveService("250.4.4.4", 12024);
CService addr5 = ResolveService("250.4.4.4", 8333);
CService addr6 = ResolveService("250.4.5.5", 7777);
CService addr7 = ResolveService("250.4.6.6", 12024);
CService addr7 = ResolveService("250.4.6.6", 8333);

BOOST_CHECK(addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333)));
addrman.Good(CAddress(addr5, NODE_NONE));
BOOST_CHECK(addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333)));
addrman.Good(CAddress(addr6, NODE_NONE));
BOOST_CHECK(addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 12024)));
BOOST_CHECK(addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333)));
addrman.Good(CAddress(addr7, NODE_NONE));

// Test: 6 addrs + 1 addr from last test = 7.
Expand Down Expand Up @@ -327,9 +327,9 @@ BOOST_AUTO_TEST_CASE(addrman_find)

BOOST_CHECK_EQUAL(addrman.size(), 0U);

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
CAddress addr3 = CAddress(ResolveService("251.255.2.1", 12024), NODE_NONE);
CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE);

CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = ResolveIP("250.1.2.2");
Expand All @@ -341,7 +341,7 @@ BOOST_AUTO_TEST_CASE(addrman_find)
// Test: ensure Find returns an IP matching what we searched on.
CAddrInfo* info1 = addrman.Find(addr1);
BOOST_REQUIRE(info1);
BOOST_CHECK_EQUAL(info1->ToString(), "250.1.2.1:12024");
BOOST_CHECK_EQUAL(info1->ToString(), "250.1.2.1:8333");

// Test 18; Find does not discriminate by port number.
CAddrInfo* info2 = addrman.Find(addr2);
Expand All @@ -351,7 +351,7 @@ BOOST_AUTO_TEST_CASE(addrman_find)
// Test: Find returns another IP matching what we searched on.
CAddrInfo* info3 = addrman.Find(addr3);
BOOST_REQUIRE(info3);
BOOST_CHECK_EQUAL(info3->ToString(), "251.255.2.1:12024");
BOOST_CHECK_EQUAL(info3->ToString(), "251.255.2.1:8333");
}

BOOST_AUTO_TEST_CASE(addrman_create)
Expand All @@ -360,17 +360,17 @@ BOOST_AUTO_TEST_CASE(addrman_create)

BOOST_CHECK_EQUAL(addrman.size(), 0U);

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1");

int nId;
CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);

// Test: The result should be the same as the input addr.
BOOST_CHECK_EQUAL(pinfo->ToString(), "250.1.2.1:12024");
BOOST_CHECK_EQUAL(pinfo->ToString(), "250.1.2.1:8333");

CAddrInfo* info2 = addrman.Find(addr1);
BOOST_CHECK_EQUAL(info2->ToString(), "250.1.2.1:12024");
BOOST_CHECK_EQUAL(info2->ToString(), "250.1.2.1:8333");
}


Expand All @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(addrman_delete)

BOOST_CHECK_EQUAL(addrman.size(), 0U);

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1");

int nId;
Expand All @@ -404,15 +404,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
std::vector<CAddress> vAddr1 = addrman.GetAddr(/* max_addresses */ 0, /* max_pct */ 0, /* network */ std::nullopt);
BOOST_CHECK_EQUAL(vAddr1.size(), 0U);

CAddress addr1 = CAddress(ResolveService("250.250.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
addr2.nTime = GetAdjustedTime();
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 12024), NODE_NONE);
CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
addr3.nTime = GetAdjustedTime();
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 12024), NODE_NONE);
CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
addr4.nTime = GetAdjustedTime();
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 12024), NODE_NONE);
CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
addr5.nTime = GetAdjustedTime();
CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = ResolveIP("250.2.3.3");
Expand Down Expand Up @@ -461,7 +461,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket_legacy)
{
CAddrManTest addrman;

CAddress addr1 = CAddress(ResolveService("250.1.1.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);

CNetAddr source1 = ResolveIP("250.1.1.1");
Expand Down Expand Up @@ -516,7 +516,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
{
CAddrManTest addrman;

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);

CNetAddr source1 = ResolveIP("250.1.2.1");
Expand Down Expand Up @@ -594,7 +594,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
{
CAddrManTest addrman;

CAddress addr1 = CAddress(ResolveService("250.1.1.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);

CNetAddr source1 = ResolveIP("250.1.1.1");
Expand Down Expand Up @@ -649,7 +649,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
{
CAddrManTest addrman;

CAddress addr1 = CAddress(ResolveService("250.1.2.1", 12024), NODE_NONE);
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);

CNetAddr source1 = ResolveIP("250.1.2.1");
Expand Down