tp_session/lib.rs
1// This file is part of Tetcore.
2
3// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Tetcore core types around sessions.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use codec::{Encode, Decode};
23
24#[cfg(feature = "std")]
25use tp_runtime::{generic::BlockId, traits::Block as BlockT};
26#[cfg(feature = "std")]
27use tp_api::ProvideRuntimeApi;
28
29use tet_core::RuntimeDebug;
30use tet_core::crypto::KeyTypeId;
31use tp_staking::SessionIndex;
32use tetcore_std::vec::Vec;
33
34tp_api::decl_runtime_apis! {
35 /// Session keys runtime api.
36 pub trait SessionKeys {
37 /// Generate a set of session keys with optionally using the given seed.
38 /// The keys should be stored within the keystore exposed via runtime
39 /// externalities.
40 ///
41 /// The seed needs to be a valid `utf8` string.
42 ///
43 /// Returns the concatenated SCALE encoded public keys.
44 fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8>;
45
46 /// Decode the given public session keys.
47 ///
48 /// Returns the list of public raw public keys + key type.
49 fn decode_session_keys(encoded: Vec<u8>) -> Option<Vec<(Vec<u8>, KeyTypeId)>>;
50 }
51}
52
53/// Number of validators in a given session.
54pub type ValidatorCount = u32;
55
56/// Proof of membership of a specific key in a given session.
57#[derive(Encode, Decode, Clone, Eq, PartialEq, Default, RuntimeDebug)]
58pub struct MembershipProof {
59 /// The session index on which the specific key is a member.
60 pub session: SessionIndex,
61 /// Trie nodes of a merkle proof of session membership.
62 pub trie_nodes: Vec<Vec<u8>>,
63 /// The validator count of the session on which the specific key is a member.
64 pub validator_count: ValidatorCount,
65}
66
67/// A utility trait to get a session number. This is implemented for
68/// `MembershipProof` below to fetch the session number the given session
69/// membership proof is for. It is useful when we need to deal with key owner
70/// proofs generically (i.e. just typing against the `KeyOwnerProofSystem`
71/// trait) but still restrict their capabilities.
72pub trait GetSessionNumber {
73 fn session(&self) -> SessionIndex;
74}
75
76/// A utility trait to get the validator count of a given session. This is
77/// implemented for `MembershipProof` below and fetches the number of validators
78/// in the session the membership proof is for. It is useful when we need to
79/// deal with key owner proofs generically (i.e. just typing against the
80/// `KeyOwnerProofSystem` trait) but still restrict their capabilities.
81pub trait GetValidatorCount {
82 fn validator_count(&self) -> ValidatorCount;
83}
84
85impl GetSessionNumber for tet_core::Void {
86 fn session(&self) -> SessionIndex {
87 Default::default()
88 }
89}
90
91impl GetValidatorCount for tet_core::Void {
92 fn validator_count(&self) -> ValidatorCount {
93 Default::default()
94 }
95}
96
97impl GetSessionNumber for MembershipProof {
98 fn session(&self) -> SessionIndex {
99 self.session
100 }
101}
102
103impl GetValidatorCount for MembershipProof {
104 fn validator_count(&self) -> ValidatorCount {
105 self.validator_count
106 }
107}
108
109/// Generate the initial session keys with the given seeds, at the given block and store them in
110/// the client's keystore.
111#[cfg(feature = "std")]
112pub fn generate_initial_session_keys<Block, T>(
113 client: std::sync::Arc<T>,
114 at: &BlockId<Block>,
115 seeds: Vec<String>,
116) -> Result<(), tp_api::ApiErrorFor<T, Block>>
117where
118 Block: BlockT,
119 T: ProvideRuntimeApi<Block>,
120 T::Api: SessionKeys<Block>,
121{
122 let runtime_api = client.runtime_api();
123
124 for seed in seeds {
125 runtime_api.generate_session_keys(at, Some(seed.as_bytes().to_vec()))?;
126 }
127
128 Ok(())
129}