Skip to content

Commit

Permalink
Merge pull request #4832 from pwojcikdev/lazy-eval-format
Browse files Browse the repository at this point in the history
Avoid unnecessary string allocations when logging
  • Loading branch information
pwojcikdev authored Jan 26, 2025
2 parents 99f95a1 + c0a278b commit caa1f54
Show file tree
Hide file tree
Showing 39 changed files with 480 additions and 493 deletions.
67 changes: 0 additions & 67 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,73 +186,6 @@ TEST (block, change_serialize_json)
ASSERT_EQ (*block1, block2);
}

TEST (uint512_union, parse_zero)
{
nano::uint512_union input (nano::uint512_t (0));
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_TRUE (output.number ().is_zero ());
}

TEST (uint512_union, parse_zero_short)
{
std::string text ("0");
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_TRUE (output.number ().is_zero ());
}

TEST (uint512_union, parse_one)
{
nano::uint512_union input (nano::uint512_t (1));
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (1, output.number ());
}

TEST (uint512_union, parse_error_symbol)
{
nano::uint512_union input (nano::uint512_t (1000));
std::string text;
input.encode_hex (text);
text[5] = '!';
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}

TEST (uint512_union, max)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (nano::uint512_t ("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), output.number ());
}

TEST (uint512_union, parse_error_overflow)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text;
input.encode_hex (text);
text.push_back (0);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}

TEST (send_block, deserialize)
{
nano::block_builder builder;
Expand Down
143 changes: 131 additions & 12 deletions nano/core_test/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ TEST (uint256_union, decode_empty)
TEST (uint256_union, parse_zero)
{
nano::uint256_union input (nano::uint256_t (0));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
Expand All @@ -366,8 +365,7 @@ TEST (uint256_union, parse_zero_short)
TEST (uint256_union, parse_one)
{
nano::uint256_union input (nano::uint256_t (1));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
Expand All @@ -378,8 +376,7 @@ TEST (uint256_union, parse_one)
TEST (uint256_union, parse_error_symbol)
{
nano::uint256_union input (nano::uint256_t (1000));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
text[5] = '!';
nano::uint256_union output;
auto error (output.decode_hex (text));
Expand All @@ -389,8 +386,7 @@ TEST (uint256_union, parse_error_symbol)
TEST (uint256_union, max_hex)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
Expand All @@ -409,8 +405,7 @@ TEST (uint256_union, decode_dec)
TEST (uint256_union, max_dec)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_dec (text);
std::string text = input.to_string_dec ();
nano::uint256_union output;
auto error (output.decode_dec (text));
ASSERT_FALSE (error);
Expand Down Expand Up @@ -445,8 +440,7 @@ TEST (uint256_union, decode_dec_leading_zero)
TEST (uint256_union, parse_error_overflow)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
text.push_back (0);
nano::uint256_union output;
auto error (output.decode_hex (text));
Expand Down Expand Up @@ -650,6 +644,68 @@ TEST (uint512_union, hash)
}
}

TEST (uint512_union, parse_zero)
{
nano::uint512_union input (nano::uint512_t (0));
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_TRUE (output.number ().is_zero ());
}

TEST (uint512_union, parse_zero_short)
{
std::string text ("0");
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_TRUE (output.number ().is_zero ());
}

TEST (uint512_union, parse_one)
{
nano::uint512_union input (nano::uint512_t (1));
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (1, output.number ());
}

TEST (uint512_union, parse_error_symbol)
{
nano::uint512_union input (nano::uint512_t (1000));
std::string text = input.to_string ();
text[5] = '!';
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}

TEST (uint512_union, max)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (nano::uint512_t ("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), output.number ());
}

TEST (uint512_union, parse_error_overflow)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text = input.to_string ();
text.push_back (0);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}

TEST (sat_math, add_sat)
{
// Test uint128_t
Expand Down Expand Up @@ -743,4 +799,67 @@ TEST (sat_math, sub_sat)
ASSERT_EQ (nano::sub_sat (hundred, nano::uint512_t (200)), min);
ASSERT_EQ (nano::sub_sat (min, max), min);
}
}

TEST (account, encode_zero)
{
nano::account number0{};
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();

/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
ASSERT_EQ (65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}

TEST (account, encode_all)
{
nano::account number0;
number0.decode_hex ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();

/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}

TEST (account, encode_fail)
{
nano::account number0{};
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();
str0[16] ^= 1;
nano::account number1;
ASSERT_TRUE (number1.decode_account (str0));
}

TEST (account, known_addresses)
{
nano::account account1{ "0000000000000000000000000000000000000000000000000000000000000000" };
ASSERT_EQ (account1.to_account (), "nano_1111111111111111111111111111111111111111111111111111hifc8npp");

nano::account account2{ "B0311EA55708D6A53C75CDBF88300259C6D018522FE3D4D0A242E431F9E8B6D0" };
ASSERT_EQ (account2.to_account (), "nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo");

nano::account account3{ "45C6FF9D1706D61F0821327752671BDA9F9ED2DA40326B01935AB566FB9E08ED" };
ASSERT_EQ (account3.to_account (), "nano_1jg8zygjg3pp5w644emqcbmjqpnzmubfni3kfe1s8pooeuxsw49fdq1mco9j");

nano::account account4{ "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA" };
ASSERT_EQ (account4.to_account (), "nano_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3");

nano::account account5{ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" };
ASSERT_EQ (account5.to_account (), "nano_3zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzc3yoon41");
}
42 changes: 0 additions & 42 deletions nano/core_test/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,48 +319,6 @@ TEST (wallet, rekey)
ASSERT_TRUE (wallet.rekey (transaction, "2"));
}

TEST (account, encode_zero)
{
nano::account number0{};
std::string str0;
number0.encode_account (str0);

/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
ASSERT_EQ (65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}

TEST (account, encode_all)
{
nano::account number0;
number0.decode_hex ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
std::string str0;
number0.encode_account (str0);

/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}

TEST (account, encode_fail)
{
nano::account number0{};
std::string str0;
number0.encode_account (str0);
str0[16] ^= 1;
nano::account number1;
ASSERT_TRUE (number1.decode_account (str0));
}

TEST (wallet, hash_password)
{
bool init;
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ add_library(
errors.cpp
files.hpp
files.cpp
formatting.hpp
formatting.cpp
fwd.hpp
id_dispenser.hpp
interval.hpp
Expand Down
Loading

0 comments on commit caa1f54

Please sign in to comment.