Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added packet handlers interface to fix dynamic pointer casts #2921

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions libraries/core_libs/consensus/include/pbft/pbft_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ namespace taraxa {
* @{
*/

namespace pillar_chain {
class PillarChainManager;
}

class FullNode;
class VoteManager;

enum PbftStates { value_proposal_state = 1, filter_state, certify_state, finish_state, finish_polling_state };
enum class PbftStateRootValidation { Valid = 0, Missing, Invalid };
Expand Down
9 changes: 6 additions & 3 deletions libraries/core_libs/network/include/network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Network {

// METHODS USED IN TESTS ONLY
template <typename PacketHandlerType>
std::shared_ptr<PacketHandlerType> getSpecificHandler() const;
std::shared_ptr<PacketHandlerType> getSpecificHandler(network::SubprotocolPacketType packet_type) const;

dev::p2p::NodeID getNodeId() const;
std::shared_ptr<network::tarcap::TaraxaPeer> getPeer(dev::p2p::NodeID const &id) const;
Expand Down Expand Up @@ -117,6 +117,9 @@ class Network {
// Syncing state
std::shared_ptr<network::tarcap::PbftSyncingState> pbft_syncing_state_;

// Pbft manager
std::shared_ptr<PbftManager> pbft_mgr_;

util::ThreadPool tp_;
std::shared_ptr<dev::p2p::Host> host_;

Expand All @@ -135,8 +138,8 @@ class Network {
};

template <typename PacketHandlerType>
std::shared_ptr<PacketHandlerType> Network::getSpecificHandler() const {
return tarcaps_.begin()->second->getSpecificHandler<PacketHandlerType>();
std::shared_ptr<PacketHandlerType> Network::getSpecificHandler(network::SubprotocolPacketType packet_type) const {
return tarcaps_.begin()->second->getSpecificHandler<PacketHandlerType>(packet_type);
}

} // namespace taraxa
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "common/encoding_rlp.hpp"
#include "common/types.hpp"

namespace taraxa::network::tarcap {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "common/encoding_rlp.hpp"
#include "common/types.hpp"

namespace taraxa::network::tarcap {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ class PacketsHandler {
*/
const std::shared_ptr<BasePacketHandler>& getSpecificHandler(SubprotocolPacketType packet_type) const;

/**
* @brief templated getSpecificHandler method for getting specific packet handler based on
* PacketHandlerType::kPacketType_
* @tparam PacketHandlerType
*
* @return std::shared_ptr<PacketHandlerType>
*/
template <typename PacketHandlerType>
std::shared_ptr<PacketHandlerType> getSpecificHandler();

/**
* @brief Registers packet handler
*
Expand All @@ -47,11 +37,6 @@ class PacketsHandler {
std::unordered_map<SubprotocolPacketType, std::shared_ptr<BasePacketHandler>> packets_handlers_;
};

template <typename PacketHandlerType>
std::shared_ptr<PacketHandlerType> PacketsHandler::getSpecificHandler() {
return std::static_pointer_cast<PacketHandlerType>(getSpecificHandler(PacketHandlerType::kPacketType_));
}

template <typename PacketHandlerType, typename... Args>
void PacketsHandler::registerHandler(Args&&... args) {
assert(packets_handlers_.find(PacketHandlerType::kPacketType_) == packets_handlers_.end());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp"

namespace taraxa::network::tarcap {

class IDagBlockPacketHandler : public ExtSyncingPacketHandler {
public:
IDagBlockPacketHandler(const FullNodeConfig &conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<PbftSyncingState> pbft_syncing_state, std::shared_ptr<PbftChain> pbft_chain,
std::shared_ptr<PbftManager> pbft_mgr, std::shared_ptr<DagManager> dag_mgr,
std::shared_ptr<DbStorage> db, const addr_t &node_addr, const std::string &logs_prefix);

void onNewBlockVerified(const std::shared_ptr<DagBlock> &block, bool proposed, const SharedTransactions &trxs);
virtual void sendBlockWithTransactions(const std::shared_ptr<TaraxaPeer> &peer,
const std::shared_ptr<DagBlock> &block, SharedTransactions &&trxs) = 0;

// Note: Used only in tests
void requestDagBlocks(std::shared_ptr<TaraxaPeer> peer);
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp"

namespace taraxa::network::tarcap {

class IGetPillarVotesBundlePacketHandler : public PacketHandler {
public:
IGetPillarVotesBundlePacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats, const addr_t& node_addr,
const std::string& logs_prefix);

virtual void requestPillarVotesBundle(PbftPeriod period, const blk_hash_t& pillar_block_hash,
const std::shared_ptr<TaraxaPeer>& peer) = 0;
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "network/tarcap/packets_handlers/latest/common/ext_pillar_vote_packet_handler.hpp"

namespace taraxa::network::tarcap {

class IPillarVotePacketHandler : public ExtPillarVotePacketHandler {
public:
IPillarVotePacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<pillar_chain::PillarChainManager> pillar_chain_manager,
const addr_t& node_addr, const std::string& logs_prefix);

void onNewPillarVote(const std::shared_ptr<PillarVote>& vote, bool rebroadcast = false);
virtual void sendPillarVote(const std::shared_ptr<TaraxaPeer>& peer, const std::shared_ptr<PillarVote>& vote) = 0;
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "network/tarcap/packets_handlers/latest/common/ext_syncing_packet_handler.hpp"

namespace taraxa::network::tarcap {

class ISyncPacketHandler : public ExtSyncingPacketHandler {
public:
ISyncPacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<PbftSyncingState> pbft_syncing_state, std::shared_ptr<PbftChain> pbft_chain,
std::shared_ptr<PbftManager> pbft_mgr, std::shared_ptr<DagManager> dag_mgr,
std::shared_ptr<DbStorage> db, const addr_t& node_addr, const std::string& logs_prefix);

/**
* @brief Start syncing pbft if needed
*
*/
void startSyncingPbft();

/**
* @brief Send sync request to the current syncing peer with specified request_period
*
* @param request_period
*
* @return true if sync request was sent, otherwise false
*/
virtual bool syncPeerPbft(PbftPeriod request_period);

void sendStatusToPeers();

virtual bool sendStatus(const dev::p2p::NodeID& node_id, bool initial);

private:
const h256 kGenesisHash;
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
#pragma once

#include "common/packet_handler.hpp"
#include "network/tarcap/packets_handlers/latest/common/packet_handler.hpp"
#include "transaction/transaction.hpp"

namespace taraxa {
class TransactionManager;
enum class TransactionStatus;
} // namespace taraxa
namespace taraxa::network::tarcap {

namespace taraxa::network::tarcap::v4 {

class TestState;

class TransactionPacketHandler : public PacketHandler {
class ITransactionPacketHandler : public PacketHandler {
public:
TransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<TransactionManager> trx_mgr, const addr_t& node_addr,
const std::string& logs_prefix = "TRANSACTION_PH");

/**
* @brief Send transactions
*
* @param peer peer to send transactions to
* @param transactions serialized transactions
*
*/
void sendTransactions(std::shared_ptr<TaraxaPeer> peer,
std::pair<SharedTransactions, std::vector<trx_hash_t>>&& transactions);
ITransactionPacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats, const addr_t& node_addr,
const std::string& logs_prefix);

/**
* @brief Sends batch of transactions to all connected peers
Expand All @@ -37,17 +19,16 @@ class TransactionPacketHandler : public PacketHandler {
*/
void periodicSendTransactions(std::vector<SharedTransactions>&& transactions);

// Packet type that is processed by this handler
static constexpr SubprotocolPacketType kPacketType_ = SubprotocolPacketType::kTransactionPacket;

// 2 items: hashes and transactions
static constexpr uint32_t kTransactionPacketItemCount = 2;

private:
virtual void validatePacketRlpFormat(const threadpool::PacketData& packet_data) const override;
virtual void process(const threadpool::PacketData& packet_data, const std::shared_ptr<TaraxaPeer>& peer) override;
/**
* @brief Send transactions
*
* @param peer peer to send transactions to
* @param transactions serialized transactions
*
*/
virtual void sendTransactions(std::shared_ptr<TaraxaPeer> peer,
std::pair<SharedTransactions, std::vector<trx_hash_t>>&& transactions) = 0;

protected:
/**
* @brief select which transactions and hashes to send to which connected peer
*
Expand All @@ -57,6 +38,7 @@ class TransactionPacketHandler : public PacketHandler {
std::vector<std::pair<std::shared_ptr<TaraxaPeer>, std::pair<SharedTransactions, std::vector<trx_hash_t>>>>
transactionsToSendToPeers(std::vector<SharedTransactions>&& transactions);

private:
/**
* @brief select which transactions and hashes to send to peer
*
Expand All @@ -68,11 +50,6 @@ class TransactionPacketHandler : public PacketHandler {
std::pair<uint32_t, std::pair<SharedTransactions, std::vector<trx_hash_t>>> transactionsToSendToPeer(
std::shared_ptr<TaraxaPeer> peer, const std::vector<SharedTransactions>& transactions,
uint32_t account_start_index);

std::shared_ptr<TransactionManager> trx_mgr_;

std::atomic<uint64_t> received_trx_count_{0};
std::atomic<uint64_t> unique_received_trx_count_{0};
};

} // namespace taraxa::network::tarcap::v4
} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "network/tarcap/packets_handlers/latest/common/ext_votes_packet_handler.hpp"

namespace taraxa::network::tarcap {

class IVotePacketHandler : public ExtVotesPacketHandler {
public:
IVotePacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats, std::shared_ptr<PbftManager> pbft_mgr,
std::shared_ptr<PbftChain> pbft_chain, std::shared_ptr<VoteManager> vote_mgr,
std::shared_ptr<SlashingManager> slashing_manager, const addr_t& node_addr,
const std::string& logs_prefix);

/**
* @brief Sends pbft vote to connected peers
*
* @param vote Votes to send
* @param block block to send - nullptr means no block
* @param rebroadcast - send even of vote i known for the peer
*/
void onNewPbftVote(const std::shared_ptr<PbftVote>& vote, const std::shared_ptr<PbftBlock>& block,
bool rebroadcast = false);

/**
* @brief Sends pbft vote to specified peer
* @param peer
* @param vote
* @param block
*/
virtual void sendPbftVote(const std::shared_ptr<TaraxaPeer>& peer, const std::shared_ptr<PbftVote>& vote,
const std::shared_ptr<PbftBlock>& block);

/**
* @brief Sends pbft votes bundle to connected peers
*
* @param votes Votes to send
* @param rebroadcast if rebroadcast is true, all votes are resent to all peers
* @param exclude_node do not send votes to excluded node
*/
void onNewPbftVotesBundle(const std::vector<std::shared_ptr<PbftVote>>& votes, bool rebroadcast = false,
const std::optional<dev::p2p::NodeID>& exclude_node = {});

/**
* @brief Sends pbft votes bundle to specified peer
* @param peer
* @param vote
* @param block
*/
virtual void sendPbftVotesBundle(const std::shared_ptr<TaraxaPeer>& peer,
std::vector<std::shared_ptr<PbftVote>>&& votes);
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,15 @@

namespace taraxa::network::tarcap {

template <class PacketType>
class ExtPillarVotePacketHandler : public PacketHandler<PacketType> {
class ExtPillarVotePacketHandler : public PacketHandler {
public:
ExtPillarVotePacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<pillar_chain::PillarChainManager> pillar_chain_manager,
const addr_t& node_addr, const std::string& log_channel)
: PacketHandler<PacketType>(conf, std::move(peers_state), std::move(packets_stats), node_addr, log_channel),
pillar_chain_manager_{std::move(pillar_chain_manager)} {}
const addr_t& node_addr, const std::string& log_channel);

protected:
bool processPillarVote(const std::shared_ptr<PillarVote>& vote, const std::shared_ptr<TaraxaPeer>& peer) {
if (!pillar_chain_manager_->isRelevantPillarVote(vote)) {
LOG(this->log_dg_) << "Drop irrelevant pillar vote " << vote->getHash() << ", period " << vote->getPeriod()
<< " from peer " << peer->getId();
return false;
}

if (!pillar_chain_manager_->validatePillarVote(vote)) {
// TODO: enable for mainnet
// std::ostringstream err_msg;
// err_msg << "Invalid pillar vote " << vote->getHash() << " from peer " << peer->getId();
// throw MaliciousPeerException(err_msg.str());
return false;
}

pillar_chain_manager_->addVerifiedPillarVote(vote);

// Mark pillar vote as known for peer
peer->markPillarVoteAsKnown(vote->getHash());
return true;
}
bool processPillarVote(const std::shared_ptr<PillarVote>& vote, const std::shared_ptr<TaraxaPeer>& peer);

protected:
std::shared_ptr<pillar_chain::PillarChainManager> pillar_chain_manager_;
Expand Down
Loading