mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-21 22:41:02 +00:00
im-online: use generic crypto (#3500)
* im-online: support using ed25519 and sr25519 crypto * app-crypto: add trait bounds to RuntimePublic::Signature * im-online: add missing type annotations * authority-discovery: depend on im-online module and use its crypto * node: set i'm online crypto to sr25519 * node: bump spec_version * rpc: don't generate i'm online pubkey in insert_key method * im-online: fix docs * im-online: move app crypto packages * aura: move app crypto packages
This commit is contained in:
committed by
Bastian Köcher
parent
b5c6cc3996
commit
574f68fd7e
@@ -17,6 +17,9 @@ srml-support = { path = "../support", default-features = false }
|
||||
sr-io = { package = "sr-io", path = "../../core/sr-io", default-features = false }
|
||||
system = { package = "srml-system", path = "../system", default-features = false }
|
||||
|
||||
[dev-dependencies]
|
||||
sr-staking-primitives = { path = "../../core/sr-staking-primitives", default-features = false }
|
||||
|
||||
[features]
|
||||
default = ["std"]
|
||||
std = [
|
||||
|
||||
@@ -33,18 +33,20 @@ use codec::{Decode, Encode};
|
||||
use rstd::prelude::*;
|
||||
use srml_support::{decl_module, decl_storage, StorageValue};
|
||||
|
||||
pub trait Trait: system::Trait + session::Trait {}
|
||||
pub trait Trait: system::Trait + session::Trait + im_online::Trait {}
|
||||
|
||||
type AuthorityIdFor<T> = <T as im_online::Trait>::AuthorityId;
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as AuthorityDiscovery {
|
||||
/// The current set of keys that may issue a heartbeat.
|
||||
Keys get(keys): Vec<im_online::AuthorityId>;
|
||||
Keys get(keys): Vec<AuthorityIdFor<T>>;
|
||||
}
|
||||
add_extra_genesis {
|
||||
config(keys): Vec<im_online::AuthorityId>;
|
||||
config(keys): Vec<AuthorityIdFor<T>>;
|
||||
build(|
|
||||
storage: &mut (sr_primitives::StorageOverlay, sr_primitives::ChildrenStorageOverlay),
|
||||
config: &GenesisConfig
|
||||
config: &GenesisConfig<T>,
|
||||
| {
|
||||
sr_io::with_storage(
|
||||
storage,
|
||||
@@ -64,10 +66,10 @@ impl<T: Trait> Module<T> {
|
||||
/// set, otherwise this function returns None. The restriction might be
|
||||
/// softened in the future in case a consumer needs to learn own authority
|
||||
/// identifier.
|
||||
pub fn authority_id() -> Option<im_online::AuthorityId> {
|
||||
let authorities = Keys::get();
|
||||
pub fn authority_id() -> Option<AuthorityIdFor<T>> {
|
||||
let authorities = Keys::<T>::get();
|
||||
|
||||
let local_keys = im_online::AuthorityId::all();
|
||||
let local_keys = <AuthorityIdFor<T>>::all();
|
||||
|
||||
authorities.into_iter().find_map(|authority| {
|
||||
if local_keys.contains(&authority) {
|
||||
@@ -79,12 +81,12 @@ impl<T: Trait> Module<T> {
|
||||
}
|
||||
|
||||
/// Retrieve authority identifiers of the current authority set.
|
||||
pub fn authorities() -> Vec<im_online::AuthorityId> {
|
||||
Keys::get()
|
||||
pub fn authorities() -> Vec<AuthorityIdFor<T>> {
|
||||
Keys::<T>::get()
|
||||
}
|
||||
|
||||
/// Sign the given payload with the private key corresponding to the given authority id.
|
||||
pub fn sign(payload: Vec<u8>, authority_id: im_online::AuthorityId) -> Option<Vec<u8>> {
|
||||
pub fn sign(payload: Vec<u8>, authority_id: AuthorityIdFor<T>) -> Option<Vec<u8>> {
|
||||
authority_id.sign(&payload).map(|s| s.encode())
|
||||
}
|
||||
|
||||
@@ -93,27 +95,27 @@ impl<T: Trait> Module<T> {
|
||||
pub fn verify(
|
||||
payload: Vec<u8>,
|
||||
signature: Vec<u8>,
|
||||
authority_id: im_online::AuthorityId,
|
||||
authority_id: AuthorityIdFor<T>,
|
||||
) -> bool {
|
||||
im_online::AuthoritySignature::decode(&mut &signature[..])
|
||||
<AuthorityIdFor<T> as RuntimeAppPublic>::Signature::decode(&mut &signature[..])
|
||||
.map(|s| authority_id.verify(&payload, &s))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn initialize_keys(keys: &[im_online::AuthorityId]) {
|
||||
fn initialize_keys(keys: &[AuthorityIdFor<T>]) {
|
||||
if !keys.is_empty() {
|
||||
assert!(Keys::get().is_empty(), "Keys are already initialized!");
|
||||
Keys::put_ref(keys);
|
||||
assert!(Keys::<T>::get().is_empty(), "Keys are already initialized!");
|
||||
Keys::<T>::put_ref(keys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
|
||||
type Key = im_online::AuthorityId;
|
||||
type Key = AuthorityIdFor<T>;
|
||||
|
||||
fn on_genesis_session<'a, I: 'a>(validators: I)
|
||||
where
|
||||
I: Iterator<Item = (&'a T::AccountId, im_online::AuthorityId)>,
|
||||
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
|
||||
{
|
||||
let keys = validators.map(|x| x.1).collect::<Vec<_>>();
|
||||
Self::initialize_keys(&keys);
|
||||
@@ -121,10 +123,10 @@ impl<T: Trait> session::OneSessionHandler<T::AccountId> for Module<T> {
|
||||
|
||||
fn on_new_session<'a, I: 'a>(_changed: bool, _validators: I, next_validators: I)
|
||||
where
|
||||
I: Iterator<Item = (&'a T::AccountId, im_online::AuthorityId)>,
|
||||
I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
|
||||
{
|
||||
// Remember who the authorities are for the new session.
|
||||
Keys::put(next_validators.map(|x| x.1).collect::<Vec<_>>());
|
||||
Keys::<T>::put(next_validators.map(|x| x.1).collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
fn on_disabled(_i: usize) {
|
||||
@@ -139,9 +141,11 @@ mod tests {
|
||||
use primitives::testing::KeyStore;
|
||||
use primitives::{crypto::key_types, sr25519, traits::BareCryptoStore, H256};
|
||||
use sr_io::{with_externalities, TestExternalities};
|
||||
use sr_primitives::generic::UncheckedExtrinsic;
|
||||
use sr_primitives::testing::{Header, UintAuthorityId};
|
||||
use sr_primitives::traits::{ConvertInto, IdentityLookup, OpaqueKeys};
|
||||
use sr_primitives::Perbill;
|
||||
use sr_staking_primitives::CurrentElectedSet;
|
||||
use srml_support::{impl_outer_origin, parameter_types};
|
||||
|
||||
type AuthorityDiscovery = Module<Test>;
|
||||
@@ -151,12 +155,21 @@ mod tests {
|
||||
pub struct Test;
|
||||
impl Trait for Test {}
|
||||
|
||||
type AuthorityId = im_online::sr25519::AuthorityId;
|
||||
|
||||
pub struct DummyCurrentElectedSet<T>(std::marker::PhantomData<T>);
|
||||
impl<T> CurrentElectedSet<T> for DummyCurrentElectedSet<T> {
|
||||
fn current_elected_set() -> Vec<T> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TestOnSessionEnding;
|
||||
impl session::OnSessionEnding<im_online::AuthorityId> for TestOnSessionEnding {
|
||||
impl session::OnSessionEnding<AuthorityId> for TestOnSessionEnding {
|
||||
fn on_session_ending(
|
||||
_: SessionIndex,
|
||||
_: SessionIndex,
|
||||
) -> Option<Vec<im_online::AuthorityId>> {
|
||||
) -> Option<Vec<AuthorityId>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -167,11 +180,25 @@ mod tests {
|
||||
type ShouldEndSession = session::PeriodicSessions<Period, Offset>;
|
||||
type SessionHandler = TestSessionHandler;
|
||||
type Event = ();
|
||||
type ValidatorId = im_online::AuthorityId;
|
||||
type ValidatorId = AuthorityId;
|
||||
type ValidatorIdOf = ConvertInto;
|
||||
type SelectInitialValidators = ();
|
||||
}
|
||||
|
||||
impl session::historical::Trait for Test {
|
||||
type FullIdentification = ();
|
||||
type FullIdentificationOf = ();
|
||||
}
|
||||
|
||||
impl im_online::Trait for Test {
|
||||
type AuthorityId = AuthorityId;
|
||||
type Call = im_online::Call<Test>;
|
||||
type Event = ();
|
||||
type UncheckedExtrinsic = UncheckedExtrinsic<(), im_online::Call<Test>, (), ()>;
|
||||
type ReportUnresponsiveness = ();
|
||||
type CurrentElectedSet = DummyCurrentElectedSet<AuthorityId>;
|
||||
}
|
||||
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
parameter_types! {
|
||||
@@ -191,7 +218,7 @@ mod tests {
|
||||
type Call = ();
|
||||
type Hash = H256;
|
||||
type Hashing = ::sr_primitives::traits::BlakeTwo256;
|
||||
type AccountId = im_online::AuthorityId;
|
||||
type AccountId = AuthorityId;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type WeightMultiplierUpdate = ();
|
||||
@@ -208,17 +235,17 @@ mod tests {
|
||||
}
|
||||
|
||||
pub struct TestSessionHandler;
|
||||
impl session::SessionHandler<im_online::AuthorityId> for TestSessionHandler {
|
||||
impl session::SessionHandler<AuthorityId> for TestSessionHandler {
|
||||
fn on_new_session<Ks: OpaqueKeys>(
|
||||
_changed: bool,
|
||||
_validators: &[(im_online::AuthorityId, Ks)],
|
||||
_queued_validators: &[(im_online::AuthorityId, Ks)],
|
||||
_validators: &[(AuthorityId, Ks)],
|
||||
_queued_validators: &[(AuthorityId, Ks)],
|
||||
) {
|
||||
}
|
||||
|
||||
fn on_disabled(_validator_index: usize) {}
|
||||
|
||||
fn on_genesis_session<Ks: OpaqueKeys>(_validators: &[(im_online::AuthorityId, Ks)]) {}
|
||||
fn on_genesis_session<Ks: OpaqueKeys>(_validators: &[(AuthorityId, Ks)]) {}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -236,17 +263,17 @@ mod tests {
|
||||
.sr25519_public_keys(key_types::IM_ONLINE)
|
||||
.pop()
|
||||
.unwrap();
|
||||
let authority_id = im_online::AuthorityId::from(public_key);
|
||||
let authority_id = AuthorityId::from(public_key);
|
||||
|
||||
// Build genesis.
|
||||
let mut t = system::GenesisConfig::default()
|
||||
.build_storage::<Test>()
|
||||
.unwrap();
|
||||
|
||||
GenesisConfig {
|
||||
GenesisConfig::<Test> {
|
||||
keys: vec![authority_id.clone()],
|
||||
}
|
||||
.assimilate_storage::<Test>(&mut t)
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
// Create externalities.
|
||||
@@ -279,11 +306,11 @@ mod tests {
|
||||
let keys = vec![(); 5]
|
||||
.iter()
|
||||
.map(|_x| sr25519::Pair::generate_with_phrase(None).0.public())
|
||||
.map(im_online::AuthorityId::from)
|
||||
.map(AuthorityId::from)
|
||||
.collect();
|
||||
|
||||
GenesisConfig { keys: keys }
|
||||
.assimilate_storage::<Test>(&mut t)
|
||||
GenesisConfig::<Test> { keys: keys }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
// Create externalities.
|
||||
@@ -310,17 +337,17 @@ mod tests {
|
||||
.sr25519_public_keys(key_types::IM_ONLINE)
|
||||
.pop()
|
||||
.unwrap();
|
||||
let authority_id = im_online::AuthorityId::from(public_key);
|
||||
let authority_id = AuthorityId::from(public_key);
|
||||
|
||||
// Build genesis.
|
||||
let mut t = system::GenesisConfig::default()
|
||||
.build_storage::<Test>()
|
||||
.unwrap();
|
||||
|
||||
GenesisConfig {
|
||||
GenesisConfig::<Test> {
|
||||
keys: vec![authority_id.clone()],
|
||||
}
|
||||
.assimilate_storage::<Test>(&mut t)
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
// Create externalities.
|
||||
|
||||
Reference in New Issue
Block a user