Skip to content

Commit

Permalink
Squash merge PR nanocurrency#4654: Add is_originator flag to publis…
Browse files Browse the repository at this point in the history
…h messages
  • Loading branch information
gr0vity committed Jul 7, 2024
1 parent dd72bae commit 159c946
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 40 deletions.
90 changes: 71 additions & 19 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ std::shared_ptr<nano::block> random_block ()
}
}

TEST (message, header_version)
{
// Simplest message type
nano::keepalive original{ nano::dev::network_params.network };

// Serialize the original keepalive message
std::vector<uint8_t> bytes;
{
nano::vectorstream stream (bytes);
original.serialize (stream);
}

// Deserialize the byte stream back to a message header
nano::bufferstream stream (bytes.data (), bytes.size ());
bool error = false;
nano::message_header header (error, stream);
ASSERT_FALSE (error);

// Check header versions
ASSERT_EQ (nano::dev::network_params.network.protocol_version_min, header.version_min);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, header.version_using);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, header.version_max);
ASSERT_EQ (nano::message_type::keepalive, header.type);
}

TEST (message, keepalive_serialization)
{
nano::keepalive request1{ nano::dev::network_params.network };
Expand Down Expand Up @@ -62,33 +87,60 @@ TEST (message, keepalive_deserialize)
ASSERT_EQ (message1.peers, message2.peers);
}

TEST (message, publish_serialization)
TEST (message, publish)
{
// Create a random block
auto block = random_block ();
nano::publish publish{ nano::dev::network_params.network, block };
ASSERT_EQ (nano::block_type::send, publish.header.block_type ());
nano::publish original{ nano::dev::network_params.network, block };
ASSERT_FALSE (original.is_originator ());

// Serialize the original publish message
std::vector<uint8_t> bytes;
{
nano::vectorstream stream (bytes);
publish.header.serialize (stream);
}
ASSERT_EQ (8, bytes.size ());
ASSERT_EQ (0x52, bytes[0]);
ASSERT_EQ (0x41, bytes[1]);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, bytes[2]);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, bytes[3]);
ASSERT_EQ (nano::dev::network_params.network.protocol_version_min, bytes[4]);
ASSERT_EQ (static_cast<uint8_t> (nano::message_type::publish), bytes[5]);
ASSERT_EQ (0x00, bytes[6]); // extensions
ASSERT_EQ (static_cast<uint8_t> (nano::block_type::send), bytes[7]);
original.serialize (stream);
}

// Deserialize the byte stream back to a publish message
nano::bufferstream stream (bytes.data (), bytes.size ());
auto error (false);
bool error = false;
nano::message_header header (error, stream);
ASSERT_FALSE (error);
ASSERT_EQ (nano::dev::network_params.network.protocol_version_min, header.version_min);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, header.version_using);
ASSERT_EQ (nano::dev::network_params.network.protocol_version, header.version_max);
ASSERT_EQ (nano::message_type::publish, header.type);
nano::publish deserialized (error, stream, header);
ASSERT_FALSE (error);

// Assert that the original and deserialized messages are equal
ASSERT_EQ (original, deserialized);
ASSERT_EQ (*original.block, *deserialized.block);
ASSERT_EQ (original.is_originator (), deserialized.is_originator ());
}

TEST (message, publish_originator_flag)
{
// Create a random block
auto block = random_block ();
nano::publish original{ nano::dev::network_params.network, block, /* originator */ true };
ASSERT_TRUE (original.is_originator ());

// Serialize the original publish message
std::vector<uint8_t> bytes;
{
nano::vectorstream stream (bytes);
original.serialize (stream);
}

// Deserialize the byte stream back to a publish message
nano::bufferstream stream (bytes.data (), bytes.size ());
bool error = false;
nano::message_header header (error, stream);
ASSERT_FALSE (error);
nano::publish deserialized (error, stream, header);
ASSERT_FALSE (error);

// Assert that the originator flag is set correctly in both the original and deserialized messages
ASSERT_TRUE (deserialized.is_originator ());
ASSERT_EQ (original, deserialized);
ASSERT_EQ (*original.block, *deserialized.block);
}

TEST (message, confirm_header_flags)
Expand Down
1 change: 1 addition & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ enum class detail

// block source
live,
live_originator,
bootstrap,
bootstrap_legacy,
unchecked,
Expand Down
2 changes: 2 additions & 0 deletions nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ nano::block_processor::block_processor (nano::node & node_a) :
switch (origin.source)
{
case nano::block_source::live:
case nano::block_source::live_originator:
return config.max_peer_queue;
default:
return config.max_system_queue;
Expand All @@ -64,6 +65,7 @@ nano::block_processor::block_processor (nano::node & node_a) :
switch (origin.source)
{
case nano::block_source::live:
case nano::block_source::live_originator:
return config.priority_live;
case nano::block_source::bootstrap:
case nano::block_source::bootstrap_legacy:
Expand Down
15 changes: 8 additions & 7 deletions nano/node/blockprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum class block_source
{
unknown = 0,
live,
live_originator,
bootstrap,
bootstrap_legacy,
unchecked,
Expand Down Expand Up @@ -67,10 +68,10 @@ class block_processor final
class context
{
public:
context (std::shared_ptr<nano::block> block, block_source source);
context (std::shared_ptr<nano::block> block, nano::block_source source);

std::shared_ptr<nano::block> const block;
block_source const source;
nano::block_source const source;
std::chrono::steady_clock::time_point const arrival{ std::chrono::steady_clock::now () };

public:
Expand All @@ -85,16 +86,16 @@ class block_processor final
};

public:
block_processor (nano::node &);
explicit block_processor (nano::node &);
~block_processor ();

void start ();
void stop ();

std::size_t size () const;
std::size_t size (block_source) const;
bool add (std::shared_ptr<nano::block> const &, block_source = block_source::live, std::shared_ptr<nano::transport::channel> const & channel = nullptr);
std::optional<nano::block_status> add_blocking (std::shared_ptr<nano::block> const & block, block_source);
std::size_t size (nano::block_source) const;
bool add (std::shared_ptr<nano::block> const &, nano::block_source = nano::block_source::live, std::shared_ptr<nano::transport::channel> const & channel = nullptr);
std::optional<nano::block_status> add_blocking (std::shared_ptr<nano::block> const & block, nano::block_source);
void force (std::shared_ptr<nano::block> const &);
bool should_log ();

Expand Down Expand Up @@ -127,7 +128,7 @@ class block_processor final
nano::node & node;

private:
nano::fair_queue<context, block_source> queue;
nano::fair_queue<context, nano::block_source> queue;

std::chrono::steady_clock::time_point next_log;

Expand Down
5 changes: 3 additions & 2 deletions nano/node/message_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ void nano::message_processor::run_batch (nano::unique_lock<nano::mutex> & lock)

namespace
{
// TODO: This was moved, so compare with latest develop before merging to avoid merge bugs
class process_visitor : public nano::message_visitor
{
public:
Expand All @@ -184,7 +183,9 @@ class process_visitor : public nano::message_visitor

void publish (nano::publish const & message) override
{
bool added = node.block_processor.add (message.block, nano::block_source::live, channel);
// Put blocks that are being initally broadcasted in a separate queue, so that they won't have to compete with rebroadcasted blocks
// Both queues have the same priority and size, so the potential for exploiting this is limited
bool added = node.block_processor.add (message.block, message.is_originator () ? nano::block_source::live_originator : nano::block_source::live, channel);
if (!added)
{
node.network.publish_filter.clear (message.digest);
Expand Down
10 changes: 9 additions & 1 deletion nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,12 @@ nano::publish::publish (bool & error_a, nano::stream & stream_a, nano::message_h
}
}

nano::publish::publish (nano::network_constants const & constants, std::shared_ptr<nano::block> const & block_a) :
nano::publish::publish (nano::network_constants const & constants, std::shared_ptr<nano::block> const & block_a, bool is_originator_a) :
message (constants, nano::message_type::publish),
block (block_a)
{
header.block_type_set (block->type ());
header.flag_set (originator_flag, is_originator_a);
}

void nano::publish::serialize (nano::stream & stream_a) const
Expand Down Expand Up @@ -465,11 +466,17 @@ bool nano::publish::operator== (nano::publish const & other_a) const
return *block == *other_a.block;
}

bool nano::publish::is_originator () const
{
return header.flag_test (originator_flag);
}

void nano::publish::operator() (nano::object_stream & obs) const
{
nano::message::operator() (obs); // Write common data

obs.write ("block", block);
obs.write ("originator", is_originator ());
}

/*
Expand Down Expand Up @@ -682,6 +689,7 @@ void nano::confirm_ack::operator() (nano::object_stream & obs) const
nano::message::operator() (obs); // Write common data

obs.write ("vote", vote);
obs.write ("rebroadcasted", is_rebroadcasted ());
}

/*
Expand Down
11 changes: 9 additions & 2 deletions nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,23 @@ class keepalive final : public message
*
* Header extensions:
* - [0x0f00] Block type: Identifies the specific type of the block.
* - [0x0004] Originator flag
*/
class publish final : public message
{
public:
publish (bool &, nano::stream &, nano::message_header const &, nano::uint128_t const & = 0, nano::block_uniquer * = nullptr);
publish (nano::network_constants const & constants, std::shared_ptr<nano::block> const &);
void visit (nano::message_visitor &) const override;
publish (nano::network_constants const & constants, std::shared_ptr<nano::block> const &, bool is_originator = false);

void serialize (nano::stream &) const override;
bool deserialize (nano::stream &, nano::block_uniquer * = nullptr);
void visit (nano::message_visitor &) const override;
bool operator== (nano::publish const &) const;

static uint8_t constexpr originator_flag = 2; // 0x0004
bool is_originator () const;

public: // Payload
std::shared_ptr<nano::block> block;
nano::uint128_t digest{ 0 };

Expand Down
18 changes: 9 additions & 9 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,22 @@ void nano::network::flood_keepalive_self (float const scale_a)
flood_message (message, nano::transport::buffer_drop_policy::limiter, scale_a);
}

void nano::network::flood_block (std::shared_ptr<nano::block> const & block_a, nano::transport::buffer_drop_policy const drop_policy_a)
void nano::network::flood_block (std::shared_ptr<nano::block> const & block, nano::transport::buffer_drop_policy const drop_policy)
{
nano::publish message (node.network_params.network, block_a);
flood_message (message, drop_policy_a);
nano::publish message{ node.network_params.network, block };
flood_message (message, drop_policy);
}

void nano::network::flood_block_initial (std::shared_ptr<nano::block> const & block_a)
void nano::network::flood_block_initial (std::shared_ptr<nano::block> const & block)
{
nano::publish message (node.network_params.network, block_a);
for (auto const & i : node.rep_crawler.principal_representatives ())
nano::publish message{ node.network_params.network, block, /* is_originator */ true };
for (auto const & rep : node.rep_crawler.principal_representatives ())
{
i.channel->send (message, nullptr, nano::transport::buffer_drop_policy::no_limiter_drop);
rep.channel->send (message, nullptr, nano::transport::buffer_drop_policy::no_limiter_drop);
}
for (auto & i : list_non_pr (fanout (1.0)))
for (auto & peer : list_non_pr (fanout (1.0)))
{
i->send (message, nullptr, nano::transport::buffer_drop_policy::no_limiter_drop);
peer->send (message, nullptr, nano::transport::buffer_drop_policy::no_limiter_drop);
}
}

Expand Down

0 comments on commit 159c946

Please sign in to comment.