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

Stub in memory class and usage in block deserializer. #419

Merged
merged 2 commits into from
Jul 26, 2024
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
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ src_libbitcoin_network_la_SOURCES = \
src/net/deadline.cpp \
src/net/distributor.cpp \
src/net/hosts.cpp \
src/net/memory.cpp \
src/net/proxy.cpp \
src/net/socket.cpp \
src/protocols/protocol.cpp \
Expand Down Expand Up @@ -192,6 +193,7 @@ test_libbitcoin_network_test_SOURCES = \
test/net/deadline.cpp \
test/net/distributor.cpp \
test/net/hosts.cpp \
test/net/memory.cpp \
test/net/proxy.cpp \
test/net/socket.cpp \
test/protocols/protocol.cpp \
Expand Down Expand Up @@ -342,6 +344,7 @@ include_bitcoin_network_net_HEADERS = \
include/bitcoin/network/net/deadline.hpp \
include/bitcoin/network/net/distributor.hpp \
include/bitcoin/network/net/hosts.hpp \
include/bitcoin/network/net/memory.hpp \
include/bitcoin/network/net/net.hpp \
include/bitcoin/network/net/proxy.hpp \
include/bitcoin/network/net/socket.hpp
Expand Down
2 changes: 2 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/net/deadline.cpp"
"../../src/net/distributor.cpp"
"../../src/net/hosts.cpp"
"../../src/net/memory.cpp"
"../../src/net/proxy.cpp"
"../../src/net/socket.cpp"
"../../src/protocols/protocol.cpp"
Expand Down Expand Up @@ -400,6 +401,7 @@ if (with-tests)
"../../test/net/deadline.cpp"
"../../test/net/distributor.cpp"
"../../test/net/hosts.cpp"
"../../test/net/memory.cpp"
"../../test/net/proxy.cpp"
"../../test/net/socket.cpp"
"../../test/protocols/protocol.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<ClCompile Include="..\..\..\..\test\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\test\net\distributor.cpp" />
<ClCompile Include="..\..\..\..\test\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\test\net\memory.cpp" />
<ClCompile Include="..\..\..\..\test\net\proxy.cpp" />
<ClCompile Include="..\..\..\..\test\net\socket.cpp" />
<ClCompile Include="..\..\..\..\test\p2p.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@
<ClCompile Include="..\..\..\..\test\net\hosts.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\memory.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\net\proxy.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<ClCompile Include="..\..\..\..\src\net\deadline.cpp" />
<ClCompile Include="..\..\..\..\src\net\distributor.cpp" />
<ClCompile Include="..\..\..\..\src\net\hosts.cpp" />
<ClCompile Include="..\..\..\..\src\net\memory.cpp" />
<ClCompile Include="..\..\..\..\src\net\proxy.cpp" />
<ClCompile Include="..\..\..\..\src\net\socket.cpp" />
<ClCompile Include="..\..\..\..\src\p2p.cpp" />
Expand Down Expand Up @@ -239,6 +240,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\deadline.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\distributor.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\hosts.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\memory.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\net.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\proxy.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\socket.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@
<ClCompile Include="..\..\..\..\src\net\hosts.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\memory.cpp">
<Filter>src\net</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\net\proxy.cpp">
<Filter>src\net</Filter>
</ClCompile>
Expand Down Expand Up @@ -563,6 +566,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\hosts.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\memory.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\net\net.hpp">
<Filter>include\bitcoin\network\net</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/distributor.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/net.hpp>
#include <bitcoin/network/net/proxy.hpp>
#include <bitcoin/network/net/socket.hpp>
Expand Down
50 changes: 50 additions & 0 deletions include/bitcoin/network/net/memory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (c) 2011-2024 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_NET_MEMORY_HPP
#define LIBBITCOIN_NETWORK_NET_MEMORY_HPP

#include <memory>
#include <shared_mutex>
#include <bitcoin/system.hpp>
#include <bitcoin/network/define.hpp>

namespace libbitcoin {
namespace network {

class BCT_API memory
{
public:
DELETE_COPY_MOVE_DESTRUCT(memory);

memory() NOEXCEPT;
memory(arena* arena) NOEXCEPT;

arena* get_arena() NOEXCEPT;
retainer::ptr get_retainer() NOEXCEPT;


private:
arena* arena_;
std::shared_mutex remap_mutex_{};
};

} // namespace network
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/network/net/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <bitcoin/network/net/deadline.hpp>
#include <bitcoin/network/net/distributor.hpp>
#include <bitcoin/network/net/hosts.hpp>
#include <bitcoin/network/net/memory.hpp>
#include <bitcoin/network/net/proxy.hpp>
#include <bitcoin/network/net/socket.hpp>

Expand Down
3 changes: 1 addition & 2 deletions include/bitcoin/network/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class BCT_API session
/// Subscribe to session stop notification, obtain unsubscribe key.
virtual object_key subscribe_stop(notifier&& handler) NOEXCEPT;
virtual bool notify(object_key key) NOEXCEPT;
virtual object_key create_key() NOEXCEPT;

/// Remove self from network close subscription (for session early stop).
virtual void unsubscribe_close() NOEXCEPT;
Expand Down Expand Up @@ -232,8 +233,6 @@ class BCT_API session
asio::strand& strand() NOEXCEPT;

private:
object_key create_key() NOEXCEPT;

void handle_channel_start(const code& ec, const channel::ptr& channel,
const result_handler& started, const result_handler& stopped) NOEXCEPT;

Expand Down
8 changes: 7 additions & 1 deletion src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#include <bitcoin/network/messages/block.hpp>

#include <iterator>
#include <memory>
#include <bitcoin/system.hpp>
#include <bitcoin/network/messages/enums/identifier.hpp>
#include <bitcoin/network/messages/enums/level.hpp>
#include <bitcoin/network/messages/message.hpp>
#include <bitcoin/network/messages/transaction.hpp>
#include <bitcoin/network/net/memory.hpp>

namespace libbitcoin {
namespace network {
Expand All @@ -40,8 +42,11 @@ const uint32_t block::version_maximum = level::maximum_protocol;
typename block::cptr block::deserialize(uint32_t version,
const system::data_chunk& data, bool witness) NOEXCEPT
{
static memory memory{};
system::istream source{ data };
system::byte_reader reader{ source };
system::byte_reader reader{ source, memory.get_arena() };

// message, block and block_ptr are not allocated by reader's arena.
const auto message = to_shared(deserialize(version, reader, witness));
if (!reader)
return nullptr;
Expand Down Expand Up @@ -81,6 +86,7 @@ typename block::cptr block::deserialize(uint32_t version,
std::advance(start, full);
}

message->block_ptr->set_retainer(memory.get_retainer());
return message;
}

Expand Down
52 changes: 52 additions & 0 deletions src/net/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2011-2024 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <bitcoin/network/net/memory.hpp>

#include <memory>
#include <bitcoin/system.hpp>
#include <bitcoin/network/define.hpp>

namespace libbitcoin {
namespace network {

memory::memory() NOEXCEPT
: memory(default_arena::get())
{
}

memory::memory(arena* arena) NOEXCEPT
: arena_(arena)
{
}

arena* memory::get_arena() NOEXCEPT
{
return arena_;
}

retainer::ptr memory::get_retainer() NOEXCEPT
{
// Takes a shared lock on remap_mutex_ until destruct, blocking remap.
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
return std::make_unique<retainer>(remap_mutex_);
BC_POP_WARNING()
}

} // namespace network
} // namespace libbitcoin
28 changes: 28 additions & 0 deletions test/net/memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2011-2024 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../test.hpp"

BOOST_AUTO_TEST_SUITE(memory_tests)

BOOST_AUTO_TEST_CASE(memory_test)
{
BOOST_REQUIRE(true);
}

BOOST_AUTO_TEST_SUITE_END()
Loading