Skip to content
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

frame-session: Introduce a proper proof of key ownership #1739

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4a45248
Start
bkchr Sep 17, 2023
7f9a455
Merge remote-tracking branch 'origin/master' into bkchr-set-keys-proof
bkchr Sep 25, 2023
2ecdeb3
Fixes
bkchr Sep 25, 2023
d004457
Fix more tests
bkchr Sep 25, 2023
cca49bf
Remove new RPC and fix up some comments
bkchr Sep 28, 2023
69fa5fc
Merge remote-tracking branch 'origin/master' into bkchr-set-keys-proof
bkchr Sep 28, 2023
11d5099
Remove unused type.
bkchr Sep 28, 2023
654dec7
".git/.scripts/commands/fmt/fmt.sh"
Sep 28, 2023
da937d9
Fix compilation
bkchr Sep 28, 2023
eda0139
Revert "Remove new RPC and fix up some comments"
bkchr Oct 2, 2023
412e63b
Revert "Remove unused type."
bkchr Oct 2, 2023
bfba5cd
Merge remote-tracking branch 'origin/master' into bkchr-set-keys-proof
bkchr Oct 3, 2023
5a2c397
Update docs
bkchr Oct 3, 2023
c52c6c3
Fix tests
bkchr Oct 4, 2023
59f6886
Fix docs
bkchr Oct 4, 2023
371b1cb
Fixes
bkchr Oct 10, 2023
cdf9887
Fix compilation
bkchr Oct 11, 2023
a33ab45
FMT
bkchr Oct 11, 2023
b14b399
Make clippy happy..
bkchr Oct 11, 2023
0d30e18
CLIPPY
bkchr Oct 11, 2023
a6c5d25
Merge remote-tracking branch 'origin/master' into bkchr-set-keys-proof
bkchr Oct 11, 2023
b361b48
Fix collator selection benchmarking
bkchr Oct 11, 2023
ceb2add
Fix warning
bkchr Oct 11, 2023
b219dc8
Format features
bkchr Oct 11, 2023
1c3c8c5
Merge remote-tracking branch 'refs/remotes/origin/master'
bkchr Jul 30, 2024
3fc2ea5
Fix error
bkchr Aug 14, 2024
0479cd0
Merge remote-tracking branch 'origin/master' into bkchr-set-keys-proof
bkchr Aug 14, 2024
770b471
Remove old files
bkchr Aug 14, 2024
1cd8589
Fix issues
bkchr Aug 15, 2024
165eadb
Fix warning
bkchr Aug 15, 2024
0e8af35
More warnings..
bkchr Aug 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cumulus/pallets/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use crate as collator_selection;
use crate::{mock::*, CandidateInfo, Error};
use codec::Encode;
use frame_support::{
assert_noop, assert_ok,
traits::{Currency, OnInitialize},
Expand Down Expand Up @@ -121,7 +122,12 @@ fn invulnerable_limit_works() {
if ii > 5 {
Balances::make_free_balance_be(&ii, 100);
let key = MockSessionKeys { aura: UintAuthorityId(ii) };
Session::set_keys(RuntimeOrigin::signed(ii).into(), key, Vec::new()).unwrap();
Session::set_keys(
RuntimeOrigin::signed(ii).into(),
key.clone(),
key.create_ownership_proof(&ii.encode()).unwrap(),
)
.unwrap();
}
assert_eq!(Balances::free_balance(ii), 100);
if ii < 21 {
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachain-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,8 +977,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/glutton/glutton-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(_: Vec<u8>, _: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
sp_session::GeneratedSessionKeys { keys: Vec::new(), proof: Vec::new() }
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/starters/seedling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(_: Vec<u8>, _: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
sp_session::GeneratedSessionKeys { keys: Vec::new(), proof: Vec::new() }
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/starters/shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(_: Vec<u8>, _: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
sp_session::GeneratedSessionKeys { keys: Vec::new(), proof: Vec::new() }
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions cumulus/parachains/runtimes/testing/penpal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ impl_runtime_apis! {
SessionKeys::decode_into_raw_public_keys(&encoded)
}

fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}
}

Expand Down
4 changes: 2 additions & 2 deletions cumulus/test/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ impl_runtime_apis! {
SessionKeys::decode_into_raw_public_keys(&encoded)
}

fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}
}

Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/service/src/fake_runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(_: Option<Vec<u8>>) -> Vec<u8> {
fn generate_session_keys(_: Vec<u8>, _: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
unimplemented!()
}

Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2233,8 +2233,8 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,8 +2011,8 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,8 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,8 @@ sp_api::impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions substrate/bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
opaque::SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
opaque::SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
4 changes: 2 additions & 2 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2653,8 +2653,8 @@ impl_runtime_apis! {
}

impl sp_session::SessionKeys<Block> for Runtime {
fn generate_session_keys(seed: Option<Vec<u8>>) -> Vec<u8> {
SessionKeys::generate(seed)
fn generate_session_keys(owner: Vec<u8>, seed: Option<Vec<u8>>) -> sp_session::GeneratedSessionKeys {
SessionKeys::generate(&owner, seed)
}

fn decode_session_keys(
Expand Down
3 changes: 3 additions & 0 deletions substrate/client/rpc-api/src/author/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub enum Error {
/// Invalid session keys encoding.
#[error("Session keys are not encoded correctly")]
InvalidSessionKeys,
/// `SessionKeys` runtime api missing.
#[error("`SessionKeys` runtime api not present in the runtime")]
MissingSessionKeysApi,
/// Call to an unsafe RPC was denied.
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
Expand Down
23 changes: 19 additions & 4 deletions substrate/client/rpc/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,25 @@ where

runtime_api.register_extension(KeystoreExt::from(self.keystore.clone()));

runtime_api
.generate_session_keys(best_block_hash, None)
.map(Into::into)
.map_err(|api_err| Error::Client(Box::new(api_err)).into())
let version = runtime_api
.api_version::<dyn SessionKeys<P::Block>>(best_block_hash)
.map_err(|api_err| Error::Client(Box::new(api_err)))?
.ok_or_else(|| Error::MissingSessionKeysApi)?;

let res = if version < 2 {
#[allow(deprecated)]
runtime_api
.generate_session_keys_before_version_2(best_block_hash, None)
.map(Into::into)
.map_err(|api_err| Error::Client(Box::new(api_err)))
} else {
runtime_api
.generate_session_keys(best_block_hash, Vec::new(), None)
.map(|sk| sk.keys.into())
davxy marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|api_err| Error::Client(Box::new(api_err)))
}?;

Ok(res)
}

fn has_session_keys(&self, session_keys: Bytes) -> RpcResult<bool> {
Expand Down
5 changes: 4 additions & 1 deletion substrate/frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,10 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::set_keys())]
pub fn set_keys(origin: OriginFor<T>, keys: T::Keys, proof: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(keys.ownership_proof_is_valid(&proof), Error::<T>::InvalidProof);
ensure!(
who.using_encoded(|who| keys.ownership_proof_is_valid(who, &proof)),
Error::<T>::InvalidProof,
);

Self::do_set_keys(&who, keys)?;
Ok(())
Expand Down
21 changes: 14 additions & 7 deletions substrate/frame/session/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ use crate as pallet_session;
#[cfg(feature = "historical")]
use crate::historical as pallet_session_historical;

use std::collections::BTreeMap;

use codec::Encode;
use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64},
};
use sp_core::{crypto::key_types::DUMMY, H256};
use sp_runtime::{
impl_opaque_keys,
Expand All @@ -33,11 +36,7 @@ use sp_runtime::{
};
use sp_staking::SessionIndex;
use sp_state_machine::BasicExternalities;

use frame_support::{
parameter_types,
traits::{ConstU32, ConstU64},
};
use std::collections::BTreeMap;

impl_opaque_keys! {
pub struct MockSessionKeys {
Expand Down Expand Up @@ -74,6 +73,10 @@ impl OpaqueKeys for PreUpgradeMockSessionKeys {
_ => &[],
}
}

fn ownership_proof_is_valid(&self, _: &[u8], _: &[u8]) -> bool {
true
}
}

type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -208,6 +211,10 @@ pub fn reset_before_session_end_called() {
BeforeSessionEndCalled::mutate(|b| *b = false);
}

pub fn create_set_keys_proof(owner: u64, public: &UintAuthorityId) -> Vec<u8> {
public.sign(&owner.encode()).unwrap().encode()
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let keys: Vec<_> = NextValidators::get()
Expand Down
Loading
Loading