Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Sep 23, 2023
1 parent 9bbbc08 commit 8909159
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 59 deletions.
4 changes: 2 additions & 2 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> block_bytes;
nano::vectorbuffer block_bytes;
{
nano::vectorstream stream (block_bytes);
chain[0]->serialize (stream);
Expand Down Expand Up @@ -761,7 +761,7 @@ TEST (active_transactions, fork_filter_cleanup)
.work (*system.work.generate (latest_hash))
.build_shared ();

std::vector<uint8_t> send_block_bytes{};
nano::vectorbuffer send_block_bytes{};
{
nano::vectorstream stream{ send_block_bytes };
send1->serialize (stream);
Expand Down
14 changes: 7 additions & 7 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEST (block, send_serialize)
.sign (nano::keypair ().prv, 4)
.work (5)
.build ();
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
block1->serialize (stream1);
Expand Down Expand Up @@ -101,7 +101,7 @@ TEST (block, receive_serialize)
.work (4)
.build ();
nano::keypair key1;
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
block1->serialize (stream1);
Expand Down Expand Up @@ -259,7 +259,7 @@ TEST (send_block, deserialize)
.work (5)
.build ();
ASSERT_EQ (block1->hash (), block1->hash ());
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
block1->serialize (stream1);
Expand All @@ -285,7 +285,7 @@ TEST (receive_block, deserialize)
ASSERT_EQ (block1->hash (), block1->hash ());
block1->hashables.previous = 2;
block1->hashables.source = 4;
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
block1->serialize (stream1);
Expand All @@ -310,7 +310,7 @@ TEST (open_block, deserialize)
.work (0)
.build ();
ASSERT_EQ (block1->hash (), block1->hash ());
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
block1->serialize (stream);
Expand All @@ -334,7 +334,7 @@ TEST (change_block, deserialize)
.work (5)
.build ();
ASSERT_EQ (block1->hash (), block1->hash ());
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
block1->serialize (stream1);
Expand All @@ -357,7 +357,7 @@ TEST (frontier_req, serialization)
request1.start = 1;
request1.age = 2;
request1.count = 3;
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
request1.serialize (stream);
Expand Down
4 changes: 2 additions & 2 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TEST (block_store, block_details_serialization)
details1.is_epoch = false;
details1.is_receive = true;
details1.is_send = false;
std::vector<uint8_t> vector;
nano::vectorbuffer vector;
{
nano::vectorstream stream1 (vector);
details1.serialize (stream1);
Expand All @@ -96,7 +96,7 @@ TEST (block_store, sideband_serialization)
sideband1.height = 3;
sideband1.successor = 4;
sideband1.timestamp = 5;
std::vector<uint8_t> vector;
nano::vectorbuffer vector;
{
nano::vectorstream stream1 (vector);
sideband1.serialize (stream1, nano::block_type::receive);
Expand Down
30 changes: 15 additions & 15 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::shared_ptr<nano::block> random_block ()
TEST (message, keepalive_serialization)
{
nano::keepalive request1{ nano::dev::network_params.network };
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
request1.serialize (stream);
Expand All @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
message1.serialize (stream);
Expand All @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
publish.header.serialize (stream);
Expand Down Expand Up @@ -114,7 +114,7 @@ TEST (message, confirm_ack_hash_serialization)
nano::keypair representative1;
auto vote (std::make_shared<nano::vote> (representative1.pub, representative1.prv, 0, 0, hashes));
nano::confirm_ack con1{ nano::dev::network_params.network, vote };
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream1 (bytes);
con1.serialize (stream1);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
req.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
req.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
req.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
message_in.serialize (stream);
Expand Down Expand Up @@ -301,7 +301,7 @@ TEST (message, asc_pull_req_serialization_blocks)
original.update_header ();

// Serialize
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -341,7 +341,7 @@ TEST (message, asc_pull_req_serialization_account_info)
original.update_header ();

// Serialize
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -385,7 +385,7 @@ TEST (message, asc_pull_ack_serialization_blocks)
original.update_header ();

// Serialize
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -434,7 +434,7 @@ TEST (message, asc_pull_ack_serialization_account_info)
original.update_header ();

// Serialize
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
Expand Down Expand Up @@ -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<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
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 @@ -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<nano::vote> (key1, key3, 0, 0, std::vector<nano::block_hash>{ send2->hash () });
nano::confirm_ack confirm{ nano::dev::network_params.network, vote };
std::vector<uint8_t> buffer;
nano::vectorbuffer buffer;
{
nano::vectorstream stream (buffer);
confirm.serialize (stream);
Expand Down Expand Up @@ -3270,7 +3270,7 @@ TEST (node, unchecked_cleanup)
.sign (key.prv, key.pub)
.work (*system.work.generate (key.pub))
.build_shared ();
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
{
nano::vectorstream stream (bytes);
open->serialize (stream);
Expand Down
24 changes: 12 additions & 12 deletions nano/lib/asio.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#include <nano/lib/asio.hpp>

nano::shared_const_buffer::shared_const_buffer (std::vector<uint8_t> const & data) :
m_data (std::make_shared<std::vector<uint8_t>> (data)),
nano::shared_const_buffer::shared_const_buffer (nano::vectorbuffer const & data) :
m_data (std::make_shared<nano::vectorbuffer> (data)),
m_buffer (boost::asio::buffer (*m_data))
{
}

nano::shared_const_buffer::shared_const_buffer (std::vector<uint8_t> && data) :
m_data (std::make_shared<std::vector<uint8_t>> (std::move (data))),
nano::shared_const_buffer::shared_const_buffer (nano::vectorbuffer && data) :
m_data (std::make_shared<nano::vectorbuffer> (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<uint8_t>{ 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<std::vector<uint8_t>> (data.begin (), data.end ())),
m_data (std::make_shared<nano::vectorbuffer> (data.begin (), data.end ())),
m_buffer (boost::asio::buffer (*m_data))
{
}

nano::shared_const_buffer::shared_const_buffer (std::shared_ptr<std::vector<uint8_t>> const & data) :
nano::shared_const_buffer::shared_const_buffer (std::shared_ptr<nano::vectorbuffer> const & data) :
m_data (data),
m_buffer (boost::asio::buffer (*m_data))
{
Expand All @@ -44,9 +44,9 @@ std::size_t nano::shared_const_buffer::size () const
return m_buffer.size ();
}

std::vector<uint8_t> nano::shared_const_buffer::to_bytes () const
nano::vectorbuffer nano::shared_const_buffer::to_bytes () const
{
std::vector<uint8_t> bytes;
nano::vectorbuffer bytes;
for (auto const & buffer : *this)
{
bytes.resize (bytes.size () + buffer.size ());
Expand Down
13 changes: 7 additions & 6 deletions nano/lib/asio.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <nano/boost/asio/write.hpp>
#include <nano/lib/stream.hpp>

namespace nano
{
Expand All @@ -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<uint8_t> 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<uint8_t> && data);
explicit shared_const_buffer (std::shared_ptr<std::vector<uint8_t>> const & data);
explicit shared_const_buffer (nano::vectorbuffer && data);
explicit shared_const_buffer (std::shared_ptr<nano::vectorbuffer> const & data);

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

std::size_t size () const;
std::vector<uint8_t> to_bytes () const;
nano::vectorbuffer to_bytes () const;

private:
std::shared_ptr<std::vector<uint8_t>> m_data;
std::shared_ptr<nano::vectorbuffer> m_data;
boost::asio::const_buffer m_buffer;
};

Expand Down
20 changes: 14 additions & 6 deletions nano/lib/stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
#include <nano/lib/utility.hpp>

#include <boost/endian/conversion.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/stream_buffer.hpp>

#include <array>
#include <streambuf>
#include <vector>

namespace nano
{
// We operate on streams of uint8_t by convention
using stream = std::basic_streambuf<uint8_t>;
using buffer_char_type = char;
using stream = std::basic_streambuf<buffer_char_type>;
using bufferstream = boost::iostreams::stream_buffer<boost::iostreams::basic_array_source<buffer_char_type>>;
using vectorbuffer = std::vector<buffer_char_type>;
using vectorstream = boost::iostreams::stream_buffer<boost::iostreams::back_insert_device<vectorbuffer>>;

// Read a raw byte stream the size of `T' and fill value. Returns true if there was an error, false otherwise
template <typename T>
bool try_read (nano::stream & stream_a, T & value_a)
{
static_assert (std::is_standard_layout<T>::value, "Can't stream read non-standard layout types");
auto amount_read (stream_a.sgetn (reinterpret_cast<uint8_t *> (&value_a), sizeof (value_a)));
auto amount_read (stream_a.sgetn (reinterpret_cast<buffer_char_type *> (&value_a), sizeof (value_a)));
return amount_read != sizeof (value_a);
}

Expand All @@ -31,7 +39,7 @@ void read (nano::stream & stream_a, T & value)
}
}

inline void read (nano::stream & stream_a, std::vector<uint8_t> & 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)
Expand All @@ -44,12 +52,12 @@ template <typename T>
void write (nano::stream & stream_a, T const & value_a)
{
static_assert (std::is_standard_layout<T>::value, "Can't stream write non-standard layout types");
auto amount_written (stream_a.sputn (reinterpret_cast<uint8_t const *> (&value_a), sizeof (value_a)));
auto amount_written (stream_a.sputn (reinterpret_cast<buffer_char_type const *> (&value_a), sizeof (value_a)));
(void)amount_written;
debug_assert (amount_written == sizeof (value_a));
}

inline void write (nano::stream & stream_a, std::vector<uint8_t> const & value_a)
inline void write (nano::stream & stream_a, std::vector<char> const & value_a)
{
auto amount_written (stream_a.sputn (value_a.data (), value_a.size ()));
(void)amount_written;
Expand Down
Loading

0 comments on commit 8909159

Please sign in to comment.