Skip to content
Open
3 changes: 3 additions & 0 deletions src/Makefile.test_util.include
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ EXTRA_LIBRARIES += \
TEST_UTIL_H = \
test/util/blockfilter.h \
test/util/chainstate.h \
test/util/coins.h \
test/util/json.h \
test/util/index.h \
test/util/llmq_tests.h \
test/util/logging.h \
test/util/mining.h \
test/util/net.h \
test/util/poolresourcetester.h \
test/util/random.h \
test/util/script.h \
test/util/setup_common.h \
test/util/str.h \
Expand All @@ -31,6 +33,7 @@ libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libtest_util_a_SOURCES = \
test/util/blockfilter.cpp \
test/util/index.cpp \
test/util/coins.cpp \
test/util/json.cpp \
test/util/logging.cpp \
test/util/mining.cpp \
Expand Down
26 changes: 14 additions & 12 deletions src/logging/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <util/types.h>

#include <chrono>
#include <optional>
#include <string>


Expand All @@ -28,14 +29,14 @@ class Timer
std::string prefix,
std::string end_msg,
BCLog::LogFlags log_category = BCLog::LogFlags::ALL,
bool msg_on_completion = true) :
m_prefix(std::move(prefix)),
m_title(std::move(end_msg)),
m_log_category(log_category),
m_message_on_completion(msg_on_completion)
bool msg_on_completion = true)
: m_prefix(std::move(prefix)),
m_title(std::move(end_msg)),
m_log_category(log_category),
m_message_on_completion(msg_on_completion)
{
this->Log(strprintf("%s started", m_title));
m_start_t = GetTime<std::chrono::microseconds>();
m_start_t = std::chrono::steady_clock::now();
}

~Timer()
Expand All @@ -60,24 +61,25 @@ class Timer

std::string LogMsg(const std::string& msg)
{
const auto end_time = GetTime<std::chrono::microseconds>() - m_start_t;
if (m_start_t.count() <= 0) {
const auto end_time{std::chrono::steady_clock::now()};
if (!m_start_t) {
return strprintf("%s: %s", m_prefix, msg);
}
const auto duration{end_time - *m_start_t};

if constexpr (std::is_same<TimeType, std::chrono::microseconds>::value) {
return strprintf("%s: %s (%iμs)", m_prefix, msg, end_time.count());
return strprintf("%s: %s (%iμs)", m_prefix, msg, Ticks<std::chrono::microseconds>(duration));
} else if constexpr (std::is_same<TimeType, std::chrono::milliseconds>::value) {
return strprintf("%s: %s (%.2fms)", m_prefix, msg, end_time.count() * 0.001);
return strprintf("%s: %s (%.2fms)", m_prefix, msg, Ticks<MillisecondsDouble>(duration));
} else if constexpr (std::is_same<TimeType, std::chrono::seconds>::value) {
return strprintf("%s: %s (%.2fs)", m_prefix, msg, end_time.count() * 0.000001);
return strprintf("%s: %s (%.2fs)", m_prefix, msg, Ticks<SecondsDouble>(duration));
} else {
static_assert(ALWAYS_FALSE<TimeType>, "Error: unexpected time type");
}
}

private:
std::chrono::microseconds m_start_t{};
std::optional<std::chrono::steady_clock::time_point> m_start_t{};

//! Log prefix; usually the name of the function this was created in.
const std::string m_prefix;
Expand Down
50 changes: 50 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,55 @@ static RPCHelpMan setmnthreadactive()
};
}

static RPCHelpMan getaddrmaninfo()
{
return RPCHelpMan{"getaddrmaninfo",
"\nProvides information about the node's address manager by returning the number of "
"addresses in the `new` and `tried` tables and their sum for all networks.\n"
"This RPC is for testing only.\n",
{},
RPCResult{
RPCResult::Type::OBJ_DYN, "", "json object with network type as keys",
{
{RPCResult::Type::OBJ, "network", "the network (" + Join(GetNetworkNames(), ", ") + ")",
{
{RPCResult::Type::NUM, "new", "number of addresses in the new table, which represent potential peers the node has discovered but hasn't yet successfully connected to."},
{RPCResult::Type::NUM, "tried", "number of addresses in the tried table, which represent peers the node has successfully connected to in the past."},
{RPCResult::Type::NUM, "total", "total number of addresses in both new/tried tables"},
}},
}
},
RPCExamples{
HelpExampleCli("getaddrmaninfo", "")
+ HelpExampleRpc("getaddrmaninfo", "")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
NodeContext& node = EnsureAnyNodeContext(request.context);
if (!node.addrman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
}

UniValue ret(UniValue::VOBJ);
for (int n = 0; n < NET_MAX; ++n) {
enum Network network = static_cast<enum Network>(n);
if (network == NET_UNROUTABLE || network == NET_INTERNAL) continue;
UniValue obj(UniValue::VOBJ);
obj.pushKV("new", node.addrman->Size(network, true));
obj.pushKV("tried", node.addrman->Size(network, false));
obj.pushKV("total", node.addrman->Size(network));
ret.pushKV(GetNetworkName(network), obj);
}
UniValue obj(UniValue::VOBJ);
obj.pushKV("new", node.addrman->Size(std::nullopt, true));
obj.pushKV("tried", node.addrman->Size(std::nullopt, false));
obj.pushKV("total", node.addrman->Size());
ret.pushKV("all_networks", obj);
return ret;
},
};
}

void RegisterNetRPCCommands(CRPCTable &t)
{
static const CRPCCommand commands[]{
Expand All @@ -1146,6 +1195,7 @@ void RegisterNetRPCCommands(CRPCTable &t)
{"hidden", &addpeeraddress},
{"hidden", &sendmsgtopeer},
{"hidden", &setmnthreadactive},
{"hidden", &getaddrmaninfo},
};
for (const auto& c : commands) {
t.appendCommand(c.name, &c);
Expand Down
1 change: 1 addition & 0 deletions src/test/base58_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <base58.h>
#include <test/util/json.h>
#include <test/util/setup_common.h> // for InsecureRand*
#include <test/util/random.h>
#include <util/strencodings.h>
#include <util/vector.h>

Expand Down
1 change: 1 addition & 0 deletions src/test/bip324_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <key.h>
#include <pubkey.h>
#include <span.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <util/strencodings.h>

Expand Down
1 change: 1 addition & 0 deletions src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <consensus/merkle.h>
#include <pow.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/txmempool.h>

#include <test/util/setup_common.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/bloom_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <primitives/block.h>
#include <serialize.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/strencodings.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/checkqueue_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <checkqueue.h>
#include <sync.h>
#include <test/util/random.h>
#include <util/time.h>

#include <boost/test/unit_test.hpp>
Expand Down
3 changes: 2 additions & 1 deletion src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <script/standard.h>
#include <streams.h>
#include <test/util/poolresourcetester.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <txdb.h>
#include <uint256.h>
Expand Down Expand Up @@ -173,7 +174,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)

if (InsecureRandRange(5) == 0 || coin.IsSpent()) {
Coin newcoin;
newcoin.out.nValue = InsecureRand32();
newcoin.out.nValue = InsecureRandMoneyAmount();
newcoin.nHeight = 1;

// Infrequently test adding unspendable coins.
Expand Down
1 change: 1 addition & 0 deletions src/test/crypto_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <crypto/sha512.h>
#include <random.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <util/strencodings.h>

Expand Down
1 change: 1 addition & 0 deletions src/test/cuckoocache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cuckoocache.h>
#include <random.h>
#include <script/sigcache.h>
#include <test/util/random.h>

#include <boost/test/unit_test.hpp>

Expand Down
1 change: 1 addition & 0 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <dbwrapper.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/string.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
"generate",
"generateblock",
"getaddednodeinfo",
"getaddrmaninfo",
"getbestblockhash",
"getblock",
"getblockchaininfo",
Expand Down
23 changes: 16 additions & 7 deletions src/test/fuzz/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,19 @@ void FillNode(FuzzedDataProvider& fuzzed_data_provider, ConnmanTestMsg& connman,
/*relay_txs=*/fuzzed_data_provider.ConsumeBool());
}

std::vector<uint8_t> ConstructPubKeyBytes(FuzzedDataProvider& fuzzed_data_provider, Span<const uint8_t> byte_data, const bool compressed) noexcept
{
uint8_t pk_type;
if (compressed) {
pk_type = fuzzed_data_provider.PickValueInArray({0x02, 0x03});
} else {
pk_type = fuzzed_data_provider.PickValueInArray({0x04, 0x06, 0x07});
}
std::vector<uint8_t> pk_data{byte_data.begin(), byte_data.begin() + (compressed ? CPubKey::COMPRESSED_SIZE : CPubKey::SIZE)};
pk_data[0] = pk_type;
return pk_data;
}

CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::optional<CAmount>& max) noexcept
{
return fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, max.value_or(MAX_MONEY));
Expand Down Expand Up @@ -385,16 +398,12 @@ CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider) noexcept
// navigate the highly structured multisig format.
r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
int num_data{fuzzed_data_provider.ConsumeIntegralInRange(1, 22)};
std::vector<uint8_t> pubkey_comp{buffer.begin(), buffer.begin() + CPubKey::COMPRESSED_SIZE};
pubkey_comp.front() = fuzzed_data_provider.ConsumeIntegralInRange(2, 3); // Set first byte for GetLen() to pass
std::vector<uint8_t> pubkey_uncomp{buffer.begin(), buffer.begin() + CPubKey::SIZE};
pubkey_uncomp.front() = fuzzed_data_provider.ConsumeIntegralInRange(4, 7); // Set first byte for GetLen() to pass
while (num_data--) {
auto& pubkey{fuzzed_data_provider.ConsumeBool() ? pubkey_uncomp : pubkey_comp};
auto pubkey_bytes{ConstructPubKeyBytes(fuzzed_data_provider, buffer, fuzzed_data_provider.ConsumeBool())};
if (fuzzed_data_provider.ConsumeBool()) {
pubkey.back() = num_data; // Make each pubkey different
pubkey_bytes.back() = num_data; // Make each pubkey different
}
r_script << pubkey;
r_script << pubkey_bytes;
}
r_script << fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 22);
},
Expand Down
1 change: 1 addition & 0 deletions src/test/hash_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <clientversion.h>
#include <crypto/siphash.h>
#include <hash.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <util/strencodings.h>

Expand Down
1 change: 1 addition & 0 deletions src/test/key_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <key_io.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/strencodings.h>
Expand Down
15 changes: 2 additions & 13 deletions src/test/logging_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,9 @@ struct LogSetup : public BasicTestingSetup {

BOOST_AUTO_TEST_CASE(logging_timer)
{
SetMockTime(1);
auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
SetMockTime(2);
BOOST_CHECK_EQUAL(micro_timer.LogMsg("test micros"), "tests: test micros (1000000μs)");

SetMockTime(1);
auto ms_timer = BCLog::Timer<std::chrono::milliseconds>("tests", "end_msg");
SetMockTime(2);
BOOST_CHECK_EQUAL(ms_timer.LogMsg("test ms"), "tests: test ms (1000.00ms)");

SetMockTime(1);
auto sec_timer = BCLog::Timer<std::chrono::seconds>("tests", "end_msg");
SetMockTime(2);
BOOST_CHECK_EQUAL(sec_timer.LogMsg("test secs"), "tests: test secs (1.00s)");
const std::string_view result_prefix{"tests: msg ("};
BOOST_CHECK_EQUAL(micro_timer.LogMsg("msg").substr(0, result_prefix.size()), result_prefix);
}

BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
Expand Down
1 change: 1 addition & 0 deletions src/test/merkle_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <consensus/merkle.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <policy/policy.h>
#include <pow.h>
#include <script/standard.h>
#include <test/util/random.h>
#include <test/util/txmempool.h>
#include <uint256.h>
#include <util/strencodings.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/minisketch_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <minisketch.h>
#include <node/minisketchwrapper.h>
#include <random.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <serialize.h>
#include <span.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/validation.h>
#include <timedata.h>
#include <util/strencodings.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/orphanage_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <script/sign.h>
#include <script/signingprovider.h>
#include <script/standard.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <txorphanage.h>

Expand Down
1 change: 1 addition & 0 deletions src/test/pmt_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <merkleblock.h>
#include <serialize.h>
#include <streams.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <version.h>
Expand Down
1 change: 1 addition & 0 deletions src/test/pool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memusage.h>
#include <support/allocators/pool.h>
#include <test/util/poolresourcetester.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/test/pow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <chain.h>
#include <chainparams.h>
#include <pow.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>
Expand Down
2 changes: 2 additions & 0 deletions src/test/prevector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <prevector.h>
#include <serialize.h>
#include <streams.h>

#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>
Expand Down
1 change: 1 addition & 0 deletions src/test/script_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <script/signingprovider.h>
#include <streams.h>
#include <test/util/json.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
#include <test/util/transaction_utils.h>
#include <util/strencodings.h>
Expand Down
Loading
Loading