Skip to content

Commit

Permalink
chore: improve sync-gossip transition
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrankovi committed Apr 12, 2023
1 parent aa55889 commit bcc88d8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
19 changes: 14 additions & 5 deletions libraries/core_libs/consensus/src/pbft/pbft_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ bool PbftManager::tryPushCertVotesBlock() {
auto pbft_block = getValidPbftProposedBlock(current_pbft_period, certified_block_hash);
if (!pbft_block) {
LOG(log_er_) << "Invalid certified block " << certified_block_hash;
auto net = network_.lock();
// If block/reward votes are missing but block is cert voted other nodes probably advanced, sync
if (net) {
net->restartSyncingPbft();
}
return false;
}

Expand Down Expand Up @@ -664,12 +669,16 @@ bool PbftManager::stateOperations_() {
// Process synced blocks
pushSyncedPbftBlocksIntoChain();

// (Re)broadcast votes if needed
broadcastVotes();
auto net = network_.lock();
// Only broadcast votes and try to push cert voted block if node is not syncing
if (net && !net->pbft_syncing()) {
// (Re)broadcast votes if needed
broadcastVotes();

// Check if there is 2t+1 cert votes for some valid block, if so - push it into the chain
if (tryPushCertVotesBlock()) {
return true;
// Check if there is 2t+1 cert votes for some valid block, if so - push it into the chain
if (tryPushCertVotesBlock()) {
return true;
}
}

// Check if there is 2t+1 next votes for some valid block, if so - advance round
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GetPbftSyncPacketHandler final : public PacketHandler {
std::shared_ptr<VoteManager> vote_mgr, std::shared_ptr<DbStorage> db,
const addr_t& node_addr);

void sendPbftBlocks(dev::p2p::NodeID const& peer_id, PbftPeriod from_period, size_t blocks_to_transfer,
void sendPbftBlocks(const std::shared_ptr<TaraxaPeer>& peer, PbftPeriod from_period, size_t blocks_to_transfer,
bool pbft_chain_synced);

// Packet type that is processed by this handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ void GetPbftSyncPacketHandler::process(const PacketData &packet_data,
}
LOG(log_tr_) << "Will send " << blocks_to_transfer << " PBFT blocks to " << packet_data.from_node_id_;

sendPbftBlocks(packet_data.from_node_id_, height_to_sync, blocks_to_transfer, pbft_chain_synced);
sendPbftBlocks(peer, height_to_sync, blocks_to_transfer, pbft_chain_synced);
}

// api for pbft syncing
void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, PbftPeriod from_period,
void GetPbftSyncPacketHandler::sendPbftBlocks(const std::shared_ptr<TaraxaPeer> &peer, PbftPeriod from_period,
size_t blocks_to_transfer, bool pbft_chain_synced) {
const auto &peer_id = peer->getId();
LOG(log_tr_) << "sendPbftBlocks: peer want to sync from pbft chain height " << from_period << ", will send at most "
<< blocks_to_transfer << " pbft blocks to " << peer_id;

Expand Down Expand Up @@ -95,6 +96,9 @@ void GetPbftSyncPacketHandler::sendPbftBlocks(dev::p2p::NodeID const &peer_id, P
}
LOG(log_dg_) << "Sending PbftSyncPacket period " << block_period << " to " << peer_id;
sealAndSend(peer_id, SubprotocolPacketType::PbftSyncPacket, std::move(s));
if (pbft_chain_synced && last_block) {
peer->syncing_ = false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ void VotePacketHandler::process(const PacketData &packet_data, const std::shared
LOG(log_dg_) << "Received PBFT vote " << vote->getHash();
}

// Update peer's max chain size
if (peer_chain_size.has_value() && *peer_chain_size > peer->pbft_chain_size_) {
peer->pbft_chain_size_ = *peer_chain_size;
}

const auto vote_hash = vote->getHash();

if (!isPbftRelevantVote(vote)) {
Expand Down Expand Up @@ -70,11 +75,6 @@ void VotePacketHandler::process(const PacketData &packet_data, const std::shared
// Do not mark it before, as peers have small caches of known votes. Only mark gossiping votes
peer->markVoteAsKnown(vote_hash);
onNewPbftVote(vote, pbft_block);

// Update peer's max chain size
if (peer_chain_size.has_value() && vote->getVoter() == peer->getId() && *peer_chain_size > peer->pbft_chain_size_) {
peer->pbft_chain_size_ = *peer_chain_size;
}
}

void VotePacketHandler::onNewPbftVote(const std::shared_ptr<Vote> &vote, const std::shared_ptr<PbftBlock> &block,
Expand Down

0 comments on commit bcc88d8

Please sign in to comment.