-
Notifications
You must be signed in to change notification settings - Fork 40
parachain bitfield signer #1336
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfa4376
scale bitvec
turuslan 4fabc9d
signed bitfield
turuslan 657ab4c
validator signer
turuslan cb61d4a
bitfield store
turuslan 4d2f38c
bitfield signer
turuslan f716fe6
comments
turuslan 96feba2
fix destructor
turuslan ffac04e
fix cmake
turuslan 7f8831a
session keys para
turuslan e247b8b
use session keys
turuslan 4f900b1
Merge remote-tracking branch 'origin/master' into parachain/bitfield
turuslan af994b1
fix
turuslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
HunterGate( | ||
URL "https://github.com/soramitsu/soramitsu-hunter/archive/52069249f8ed86f30d3f473c32de44c64270d186.zip" | ||
SHA1 "4edea9f8976988f8f34ca59c5341a2297315a941" | ||
URL "https://github.com/soramitsu/soramitsu-hunter/archive/refs/tags/v0.23.257-soramitsu31.zip" | ||
SHA1 "a13dcf6dae9e926441715e67c409838630f6f3e0" | ||
LOCAL | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "parachain/availability/bitfield/signer.hpp" | ||
|
||
#include "log/logger.hpp" | ||
#include "primitives/block_header.hpp" | ||
|
||
namespace kagome::parachain { | ||
constexpr std::chrono::milliseconds kDelay{1500}; | ||
|
||
namespace { | ||
inline auto log() { | ||
return log::createLogger("BitfieldSigner"); | ||
} | ||
} // namespace | ||
|
||
BitfieldSigner::BitfieldSigner( | ||
std::shared_ptr<crypto::Hasher> hasher, | ||
std::shared_ptr<ValidatorSignerFactory> signer_factory, | ||
std::shared_ptr<libp2p::basic::Scheduler> scheduler, | ||
std::shared_ptr<runtime::ParachainHost> parachain_api, | ||
std::shared_ptr<AvailabilityStore> store, | ||
std::shared_ptr<BitfieldStore> bitfield_store) | ||
: hasher_{std::move(hasher)}, | ||
signer_factory_{std::move(signer_factory)}, | ||
scheduler_{std::move(scheduler)}, | ||
parachain_api_{std::move(parachain_api)}, | ||
store_{std::move(store)}, | ||
bitfield_store_{std::move(bitfield_store)} {} | ||
|
||
void BitfieldSigner::start( | ||
std::shared_ptr<primitives::events::ChainSubscriptionEngine> | ||
chain_sub_engine) { | ||
chain_sub_ = std::make_shared<primitives::events::ChainEventSubscriber>( | ||
chain_sub_engine); | ||
chain_sub_->subscribe(chain_sub_->generateSubscriptionSetId(), | ||
primitives::events::ChainEventType::kNewHeads); | ||
chain_sub_->setCallback( | ||
[weak = weak_from_this()]( | ||
subscription::SubscriptionSetId, | ||
auto &&, | ||
primitives::events::ChainEventType, | ||
const primitives::events::ChainEventParams &event) { | ||
if (auto self = weak.lock()) { | ||
auto r = self->onBlock(self->hasher_->blake2b_256( | ||
scale::encode( | ||
boost::get<primitives::events::HeadsEventParams>(event)) | ||
.value())); | ||
if (r.has_error()) { | ||
SL_WARN(log(), "onBlock error {}", r.error()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
outcome::result<void> BitfieldSigner::sign(const ValidatorSigner &signer) { | ||
auto &relay_parent = signer.context().relay_parent; | ||
scale::BitVec bitfield; | ||
OUTCOME_TRY(cores, parachain_api_->availability_cores(relay_parent)); | ||
bitfield.bits.reserve(cores.size()); | ||
for (auto &core : cores) { | ||
auto occupied = boost::get<runtime::OccupiedCore>(&core); | ||
bitfield.bits.push_back(occupied != nullptr | ||
&& store_->hasChunk(occupied->candidate_hash, | ||
signer.validatorIndex())); | ||
} | ||
|
||
OUTCOME_TRY(signed_bitfield, signer.sign(bitfield)); | ||
bitfield_store_->putBitfield(relay_parent, signed_bitfield); | ||
// TODO(turuslan): broadcast | ||
return outcome::success(); | ||
} | ||
|
||
outcome::result<void> BitfieldSigner::onBlock(const BlockHash &relay_parent) { | ||
OUTCOME_TRY(signer, signer_factory_->at(relay_parent)); | ||
if (not signer.has_value()) { | ||
return outcome::success(); | ||
} | ||
scheduler_->schedule( | ||
[weak = weak_from_this(), relay_parent, signer{std::move(*signer)}]() { | ||
if (auto self = weak.lock()) { | ||
auto r = self->sign(signer); | ||
if (r.has_error()) { | ||
SL_WARN(log(), "sign error {}", r.error()); | ||
} | ||
} | ||
}, | ||
kDelay); | ||
return outcome::success(); | ||
} | ||
} // namespace kagome::parachain |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_SIGNER_HPP | ||
#define KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_SIGNER_HPP | ||
|
||
#include <libp2p/basic/scheduler.hpp> | ||
|
||
#include "crypto/hasher.hpp" | ||
#include "parachain/availability/bitfield/store.hpp" | ||
#include "parachain/availability/store/store.hpp" | ||
#include "parachain/validator/signer.hpp" | ||
#include "primitives/event_types.hpp" | ||
#include "runtime/runtime_api/parachain_host.hpp" | ||
|
||
namespace kagome::parachain { | ||
/// Signs, stores and broadcasts bitfield for every new head. | ||
class BitfieldSigner : public std::enable_shared_from_this<BitfieldSigner> { | ||
public: | ||
BitfieldSigner(std::shared_ptr<crypto::Hasher> hasher, | ||
std::shared_ptr<ValidatorSignerFactory> signer_factory, | ||
std::shared_ptr<libp2p::basic::Scheduler> scheduler, | ||
std::shared_ptr<runtime::ParachainHost> parachain_api, | ||
std::shared_ptr<AvailabilityStore> store, | ||
std::shared_ptr<BitfieldStore> bitfield_store); | ||
|
||
/// Subscribes to new heads. | ||
void start(std::shared_ptr<primitives::events::ChainSubscriptionEngine> | ||
chain_sub_engine); | ||
|
||
/// Sign bitfield for given block. | ||
outcome::result<void> sign(const ValidatorSigner &signer); | ||
|
||
private: | ||
using BlockHash = primitives::BlockHash; | ||
|
||
outcome::result<void> onBlock(const BlockHash &relay_parent); | ||
|
||
std::shared_ptr<crypto::Hasher> hasher_; | ||
std::shared_ptr<ValidatorSignerFactory> signer_factory_; | ||
std::shared_ptr<libp2p::basic::Scheduler> scheduler_; | ||
std::shared_ptr<runtime::ParachainHost> parachain_api_; | ||
std::shared_ptr<AvailabilityStore> store_; | ||
std::shared_ptr<BitfieldStore> bitfield_store_; | ||
std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_; | ||
}; | ||
} // namespace kagome::parachain | ||
|
||
#endif // KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_SIGNER_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_STORE_HPP | ||
#define KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_STORE_HPP | ||
|
||
#include "network/types/collator_messages.hpp" | ||
|
||
namespace kagome::parachain { | ||
/// Stores bitfields signed by validators. | ||
class BitfieldStore { | ||
public: | ||
using BlockHash = primitives::BlockHash; | ||
using SignedBitfield = network::SignedBitfield; | ||
|
||
virtual ~BitfieldStore() = default; | ||
|
||
/// Store bitfield at given block. | ||
virtual void putBitfield(const BlockHash &relay_parent, | ||
const SignedBitfield &bitfield) = 0; | ||
|
||
/// Get bitfields for given block. | ||
virtual std::vector<SignedBitfield> getBitfields( | ||
const BlockHash &relay_parent) const = 0; | ||
}; | ||
} // namespace kagome::parachain | ||
|
||
#endif // KAGOME_PARACHAIN_AVAILABILITY_BITFIELD_STORE_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "parachain/availability/bitfield/store_impl.hpp" | ||
|
||
namespace kagome::parachain { | ||
void BitfieldStoreImpl::putBitfield(const BlockHash &relay_parent, | ||
const SignedBitfield &bitfield) { | ||
bitfields_[relay_parent].push_back(bitfield); | ||
} | ||
|
||
std::vector<BitfieldStore::SignedBitfield> BitfieldStoreImpl::getBitfields( | ||
const BlockHash &relay_parent) const { | ||
std::vector<SignedBitfield> bitfields; | ||
auto it = bitfields_.find(relay_parent); | ||
if (it != bitfields_.end()) { | ||
bitfields = it->second; | ||
} | ||
return bitfields; | ||
} | ||
} // namespace kagome::parachain |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.