From 8909159b5e6aae6c213b21643df6d5a16eb90a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Fri, 22 Sep 2023 16:08:09 +0200 Subject: [PATCH] WIP --- nano/core_test/active_transactions.cpp | 4 +-- nano/core_test/block.cpp | 14 ++++----- nano/core_test/block_store.cpp | 4 +-- nano/core_test/message.cpp | 30 ++++++++++---------- nano/core_test/node.cpp | 4 +-- nano/lib/asio.cpp | 24 ++++++++-------- nano/lib/asio.hpp | 13 +++++---- nano/lib/stream.hpp | 20 +++++++++---- nano/node/bootstrap/block_deserializer.hpp | 2 +- nano/node/transport/message_deserializer.cpp | 2 +- nano/node/transport/socket.cpp | 4 +-- nano/secure/buffer.hpp | 6 ++-- nano/secure/ledger.cpp | 2 +- 13 files changed, 70 insertions(+), 59 deletions(-) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index 066fa04eae..8ebe96cedf 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -625,7 +625,7 @@ TEST (active_transactions, dropped_cleanup) auto hash = chain[0]->hash (); // Add to network filter to ensure proper cleanup after the election is dropped - std::vector block_bytes; + nano::vectorbuffer block_bytes; { nano::vectorstream stream (block_bytes); chain[0]->serialize (stream); @@ -761,7 +761,7 @@ TEST (active_transactions, fork_filter_cleanup) .work (*system.work.generate (latest_hash)) .build_shared (); - std::vector send_block_bytes{}; + nano::vectorbuffer send_block_bytes{}; { nano::vectorstream stream{ send_block_bytes }; send1->serialize (stream); diff --git a/nano/core_test/block.cpp b/nano/core_test/block.cpp index 8e1e3c7ea8..bd2f86076b 100644 --- a/nano/core_test/block.cpp +++ b/nano/core_test/block.cpp @@ -51,7 +51,7 @@ TEST (block, send_serialize) .sign (nano::keypair ().prv, 4) .work (5) .build (); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); block1->serialize (stream1); @@ -101,7 +101,7 @@ TEST (block, receive_serialize) .work (4) .build (); nano::keypair key1; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); block1->serialize (stream1); @@ -259,7 +259,7 @@ TEST (send_block, deserialize) .work (5) .build (); ASSERT_EQ (block1->hash (), block1->hash ()); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); block1->serialize (stream1); @@ -285,7 +285,7 @@ TEST (receive_block, deserialize) ASSERT_EQ (block1->hash (), block1->hash ()); block1->hashables.previous = 2; block1->hashables.source = 4; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); block1->serialize (stream1); @@ -310,7 +310,7 @@ TEST (open_block, deserialize) .work (0) .build (); ASSERT_EQ (block1->hash (), block1->hash ()); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); block1->serialize (stream); @@ -334,7 +334,7 @@ TEST (change_block, deserialize) .work (5) .build (); ASSERT_EQ (block1->hash (), block1->hash ()); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); block1->serialize (stream1); @@ -357,7 +357,7 @@ TEST (frontier_req, serialization) request1.start = 1; request1.age = 2; request1.count = 3; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); request1.serialize (stream); diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index 08f0bd9b7c..f3a4161bbc 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -77,7 +77,7 @@ TEST (block_store, block_details_serialization) details1.is_epoch = false; details1.is_receive = true; details1.is_send = false; - std::vector vector; + nano::vectorbuffer vector; { nano::vectorstream stream1 (vector); details1.serialize (stream1); @@ -96,7 +96,7 @@ TEST (block_store, sideband_serialization) sideband1.height = 3; sideband1.successor = 4; sideband1.timestamp = 5; - std::vector vector; + nano::vectorbuffer vector; { nano::vectorstream stream1 (vector); sideband1.serialize (stream1, nano::block_type::receive); diff --git a/nano/core_test/message.cpp b/nano/core_test/message.cpp index fcec2417bc..acfe2e997d 100644 --- a/nano/core_test/message.cpp +++ b/nano/core_test/message.cpp @@ -28,7 +28,7 @@ std::shared_ptr random_block () TEST (message, keepalive_serialization) { nano::keepalive request1{ nano::dev::network_params.network }; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); request1.serialize (stream); @@ -46,7 +46,7 @@ TEST (message, keepalive_deserialize) { nano::keepalive message1{ nano::dev::network_params.network }; message1.peers[0] = nano::endpoint (boost::asio::ip::address_v6::loopback (), 10000); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); message1.serialize (stream); @@ -66,7 +66,7 @@ TEST (message, publish_serialization) auto block = random_block (); nano::publish publish{ nano::dev::network_params.network, block }; ASSERT_EQ (nano::block_type::send, publish.header.block_type ()); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); publish.header.serialize (stream); @@ -114,7 +114,7 @@ TEST (message, confirm_ack_hash_serialization) nano::keypair representative1; auto vote (std::make_shared (representative1.pub, representative1.prv, 0, 0, hashes)); nano::confirm_ack con1{ nano::dev::network_params.network, vote }; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream1 (bytes); con1.serialize (stream1); @@ -145,7 +145,7 @@ TEST (message, confirm_req_serialization) .work (3) .build_shared (); nano::confirm_req req{ nano::dev::network_params.network, block }; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); req.serialize (stream); @@ -173,7 +173,7 @@ TEST (message, confirm_req_hash_serialization) .work (3) .build (); nano::confirm_req req{ nano::dev::network_params.network, block->hash (), block->root () }; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); req.serialize (stream); @@ -225,7 +225,7 @@ TEST (message, confirm_req_hash_batch_serialization) } roots_hashes.push_back (std::make_pair (open->hash (), open->root ())); nano::confirm_req req{ nano::dev::network_params.network, roots_hashes }; - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); req.serialize (stream); @@ -273,7 +273,7 @@ TEST (message, bulk_pull_serialization) { nano::bulk_pull message_in{ nano::dev::network_params.network }; message_in.header.flag_set (nano::message_header::bulk_pull_ascending_flag); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; message_in.serialize (stream); @@ -301,7 +301,7 @@ TEST (message, asc_pull_req_serialization_blocks) original.update_header (); // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -341,7 +341,7 @@ TEST (message, asc_pull_req_serialization_account_info) original.update_header (); // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -385,7 +385,7 @@ TEST (message, asc_pull_ack_serialization_blocks) original.update_header (); // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -434,7 +434,7 @@ TEST (message, asc_pull_ack_serialization_account_info) original.update_header (); // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -473,7 +473,7 @@ TEST (message, node_id_handshake_query_serialization) nano::node_id_handshake original{ nano::dev::network_params.network, query }; // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -505,7 +505,7 @@ TEST (message, node_id_handshake_response_serialization) nano::node_id_handshake original{ nano::dev::network_params.network, std::nullopt, response }; // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); @@ -544,7 +544,7 @@ TEST (message, node_id_handshake_response_v2_serialization) nano::node_id_handshake original{ nano::dev::network_params.network, std::nullopt, response }; // Serialize - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream{ bytes }; original.serialize (stream); diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index ab4408a53d..58cdafbbc7 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -1093,7 +1093,7 @@ TEST (node, fork_no_vote_quorum) ASSERT_FALSE (system.wallet (1)->store.fetch (transaction, key1, key3)); auto vote = std::make_shared (key1, key3, 0, 0, std::vector{ send2->hash () }); nano::confirm_ack confirm{ nano::dev::network_params.network, vote }; - std::vector buffer; + nano::vectorbuffer buffer; { nano::vectorstream stream (buffer); confirm.serialize (stream); @@ -3270,7 +3270,7 @@ TEST (node, unchecked_cleanup) .sign (key.prv, key.pub) .work (*system.work.generate (key.pub)) .build_shared (); - std::vector bytes; + nano::vectorbuffer bytes; { nano::vectorstream stream (bytes); open->serialize (stream); diff --git a/nano/lib/asio.cpp b/nano/lib/asio.cpp index 9dbc97c2e5..e006989a15 100644 --- a/nano/lib/asio.cpp +++ b/nano/lib/asio.cpp @@ -1,29 +1,29 @@ #include -nano::shared_const_buffer::shared_const_buffer (std::vector const & data) : - m_data (std::make_shared> (data)), +nano::shared_const_buffer::shared_const_buffer (nano::vectorbuffer const & data) : + m_data (std::make_shared (data)), m_buffer (boost::asio::buffer (*m_data)) { } -nano::shared_const_buffer::shared_const_buffer (std::vector && data) : - m_data (std::make_shared> (std::move (data))), +nano::shared_const_buffer::shared_const_buffer (nano::vectorbuffer && data) : + m_data (std::make_shared (std::move (data))), m_buffer (boost::asio::buffer (*m_data)) { } -nano::shared_const_buffer::shared_const_buffer (uint8_t data) : - shared_const_buffer (std::vector{ data }) -{ -} +//nano::shared_const_buffer::shared_const_buffer (nano::vectorbuffer::value_type data) : +// shared_const_buffer (nano::vectorbuffer{ data }) +//{ +//} nano::shared_const_buffer::shared_const_buffer (std::string const & data) : - m_data (std::make_shared> (data.begin (), data.end ())), + m_data (std::make_shared (data.begin (), data.end ())), m_buffer (boost::asio::buffer (*m_data)) { } -nano::shared_const_buffer::shared_const_buffer (std::shared_ptr> const & data) : +nano::shared_const_buffer::shared_const_buffer (std::shared_ptr const & data) : m_data (data), m_buffer (boost::asio::buffer (*m_data)) { @@ -44,9 +44,9 @@ std::size_t nano::shared_const_buffer::size () const return m_buffer.size (); } -std::vector nano::shared_const_buffer::to_bytes () const +nano::vectorbuffer nano::shared_const_buffer::to_bytes () const { - std::vector bytes; + nano::vectorbuffer bytes; for (auto const & buffer : *this) { bytes.resize (bytes.size () + buffer.size ()); diff --git a/nano/lib/asio.hpp b/nano/lib/asio.hpp index dcbce2a536..0d3989fc63 100644 --- a/nano/lib/asio.hpp +++ b/nano/lib/asio.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace nano { @@ -10,20 +11,20 @@ class shared_const_buffer using value_type = boost::asio::const_buffer; using const_iterator = boost::asio::const_buffer const *; - explicit shared_const_buffer (std::vector const & data); - explicit shared_const_buffer (uint8_t data); + explicit shared_const_buffer (nano::vectorbuffer const & data); + // explicit shared_const_buffer (nano::vectorbuffer::value_type data); explicit shared_const_buffer (std::string const & data); - explicit shared_const_buffer (std::vector && data); - explicit shared_const_buffer (std::shared_ptr> const & data); + explicit shared_const_buffer (nano::vectorbuffer && data); + explicit shared_const_buffer (std::shared_ptr const & data); boost::asio::const_buffer const * begin () const; boost::asio::const_buffer const * end () const; std::size_t size () const; - std::vector to_bytes () const; + nano::vectorbuffer to_bytes () const; private: - std::shared_ptr> m_data; + std::shared_ptr m_data; boost::asio::const_buffer m_buffer; }; diff --git a/nano/lib/stream.hpp b/nano/lib/stream.hpp index 77bb86185c..ef2ac6f8c1 100644 --- a/nano/lib/stream.hpp +++ b/nano/lib/stream.hpp @@ -3,20 +3,28 @@ #include #include +#include +#include +#include +#include #include +#include namespace nano { -// We operate on streams of uint8_t by convention -using stream = std::basic_streambuf; +using buffer_char_type = char; +using stream = std::basic_streambuf; +using bufferstream = boost::iostreams::stream_buffer>; +using vectorbuffer = std::vector; +using vectorstream = boost::iostreams::stream_buffer>; // Read a raw byte stream the size of `T' and fill value. Returns true if there was an error, false otherwise template bool try_read (nano::stream & stream_a, T & value_a) { static_assert (std::is_standard_layout::value, "Can't stream read non-standard layout types"); - auto amount_read (stream_a.sgetn (reinterpret_cast (&value_a), sizeof (value_a))); + auto amount_read (stream_a.sgetn (reinterpret_cast (&value_a), sizeof (value_a))); return amount_read != sizeof (value_a); } @@ -31,7 +39,7 @@ void read (nano::stream & stream_a, T & value) } } -inline void read (nano::stream & stream_a, std::vector & value_a, size_t size_a) +inline void read (nano::stream & stream_a, vectorbuffer & value_a, size_t size_a) { value_a.resize (size_a); if (stream_a.sgetn (value_a.data (), size_a) != size_a) @@ -44,12 +52,12 @@ template void write (nano::stream & stream_a, T const & value_a) { static_assert (std::is_standard_layout::value, "Can't stream write non-standard layout types"); - auto amount_written (stream_a.sputn (reinterpret_cast (&value_a), sizeof (value_a))); + auto amount_written (stream_a.sputn (reinterpret_cast (&value_a), sizeof (value_a))); (void)amount_written; debug_assert (amount_written == sizeof (value_a)); } -inline void write (nano::stream & stream_a, std::vector const & value_a) +inline void write (nano::stream & stream_a, std::vector const & value_a) { auto amount_written (stream_a.sputn (value_a.data (), value_a.size ())); (void)amount_written; diff --git a/nano/node/bootstrap/block_deserializer.hpp b/nano/node/bootstrap/block_deserializer.hpp index 9e53aa7903..6d4ace7145 100644 --- a/nano/node/bootstrap/block_deserializer.hpp +++ b/nano/node/bootstrap/block_deserializer.hpp @@ -43,7 +43,7 @@ namespace bootstrap */ void received_block (nano::block_type type, callback_type const && callback); - std::shared_ptr> read_buffer; + std::shared_ptr read_buffer; }; } } diff --git a/nano/node/transport/message_deserializer.cpp b/nano/node/transport/message_deserializer.cpp index 661a70c511..1095f2a9b5 100644 --- a/nano/node/transport/message_deserializer.cpp +++ b/nano/node/transport/message_deserializer.cpp @@ -3,7 +3,7 @@ nano::transport::message_deserializer::message_deserializer (nano::network_constants const & network_constants_a, nano::network_filter & publish_filter_a, nano::block_uniquer & block_uniquer_a, nano::vote_uniquer & vote_uniquer_a, read_query read_op) : - read_buffer{ std::make_shared> () }, + read_buffer{ std::make_shared () }, network_constants_m{ network_constants_a }, publish_filter_m{ publish_filter_a }, block_uniquer_m{ block_uniquer_a }, diff --git a/nano/node/transport/socket.cpp b/nano/node/transport/socket.cpp index 629fe3b5fa..112a8f04e4 100644 --- a/nano/node/transport/socket.cpp +++ b/nano/node/transport/socket.cpp @@ -89,7 +89,7 @@ void nano::transport::socket::async_connect (nano::tcp_endpoint const & endpoint })); } -void nano::transport::socket::async_read (std::shared_ptr> const & buffer_a, std::size_t size_a, std::function callback_a) +void nano::transport::socket::async_read (std::shared_ptr const & buffer_a, std::size_t size_a, std::function callback_a) { debug_assert (callback_a); @@ -289,7 +289,7 @@ void nano::transport::socket::ongoing_checkup () }); } -void nano::transport::socket::read_impl (std::shared_ptr> const & data_a, std::size_t size_a, std::function callback_a) +void nano::transport::socket::read_impl (std::shared_ptr const & data_a, std::size_t size_a, std::function callback_a) { // Increase timeout to receive TCP header (idle server socket) auto const prev_timeout = get_default_timeout_value (); diff --git a/nano/secure/buffer.hpp b/nano/secure/buffer.hpp index d1a4511789..98c790d2de 100644 --- a/nano/secure/buffer.hpp +++ b/nano/secure/buffer.hpp @@ -1,13 +1,15 @@ #pragma once +#include + #include #include #include +#include #include namespace nano { -using bufferstream = boost::iostreams::stream_buffer>; -using vectorstream = boost::iostreams::stream_buffer>>; +// TODO: DELETE } diff --git a/nano/secure/ledger.cpp b/nano/secure/ledger.cpp index 94da1d0d39..e11ed704ad 100644 --- a/nano/secure/ledger.cpp +++ b/nano/secure/ledger.cpp @@ -1458,7 +1458,7 @@ bool nano::ledger::migrate_lmdb_to_rocksdb (boost::filesystem::path const & data { auto rocksdb_transaction (rocksdb_store->tx_begin_write ({}, { nano::tables::blocks })); - std::vector vector; + nano::vectorbuffer vector; { nano::vectorstream stream (vector); nano::serialize_block (stream, *i->second.block);