Skip to content

Commit

Permalink
Merge branch 'master' into 2255-enhancement-refactor-block-tree-and-h…
Browse files Browse the repository at this point in the history
…eader-repo
  • Loading branch information
ErakhtinB authored Dec 19, 2024
2 parents ce48923 + 0576457 commit 2817836
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 23 deletions.
1 change: 1 addition & 0 deletions core/authority_discovery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(address_publisher
target_link_libraries(address_publisher
authority_discovery_proto
logger
p2p::p2p_kademlia
scale_libp2p_types
sha
)
43 changes: 30 additions & 13 deletions core/authority_discovery/publisher/address_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,26 @@ namespace kagome::authority_discovery {
return outcome::success();
}

OUTCOME_TRY(
raw,
audiEncode(ed_crypto_provider_,
sr_crypto_provider_,
*libp2p_key_,
*libp2p_key_pb_,
peer_info,
*audi_key,
std::chrono::system_clock::now().time_since_epoch()));
return kademlia_->putValue(std::move(raw.first), std::move(raw.second));
}

outcome::result<std::pair<Buffer, Buffer>> audiEncode(
std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
const Ed25519Keypair &libp2p_key,
const ProtobufKey &libp2p_key_pb,
const PeerInfo &peer_info,
const Sr25519Keypair &audi_key,
std::optional<std::chrono::nanoseconds> now) {
std::unordered_set<libp2p::multi::Multiaddress> addresses;
for (const auto &address : peer_info.addresses) {
if (address.getPeerId()) {
Expand All @@ -133,27 +153,24 @@ namespace kagome::authority_discovery {
for (const auto &address : addresses) {
PB_SPAN_ADD(record, addresses, address.getBytesAddress());
}
TimestampScale time{std::chrono::nanoseconds{
std::chrono::system_clock::now().time_since_epoch()}
.count()};
PB_SPAN_SET(*record.mutable_creation_time(),
timestamp,
scale::encode(time).value());
if (now) {
TimestampScale time{now->count()};
OUTCOME_TRY(encoded_time, scale::encode(time));
PB_SPAN_SET(*record.mutable_creation_time(), timestamp, encoded_time);
}

auto record_pb = pbEncodeVec(record);
OUTCOME_TRY(signature, ed_crypto_provider_->sign(*libp2p_key_, record_pb));
OUTCOME_TRY(auth_signature,
sr_crypto_provider_->sign(*audi_key, record_pb));
OUTCOME_TRY(signature, ed_crypto_provider->sign(libp2p_key, record_pb));
OUTCOME_TRY(auth_signature, sr_crypto_provider->sign(audi_key, record_pb));

::authority_discovery_v3::SignedAuthorityRecord signed_record;
PB_SPAN_SET(signed_record, auth_signature, auth_signature);
PB_SPAN_SET(signed_record, record, record_pb);
auto &ps = *signed_record.mutable_peer_signature();
PB_SPAN_SET(ps, signature, signature);
PB_SPAN_SET(ps, public_key, libp2p_key_pb_->key);
PB_SPAN_SET(ps, public_key, libp2p_key_pb.key);

auto hash = crypto::sha256(audi_key->public_key);
return kademlia_->putValue({hash.begin(), hash.end()},
pbEncodeVec(signed_record));
auto hash = crypto::sha256(audi_key.public_key);
return std::make_pair(Buffer{hash}, Buffer{pbEncodeVec(signed_record)});
}
} // namespace kagome::authority_discovery
16 changes: 16 additions & 0 deletions core/authority_discovery/publisher/address_publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
#include <memory>

namespace kagome::authority_discovery {
using crypto::Ed25519Keypair;
using crypto::Ed25519Provider;
using crypto::Sr25519Keypair;
using crypto::Sr25519Provider;
using libp2p::crypto::ProtobufKey;
using libp2p::peer::PeerInfo;

outcome::result<std::pair<Buffer, Buffer>> audiEncode(
std::shared_ptr<crypto::Ed25519Provider> ed_crypto_provider,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
const Ed25519Keypair &libp2p_key,
const ProtobufKey &libp2p_key_pb,
const PeerInfo &peer_info,
const Sr25519Keypair &audi_key,
std::optional<std::chrono::nanoseconds> now);

/**
* Publishes listening addresses for authority discovery.
* Authority discovery public key is used for Kademlia DHT key.
Expand Down
4 changes: 2 additions & 2 deletions core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace kagome::authority_discovery {
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api,
LazySPtr<network::ValidationProtocol> validation_protocol,
LazySPtr<network::ValidationProtocolReserve> validation_protocol,
std::shared_ptr<crypto::KeyStore> key_store,
std::shared_ptr<AudiStore> audi_store,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
Expand Down Expand Up @@ -344,7 +344,7 @@ namespace kagome::authority_discovery {
authority,
AuthorityPeerInfo{
.raw = std::move(signed_record_pb),
.time = time.has_value() ? std::make_optional<TimestampScale>(*time)
.time = time.has_value() ? std::make_optional(TimestampScale{*time})
: std::nullopt,
.peer = peer,
});
Expand Down
6 changes: 3 additions & 3 deletions core/authority_discovery/query/query_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <random>

namespace kagome::network {
class ValidationProtocol;
class ValidationProtocolReserve;
} // namespace kagome::network

namespace kagome::authority_discovery {
Expand All @@ -47,7 +47,7 @@ namespace kagome::authority_discovery {
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<blockchain::BlockTree> block_tree,
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api,
LazySPtr<network::ValidationProtocol> validation_protocol,
LazySPtr<network::ValidationProtocolReserve> validation_protocol,
std::shared_ptr<crypto::KeyStore> key_store,
std::shared_ptr<AudiStore> audi_store,
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider,
Expand Down Expand Up @@ -85,7 +85,7 @@ namespace kagome::authority_discovery {

std::shared_ptr<blockchain::BlockTree> block_tree_;
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api_;
LazySPtr<network::ValidationProtocol> validation_protocol_;
LazySPtr<network::ValidationProtocolReserve> validation_protocol_;
std::shared_ptr<crypto::KeyStore> key_store_;
std::shared_ptr<AudiStore> audi_store_;
std::shared_ptr<crypto::Sr25519Provider> sr_crypto_provider_;
Expand Down
5 changes: 3 additions & 2 deletions core/injector/application_injector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
#include "application/app_configuration.hpp"
#include "application/impl/app_state_manager_impl.hpp"
#include "application/impl/chain_spec_impl.hpp"
#include "application/modes/key.hpp"
#include "application/modes/precompile_wasm.hpp"
#include "application/modes/print_chain_info_mode.hpp"
#include "application/modes/recovery_mode.hpp"
#include "application/modes/key.hpp"
#include "authority_discovery/publisher/address_publisher.hpp"
#include "authority_discovery/query/audi_store_impl.hpp"
#include "authority_discovery/query/query_impl.hpp"
Expand Down Expand Up @@ -870,6 +870,7 @@ namespace {
di::bind<crypto::SessionKeys>.template to<crypto::SessionKeysImpl>(),
di::bind<network::SyncProtocol>.template to<network::SyncProtocolImpl>(),
di::bind<network::StateProtocol>.template to<network::StateProtocolImpl>(),
di::bind<network::ValidationProtocolReserve>.template to<network::ValidationProtocol>(),
di::bind<network::BeefyProtocol>.template to<network::BeefyProtocolImpl>(),
di::bind<consensus::beefy::FetchJustification>.template to<network::BeefyJustificationProtocol>(),
di::bind<network::Beefy>.template to<network::BeefyImpl>(),
Expand Down Expand Up @@ -935,7 +936,7 @@ namespace kagome::injector {
KagomeNodeInjector::KagomeNodeInjector(
sptr<application::AppConfiguration> app_config)
: pimpl_{std::make_unique<KagomeNodeInjectorImpl>(
makeKagomeNodeInjector(std::move(app_config)))} {}
makeKagomeNodeInjector(std::move(app_config)))} {}

sptr<application::AppConfiguration> KagomeNodeInjector::injectAppConfig() {
return pimpl_->injector_
Expand Down
12 changes: 10 additions & 2 deletions core/network/impl/protocols/parachain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ namespace kagome::network {
std::shared_ptr<CollationObserver> observer_;
};

class ValidationProtocol final : public ParachainProtocol {
class ValidationProtocolReserve {
public:
virtual ~ValidationProtocolReserve() = default;

virtual void reserve(const PeerId &peer_id, bool add) = 0;
};

class ValidationProtocol final : public ParachainProtocol,
public ValidationProtocolReserve {
public:
ValidationProtocol(ParachainProtocolInject inject,
std::shared_ptr<ValidationObserver> observer);
Expand All @@ -122,7 +130,7 @@ namespace kagome::network {
}
}
void write(const BitfieldDistribution &message);
void reserve(const PeerId &peer_id, bool add);
void reserve(const PeerId &peer_id, bool add) override;

private:
std::shared_ptr<ValidationObserver> observer_;
Expand Down
10 changes: 10 additions & 0 deletions test/core/authority_discovery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ target_link_libraries(address_publisher_test
logger_for_tests
network
)

addtest(audi_query_test
query.cpp
)
target_link_libraries(audi_query_test
address_publisher
in_memory_storage
key_store
logger_for_tests
)
Loading

0 comments on commit 2817836

Please sign in to comment.