From 78db0d7bcee6cbfe3f7734cdb152811d49d30b20 Mon Sep 17 00:00:00 2001 From: cryptocode Date: Tue, 14 May 2019 12:26:35 +0200 Subject: [PATCH] Add modify callback to prevent violating boost multiindex invariants (#1979) * Add modify callback to prevent violating boost multiindex invariants * Remove BOOST_MULTI_INDEX_ENABLE_SAFE_MODE * Update tests --- nano/core_test/peer_container.cpp | 10 ++++++---- nano/node/transport/udp.cpp | 15 ++++++++++----- nano/node/transport/udp.hpp | 2 +- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/nano/core_test/peer_container.cpp b/nano/core_test/peer_container.cpp index 2de62bfe9..4fdf03239 100644 --- a/nano/core_test/peer_container.cpp +++ b/nano/core_test/peer_container.cpp @@ -58,12 +58,14 @@ TEST (peer_container, split) nano::endpoint endpoint2 (boost::asio::ip::address_v6::loopback (), 101); auto channel1 (system.nodes[0]->network.udp_channels.insert (endpoint1, 0)); ASSERT_NE (nullptr, channel1); - channel1->set_last_packet_received (now - std::chrono::seconds (1)); - system.nodes[0]->network.udp_channels.modify (channel1); + system.nodes[0]->network.udp_channels.modify (channel1, [&now](auto channel) { + channel->set_last_packet_received (now - std::chrono::seconds (1)); + }); auto channel2 (system.nodes[0]->network.udp_channels.insert (endpoint2, 0)); ASSERT_NE (nullptr, channel2); - channel2->set_last_packet_received (now + std::chrono::seconds (1)); - system.nodes[0]->network.udp_channels.modify (channel2); + system.nodes[0]->network.udp_channels.modify (channel2, [&now](auto channel) { + channel->set_last_packet_received (now + std::chrono::seconds (1)); + }); ASSERT_EQ (2, system.nodes[0]->network.size ()); ASSERT_EQ (2, system.nodes[0]->network.udp_channels.size ()); system.nodes[0]->network.cleanup (now); diff --git a/nano/node/transport/udp.cpp b/nano/node/transport/udp.cpp index 0ff9c3aa4..2b7c5f95d 100644 --- a/nano/node/transport/udp.cpp +++ b/nano/node/transport/udp.cpp @@ -493,7 +493,9 @@ public: auto channel (node.network.udp_channels.insert (endpoint, message_a.header.version_using)); if (channel) { - channel->set_node_id (message_a.response->first); + node.network.udp_channels.modify (channel, [&message_a](std::shared_ptr channel_a) { + channel_a->set_node_id (message_a.response->first); + }); } } } @@ -517,8 +519,9 @@ public: auto channel (node.network.udp_channels.channel (endpoint)); if (channel) { - channel->set_last_packet_received (std::chrono::steady_clock::now ()); - node.network.udp_channels.modify (channel); + node.network.udp_channels.modify (channel, [](std::shared_ptr channel_a) { + channel_a->set_last_packet_received (std::chrono::steady_clock::now ()); + }); node.process_message (message_a, channel); } } @@ -833,12 +836,14 @@ std::deque> nano::transport::udp_c return result; } -void nano::transport::udp_channels::modify (std::shared_ptr channel_a) +void nano::transport::udp_channels::modify (std::shared_ptr channel_a, std::function)> modify_callback_a) { std::lock_guard lock (mutex); auto existing (channels.get ().find (channel_a->endpoint)); if (existing != channels.get ().end ()) { - channels.get ().modify (existing, [](channel_udp_wrapper &) {}); + channels.get ().modify (existing, [modify_callback_a](channel_udp_wrapper & wrapper_a) { + modify_callback_a (wrapper_a.channel); + }); } } diff --git a/nano/node/transport/udp.hpp b/nano/node/transport/udp.hpp index 7ee6b31c2..2ab956b87 100644 --- a/nano/node/transport/udp.hpp +++ b/nano/node/transport/udp.hpp @@ -124,7 +124,7 @@ namespace transport std::deque> list (size_t); // A list of random peers sized for the configured rebroadcast fanout std::deque> list_fanout (); - void modify (std::shared_ptr); + void modify (std::shared_ptr, std::function)>); // Maximum number of peers per IP static size_t constexpr max_peers_per_ip = 10; static std::chrono::seconds constexpr syn_cookie_cutoff = std::chrono::seconds (5);