Remove Default bound for AccountId (#10403)

* Remove Default for AccountId

* More removals of default

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/authorship/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* More work

* More work

* Remove old code

* More work

* pallet-asset-tx-payment

* tips

* sc-consensus-babe

* sc-finality-grandpa

* sc-consensus-babe-rpc

* sc-cli

* make npos crates accept non-default account (#10420)

* minimal changes to make npos pallets all work

* make this pesky reduce.rs a bit cleaner

* more work

* more work

* Tests build

* Fix imonline tests

* Formatting

* Fixes

* Fixes

* Fix bench

* Fixes

* Fixes

* Fixes

* Fixes

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Fixes

* Formatting

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/finality-grandpa/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update client/keystore/src/local.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update frame/staking/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Update primitives/runtime/src/traits.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* Formatting

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
This commit is contained in:
Gavin Wood
2021-12-13 15:03:59 +01:00
committed by GitHub
parent a4ccc26e33
commit 1e24e45ea1
118 changed files with 998 additions and 4181 deletions
+43 -43
View File
@@ -222,7 +222,7 @@ pub enum PublicError {
/// See <https://docs.substrate.io/v3/advanced/ss58/>
/// for information on the codec.
#[cfg(feature = "full_crypto")]
pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + ByteArray {
/// A format filterer, can be used to ensure that `from_ss58check` family only decode for
/// allowed identifiers. By default just refuses the two reserved identifiers.
fn format_is_allowed(f: Ss58AddressFormat) -> bool {
@@ -243,10 +243,7 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
#[cfg(feature = "std")]
fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> {
const CHECKSUM_LEN: usize = 2;
let mut res = Self::default();
// Must decode to our type.
let body_len = res.as_mut().len();
let body_len = Self::LEN;
let data = s.from_base58().map_err(|_| PublicError::BadBase58)?;
if data.len() < 2 {
@@ -280,8 +277,10 @@ pub trait Ss58Codec: Sized + AsMut<[u8]> + AsRef<[u8]> + Default {
// Invalid checksum.
return Err(PublicError::InvalidChecksum)
}
res.as_mut().copy_from_slice(&data[prefix_len..body_len + prefix_len]);
Ok((res, format))
let result = Self::from_slice(&data[prefix_len..body_len + prefix_len])
.map_err(|()| PublicError::BadLength)?;
Ok((result, format))
}
/// Some if the string is a properly encoded SS58Check address, optionally with
@@ -391,19 +390,13 @@ lazy_static::lazy_static! {
}
#[cfg(feature = "std")]
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Public + Derive> Ss58Codec for T {
fn from_string(s: &str) -> Result<Self, PublicError> {
let cap = SS58_REGEX.captures(s).ok_or(PublicError::InvalidFormat)?;
let s = cap.name("ss58").map(|r| r.as_str()).unwrap_or(DEV_ADDRESS);
let addr = if let Some(stripped) = s.strip_prefix("0x") {
let d = hex::decode(stripped).map_err(|_| PublicError::InvalidFormat)?;
let mut r = Self::default();
if d.len() == r.as_ref().len() {
r.as_mut().copy_from_slice(&d);
r
} else {
return Err(PublicError::BadLength)
}
Self::from_slice(&d).map_err(|()| PublicError::BadLength)?
} else {
Self::from_ss58check(s)?
};
@@ -431,25 +424,15 @@ impl<T: Sized + AsMut<[u8]> + AsRef<[u8]> + Default + Derive> Ss58Codec for T {
}
}
/// Trait suitable for typical cryptographic PKI key public type.
pub trait Public:
AsRef<[u8]>
+ AsMut<[u8]>
+ Default
+ Derive
+ CryptoType
+ PartialEq
+ Eq
+ Clone
+ Send
+ Sync
+ for<'a> TryFrom<&'a [u8]>
{
/// A new instance from the given slice.
///
/// NOTE: No checking goes on to ensure this is a real public key. Only use it if
/// you are certain that the array actually is a pubkey. GIGO!
fn from_slice(data: &[u8]) -> Self;
/// Trait used for types that are really just a fixed-length array.
pub trait ByteArray: AsRef<[u8]> + AsMut<[u8]> + for<'a> TryFrom<&'a [u8], Error = ()> {
/// The "length" of the values of this type, which is always the same.
const LEN: usize;
/// A new instance from the given slice that should be `Self::LEN` bytes long.
fn from_slice(data: &[u8]) -> Result<Self, ()> {
Self::try_from(data)
}
/// Return a `Vec<u8>` filled with raw data.
fn to_raw_vec(&self) -> Vec<u8> {
@@ -460,7 +443,10 @@ pub trait Public:
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}
/// Trait suitable for typical cryptographic PKI key public type.
pub trait Public: ByteArray + Derive + CryptoType + PartialEq + Eq + Clone + Send + Sync {
/// Return `CryptoTypePublicPair` from public key.
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair;
}
@@ -488,6 +474,10 @@ impl UncheckedFrom<crate::hash::H256> for AccountId32 {
}
}
impl ByteArray for AccountId32 {
const LEN: usize = 32;
}
#[cfg(feature = "std")]
impl Ss58Codec for AccountId32 {}
@@ -650,9 +640,10 @@ mod dummy {
impl Derive for Dummy {}
impl Public for Dummy {
fn from_slice(_: &[u8]) -> Self {
Self
impl ByteArray for Dummy {
const LEN: usize = 0;
fn from_slice(_: &[u8]) -> Result<Self, ()> {
Ok(Self)
}
#[cfg(feature = "std")]
fn to_raw_vec(&self) -> Vec<u8> {
@@ -661,8 +652,10 @@ mod dummy {
fn as_slice(&self) -> &[u8] {
b""
}
}
impl Public for Dummy {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), Public::to_raw_vec(self))
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), <Self as ByteArray>::to_raw_vec(self))
}
}
@@ -1146,17 +1139,22 @@ mod tests {
impl<'a> TryFrom<&'a [u8]> for TestPublic {
type Error = ();
fn try_from(_: &'a [u8]) -> Result<Self, ()> {
Ok(Self)
fn try_from(data: &'a [u8]) -> Result<Self, ()> {
Self::from_slice(data)
}
}
impl CryptoType for TestPublic {
type Pair = TestPair;
}
impl Derive for TestPublic {}
impl Public for TestPublic {
fn from_slice(_bytes: &[u8]) -> Self {
Self
impl ByteArray for TestPublic {
const LEN: usize = 0;
fn from_slice(bytes: &[u8]) -> Result<Self, ()> {
if bytes.len() == 0 {
Ok(Self)
} else {
Err(())
}
}
fn as_slice(&self) -> &[u8] {
&[]
@@ -1164,6 +1162,8 @@ mod tests {
fn to_raw_vec(&self) -> Vec<u8> {
vec![]
}
}
impl Public for TestPublic {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CryptoTypeId(*b"dumm"), self.to_raw_vec())
}
+17 -21
View File
@@ -27,7 +27,8 @@ use sp_std::cmp::Ordering;
#[cfg(feature = "std")]
use crate::crypto::Ss58Codec;
use crate::crypto::{
CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic, UncheckedFrom,
ByteArray, CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic,
UncheckedFrom,
};
#[cfg(feature = "full_crypto")]
use crate::{
@@ -113,17 +114,11 @@ impl Public {
}
}
impl TraitPublic for Public {
/// A new instance from the given slice that should be 33 bytes long.
///
/// NOTE: No checking goes on to ensure this is a real public key. Only use it if
/// you are certain that the array actually is a pubkey. GIGO!
fn from_slice(data: &[u8]) -> Self {
let mut r = [0u8; 33];
r.copy_from_slice(data);
Self(r)
}
impl ByteArray for Public {
const LEN: usize = 33;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
@@ -143,12 +138,6 @@ impl From<&Public> for CryptoTypePublicPair {
impl Derive for Public {}
impl Default for Public {
fn default() -> Self {
Public([0u8; 33])
}
}
impl AsRef<[u8]> for Public {
fn as_ref(&self) -> &[u8] {
&self.0[..]
@@ -165,11 +154,12 @@ impl sp_std::convert::TryFrom<&[u8]> for Public {
type Error = ();
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() == 33 {
Ok(Self::from_slice(data))
} else {
Err(())
if data.len() != Self::LEN {
return Err(())
}
let mut r = [0u8; Self::LEN];
r.copy_from_slice(data);
Ok(Self::unchecked_from(r))
}
}
@@ -340,6 +330,12 @@ impl sp_std::hash::Hash for Signature {
}
}
impl UncheckedFrom<[u8; 65]> for Signature {
fn unchecked_from(data: [u8; 65]) -> Signature {
Signature(data)
}
}
impl Signature {
/// A new instance from the given 65-byte `data`.
///
+19 -24
View File
@@ -22,7 +22,10 @@
#[cfg(feature = "full_crypto")]
use sp_std::vec::Vec;
use crate::hash::{H256, H512};
use crate::{
crypto::ByteArray,
hash::{H256, H512},
};
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
@@ -66,7 +69,6 @@ type Seed = [u8; 32];
Copy,
Encode,
Decode,
Default,
PassByInner,
MaxEncodedLen,
TypeInfo,
@@ -118,13 +120,12 @@ impl sp_std::convert::TryFrom<&[u8]> for Public {
type Error = ();
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() == 32 {
let mut inner = [0u8; 32];
inner.copy_from_slice(data);
Ok(Public(inner))
} else {
Err(())
if data.len() != Self::LEN {
return Err(())
}
let mut r = [0u8; Self::LEN];
r.copy_from_slice(data);
Ok(Self::unchecked_from(r))
}
}
@@ -258,12 +259,6 @@ impl Clone for Signature {
}
}
impl Default for Signature {
fn default() -> Self {
Signature([0u8; 64])
}
}
impl PartialEq for Signature {
fn eq(&self, b: &Self) -> bool {
self.0[..] == b.0[..]
@@ -321,6 +316,12 @@ impl sp_std::hash::Hash for Signature {
}
}
impl UncheckedFrom<[u8; 64]> for Signature {
fn unchecked_from(data: [u8; 64]) -> Signature {
Signature(data)
}
}
impl Signature {
/// A new instance from the given 64-byte `data`.
///
@@ -400,17 +401,11 @@ impl Public {
}
}
impl TraitPublic for Public {
/// A new instance from the given slice that should be 32 bytes long.
///
/// NOTE: No checking goes on to ensure this is a real public key. Only use it if
/// you are certain that the array actually is a pubkey. GIGO!
fn from_slice(data: &[u8]) -> Self {
let mut r = [0u8; 32];
r.copy_from_slice(data);
Public(r)
}
impl ByteArray for Public {
const LEN: usize = 32;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}
+1 -1
View File
@@ -76,7 +76,7 @@ pub use self::{
uint::{U256, U512},
};
#[cfg(feature = "full_crypto")]
pub use crypto::{DeriveJunction, Pair, Public};
pub use crypto::{ByteArray, DeriveJunction, Pair, Public};
#[cfg(feature = "std")]
pub use self::hasher::blake2::Blake2Hasher;
+16 -24
View File
@@ -41,7 +41,7 @@ use substrate_bip39::mini_secret_from_entropy;
use crate::{
crypto::{
CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic,
ByteArray, CryptoType, CryptoTypeId, CryptoTypePublicPair, Derive, Public as TraitPublic,
UncheckedFrom,
},
hash::{H256, H512},
@@ -74,7 +74,6 @@ pub const CRYPTO_ID: CryptoTypeId = CryptoTypeId(*b"sr25");
Copy,
Encode,
Decode,
Default,
PassByInner,
MaxEncodedLen,
TypeInfo,
@@ -147,13 +146,12 @@ impl sp_std::convert::TryFrom<&[u8]> for Public {
type Error = ();
fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
if data.len() == 32 {
let mut inner = [0u8; 32];
inner.copy_from_slice(data);
Ok(Public(inner))
} else {
Err(())
if data.len() != Self::LEN {
return Err(())
}
let mut r = [0u8; 32];
r.copy_from_slice(data);
Ok(Self::unchecked_from(r))
}
}
@@ -261,12 +259,6 @@ impl Clone for Signature {
}
}
impl Default for Signature {
fn default() -> Self {
Signature([0u8; 64])
}
}
impl PartialEq for Signature {
fn eq(&self, b: &Self) -> bool {
self.0[..] == b.0[..]
@@ -342,6 +334,12 @@ pub struct LocalizedSignature {
pub signature: Signature,
}
impl UncheckedFrom<[u8; 64]> for Signature {
fn unchecked_from(data: [u8; 64]) -> Signature {
Signature(data)
}
}
impl Signature {
/// A new instance from the given 64-byte `data`.
///
@@ -412,17 +410,11 @@ impl Public {
}
}
impl TraitPublic for Public {
/// A new instance from the given slice that should be 32 bytes long.
///
/// NOTE: No checking goes on to ensure this is a real public key. Only use it if
/// you are certain that the array actually is a pubkey. GIGO!
fn from_slice(data: &[u8]) -> Self {
let mut r = [0u8; 32];
r.copy_from_slice(data);
Public(r)
}
impl ByteArray for Public {
const LEN: usize = 32;
}
impl TraitPublic for Public {
fn to_public_crypto_pair(&self) -> CryptoTypePublicPair {
CryptoTypePublicPair(CRYPTO_ID, self.to_raw_vec())
}