TARAXA
packets_blocking_mask.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <libp2p/Common.h>
4 
5 #include <map>
6 
9 
11 
13  public:
14  void markPacketAsHardBlocked(const PacketData& blocking_packet, SubprotocolPacketType packet_type_to_block);
15  void markPacketAsHardUnblocked(const PacketData& blocking_packet, SubprotocolPacketType packet_type_to_unblock);
16 
17  void markPacketAsPeerOrderBlocked(const PacketData& blocking_packet, SubprotocolPacketType packet_type_to_block);
18  void markPacketAsPeerOrderUnblocked(const PacketData& blocking_packet, SubprotocolPacketType packet_type_to_unblock);
19 
20  void setDagBlockLevelBeingProcessed(const PacketData& packet);
21  void unsetDagBlockLevelBeingProcessed(const PacketData& packet);
22 
23  void setDagBlockBeingProcessed(const PacketData& packet);
24  void unsetDagBlockBeingProcessed(const PacketData& packet);
25 
26  bool isPacketBlocked(const PacketData& packet_data) const;
27 
28  private:
29  bool isPacketHardBlocked(const PacketData& packet_data) const;
30  bool isPacketPeerOrderBlocked(const PacketData& packet_data) const;
31  bool isDagBlockPacketBlockedByLevel(const PacketData& packet_data) const;
32  bool isDagBlockPacketBlockedBySameDagBlock(const PacketData& packet_data) const;
33  dev::RLP dagBlockFromDagPacket(const PacketData& packet_data) const;
34 
35  std::optional<taraxa::level_t> getSmallestDagLevelBeingProcessed() const;
36 
37  private:
38  // Packets types that are currently hard blocked for processing in another threads due to dependencies,
39  // e.g. syncing packets must be processed synchronously one by one, etc...
40  // Each packet type might be simultaneously blocked by multiple different packets that are being processed. They
41  // are saved in std::unordered_set<PacketData::PacketId> so hard block is released after the last blocking packet
42  // is processed.
43  std::unordered_map<SubprotocolPacketType, std::unordered_set<PacketData::PacketId>> hard_blocked_packet_types_;
44 
45  // Packets types that are blocked only for processing when received from specific peer & after specific
46  // time (order), e.g.: new dag block packet processing is blocked until all transactions packets that were received
47  // before it are processed. This blocking dependency is applied only for the same peer so transaction packet from one
48  // peer does not block new dag block packet from another peer Order of blocks must be preserved, therefore using
49  // std::set<PacketData::PacketId>
50  std::unordered_map<SubprotocolPacketType, std::unordered_map<dev::p2p::NodeID, std::set<PacketData::PacketId>>>
52 
53  // This "blocking dependency" is specific just for DagBlockPacket. Ideally only dag blocks with the same level
54  // should be processed. In reality there are situation when node receives dag block with smaller level than the level
55  // of blocks that are already being processed. In such case these blocks with smaller levels can be processed
56  // concurrently with blocks that have higher level. All new dag blocks with higher level than the lowest level from
57  // all the blocks that currently being processed are blocked for processing In this map are saved pairs of <level,
58  // list>, where:
59  // level - What dag level have blocks that are currently being processed
60  // list - list of "DagBlockPacket" packets (of the same dag block level) that are currently being processed
61  // concurrently
62  // Order of levels must be preserved, therefore using std::map
63  std::map<taraxa::level_t, std::unordered_set<PacketData::PacketId>> processing_dag_levels_;
64 
65  // This "blocking dependency" is specific just for DagBlockPacket. Multiple nodes can send same dag blocks
66  // concurrently, to reduce perofrmance impact only one packet/block will be processsed and others will be waiting.
67  // This map contains dag blocks that are currently processed with the associated packet id
68  std::map<taraxa::sig_t, PacketData::PacketId> processing_dag_blocks_;
69 
70  static constexpr size_t kRequiredDagPacketSizeV3 = 2;
71  static constexpr size_t kDagBlockPosV3 = 1;
72  static constexpr size_t kRequiredDagPacketSizeV2 = 8;
73 };
74 
75 } // namespace taraxa::network::threadpool
Definition: RLP.h:56
Definition: packet_data.hpp:12
Definition: packets_blocking_mask.hpp:12
dev::RLP dagBlockFromDagPacket(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:79
bool isDagBlockPacketBlockedByLevel(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:161
void markPacketAsPeerOrderUnblocked(const PacketData &blocking_packet, SubprotocolPacketType packet_type_to_unblock)
Definition: packets_blocking_mask.cpp:46
void setDagBlockBeingProcessed(const PacketData &packet)
Definition: packets_blocking_mask.cpp:90
void markPacketAsHardUnblocked(const PacketData &blocking_packet, SubprotocolPacketType packet_type_to_unblock)
Definition: packets_blocking_mask.cpp:17
static constexpr size_t kRequiredDagPacketSizeV3
Definition: packets_blocking_mask.hpp:70
std::unordered_map< SubprotocolPacketType, std::unordered_map< dev::p2p::NodeID, std::set< PacketData::PacketId > > > peer_order_blocked_packet_types_
Definition: packets_blocking_mask.hpp:51
std::map< taraxa::sig_t, PacketData::PacketId > processing_dag_blocks_
Definition: packets_blocking_mask.hpp:68
void unsetDagBlockBeingProcessed(const PacketData &packet)
Definition: packets_blocking_mask.cpp:101
std::unordered_map< SubprotocolPacketType, std::unordered_set< PacketData::PacketId > > hard_blocked_packet_types_
Definition: packets_blocking_mask.hpp:43
std::map< taraxa::level_t, std::unordered_set< PacketData::PacketId > > processing_dag_levels_
Definition: packets_blocking_mask.hpp:63
static constexpr size_t kRequiredDagPacketSizeV2
Definition: packets_blocking_mask.hpp:72
bool isPacketBlocked(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:202
bool isPacketHardBlocked(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:151
void unsetDagBlockLevelBeingProcessed(const PacketData &packet)
Definition: packets_blocking_mask.cpp:129
static constexpr size_t kDagBlockPosV3
Definition: packets_blocking_mask.hpp:71
void setDagBlockLevelBeingProcessed(const PacketData &packet)
Definition: packets_blocking_mask.cpp:111
std::optional< taraxa::level_t > getSmallestDagLevelBeingProcessed() const
Definition: packets_blocking_mask.cpp:71
bool isPacketPeerOrderBlocked(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:176
void markPacketAsHardBlocked(const PacketData &blocking_packet, SubprotocolPacketType packet_type_to_block)
Definition: packets_blocking_mask.cpp:7
void markPacketAsPeerOrderBlocked(const PacketData &blocking_packet, SubprotocolPacketType packet_type_to_block)
Definition: packets_blocking_mask.cpp:35
bool isDagBlockPacketBlockedBySameDagBlock(const PacketData &packet_data) const
Definition: packets_blocking_mask.cpp:156
Definition: node_stats.hpp:17
SubprotocolPacketType
SubprotocolPacketType is used in networking layer to differentiate packet types.
Definition: packet_types.hpp:12