Skip to content

Commit

Permalink
added bls siagntures syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubFornadel committed Sep 19, 2023
1 parent 7367bf3 commit a005a9b
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

#include "common/types.hpp"
#include <bls/bls.h>
#include <libdevcore/RLP.h>

#include <optional>

#include "common/types.hpp"
#include "pillar_chain/pillar_block.hpp"

namespace taraxa {


/** @addtogroup PILLAR_CHAIN
* @{
*/
Expand All @@ -23,7 +24,8 @@ class BlsSignature {

public:
BlsSignature(const dev::RLP& rlp);
BlsSignature(PillarBlock::Hash pillar_block_hash, const libff::alt_bn128_G2& public_key, const libff::alt_bn128_Fr& secret);
BlsSignature(PillarBlock::Hash pillar_block_hash, const libff::alt_bn128_G2& public_key,
const libff::alt_bn128_Fr& secret);

/**
* @brief Validates BLS signature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ class PillarChainManager {
bool addVerifiedBlsSig(const std::shared_ptr<BlsSignature>& signature);

/**
* @brief Get all bls signatures
* @brief Get all bls signatures for specified pillar block
* @return all bls signatures
*/
// std::vector<std::shared_ptr<BlsSignature>> getVerifiedBlsSigs() const;
std::vector<std::shared_ptr<BlsSignature>> getVerifiedBlsSigs(const PillarBlock::Hash pillar_block_hash) const;

private:
std::shared_ptr<DbStorage> db_;
Expand Down
41 changes: 24 additions & 17 deletions libraries/core_libs/consensus/src/pillar_chain/bls_signature.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
#include "pillar_chain/bls_signature.hpp"
#include "common/encoding_rlp.hpp"

#include <libdevcore/SHA3.h>

#include "common/encoding_rlp.hpp"

namespace taraxa {

BlsSignature::BlsSignature(const dev::RLP& rlp) {
// TODO: parse rlp
}
std::string sig_str, pk_str;
util::rlp_tuple(util::RLPDecoderRef(rlp, true), pillar_block_hash_, sig_str, pk_str);

BlsSignature::BlsSignature(PillarBlock::Hash pillar_block_hash, const libff::alt_bn128_G2& public_key, const libff::alt_bn128_Fr& secret) {
pillar_block_hash_ = pillar_block_hash;
public_key_ = public_key;
signature_ = libBLS::Bls::CoreSignAggregated( pillar_block_hash_.toString(), secret );
std::stringstream(sig_str) >> signature_;
std::stringstream(pk_str) >> public_key_;
}

bool BlsSignature::isValid() const {
return libBLS::Bls::CoreVerify( public_key_, pillar_block_hash_.toString(), signature_);
}
BlsSignature::BlsSignature(PillarBlock::Hash pillar_block_hash, const libff::alt_bn128_G2& public_key,
const libff::alt_bn128_Fr& secret)
: pillar_block_hash_(pillar_block_hash),
signature_(libBLS::Bls::CoreSignAggregated(getHash().toString(), secret)),
public_key_(public_key) {}

blk_hash_t BlsSignature::getPillarBlockHash() const {
return pillar_block_hash_;
}
bool BlsSignature::isValid() const { return libBLS::Bls::CoreVerify(public_key_, getHash().toString(), signature_); }

blk_hash_t BlsSignature::getPillarBlockHash() const { return pillar_block_hash_; }

dev::bytes BlsSignature::getRlp() const {
dev::RLPStream s(1);
dev::RLPStream s(3);
s << pillar_block_hash_;
// TODO: add signature into rlp
//s << signature_;
//s << public_key_;

std::stringstream sig_ss;
sig_ss << signature_;
s << sig_ss.str();

std::stringstream pk_ss;
pk_ss << public_key_;
s << pk_ss.str();

return s.invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,28 @@ bool PillarChainManager::isRelevantBlsSig(const std::shared_ptr<BlsSignature> si
bool PillarChainManager::addVerifiedBlsSig(const std::shared_ptr<BlsSignature>& signature) {
std::scoped_lock<std::shared_mutex> lock(mutex_);

// TODO: adjust also last_pillar_block_signatures_weight_
return last_pillar_block_signatures_.emplace(std::make_pair(signature->getHash(), signature)).second;
if (last_pillar_block_signatures_.emplace(std::make_pair(signature->getHash(), signature)).second) {
// TODO: adjust also last_pillar_block_signatures_weight_

return true;
}

return false;
}

std::vector<std::shared_ptr<BlsSignature>> PillarChainManager::getVerifiedBlsSigs(const PillarBlock::Hash pillar_block_hash) const {
std::shared_lock<std::shared_mutex> lock(mutex_);
if (pillar_block_hash != last_pillar_block_->getHash()) {
return {};
}

std::vector<std::shared_ptr<BlsSignature>> signatures;
signatures.reserve(last_pillar_block_signatures_.size());
for (const auto& sig: last_pillar_block_signatures_) {
signatures.push_back(sig.second);
}

return signatures;
}

void PillarChainManager::setNetwork(std::weak_ptr<Network> network) { network_ = std::move(network); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ enum SubprotocolPacketType : uint32_t {
PbftSyncPacket,
GetDagSyncPacket,
BlsSigPacket,
GetBlsSigsBundlePacket,
BlsSigsBundlePacket,

PacketCount
};
Expand Down Expand Up @@ -60,6 +62,12 @@ inline std::string convertPacketTypeToString(SubprotocolPacketType packet_type)
return "GetPbftSyncPacket";
case PbftSyncPacket:
return "PbftSyncPacket";
case BlsSigPacket:
return "BlsSigPacket";
case GetBlsSigsBundlePacket:
return "GetBlsSigsBundlePacket";
case BlsSigsBundlePacket:
return "BlsSigsBundlePacket";
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include "common/packet_handler.hpp"
#include "pillar_chain/pillar_chain_manager.hpp"
#include "common/ext_bls_sig_packet_handler.hpp"

namespace taraxa::network::tarcap {

class BlsSigPacketHandler : public PacketHandler {
class BlsSigPacketHandler : public ExtBlsSigPacketHandler {
public:
BlsSigPacketHandler(const FullNodeConfig& conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
Expand All @@ -24,8 +23,6 @@ class BlsSigPacketHandler : public PacketHandler {

protected:
constexpr static size_t kBlsSigPacketSize{1};

std::shared_ptr<PillarChainManager> pillar_chain_manager_;
};

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

#include "common/ext_bls_sig_packet_handler.hpp"

namespace taraxa::network::tarcap {

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

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

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;

protected:
constexpr static size_t kMaxSignaturesInBundleRlp{250};
};

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

#include "packet_handler.hpp"
#include "pillar_chain/pillar_chain_manager.hpp"

namespace taraxa::network::tarcap {

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

protected:
void processBlsSignature(const std::shared_ptr<BlsSignature>& signature, const std::shared_ptr<TaraxaPeer>& peer);

protected:
std::shared_ptr<PillarChainManager> pillar_chain_manager_;
};

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

#include "common/packet_handler.hpp"
#include "pillar_chain/pillar_block.hpp"
#include "pillar_chain/pillar_chain_manager.hpp"

namespace taraxa::network::tarcap {

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

void requestBlsSigsBundle(PillarBlock::Hash pillar_block_hash, const std::shared_ptr<TaraxaPeer>& peer);

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

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;

protected:
constexpr static size_t kGetBlsSigsPacketSize{1};

std::shared_ptr<PillarChainManager> pillar_chain_manager_;
};

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "network/tarcap/packets_handlers/latest/bls_sig_packet_handler.hpp"

#include "pillar_chain/pillar_chain_manager.hpp"

namespace taraxa::network::tarcap {

BlsSigPacketHandler::BlsSigPacketHandler(const FullNodeConfig &conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<PillarChainManager> pillar_chain_manager,
const addr_t &node_addr, const std::string &logs_prefix)
: PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, logs_prefix + "BLS_SIG_PH"),
pillar_chain_manager_{std::move(pillar_chain_manager)} {}
: ExtBlsSigPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pillar_chain_manager),
node_addr, logs_prefix + "BLS_SIG_PH") {}

void BlsSigPacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadpool::PacketData &packet_data) const {
auto items = packet_data.rlp_.itemCount();
Expand All @@ -19,29 +17,15 @@ void BlsSigPacketHandler::validatePacketRlpFormat([[maybe_unused]] const threadp
}

void BlsSigPacketHandler::process(const threadpool::PacketData &packet_data, const std::shared_ptr<TaraxaPeer> &peer) {
const auto bls_signature = std::make_shared<BlsSignature>(packet_data.rlp_[0]);

if (!pillar_chain_manager_->isRelevantBlsSig(bls_signature)) {
LOG(log_dg_) << "Drop irrelevant signature " << bls_signature->getHash() << " from peer " << peer->getId();
}

if (!bls_signature->isValid()) {
std::ostringstream err_msg;
err_msg << "Invalid signature " << bls_signature->getHash() << " from peer " << peer->getId();
throw MaliciousPeerException(err_msg.str());
}

pillar_chain_manager_->addVerifiedBlsSig(bls_signature);

// Mark bls signature as known for peer
peer->markBlsSigAsKnown(bls_signature->getHash());
const auto bls_signature = std::make_shared<BlsSignature>(packet_data.rlp_);
processBlsSignature(bls_signature, peer);
onNewBlsSig(bls_signature);
}

void BlsSigPacketHandler::onNewBlsSig(const std::shared_ptr<BlsSignature> &signature, bool rebroadcast) {
for (const auto &peer : peers_state_->getAllPeers()) {
if (peer.second->syncing_) {
LOG(log_dg_) << " PBFT vote " << signature << " not sent to " << peer.first << " peer syncing";
LOG(log_dg_) << "BLS signature " << signature->getHash() << " not sent to " << peer.first << " peer syncing";
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "network/tarcap/packets_handlers/latest/bls_sigs_bundle_packet_handler.hpp"

namespace taraxa::network::tarcap {

BlsSigsBundlePacketHandler::BlsSigsBundlePacketHandler(const FullNodeConfig &conf,
std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<PillarChainManager> pillar_chain_manager,
const addr_t &node_addr, const std::string &logs_prefix)
: ExtBlsSigPacketHandler(conf, std::move(peers_state), std::move(packets_stats), std::move(pillar_chain_manager),
node_addr, logs_prefix + "BLS_SIGS_BUNDLE_PH") {}

void BlsSigsBundlePacketHandler::validatePacketRlpFormat(
[[maybe_unused]] const threadpool::PacketData &packet_data) const {
auto items = packet_data.rlp_.itemCount();
if (items == 0 || items > kMaxSignaturesInBundleRlp) {
throw InvalidRlpItemsCountException(packet_data.type_str_, items, kMaxSignaturesInBundleRlp);
}
}

void BlsSigsBundlePacketHandler::process(const threadpool::PacketData &packet_data,
const std::shared_ptr<TaraxaPeer> &peer) {
for (const auto signature_rlp : packet_data.rlp_) {
const auto bls_signature = std::make_shared<BlsSignature>(signature_rlp);
processBlsSignature(bls_signature, peer);
}
}

} // namespace taraxa::network::tarcap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "network/tarcap/packets_handlers/latest/common/ext_bls_sig_packet_handler.hpp"

#include "pillar_chain/pillar_chain_manager.hpp"

namespace taraxa::network::tarcap {

ExtBlsSigPacketHandler::ExtBlsSigPacketHandler(const FullNodeConfig &conf, std::shared_ptr<PeersState> peers_state,
std::shared_ptr<TimePeriodPacketsStats> packets_stats,
std::shared_ptr<PillarChainManager> pillar_chain_manager,
const addr_t &node_addr, const std::string &logs_prefix)
: PacketHandler(conf, std::move(peers_state), std::move(packets_stats), node_addr, logs_prefix + "BLS_SIG_PH"),
pillar_chain_manager_{std::move(pillar_chain_manager)} {}

void ExtBlsSigPacketHandler::processBlsSignature(const std::shared_ptr<BlsSignature> &signature,
const std::shared_ptr<TaraxaPeer> &peer) {
if (!pillar_chain_manager_->isRelevantBlsSig(signature)) {
LOG(log_dg_) << "Drop irrelevant signature " << signature->getHash() << " from peer " << peer->getId();
}

if (!signature->isValid()) {
std::ostringstream err_msg;
err_msg << "Invalid signature " << signature->getHash() << " from peer " << peer->getId();
throw MaliciousPeerException(err_msg.str());
}

pillar_chain_manager_->addVerifiedBlsSig(signature);

// Mark bls signature as known for peer
peer->markBlsSigAsKnown(signature->getHash());
}

} // namespace taraxa::network::tarcap
Loading

0 comments on commit a005a9b

Please sign in to comment.