Skip to content

Commit

Permalink
use hasher
Browse files Browse the repository at this point in the history
Signed-off-by: turuslan <turuslan.devbox@gmail.com>
  • Loading branch information
turuslan committed Oct 3, 2022
1 parent 65ad916 commit 50d52c9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
23 changes: 23 additions & 0 deletions core/network/types/collator_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "common/blob.hpp"
#include "consensus/grandpa/common.hpp"
#include "crypto/hasher.hpp"
#include "primitives/common.hpp"
#include "primitives/compact_integer.hpp"
#include "primitives/digest.hpp"
Expand Down Expand Up @@ -246,6 +247,28 @@ namespace kagome::network {
ViewUpdate /// view update message
>;

inline CandidateHash candidateHash(const crypto::Hasher &hasher,
const CandidateReceipt &receipt) {
return hasher.blake2b_256(scale::encode(receipt).value());
}

inline CandidateHash candidateHash(const crypto::Hasher &hasher,
const CommittedCandidateReceipt &receipt) {
return candidateHash(
hasher,
CandidateReceipt{
receipt.descriptor,
hasher.blake2b_256(scale::encode(receipt.commitments).value()),
});
}

inline CandidateHash candidateHash(const crypto::Hasher &hasher,
const CandidateState &statement) {
if (auto receipt = boost::get<CommittedCandidateReceipt>(&statement)) {
return candidateHash(hasher, *receipt);
}
return boost::get<CandidateHash>(statement);
}
} // namespace kagome::network

#endif // KAGOME_COLLATOR_DECLARE_HPP
2 changes: 1 addition & 1 deletion core/parachain/availability/bitfield/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace kagome::parachain {
}

outcome::result<void> BitfieldSigner::sign(const ValidatorSigner &signer) {
auto &relay_parent = signer.context().relay_parent;
auto &relay_parent = signer.relayParent();
scale::BitVec bitfield;
OUTCOME_TRY(cores, parachain_api_->availability_cores(relay_parent));
bitfield.bits.reserve(cores.size());
Expand Down
9 changes: 7 additions & 2 deletions core/parachain/validator/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@ namespace kagome::parachain {
ValidatorIndex validator_index,
SigningContext context,
std::shared_ptr<crypto::Sr25519Keypair> keypair,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider)
: validator_index_{validator_index},
context_{context},
keypair_{std::move(keypair)},
hasher_{std::move(hasher)},
sr25519_provider_{std::move(sr25519_provider)} {}

ValidatorSigner::ValidatorIndex ValidatorSigner::validatorIndex() const {
return validator_index_;
}

const SigningContext &ValidatorSigner::context() const {
return context_;
const primitives::BlockHash &ValidatorSigner::relayParent() const {
return context_.relay_parent;
}

ValidatorSignerFactory::ValidatorSignerFactory(
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<crypto::SessionKeys> session_keys,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider)
: parachain_api_{std::move(parachain_api)},
session_keys_{std::move(session_keys)},
hasher_{std::move(hasher)},
sr25519_provider_{std::move(sr25519_provider)} {}

outcome::result<std::optional<ValidatorSigner>> ValidatorSignerFactory::at(
Expand All @@ -58,6 +62,7 @@ namespace kagome::parachain {
validator_index,
context,
keypair,
hasher_,
sr25519_provider_,
};
}
Expand Down
30 changes: 23 additions & 7 deletions core/parachain/validator/signer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@

namespace kagome::parachain {
/// A type returned by runtime with current session index and a parent hash.
struct SigningContext {
class SigningContext {
private:
static auto &toSignable(const crypto::Hasher &, const scale::BitVec &v) {
return v;
}
static auto toSignable(const crypto::Hasher &hasher,
const network::CandidateState &v) {
constexpr std::array<uint8_t, 4> kMagic{'B', 'K', 'N', 'G'};
return std::make_tuple(
kMagic, static_cast<uint8_t>(v.which()), candidateHash(hasher, v));
}

public:
SCALE_TIE(2);

/// Make signing context for given block.
Expand All @@ -25,9 +37,9 @@ namespace kagome::parachain {

/// Make signable message for payload.
template <typename T>
auto signable(const T &payload) const {
// TODO(turuslan): Statement requires conversion
return scale::encode(std::tie(payload, *this));
auto signable(const crypto::Hasher &hasher, const T &payload) const {
auto &&signable = toSignable(hasher, payload);
return scale::encode(std::tie(signable, *this)).value();
}

/// Current session index.
Expand All @@ -44,12 +56,13 @@ namespace kagome::parachain {
ValidatorSigner(ValidatorIndex validator_index,
SigningContext context,
std::shared_ptr<crypto::Sr25519Keypair> keypair,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider);

/// Sign payload.
template <typename T>
outcome::result<network::Signed<T>> sign(T payload) const {
OUTCOME_TRY(data, context_.signable(payload));
auto data = context_.signable(*hasher_, payload);
OUTCOME_TRY(signature, sr25519_provider_->sign(*keypair_, data));
return network::Signed<T>{
std::move(payload),
Expand All @@ -61,13 +74,14 @@ namespace kagome::parachain {
/// Get validator index.
ValidatorIndex validatorIndex() const;

/// Get signing context.
const SigningContext &context() const;
/// Get relay parent hash.
const primitives::BlockHash &relayParent() const;

private:
ValidatorIndex validator_index_;
SigningContext context_;
std::shared_ptr<crypto::Sr25519Keypair> keypair_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
};

Expand All @@ -77,6 +91,7 @@ namespace kagome::parachain {
ValidatorSignerFactory(
std::shared_ptr<runtime::ParachainHost> parachain_api,
std::shared_ptr<crypto::SessionKeys> session_keys,
std::shared_ptr<crypto::Hasher> hasher,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider);

/// Create validator signer if keypair belongs to validator at given block.
Expand All @@ -86,6 +101,7 @@ namespace kagome::parachain {
private:
std::shared_ptr<runtime::ParachainHost> parachain_api_;
std::shared_ptr<crypto::SessionKeys> session_keys_;
std::shared_ptr<crypto::Hasher> hasher_;
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
};
} // namespace kagome::parachain
Expand Down

0 comments on commit 50d52c9

Please sign in to comment.