Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions core/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ macro_rules! app_crypto {
}

impl $crate::RuntimeAppPublic for Public where $public: $crate::RuntimePublic<Signature=$sig> {
const ID: $crate::KeyTypeId = $key_type;
type Signature = Signature;

fn all() -> $crate::Vec<Self> {
Expand Down
5 changes: 4 additions & 1 deletion core/application-crypto/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ pub trait RuntimePublic: Sized {
}

/// A runtime interface for an application's public key.
pub trait RuntimeAppPublic: Sized {
pub trait RuntimeAppPublic: Sized {
/// An identifier for this application-specific key type.
const ID: KeyTypeId;

/// The signature that will be generated when signing with the corresponding private key.
type Signature: Codec + MaybeDebugHash + Eq + PartialEq + Clone;

Expand Down
1 change: 0 additions & 1 deletion core/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ client = { package = "substrate-client", path = "../../core/client" }
codec = { package = "parity-scale-codec", default-features = false, version = "1.0.3" }
derive_more = "0.14.0"
futures = "0.1"
keystore = { package = "substrate-keystore", path = "../../core/keystore" }
libp2p = { version = "0.12.0", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
log = "0.4"
network = { package = "substrate-network", path = "../../core/network" }
Expand Down
13 changes: 10 additions & 3 deletions core/offchain/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ pub struct State {
pub persistent_storage: client::in_mem::OffchainStorage,
/// Local storage
pub local_storage: client::in_mem::OffchainStorage,
/// A vector of transactions submitted from the runtime.
pub transactions: Vec<Vec<u8>>,
}

impl State {
Expand Down Expand Up @@ -138,12 +140,17 @@ impl offchain::Externalities for TestOffchainExt {
unimplemented!("not needed in tests so far")
}

fn submit_transaction(&mut self, _ex: Vec<u8>) -> Result<(), ()> {
unimplemented!("not needed in tests so far")
fn submit_transaction(&mut self, ex: Vec<u8>) -> Result<(), ()> {
let mut state = self.0.write();
state.transactions.push(ex);
Ok(())
}

fn network_state(&self) -> Result<OpaqueNetworkState, ()> {
unimplemented!("not needed in tests so far")
Ok(OpaqueNetworkState {
peer_id: Default::default(),
external_addresses: vec![],
})
}

fn timestamp(&mut self) -> Timestamp {
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub struct OpaqueNetworkState {
}

/// Simple blob to hold a `PeerId` without committing to its format.
#[derive(Clone, Eq, PartialEq, Encode, Decode)]
#[derive(Default, Clone, Eq, PartialEq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct OpaquePeerId(pub Vec<u8>);

Expand Down
2 changes: 1 addition & 1 deletion core/sr-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub use generic::{DigestItem, Digest};

/// Re-export this since it's part of the API of this crate.
pub use primitives::crypto::{key_types, KeyTypeId, CryptoType};
pub use app_crypto::AppKey;
pub use app_crypto::RuntimeAppPublic;

/// Justification type.
pub type Justification = Vec<u8>;
Expand Down
55 changes: 34 additions & 21 deletions core/sr-primitives/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Testing utilities.

use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializer};
use std::{fmt::Debug, ops::Deref, fmt};
use std::{fmt::Debug, ops::Deref, fmt, cell::RefCell};
use crate::codec::{Codec, Encode, Decode};
use crate::traits::{
self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, ValidateUnsigned,
Expand All @@ -30,9 +30,15 @@ use primitives::{crypto::{CryptoType, Dummy, key_types, Public}, U256};
use crate::transaction_validity::{TransactionValidity, TransactionValidityError};

/// Authority Id
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, Debug, Hash, Serialize, Deserialize)]
#[derive(Default, PartialEq, Eq, Clone, Encode, Decode, Debug, Hash, Serialize, Deserialize, PartialOrd, Ord)]
pub struct UintAuthorityId(pub u64);

impl From<u64> for UintAuthorityId {
fn from(id: u64) -> Self {
UintAuthorityId(id)
}
}

impl UintAuthorityId {
/// Convert this authority id into a public key.
pub fn to_public_key<T: Public>(&self) -> T {
Expand All @@ -47,34 +53,44 @@ impl CryptoType for UintAuthorityId {

impl AsRef<[u8]> for UintAuthorityId {
fn as_ref(&self) -> &[u8] {
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
unsafe {
std::slice::from_raw_parts(&self.0 as *const u64 as *const _, std::mem::size_of::<u64>())
}
}
}

thread_local! {
/// A list of all UintAuthorityId keys returned to the runtime.
static ALL_KEYS: RefCell<Vec<UintAuthorityId>> = RefCell::new(vec![]);
}

impl UintAuthorityId {
/// Set the list of keys returned by the runtime call for all keys of that type.
pub fn set_all_keys<T: Into<UintAuthorityId>>(keys: impl IntoIterator<Item=T>) {
ALL_KEYS.with(|l| *l.borrow_mut() = keys.into_iter().map(Into::into).collect())
}
}

impl app_crypto::RuntimeAppPublic for UintAuthorityId {
const ID: KeyTypeId = key_types::DUMMY;

type Signature = u64;

fn all() -> Vec<Self> {
unimplemented!("`all()` not available for `UintAuthorityId`.")
ALL_KEYS.with(|l| l.borrow().clone())
}

#[cfg(feature = "std")]
fn generate_pair(_: Option<&str>) -> Self {
use rand::RngCore;
UintAuthorityId(rand::thread_rng().next_u64())
}

#[cfg(not(feature = "std"))]
fn generate_pair(_: Option<&str>) -> Self {
unimplemented!("`generate_pair` not implemented for `UIntAuthorityId` on `no_std`.")
}

fn sign<M: AsRef<[u8]>>(&self, msg: &M) -> Option<Self::Signature> {
let mut signature = [0u8; 8];
msg.as_ref().iter()
.chain(rstd::iter::repeat(&42u8))
.chain(std::iter::repeat(&42u8))
.take(8)
.enumerate()
.for_each(|(i, v)| { signature[i] = *v; });
Expand All @@ -85,7 +101,7 @@ impl app_crypto::RuntimeAppPublic for UintAuthorityId {
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
let mut msg_signature = [0u8; 8];
msg.as_ref().iter()
.chain(rstd::iter::repeat(&42))
.chain(std::iter::repeat(&42))
.take(8)
.enumerate()
.for_each(|(i, v)| { msg_signature[i] = *v; });
Expand All @@ -97,19 +113,16 @@ impl app_crypto::RuntimeAppPublic for UintAuthorityId {
impl OpaqueKeys for UintAuthorityId {
type KeyTypeIds = std::iter::Cloned<std::slice::Iter<'static, KeyTypeId>>;

fn key_ids() -> Self::KeyTypeIds { [key_types::DUMMY].iter().cloned() }
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
fn key_ids() -> Self::KeyTypeIds {
[key_types::DUMMY].iter().cloned()
}

fn get_raw(&self, _: KeyTypeId) -> &[u8] {
unsafe {
std::slice::from_raw_parts(
&self.0 as *const _ as *const u8,
std::mem::size_of::<u64>(),
)
}
self.as_ref()
}

fn get<T: Decode>(&self, _: KeyTypeId) -> Option<T> {
self.0.using_encoded(|mut x| T::decode(&mut x)).ok()
self.using_encoded(|mut x| T::decode(&mut x)).ok()
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/sr-primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use rstd::ops::{
Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign,
RemAssign, Shl, Shr
};
use crate::AppKey;
use app_crypto::AppKey;
use impl_trait_for_tuples::impl_for_tuples;

/// A lazy value.
Expand Down
9 changes: 0 additions & 9 deletions core/sr-staking-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@
//! A crate which contains primitives that are useful for implementation that uses staking
//! approaches in general. Definitions related to sessions, slashing, etc go here.

use rstd::vec::Vec;

pub mod offence;

/// Simple index type with which we can count sessions.
pub type SessionIndex = u32;

/// A trait for getting the currently elected validator set without coupling to the module that
/// provides this information.
pub trait CurrentElectedSet<ValidatorId> {
/// Returns the validator ids for the currently elected validator set.
fn current_elected_set() -> Vec<ValidatorId>;
}
1 change: 0 additions & 1 deletion node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ impl im_online::Trait for Runtime {
type Event = Event;
type SubmitTransaction = SubmitTransaction;
type ReportUnresponsiveness = Offences;
type CurrentElectedSet = staking::CurrentElectedStashAccounts<Runtime>;
}

impl offences::Trait for Runtime {
Expand Down
4 changes: 2 additions & 2 deletions srml/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ use support::{
decl_storage, decl_module, Parameter, storage::StorageValue, traits::{Get, FindAuthor},
ConsensusEngineId,
};
use app_crypto::AppPublic;
use sr_primitives::{
RuntimeAppPublic,
traits::{SaturatedConversion, Saturating, Zero, Member, IsMember}, generic::DigestItem,
};
use timestamp::OnTimestampSet;
Expand Down Expand Up @@ -142,7 +142,7 @@ impl ProvideInherentData for InherentDataProvider {

pub trait Trait: timestamp::Trait {
/// The identifier type for an authority.
type AuthorityId: Member + Parameter + AppPublic + Default;
type AuthorityId: Member + Parameter + RuntimeAppPublic + Default;
}

decl_storage! {
Expand Down
9 changes: 0 additions & 9 deletions srml/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ mod tests {
use sr_primitives::testing::{Header, UintAuthorityId};
use sr_primitives::traits::{ConvertInto, IdentityLookup, OpaqueKeys};
use sr_primitives::Perbill;
use sr_staking_primitives::CurrentElectedSet;
use support::{impl_outer_origin, parameter_types};

type AuthorityDiscovery = Module<Test>;
Expand All @@ -149,13 +148,6 @@ mod tests {

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<AuthorityId> for TestOnSessionEnding {
fn on_session_ending(_: SessionIndex, _: SessionIndex) -> Option<Vec<AuthorityId>> {
Expand Down Expand Up @@ -189,7 +181,6 @@ mod tests {
UncheckedExtrinsic<(), im_online::Call<Test>, (), ()>,
>;
type ReportUnresponsiveness = ();
type CurrentElectedSet = DummyCurrentElectedSet<AuthorityId>;
}

pub type BlockNumber = u64;
Expand Down
3 changes: 3 additions & 0 deletions srml/im-online/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ sr-staking-primitives = { path = "../../core/sr-staking-primitives", default-fea
support = { package = "srml-support", path = "../support", default-features = false }
system = { package = "srml-system", path = "../system", default-features = false }

[dev-dependencies]
offchain = { package = "substrate-offchain", path = "../../core/offchain" }

[features]
default = ["std", "session/historical"]
std = [
Expand Down
Loading