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

Improvements in const correctness and 'const' positioning #3510

Merged
merged 6 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nano/core_test/fakes/work_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class work_peer_type

class work_peer_connection : public std::enable_shared_from_this<work_peer_connection>
{
const std::string generic_error = "Unable to parse JSON";
std::string const generic_error = "Unable to parse JSON";

public:
work_peer_connection (asio::io_context & ioc_a, work_peer_type const type_a, nano::work_version const version_a, nano::work_pool & pool_a, std::function<void (bool const)> on_generation_a, std::function<void ()> on_cancel_a) :
Expand Down Expand Up @@ -155,7 +155,7 @@ class work_peer_connection : public std::enable_shared_from_this<work_peer_conne
beast::ostream (this_l->response.body ()) << ostream.str ();
// Delay response by 500ms as a slow peer, immediate async call for a good peer
this_l->timer.expires_from_now (boost::posix_time::milliseconds (this_l->type == work_peer_type::slow ? 500 : 0));
this_l->timer.async_wait ([this_l, result] (const boost::system::error_code & ec) {
this_l->timer.async_wait ([this_l, result] (boost::system::error_code const & ec) {
if (this_l->on_generation)
{
this_l->on_generation (result != 0);
Expand Down
56 changes: 28 additions & 28 deletions nano/core_test/ledger_walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ using namespace std::chrono_literals;
TEST (ledger_walker, genesis_block)
{
nano::system system{};
const auto node = system.add_node ();
auto const node = system.add_node ();

nano::ledger_walker ledger_walker{ node->ledger };

std::size_t walked_blocks_count = 0;
ledger_walker.walk_backward (nano::dev::genesis->hash (),
[&] (const auto & block) {
[&] (auto const & block) {
++walked_blocks_count;
EXPECT_EQ (block->hash (), nano::dev::genesis->hash ());
});
Expand All @@ -29,7 +29,7 @@ TEST (ledger_walker, genesis_block)

walked_blocks_count = 0;
ledger_walker.walk (nano::dev::genesis->hash (),
[&] (const auto & block) {
[&] (auto const & block) {
++walked_blocks_count;
EXPECT_EQ (block->hash (), nano::dev::genesis->hash ());
});
Expand All @@ -46,24 +46,24 @@ TEST (ledger_walker, genesis_account_longer)
node_config.enable_voting = true;
node_config.receive_minimum = 1;

const auto node = system.add_node (node_config);
auto const node = system.add_node (node_config);

nano::ledger_walker ledger_walker{ node->ledger };
EXPECT_TRUE (ledger_walker.walked_blocks.empty ());
EXPECT_LE (ledger_walker.walked_blocks.bucket_count (), 1);
EXPECT_TRUE (ledger_walker.blocks_to_walk.empty ());

const auto get_number_of_walked_blocks = [&ledger_walker] (const auto & start_block_hash) {
auto const get_number_of_walked_blocks = [&ledger_walker] (auto const & start_block_hash) {
std::size_t walked_blocks_count = 0;
ledger_walker.walk_backward (start_block_hash,
[&] (const auto & block) {
[&] (auto const & block) {
++walked_blocks_count;
});

return walked_blocks_count;
};

const auto transaction = node->ledger.store.tx_begin_read ();
auto const transaction = node->ledger.store.tx_begin_read ();
nano::account_info genesis_account_info{};
ASSERT_FALSE (node->ledger.store.account.get (transaction, nano::dev::genesis_key.pub, genesis_account_info));
EXPECT_EQ (get_number_of_walked_blocks (genesis_account_info.open_block), 1);
Expand All @@ -72,7 +72,7 @@ TEST (ledger_walker, genesis_account_longer)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
for (auto itr = 1; itr <= 5; ++itr)
{
const auto send = system.wallet (0)->send_action (nano::dev::genesis_key.pub, nano::dev::genesis_key.pub, 1);
auto const send = system.wallet (0)->send_action (nano::dev::genesis_key.pub, nano::dev::genesis_key.pub, 1);
ASSERT_TRUE (send);
EXPECT_EQ (get_number_of_walked_blocks (send->hash ()), 1 + itr * 2 - 1);
ASSERT_TIMELY (3s, 1 + itr * 2 == node->ledger.cache.cemented_count);
Expand All @@ -95,7 +95,7 @@ TEST (ledger_walker, cross_account)
node_config.enable_voting = true;
node_config.receive_minimum = 1;

const auto node = system.add_node (node_config);
auto const node = system.add_node (node_config);

system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
ASSERT_TRUE (system.wallet (0)->send_action (nano::dev::genesis_key.pub, nano::dev::genesis_key.pub, 1));
Expand All @@ -106,22 +106,22 @@ TEST (ledger_walker, cross_account)
ASSERT_TRUE (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key.pub, 1));
ASSERT_TIMELY (3s, 5 == node->ledger.cache.cemented_count);

const auto transaction = node->ledger.store.tx_begin_read ();
auto const transaction = node->ledger.store.tx_begin_read ();
nano::account_info account_info{};
ASSERT_FALSE (node->ledger.store.account.get (transaction, key.pub, account_info));

// TODO: check issue with account head
// const auto first = node->ledger.store.block.get_no_sideband(transaction, account_info.head);
// const auto second = node->ledger.store.block.get_no_sideband(transaction, first->previous());
// const auto third = node->ledger.store.block.get_no_sideband(transaction, second->previous());
// const auto fourth = node->ledger.store.block.get_no_sideband(transaction, third->previous());
// const auto fifth = node->ledger.store.block.get_no_sideband(transaction, fourth->previous());
// auto const first = node->ledger.store.block.get_no_sideband(transaction, account_info.head);
// auto const second = node->ledger.store.block.get_no_sideband(transaction, first->previous());
// auto const third = node->ledger.store.block.get_no_sideband(transaction, second->previous());
// auto const fourth = node->ledger.store.block.get_no_sideband(transaction, third->previous());
// auto const fifth = node->ledger.store.block.get_no_sideband(transaction, fourth->previous());
//
// const auto expected_blocks_to_walk = { first, second, third, fourth, fifth };
// auto const expected_blocks_to_walk = { first, second, third, fourth, fifth };
// auto expected_blocks_to_walk_itr = expected_blocks_to_walk.begin();
//
// nano::ledger_walker ledger_walker{ node->ledger };
// ledger_walker.walk_backward (account_info.block_count, [&] (const auto & block) {
// ledger_walker.walk_backward (account_info.block_count, [&] (auto const & block) {
// if (expected_blocks_to_walk_itr == expected_blocks_to_walk.end())
// {
// EXPECT_TRUE(false);
Expand All @@ -143,36 +143,36 @@ TEST (ledger_walker, ladder_geometry)
node_config.enable_voting = true;
node_config.receive_minimum = 1;

const auto node = system.add_node (node_config);
auto const node = system.add_node (node_config);
std::array<nano::keypair, 3> keys{};

system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
for (auto itr = 0; itr != keys.size (); ++itr)
{
system.wallet (0)->insert_adhoc (keys[itr].prv);
const auto block = system.wallet (0)->send_action (nano::dev::genesis_key.pub, keys[itr].pub, 1000);
auto const block = system.wallet (0)->send_action (nano::dev::genesis_key.pub, keys[itr].pub, 1000);
ASSERT_TRUE (block);
ASSERT_TIMELY (3s, 1 + (itr + 1) * 2 == node->ledger.cache.cemented_count);
}

std::vector<nano::uint128_t> amounts_to_send (10);
std::iota (amounts_to_send.begin (), amounts_to_send.end (), 1);

const nano::account * last_destination{};
nano::account const * last_destination{};
for (auto itr = 0; itr != amounts_to_send.size (); ++itr)
{
const auto source_index = itr % keys.size ();
const auto destination_index = (source_index + 1) % keys.size ();
auto const source_index = itr % keys.size ();
auto const destination_index = (source_index + 1) % keys.size ();
last_destination = &keys[destination_index].pub;

const auto send = system.wallet (0)->send_action (keys[source_index].pub, keys[destination_index].pub, amounts_to_send[itr]);
auto const send = system.wallet (0)->send_action (keys[source_index].pub, keys[destination_index].pub, amounts_to_send[itr]);
ASSERT_TRUE (send);
ASSERT_TIMELY (3s, 1 + keys.size () * 2 + (itr + 1) * 2 == node->ledger.cache.cemented_count);
}

ASSERT_TRUE (last_destination);
nano::account_info last_destination_info{};
const auto last_destination_read_error = node->ledger.store.account.get (node->ledger.store.tx_begin_read (), *last_destination, last_destination_info);
auto const last_destination_read_error = node->ledger.store.account.get (node->ledger.store.tx_begin_read (), *last_destination, last_destination_info);
ASSERT_FALSE (last_destination_read_error);

// This is how we expect chains to look like (for 3 accounts and 10 amounts to be sent)
Expand All @@ -185,13 +185,13 @@ TEST (ledger_walker, ladder_geometry)

nano::ledger_walker ledger_walker{ node->ledger };
ledger_walker.walk_backward (last_destination_info.head,
[&] (const auto & block) {
[&] (auto const & block) {
if (block->sideband ().details.is_receive)
{
nano::amount previous_balance{};
if (!block->previous ().is_zero ())
{
const auto previous_block = node->ledger.store.block.get_no_sideband (node->ledger.store.tx_begin_read (), block->previous ());
auto const previous_block = node->ledger.store.block.get_no_sideband (node->ledger.store.tx_begin_read (), block->previous ());
previous_balance = previous_block->balance ();
}

Expand All @@ -204,13 +204,13 @@ TEST (ledger_walker, ladder_geometry)
auto amounts_expected_itr = amounts_expected_backwards.crbegin ();

ledger_walker.walk (last_destination_info.head,
[&] (const auto & block) {
[&] (auto const & block) {
if (block->sideband ().details.is_receive)
{
nano::amount previous_balance{};
if (!block->previous ().is_zero ())
{
const auto previous_block = node->ledger.store.block.get_no_sideband (node->ledger.store.tx_begin_read (), block->previous ());
auto const previous_block = node->ledger.store.block.get_no_sideband (node->ledger.store.tx_begin_read (), block->previous ());
previous_balance = previous_block->balance ();
}

Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void add_required_children_node_config_tree (nano::jsonconfig & tree);

TEST (node, null_account)
{
const auto & null_account = nano::account::null ();
auto const & null_account = nano::account::null ();
ASSERT_TRUE (null_account == nullptr);
ASSERT_FALSE (null_account != nullptr);

Expand Down Expand Up @@ -949,7 +949,7 @@ TEST (json, backup)
};

auto get_file_count = [&dir] () {
return std::count_if (boost::filesystem::directory_iterator (dir), boost::filesystem::directory_iterator (), static_cast<bool (*) (const boost::filesystem::path &)> (boost::filesystem::is_regular_file));
return std::count_if (boost::filesystem::directory_iterator (dir), boost::filesystem::directory_iterator (), static_cast<bool (*) (boost::filesystem::path const &)> (boost::filesystem::is_regular_file));
};

// There should only be the original file in this directory
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/peer_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ TEST (peer_container, list_fanout)
ASSERT_EQ (0, node.network.fanout ());
auto list1 (node.network.list (node.network.fanout ()));
ASSERT_TRUE (list1.empty ());
auto add_peer = [&node] (const uint16_t port_a) {
auto add_peer = [&node] (uint16_t const port_a) {
ASSERT_NE (nullptr, node.network.udp_channels.insert (nano::endpoint (boost::asio::ip::address_v6::loopback (), port_a), node.network_params.network.protocol_version));
};
add_peer (9998);
Expand Down
4 changes: 2 additions & 2 deletions nano/fuzzer_test/fuzz_bignum.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nano/node/common.hpp>

/** Fuzz decimal, hex and account parsing */
void fuzz_bignum_parsers (const uint8_t * Data, size_t Size)
void fuzz_bignum_parsers (uint8_t const * Data, size_t Size)
{
try
{
Expand Down Expand Up @@ -29,7 +29,7 @@ void fuzz_bignum_parsers (const uint8_t * Data, size_t Size)
}

/** Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput (const uint8_t * Data, size_t Size)
extern "C" int LLVMFuzzerTestOneInput (uint8_t const * Data, size_t Size)
{
fuzz_bignum_parsers (Data, Size);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions nano/fuzzer_test/fuzz_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class fuzz_visitor : public nano::message_visitor
}

/** Fuzz live message parsing. This covers parsing and block/vote uniquing. */
void fuzz_message_parser (const uint8_t * Data, size_t Size)
void fuzz_message_parser (uint8_t const * Data, size_t Size)
{
static bool initialized = false;
if (!initialized)
Expand All @@ -73,7 +73,7 @@ void fuzz_message_parser (const uint8_t * Data, size_t Size)
}

/** Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput (const uint8_t * Data, size_t Size)
extern "C" int LLVMFuzzerTestOneInput (uint8_t const * Data, size_t Size)
{
fuzz_message_parser (Data, Size);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions nano/fuzzer_test/fuzz_endpoint_parsing.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <nano/node/common.hpp>

/** Fuzz endpoint parsing */
void fuzz_endpoint_parsing (const uint8_t * Data, size_t Size)
void fuzz_endpoint_parsing (uint8_t const * Data, size_t Size)
{
auto data (std::string (reinterpret_cast<char *> (const_cast<uint8_t *> (Data)), Size));
nano::endpoint endpoint;
Expand All @@ -11,7 +11,7 @@ void fuzz_endpoint_parsing (const uint8_t * Data, size_t Size)
}

/** Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput (const uint8_t * Data, size_t Size)
extern "C" int LLVMFuzzerTestOneInput (uint8_t const * Data, size_t Size)
{
fuzz_endpoint_parsing (Data, Size);
return 0;
Expand Down
8 changes: 4 additions & 4 deletions nano/lib/asio.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <nano/lib/asio.hpp>

nano::shared_const_buffer::shared_const_buffer (const std::vector<uint8_t> & data) :
nano::shared_const_buffer::shared_const_buffer (std::vector<uint8_t> const & data) :
m_data (std::make_shared<std::vector<uint8_t>> (data)),
m_buffer (boost::asio::buffer (*m_data))
{
Expand Down Expand Up @@ -29,17 +29,17 @@ nano::shared_const_buffer::shared_const_buffer (std::shared_ptr<std::vector<uint
{
}

const boost::asio::const_buffer * nano::shared_const_buffer::begin () const
boost::asio::const_buffer const * nano::shared_const_buffer::begin () const
{
return &m_buffer;
}

const boost::asio::const_buffer * nano::shared_const_buffer::end () const
boost::asio::const_buffer const * nano::shared_const_buffer::end () const
{
return &m_buffer + 1;
}

size_t nano::shared_const_buffer::size () const
std::size_t nano::shared_const_buffer::size () const
clemahieu marked this conversation as resolved.
Show resolved Hide resolved
{
return m_buffer.size ();
}
8 changes: 4 additions & 4 deletions nano/lib/asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ class shared_const_buffer
{
public:
using value_type = boost::asio::const_buffer;
using const_iterator = const boost::asio::const_buffer *;
using const_iterator = boost::asio::const_buffer const *;

explicit shared_const_buffer (std::vector<uint8_t> const & data);
explicit shared_const_buffer (uint8_t data);
explicit shared_const_buffer (std::string const & data);
explicit shared_const_buffer (std::vector<uint8_t> && data);
explicit shared_const_buffer (std::shared_ptr<std::vector<uint8_t>> const & data);

const boost::asio::const_buffer * begin () const;
const boost::asio::const_buffer * end () const;
boost::asio::const_buffer const * begin () const;
boost::asio::const_buffer const * end () const;

size_t size () const;
std::size_t size () const;

private:
std::shared_ptr<std::vector<uint8_t>> m_data;
Expand Down
Loading