Skip to content

Commit 6980451

Browse files
committed
GH-1071 Use msg_type_t instead of a few which constants
1 parent d424891 commit 6980451

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

plugins/net_plugin/net_plugin.cpp

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,31 @@ namespace eosio {
253253

254254
constexpr auto message_header_size = sizeof(uint32_t);
255255

256-
constexpr uint32_t signed_block_which = fc::get_index<net_message, signed_block>(); // see protocol net_message
257-
constexpr uint32_t packed_transaction_which = fc::get_index<net_message, packed_transaction>(); // see protocol net_message
258-
constexpr uint32_t vote_message_which = fc::get_index<net_message, vote_message>(); // see protocol net_message
256+
// see protocol net_message
257+
enum class msg_type_t {
258+
handshake_message = fc::get_index<net_message, handshake_message>(),
259+
chain_size_message = fc::get_index<net_message, chain_size_message>(),
260+
go_away_message = fc::get_index<net_message, go_away_message>(),
261+
time_message = fc::get_index<net_message, time_message>(),
262+
notice_message = fc::get_index<net_message, notice_message>(),
263+
request_message = fc::get_index<net_message, request_message>(),
264+
sync_request_message = fc::get_index<net_message, sync_request_message>(),
265+
signed_block = fc::get_index<net_message, signed_block>(),
266+
packed_transaction = fc::get_index<net_message, packed_transaction>(),
267+
vote_message = fc::get_index<net_message, vote_message>(),
268+
unknown
269+
};
270+
271+
constexpr uint32_t to_index(msg_type_t net_msg) {
272+
static_assert( std::variant_size_v<net_message> == static_cast<uint32_t>(msg_type_t::unknown));
273+
return static_cast<uint32_t>(net_msg);
274+
}
275+
276+
constexpr msg_type_t to_msg_type_t(size_t v) {
277+
static_assert( std::variant_size_v<net_message> == static_cast<size_t>(msg_type_t::unknown));
278+
EOS_ASSERT(v < to_index(msg_type_t::unknown), plugin_exception, "Invalid net_message index: ${v}", ("v", v));
279+
return static_cast<msg_type_t>(v);
280+
}
259281

260282
class connections_manager {
261283
public:
@@ -645,9 +667,9 @@ namespace eosio {
645667
// @param callback must not callback into queued_buffer
646668
bool add_write_queue( const std::shared_ptr<vector<char>>& buff,
647669
std::function<void( boost::system::error_code, std::size_t )> callback,
648-
uint32_t net_message_which ) {
670+
msg_type_t net_msg ) {
649671
fc::lock_guard g( _mtx );
650-
if( net_message_which == packed_transaction_which ) {
672+
if( net_msg == msg_type_t::packed_transaction ) {
651673
_trx_write_queue.push_back( {buff, std::move(callback)} );
652674
} else {
653675
_write_queue.push_back( {buff, std::move(callback)} );
@@ -698,7 +720,7 @@ namespace eosio {
698720
mutable fc::mutex _mtx;
699721
uint32_t _write_queue_size GUARDED_BY(_mtx) {0};
700722
deque<queued_write> _write_queue GUARDED_BY(_mtx);
701-
deque<queued_write> _trx_write_queue GUARDED_BY(_mtx); // trx_write_queue will be sent last
723+
deque<queued_write> _trx_write_queue GUARDED_BY(_mtx); // trx_write_queue will be sent last
702724
deque<queued_write> _out_queue GUARDED_BY(_mtx);
703725

704726
}; // queued_buffer
@@ -972,9 +994,9 @@ namespace eosio {
972994
void blk_send_branch( const block_id_type& msg_head_id );
973995
void blk_send_branch( uint32_t msg_head_num, uint32_t fork_db_root_num, uint32_t head_num );
974996

975-
void enqueue( const net_message &msg );
997+
void enqueue( const net_message& msg );
976998
size_t enqueue_block( const std::vector<char>& sb, uint32_t block_num );
977-
void enqueue_buffer( uint32_t net_message_which,
999+
void enqueue_buffer( msg_type_t net_msg,
9781000
const std::shared_ptr<std::vector<char>>& send_buffer,
9791001
block_num_type block_num,
9801002
go_away_reason close_after_send);
@@ -986,7 +1008,7 @@ namespace eosio {
9861008
void cancel_sync_wait();
9871009
void sync_wait();
9881010

989-
void queue_write(uint32_t net_message_which,
1011+
void queue_write(msg_type_t net_msg,
9901012
const std::shared_ptr<vector<char>>& buff,
9911013
std::function<void(boost::system::error_code, std::size_t)> callback);
9921014
void do_queue_write();
@@ -1551,10 +1573,10 @@ namespace eosio {
15511573
}
15521574

15531575
// called from connection strand
1554-
void connection::queue_write(uint32_t net_message_which,
1576+
void connection::queue_write(msg_type_t net_msg,
15551577
const std::shared_ptr<vector<char>>& buff,
15561578
std::function<void(boost::system::error_code, std::size_t)> callback) {
1557-
if( !buffer_queue.add_write_queue( buff, std::move(callback), net_message_which )) {
1579+
if( !buffer_queue.add_write_queue( buff, std::move(callback), net_msg )) {
15581580
peer_wlog( this, "write_queue full ${s} bytes, giving up on connection", ("s", buffer_queue.write_queue_size()) );
15591581
close();
15601582
return;
@@ -1740,7 +1762,7 @@ namespace eosio {
17401762
}
17411763

17421764
static send_buffer_type create_send_buffer_from_serialized_block( const std::vector<char>& v ) {
1743-
static_assert( signed_block_which == fc::get_index<net_message, signed_block>() );
1765+
constexpr uint32_t signed_block_which = to_index(msg_type_t::signed_block);
17441766

17451767
// match net_message static_variant pack
17461768
const uint32_t which_size = fc::raw::pack_size( unsigned_int( signed_block_which ) );
@@ -1780,7 +1802,8 @@ namespace eosio {
17801802
private:
17811803

17821804
static std::shared_ptr<std::vector<char>> create_send_buffer( const signed_block_ptr& sb ) {
1783-
static_assert( signed_block_which == fc::get_index<net_message, signed_block>() );
1805+
constexpr uint32_t signed_block_which = to_index(msg_type_t::signed_block);
1806+
17841807
// this implementation is to avoid copy of signed_block to net_message
17851808
// matches which of net_message for signed_block
17861809
fc_dlog( logger, "sending block ${bn}", ("bn", sb->block_num()) );
@@ -1807,7 +1830,8 @@ namespace eosio {
18071830
private:
18081831

18091832
static std::shared_ptr<std::vector<char>> create_send_buffer( const packed_transaction_ptr& trx ) {
1810-
static_assert( packed_transaction_which == fc::get_index<net_message, packed_transaction>() );
1833+
constexpr uint32_t packed_transaction_which = to_index(msg_type_t::packed_transaction);
1834+
18111835
// this implementation is to avoid copy of packed_transaction to net_message
18121836
// matches which of net_message for packed_transaction
18131837
return buffer_factory::create_send_buffer( packed_transaction_which, *trx );
@@ -1826,7 +1850,7 @@ namespace eosio {
18261850

18271851
buffer_factory buff_factory;
18281852
const auto& send_buffer = buff_factory.get_send_buffer( m );
1829-
enqueue_buffer( m.index(), send_buffer, 0, close_after_send );
1853+
enqueue_buffer( to_msg_type_t(m.index()), send_buffer, 0, close_after_send );
18301854
}
18311855

18321856
// called from connection strand
@@ -1837,24 +1861,24 @@ namespace eosio {
18371861
block_buffer_factory buff_factory;
18381862
const auto& sb = buff_factory.get_send_buffer( b );
18391863
latest_blk_time = std::chrono::steady_clock::now();
1840-
enqueue_buffer( signed_block_which, sb, block_num, no_reason);
1864+
enqueue_buffer( msg_type_t::signed_block, sb, block_num, no_reason);
18411865
return sb->size();
18421866
}
18431867

18441868
// called from connection strand
1845-
void connection::enqueue_buffer( uint32_t net_message_which,
1869+
void connection::enqueue_buffer( msg_type_t net_msg,
18461870
const std::shared_ptr<std::vector<char>>& send_buffer,
1847-
block_num_type block_num, // only valid for net_message_which == signed_block_which
1871+
block_num_type block_num, // only valid for net_msg == signed_block variant which
18481872
go_away_reason close_after_send)
18491873
{
18501874
connection_ptr self = shared_from_this();
1851-
queue_write(net_message_which, send_buffer,
1852-
[conn{std::move(self)}, close_after_send, net_message_which, block_num](boost::system::error_code ec, std::size_t ) {
1875+
queue_write(net_msg, send_buffer,
1876+
[conn{std::move(self)}, close_after_send, net_msg, block_num](boost::system::error_code ec, std::size_t ) {
18531877
if (ec) {
18541878
fc_elog(logger, "Connection - ${cid} - send failed with: ${e}", ("cid", conn->connection_id)("e", ec.to_string()));
18551879
return;
18561880
}
1857-
if (net_message_which == signed_block_which)
1881+
if (net_msg == msg_type_t::signed_block)
18581882
fc_dlog(logger, "Connection - ${cid} - done sending block ${bn}", ("cid", conn->connection_id)("bn", block_num));
18591883
if (close_after_send != no_reason) {
18601884
fc_ilog( logger, "sent a go away message: ${r}, closing connection ${cid}",
@@ -2685,7 +2709,7 @@ namespace eosio {
26852709
bool has_block = cp->peer_fork_db_root_num >= bnum;
26862710
if( !has_block ) {
26872711
peer_dlog( cp, "bcast block ${b}", ("b", bnum) );
2688-
cp->enqueue_buffer( signed_block_which, sb, bnum, no_reason );
2712+
cp->enqueue_buffer( msg_type_t::signed_block, sb, bnum, no_reason );
26892713
}
26902714
});
26912715
} );
@@ -2699,7 +2723,7 @@ namespace eosio {
26992723
if (cp->protocol_version >= proto_savanna) {
27002724
if (vote_logger.is_enabled(fc::log_level::debug))
27012725
peer_dlog(cp, "sending vote msg");
2702-
cp->enqueue_buffer( vote_message_which, msg, 0, no_reason );
2726+
cp->enqueue_buffer( msg_type_t::vote_message, msg, 0, no_reason );
27032727
}
27042728
});
27052729
return true;
@@ -2721,7 +2745,7 @@ namespace eosio {
27212745
send_buffer_type sb = buff_factory.get_send_buffer( trx );
27222746
fc_dlog( logger, "sending trx: ${id}, to connection - ${cid}", ("id", trx->id())("cid", cp->connection_id) );
27232747
boost::asio::post(cp->strand, [cp, sb{std::move(sb)}]() {
2724-
cp->enqueue_buffer( packed_transaction_which, sb, 0, no_reason );
2748+
cp->enqueue_buffer( msg_type_t::packed_transaction, sb, 0, no_reason );
27252749
} );
27262750
} );
27272751
}
@@ -2990,12 +3014,14 @@ namespace eosio {
29903014
unsigned_int which{};
29913015
fc::raw::unpack( peek_ds, which );
29923016

2993-
if( which == signed_block_which ) {
3017+
msg_type_t net_msg = to_msg_type_t(which.value);
3018+
3019+
if( net_msg == msg_type_t::signed_block ) {
29943020
latest_blk_time = now;
29953021
return process_next_block_message( message_length );
2996-
} else if( which == packed_transaction_which ) {
3022+
} else if( net_msg == msg_type_t::packed_transaction ) {
29973023
return process_next_trx_message( message_length );
2998-
} else if( which == vote_message_which ) {
3024+
} else if( net_msg == msg_type_t::vote_message ) {
29993025
return process_next_vote_message( message_length );
30003026
} else {
30013027
auto ds = pending_message_buffer.create_datastream();

0 commit comments

Comments
 (0)