Kagome
Polkadot Runtime Engine in C++17
signer.cpp
Go to the documentation of this file.
1 
7 
8 namespace kagome::parachain {
9  outcome::result<SigningContext> SigningContext::make(
10  const std::shared_ptr<runtime::ParachainHost> &parachain_api,
11  const primitives::BlockHash &relay_parent) {
12  OUTCOME_TRY(session_index,
13  parachain_api->session_index_for_child(relay_parent));
14  return SigningContext{session_index, relay_parent};
15  }
16 
18  ValidatorIndex validator_index,
19  SigningContext context,
20  std::shared_ptr<crypto::Sr25519Keypair> keypair,
21  std::shared_ptr<crypto::Hasher> hasher,
22  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider)
23  : validator_index_{validator_index},
24  context_{context},
25  keypair_{std::move(keypair)},
26  hasher_{std::move(hasher)},
27  sr25519_provider_{std::move(sr25519_provider)} {}
28 
30  return validator_index_;
31  }
32 
34  return context_.relay_parent;
35  }
36 
38  std::shared_ptr<runtime::ParachainHost> parachain_api,
39  std::shared_ptr<crypto::SessionKeys> session_keys,
40  std::shared_ptr<crypto::Hasher> hasher,
41  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider)
42  : parachain_api_{std::move(parachain_api)},
43  session_keys_{std::move(session_keys)},
44  hasher_{std::move(hasher)},
45  sr25519_provider_{std::move(sr25519_provider)} {}
46 
47  outcome::result<std::optional<ValidatorSigner>> ValidatorSignerFactory::at(
48  const primitives::BlockHash &relay_parent) {
49  auto &keypair = session_keys_->getParaKeyPair();
50  if (keypair == nullptr) {
51  return std::nullopt;
52  }
53  OUTCOME_TRY(validators, parachain_api_->validators(relay_parent));
54  auto it =
55  std::find(validators.begin(), validators.end(), keypair->public_key);
56  if (it == validators.end()) {
57  return std::nullopt;
58  }
59  network::ValidatorIndex validator_index = it - validators.begin();
60  OUTCOME_TRY(context, SigningContext::make(parachain_api_, relay_parent));
61  return ValidatorSigner{
62  validator_index,
63  context,
64  keypair,
65  hasher_,
67  };
68  }
69 } // namespace kagome::parachain
network::ValidatorIndex ValidatorIndex
Definition: signer.hpp:54
outcome::result< std::optional< ValidatorSigner > > at(const primitives::BlockHash &relay_parent)
Create validator signer if keypair belongs to validator at given block.
Definition: signer.cpp:47
std::shared_ptr< crypto::Sr25519Keypair > keypair_
Definition: signer.hpp:83
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)
Definition: signer.cpp:17
runtime::SessionIndex session_index
Current session index.
Definition: signer.hpp:46
ValidatorIndex validatorIndex() const
Get validator index.
Definition: signer.cpp:29
std::shared_ptr< crypto::Sr25519Provider > sr25519_provider_
Definition: signer.hpp:85
std::shared_ptr< crypto::Hasher > hasher_
Definition: signer.hpp:104
std::shared_ptr< runtime::ParachainHost > parachain_api_
Definition: signer.hpp:102
const primitives::BlockHash & relayParent() const
Get relay parent hash.
Definition: signer.cpp:33
std::shared_ptr< crypto::SessionKeys > session_keys_
Definition: signer.hpp:103
std::shared_ptr< crypto::Hasher > hasher_
Definition: signer.hpp:84
std::shared_ptr< crypto::Sr25519Provider > sr25519_provider_
Definition: signer.hpp:105
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)
Definition: signer.cpp:37
static outcome::result< SigningContext > make(const std::shared_ptr< runtime::ParachainHost > &parachain_api, const primitives::BlockHash &relay_parent)
Make signing context for given block.
Definition: signer.cpp:9
Signs payload with signing context and validator keypair.
Definition: signer.hpp:52
A type returned by runtime with current session index and a parent hash.
Definition: signer.hpp:18
primitives::BlockHash relay_parent
Hash of the parent.
Definition: signer.hpp:48