Fix MultiSigner, simplify tests (#2033)

* Fix MultiSigner, use `into_signed_tx`

* Rebuild.
This commit is contained in:
Tomasz Drwięga
2019-04-23 18:18:32 +02:00
committed by Gavin Wood
parent b483c5608f
commit 08fda211d8
16 changed files with 101 additions and 46 deletions
+1
View File
@@ -4057,6 +4057,7 @@ version = "1.0.0"
dependencies = [
"hex-literal 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-primitives 1.0.0",
@@ -291,7 +291,6 @@ impl<Block, C, A> Proposer<Block, C, A> where
mod tests {
use super::*;
use codec::Encode;
use std::cell::RefCell;
use consensus_common::{Environment, Proposer};
use test_client::{self, runtime::{Extrinsic, Transfer}, AccountKeyring};
@@ -303,8 +302,7 @@ mod tests {
from: AccountKeyring::Alice.into(),
to: Default::default(),
};
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
tx.into_signed_tx()
}
#[test]
+2 -7
View File
@@ -46,7 +46,7 @@ mod tests {
use state_machine::backend::InMemory;
use test_client::{
runtime::genesismap::{GenesisConfig, additional_storage_with_genesis},
runtime::{Hash, Transfer, Block, BlockNumber, Header, Digest, Extrinsic},
runtime::{Hash, Transfer, Block, BlockNumber, Header, Digest},
AccountKeyring, AuthorityKeyring
};
use runtime_primitives::traits::BlakeTwo256;
@@ -68,12 +68,7 @@ mod tests {
) -> (Vec<u8>, Hash) {
use trie::ordered_trie_root;
let transactions = txs.into_iter().map(|tx| {
let signature = AccountKeyring::from_public(&tx.from).unwrap()
.sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
}).collect::<Vec<_>>();
let transactions = txs.into_iter().map(|tx| tx.into_signed_tx()).collect::<Vec<_>>();
let extrinsics_root = ordered_trie_root::<Blake2Hasher, _, _>(transactions.iter().map(Encode::encode)).into();
+1
View File
@@ -6,6 +6,7 @@ edition = "2018"
[dependencies]
substrate-primitives = { path = "../primitives" }
sr-primitives = { path = "../sr-primitives" }
hex-literal = { version = "0.1.0" }
lazy_static = { version = "1.0" }
strum = "0.14.0"
+6
View File
@@ -100,6 +100,12 @@ impl From<Keyring> for &'static str {
}
}
impl From<Keyring> for sr_primitives::MultiSigner {
fn from(x: Keyring) -> Self {
sr_primitives::MultiSigner::Ed25519(x.into())
}
}
lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> = {
Keyring::iter().map(|i| (i, i.pair())).collect()
+6
View File
@@ -96,6 +96,12 @@ impl From<Keyring> for &'static str {
}
}
impl From<Keyring> for sr_primitives::MultiSigner {
fn from(x: Keyring) -> Self {
sr_primitives::MultiSigner::Sr25519(x.into())
}
}
lazy_static! {
static ref PRIVATE_KEYS: HashMap<Keyring, Pair> = {
[
+8 -8
View File
@@ -23,7 +23,7 @@ mod sync;
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::AtomicBool;
use log::trace;
use client;
@@ -39,7 +39,6 @@ use futures::Future;
use futures::sync::{mpsc, oneshot};
use crate::message::Message;
use network_libp2p::PeerId;
use parity_codec::Encode;
use parking_lot::{Mutex, RwLock};
use primitives::{H256, ed25519::Public as AuthorityId};
use crate::protocol::{ConnectedPeer, Context, FromNetworkMsg, Protocol, ProtocolMsg};
@@ -333,14 +332,16 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
self.net_proto_channel.send_from_client(ProtocolMsg::BlockImported(hash, header.clone()));
}
// SyncOracle: are we connected to any peer?
/// SyncOracle: are we connected to any peer?
#[cfg(test)]
pub fn is_offline(&self) -> bool {
self.is_offline.load(Ordering::Relaxed)
self.is_offline.load(std::sync::atomic::Ordering::Relaxed)
}
// SyncOracle: are we in the process of catching-up with the chain?
/// SyncOracle: are we in the process of catching-up with the chain?
#[cfg(test)]
pub fn is_major_syncing(&self) -> bool {
self.is_major_syncing.load(Ordering::Relaxed)
self.is_major_syncing.load(std::sync::atomic::Ordering::Relaxed)
}
/// Called on connection to other indicated peer.
@@ -533,8 +534,7 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
amount: 1,
nonce,
};
let signature = AccountKeyring::from_public(&transfer.from).unwrap().sign(&transfer.encode()).into();
builder.push(Extrinsic::Transfer(transfer, signature)).unwrap();
builder.push(transfer.into_signed_tx()).unwrap();
nonce = nonce + 1;
builder.bake().unwrap()
})
+2 -4
View File
@@ -34,8 +34,7 @@ fn uxt(sender: AccountKeyring, nonce: u64) -> Extrinsic {
from: sender.into(),
to: Default::default(),
};
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
tx.into_signed_tx()
}
#[test]
@@ -106,8 +105,7 @@ fn should_watch_extrinsic() {
from: AccountKeyring::Alice.into(),
to: Default::default(),
};
let signature = AccountKeyring::from_public(&tx.from).unwrap().sign(&tx.encode()).into();
Extrinsic::Transfer(tx, signature)
tx.into_signed_tx()
};
AuthorApi::submit_extrinsic(&p, replacement.encode().into()).unwrap();
let (res, data) = runtime.block_on(data.into_future()).unwrap();
+2 -4
View File
@@ -561,9 +561,8 @@ impl<Factory: ServiceFactory> Components for LightComponents<Factory> {
#[cfg(test)]
mod tests {
use super::*;
use parity_codec::Encode;
use consensus_common::BlockOrigin;
use substrate_test_client::{self, TestClient, AccountKeyring, runtime::{Extrinsic, Transfer}};
use substrate_test_client::{self, TestClient, AccountKeyring, runtime::Transfer};
#[test]
fn should_remove_transactions_from_the_pool() {
@@ -576,8 +575,7 @@ mod tests {
from: AccountKeyring::Alice.into(),
to: Default::default(),
};
let signature = AccountKeyring::from_public(&transfer.from).unwrap().sign(&transfer.encode()).into();
Extrinsic::Transfer(transfer, signature)
transfer.into_signed_tx()
};
// store the transaction in the pool
pool.submit_one(&BlockId::hash(client.best_block_header().unwrap().hash()), transaction.clone()).unwrap();
+60 -3
View File
@@ -30,7 +30,7 @@ pub use serde_derive;
pub use runtime_io::{StorageOverlay, ChildrenStorageOverlay};
use rstd::prelude::*;
use substrate_primitives::{ed25519, sr25519, hash::H512};
use substrate_primitives::{crypto, ed25519, sr25519, hash::{H256, H512}};
use codec::{Encode, Decode};
#[cfg(feature = "std")]
@@ -310,6 +310,18 @@ pub enum MultiSignature {
Sr25519(sr25519::Signature),
}
impl From<ed25519::Signature> for MultiSignature {
fn from(x: ed25519::Signature) -> Self {
MultiSignature::Ed25519(x)
}
}
impl From<sr25519::Signature> for MultiSignature {
fn from(x: sr25519::Signature) -> Self {
MultiSignature::Sr25519(x)
}
}
impl Default for MultiSignature {
fn default() -> Self {
MultiSignature::Ed25519(Default::default())
@@ -332,6 +344,45 @@ impl Default for MultiSigner {
}
}
/// NOTE: This implementations is required by `SimpleAddressDeterminator`,
/// we convert the hash into some AccountId, it's fine to use any scheme.
impl<T: Into<H256>> crypto::UncheckedFrom<T> for MultiSigner {
fn unchecked_from(x: T) -> Self {
ed25519::Public::unchecked_from(x.into()).into()
}
}
impl AsRef<[u8]> for MultiSigner {
fn as_ref(&self) -> &[u8] {
match *self {
MultiSigner::Ed25519(ref who) => who.as_ref(),
MultiSigner::Sr25519(ref who) => who.as_ref(),
}
}
}
impl From<ed25519::Public> for MultiSigner {
fn from(x: ed25519::Public) -> Self {
MultiSigner::Ed25519(x)
}
}
impl From<sr25519::Public> for MultiSigner {
fn from(x: sr25519::Public) -> Self {
MultiSigner::Sr25519(x)
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for MultiSigner {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
MultiSigner::Ed25519(ref who) => write!(fmt, "ed25519: {}", who),
MultiSigner::Sr25519(ref who) => write!(fmt, "sr25519: {}", who),
}
}
}
impl Verify for MultiSignature {
type Signer = MultiSigner;
fn verify<L: Lazy<[u8]>>(&self, msg: L, signer: &Self::Signer) -> bool {
@@ -357,8 +408,14 @@ impl Verify for AnySignature {
}
impl From<sr25519::Signature> for AnySignature {
fn from(s: sr25519::Signature) -> AnySignature {
AnySignature(s.0.into())
fn from(s: sr25519::Signature) -> Self {
AnySignature(s.into())
}
}
impl From<ed25519::Signature> for AnySignature {
fn from(s: ed25519::Signature) -> Self {
AnySignature(s.into())
}
}
@@ -17,7 +17,6 @@
//! Block Builder extensions for tests.
use client;
use super::AccountKeyring;
use runtime;
use runtime_primitives::traits::ProvideRuntimeApi;
use client::block_builder::api::BlockBuilder;
@@ -33,13 +32,6 @@ impl<'a, A> BlockBuilderExt for client::block_builder::BlockBuilder<'a, runtime:
A::Api: BlockBuilder<runtime::Block>
{
fn push_transfer(&mut self, transfer: runtime::Transfer) -> Result<(), client::error::Error> {
self.push(sign_tx(transfer))
self.push(transfer.into_signed_tx())
}
}
fn sign_tx(transfer: runtime::Transfer) -> runtime::Extrinsic {
let signature = AccountKeyring::from_public(&transfer.from)
.unwrap()
.sign(&parity_codec::Encode::encode(&transfer));
runtime::Extrinsic::Transfer(transfer, signature)
}
+7 -7
View File
@@ -38,7 +38,7 @@ use runtime_primitives::{
create_runtime_str,
traits::{
BlindCheckable, BlakeTwo256, Block as BlockT, Extrinsic as ExtrinsicT,
GetNodeBlockType, GetRuntimeBlockType, AuthorityIdFor,
GetNodeBlockType, GetRuntimeBlockType, AuthorityIdFor, Verify,
},
};
use runtime_version::RuntimeVersion;
@@ -142,14 +142,14 @@ impl Extrinsic {
}
}
// The identity type used by authorities.
pub type AuthorityId = ed25519::Public;
// The signature type used by authorities.
/// The signature type used by authorities.
pub type AuthoritySignature = ed25519::Signature;
/// An identifier for an account on this system.
pub type AccountId = sr25519::Public;
// The signature type used by accounts/transactions.
/// The identity type used by authorities.
pub type AuthorityId = <AuthoritySignature as Verify>::Signer;
/// The signature type used by accounts/transactions.
pub type AccountSignature = sr25519::Signature;
/// An identifier for an account on this system.
pub type AccountId = <AccountSignature as Verify>::Signer;
/// A simple hash type for all our hashing.
pub type Hash = H256;
/// The block number type used in this runtime.
+1
View File
@@ -2373,6 +2373,7 @@ version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-primitives 1.0.0",
+1
View File
@@ -2535,6 +2535,7 @@ version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-primitives 1.0.0",
+1 -1
View File
@@ -60,7 +60,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 63,
impl_version: 65,
impl_version: 66,
apis: RUNTIME_API_VERSIONS,
};
+1
View File
@@ -2690,6 +2690,7 @@ version = "1.0.0"
dependencies = [
"hex-literal 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sr-primitives 1.0.0",
"strum 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"substrate-primitives 1.0.0",