diff --git a/Cargo.lock b/Cargo.lock index 93cb8a6f23448..34b50ae3bd33f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4614,8 +4614,10 @@ dependencies = [ "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex-literal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "impl-serde 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "primitive-types 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/core/primitives/Cargo.toml b/core/primitives/Cargo.toml index 94dac26bec96c..1d55f82fab230 100644 --- a/core/primitives/Cargo.toml +++ b/core/primitives/Cargo.toml @@ -28,6 +28,8 @@ hex = { version = "0.3", optional = true } regex = { version = "1.1", optional = true } num-traits = { version = "0.2", default-features = false } zeroize = { version = "0.9.2", default-features = false } +lazy_static = { version = "1.3", optional = true } +parking_lot = { version = "0.8", optional = true } [dev-dependencies] substrate-serializer = { path = "../serializer" } @@ -47,6 +49,8 @@ bench = false default = ["std"] std = [ "wasmi", + "lazy_static", + "parking_lot", "primitive-types/std", "primitive-types/serde", "primitive-types/byteorder", diff --git a/core/primitives/src/crypto.rs b/core/primitives/src/crypto.rs index 6aac4e08bcdc6..9654a948afd74 100644 --- a/core/primitives/src/crypto.rs +++ b/core/primitives/src/crypto.rs @@ -18,6 +18,10 @@ //! Cryptographic utilities. // end::description[] +#[cfg(feature = "std")] +use std::convert::{TryFrom, TryInto}; +#[cfg(feature = "std")] +use parking_lot::Mutex; #[cfg(feature = "std")] use rand::{RngCore, rngs::OsRng}; #[cfg(feature = "std")] @@ -243,12 +247,23 @@ pub enum PublicError { #[cfg(feature = "std")] pub trait Ss58Codec: Sized { /// Some if the string is a properly encoded SS58Check address. - fn from_ss58check(s: &str) -> Result; + fn from_ss58check(s: &str) -> Result { + Self::from_ss58check_with_version(s) + .and_then(|(r, v)| match v { + Ss58AddressFormat::SubstrateAccountDirect => Ok(r), + v if v == *DEFAULT_VERSION.lock() => Ok(r), + _ => Err(PublicError::UnknownVersion), + }) + } + /// Some if the string is a properly encoded SS58Check address. + fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError>; /// Some if the string is a properly encoded SS58Check address, optionally with /// a derivation path following. fn from_string(s: &str) -> Result { Self::from_ss58check(s) } /// Return the ss58-check string for this key. - fn to_ss58check(&self) -> String; + fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String; + /// Return the ss58-check string for this key. + fn to_ss58check(&self) -> String { self.to_ss58check_with_version(*DEFAULT_VERSION.lock()) } } #[cfg(feature = "std")] @@ -273,9 +288,92 @@ fn ss58hash(data: &[u8]) -> blake2_rfc::blake2b::Blake2bResult { context.finalize() } +#[cfg(feature = "std")] +lazy_static::lazy_static! { + static ref DEFAULT_VERSION: Mutex + = Mutex::new(Ss58AddressFormat::SubstrateAccountDirect); +} + +/// A known address (sub)format/network ID for SS58. +#[cfg(feature = "std")] +#[derive(Copy, Clone, PartialEq, Eq)] +pub enum Ss58AddressFormat { + /// Any Substrate network, direct checksum, standard account (*25519). + SubstrateAccountDirect, + /// Polkadot Relay-chain, direct checksum, standard account (*25519). + PolkadotAccountDirect, + /// Kusama Relay-chain, direct checksum, standard account (*25519). + KusamaAccountDirect, + /// Override with a manually provided numeric value. + Override(u8), +} + +#[cfg(feature = "std")] +impl From for u8 { + fn from(x: Ss58AddressFormat) -> u8 { + match x { + Ss58AddressFormat::SubstrateAccountDirect => 42, + Ss58AddressFormat::PolkadotAccountDirect => 0, + Ss58AddressFormat::KusamaAccountDirect => 4, + Ss58AddressFormat::Override(n) => n, + } + } +} + +#[cfg(feature = "std")] +impl TryFrom for Ss58AddressFormat { + type Error = (); + fn try_from(x: u8) -> Result { + match x { + 42 => Ok(Ss58AddressFormat::SubstrateAccountDirect), + 0 => Ok(Ss58AddressFormat::PolkadotAccountDirect), + 2 => Ok(Ss58AddressFormat::KusamaAccountDirect), + _ => Err(()), + } + } +} + +#[cfg(feature = "std")] +impl<'a> TryFrom<&'a str> for Ss58AddressFormat { + type Error = (); + fn try_from(x: &'a str) -> Result { + match x { + "substrate" => Ok(Ss58AddressFormat::SubstrateAccountDirect), + "polkadot" => Ok(Ss58AddressFormat::PolkadotAccountDirect), + "kusama" => Ok(Ss58AddressFormat::KusamaAccountDirect), + a => a.parse::().map(Ss58AddressFormat::Override).map_err(|_| ()), + } + } +} + +#[cfg(feature = "std")] +impl From for &'static str { + fn from(x: Ss58AddressFormat) -> &'static str { + match x { + Ss58AddressFormat::SubstrateAccountDirect => "substrate", + Ss58AddressFormat::PolkadotAccountDirect => "polkadot", + Ss58AddressFormat::KusamaAccountDirect => "kusama", + Ss58AddressFormat::Override(_) => "(override)", + } + } +} + +/// Set the default "version" (actually, this is a bit of a misnomer and the version byte is +/// typically used not just to encode format/version but also network identity) that is used for +/// encoding and decoding SS58 addresses. If an unknown version is provided then it fails. +/// +/// Current known "versions" are: +/// - 0 direct (payload) checksum for 32-byte *25519 Polkadot addresses. +/// - 2 direct (payload) checksum for 32-byte *25519 Polkadot Milestone 'K' addresses. +/// - 42 direct (payload) checksum for 32-byte *25519 addresses on any Substrate-based network. +#[cfg(feature = "std")] +pub fn set_default_ss58_version(version: Ss58AddressFormat) { + *DEFAULT_VERSION.lock() = version +} + #[cfg(feature = "std")] impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { - fn from_ss58check(s: &str) -> Result { + fn from_ss58check_with_version(s: &str) -> Result<(Self, Ss58AddressFormat), PublicError> { let mut res = T::default(); let len = res.as_mut().len(); let d = s.from_base58().map_err(|_| PublicError::BadBase58)?; // failure here would be invalid encoding. @@ -283,21 +381,18 @@ impl + AsRef<[u8]> + Default + Derive> Ss58Codec for T { // Invalid length. return Err(PublicError::BadLength); } - if d[0] != 42 { - // Invalid version. - return Err(PublicError::UnknownVersion); - } + let ver = d[0].try_into().map_err(|_: ()| PublicError::UnknownVersion)?; if d[len+1..len+3] != ss58hash(&d[0..len+1]).as_bytes()[0..2] { // Invalid checksum. return Err(PublicError::InvalidChecksum); } res.as_mut().copy_from_slice(&d[1..len+1]); - Ok(res) + Ok((res, ver)) } - fn to_ss58check(&self) -> String { - let mut v = vec![42u8]; + fn to_ss58check_with_version(&self, version: Ss58AddressFormat) -> String { + let mut v = vec![version.into()]; v.extend(self.as_ref()); let r = ss58hash(&v); v.extend(&r.as_bytes()[0..2]); diff --git a/core/sr-primitives/src/generic/checked_extrinsic.rs b/core/sr-primitives/src/generic/checked_extrinsic.rs index ee43b3af2e951..bd9a711fc0e54 100644 --- a/core/sr-primitives/src/generic/checked_extrinsic.rs +++ b/core/sr-primitives/src/generic/checked_extrinsic.rs @@ -17,46 +17,77 @@ //! Generic implementation of an extrinsic that has passed the verification //! stage. -use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay}; +use rstd::result::Result; +use crate::traits::{ + self, Member, MaybeDisplay, SignedExtension, DispatchError, Dispatchable, DispatchResult, + ValidateUnsigned +}; use crate::weights::{Weighable, Weight}; +use crate::transaction_validity::TransactionValidity; /// Definition of something that the external world might want to say; its /// existence implies that it has been checked and is good, particularly with /// regards to the signature. #[derive(PartialEq, Eq, Clone)] #[cfg_attr(feature = "std", derive(Debug))] -pub struct CheckedExtrinsic { +pub struct CheckedExtrinsic { /// Who this purports to be from and the number of extrinsics have come before /// from the same signer, if anyone (note this is not a signature). - pub signed: Option<(AccountId, Index)>, + pub signed: Option<(AccountId, Extra)>, + /// The function that should be called. pub function: Call, } -impl traits::Applyable for CheckedExtrinsic +impl traits::Applyable +for + CheckedExtrinsic where AccountId: Member + MaybeDisplay, - Index: Member + MaybeDisplay + SimpleArithmetic, - Call: Member, + Call: Member + Dispatchable, + Extra: SignedExtension, + Origin: From>, { - type Index = Index; type AccountId = AccountId; - type Call = Call; - fn index(&self) -> Option<&Self::Index> { - self.signed.as_ref().map(|x| &x.1) - } + type Call = Call; fn sender(&self) -> Option<&Self::AccountId> { self.signed.as_ref().map(|x| &x.0) } - fn deconstruct(self) -> (Self::Call, Option) { - (self.function, self.signed.map(|x| x.0)) + fn validate>(&self, + weight: crate::weights::Weight, + ) -> TransactionValidity { + if let Some((ref id, ref extra)) = self.signed { + Extra::validate(extra, id, weight).into() + } else { + match Extra::validate_unsigned(weight) { + Ok(extra) => match U::validate_unsigned(&self.function) { + TransactionValidity::Valid(v) => + TransactionValidity::Valid(v.combine_with(extra)), + x => x, + }, + x => x.into(), + } + } + } + + fn dispatch(self, + weight: crate::weights::Weight, + ) -> Result { + let maybe_who = if let Some((id, extra)) = self.signed { + Extra::pre_dispatch(extra, &id, weight)?; + Some(id) + } else { + Extra::pre_dispatch_unsigned(weight)?; + None + }; + Ok(self.function.dispatch(Origin::from(maybe_who))) } } -impl Weighable for CheckedExtrinsic +impl Weighable for CheckedExtrinsic where Call: Weighable, { diff --git a/core/sr-primitives/src/generic/mod.rs b/core/sr-primitives/src/generic/mod.rs index a4e4106780efc..1511753d2c524 100644 --- a/core/sr-primitives/src/generic/mod.rs +++ b/core/sr-primitives/src/generic/mod.rs @@ -19,8 +19,6 @@ // end::description[] mod unchecked_extrinsic; -mod unchecked_mortal_extrinsic; -mod unchecked_mortal_compact_extrinsic; mod era; mod checked_extrinsic; mod header; @@ -30,8 +28,6 @@ mod digest; mod tests; pub use self::unchecked_extrinsic::UncheckedExtrinsic; -pub use self::unchecked_mortal_extrinsic::UncheckedMortalExtrinsic; -pub use self::unchecked_mortal_compact_extrinsic::UncheckedMortalCompactExtrinsic; pub use self::era::{Era, Phase}; pub use self::checked_extrinsic::CheckedExtrinsic; pub use self::header::Header; diff --git a/core/sr-primitives/src/generic/unchecked_extrinsic.rs b/core/sr-primitives/src/generic/unchecked_extrinsic.rs index d6e0d60e2c218..a500283b7a947 100644 --- a/core/sr-primitives/src/generic/unchecked_extrinsic.rs +++ b/core/sr-primitives/src/generic/unchecked_extrinsic.rs @@ -20,48 +20,44 @@ use std::fmt; use rstd::prelude::*; -use crate::codec::{Decode, Encode, Codec, Input, HasCompact}; -use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay, Lookup, Extrinsic}; -use super::CheckedExtrinsic; +use runtime_io::blake2_256; +use crate::codec::{Decode, Encode, Input}; +use crate::traits::{ + self, Member, SimpleArithmetic, MaybeDisplay, CurrentHeight, SignedExtension, + BlockNumberToHash, Lookup, Checkable, Extrinsic, SaturatedConversion +}; +use super::{CheckedExtrinsic, Era}; -#[derive(PartialEq, Eq, Clone, Encode, Decode)] -pub struct SignatureContent -where - Address: Codec, - Index: HasCompact + Codec, - Signature: Codec, -{ - signed: Address, - signature: Signature, - index: Index, -} +const TRANSACTION_VERSION: u8 = 1; /// A extrinsic right from the external world. This is unchecked and so /// can contain a signature. #[derive(PartialEq, Eq, Clone)] -pub struct UncheckedExtrinsic +pub struct UncheckedExtrinsic where - Address: Codec, - Index: HasCompact + Codec, - Signature: Codec, + Extra: SignedExtension { - /// The signature, address and number of extrinsics have come before from - /// the same signer, if this is a signed extrinsic. - pub signature: Option>, + /// The signature, address, number of extrinsics have come before from + /// the same signer and an era describing the longevity of this transaction, + /// if this is a signed extrinsic. + pub signature: Option<(Address, Signature, Era, Extra)>, /// The function that should be called. pub function: Call, } -impl UncheckedExtrinsic -where - Address: Codec, - Index: HasCompact + Codec, - Signature: Codec, +impl + UncheckedExtrinsic { /// New instance of a signed extrinsic aka "transaction". - pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature) -> Self { + pub fn new_signed( + function: Call, + signed: Address, + signature: Signature, + era: Era, + extra: Extra + ) -> Self { UncheckedExtrinsic { - signature: Some(SignatureContent{signed, signature, index}), + signature: Some((signed, signature, era, extra)), function, } } @@ -75,29 +71,52 @@ where } } -impl traits::Checkable - for UncheckedExtrinsic +impl Extrinsic + for UncheckedExtrinsic +{ + fn is_signed(&self) -> Option { + Some(self.signature.is_some()) + } +} + +impl + Checkable +for + UncheckedExtrinsic where - Address: Member + MaybeDisplay + Codec, - Index: Member + MaybeDisplay + SimpleArithmetic + Codec, + Address: Member + MaybeDisplay, Call: Encode + Member, - Signature: Member + traits::Verify + Codec, + Signature: Member + traits::Verify, + Extra: SignedExtension, AccountId: Member + MaybeDisplay, - Context: Lookup, + BlockNumber: SimpleArithmetic, + Hash: Encode, + Context: Lookup + + CurrentHeight + + BlockNumberToHash, { - type Checked = CheckedExtrinsic; + type Checked = CheckedExtrinsic; fn check(self, context: &Context) -> Result { Ok(match self.signature { - Some(SignatureContent{signed, signature, index}) => { - let payload = (index, self.function); + Some((signed, signature, era, extra)) => { + let current_u64 = context.current_height().saturated_into::(); + let h = context.block_number_to_hash(era.birth(current_u64).saturated_into()) + .ok_or("transaction birth block ancient")?; let signed = context.lookup(signed)?; - if !crate::verify_encoded_lazy(&signature, &payload, &signed) { + let raw_payload = (self.function, era, h, extra); + if !raw_payload.using_encoded(|payload| { + if payload.len() > 256 { + signature.verify(&blake2_256(payload)[..], &signed) + } else { + signature.verify(payload, &signed) + } + }) { return Err(crate::BAD_SIGNATURE) } CheckedExtrinsic { - signed: Some((signed, payload.0)), - function: payload.1, + signed: Some((signed, raw_payload.3)), + function: raw_payload.0, } } None => CheckedExtrinsic { @@ -108,19 +127,13 @@ where } } -impl< - Address: Codec, - Index: HasCompact + Codec, - Signature: Codec, - Call, -> Extrinsic for UncheckedExtrinsic { - fn is_signed(&self) -> Option { - Some(self.signature.is_some()) - } -} - -impl Decode - for UncheckedExtrinsic +impl Decode + for UncheckedExtrinsic +where + Address: Decode, + Signature: Decode, + Call: Decode, + Extra: SignedExtension, { fn decode(input: &mut I) -> Option { // This is a little more complicated than usual since the binary format must be compatible @@ -129,70 +142,188 @@ impl // to use this). let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?; + let version = input.read_byte()?; + + let is_signed = version & 0b1000_0000 != 0; + let version = version & 0b0111_1111; + if version != TRANSACTION_VERSION { + return None + } + Some(UncheckedExtrinsic { - signature: Decode::decode(input)?, + signature: if is_signed { Some(Decode::decode(input)?) } else { None }, function: Decode::decode(input)?, }) } } -impl Encode - for UncheckedExtrinsic +impl Encode + for UncheckedExtrinsic +where + Address: Encode, + Signature: Encode, + Call: Encode, + Extra: SignedExtension, { fn encode(&self) -> Vec { super::encode_with_vec_prefix::(|v| { - self.signature.encode_to(v); + // 1 byte version id. + match self.signature.as_ref() { + Some(s) => { + v.push(TRANSACTION_VERSION | 0b1000_0000); + s.encode_to(v); + } + None => { + v.push(TRANSACTION_VERSION & 0b0111_1111); + } + } self.function.encode_to(v); }) } } #[cfg(feature = "std")] -impl serde::Serialize - for UncheckedExtrinsic +impl serde::Serialize + for UncheckedExtrinsic { fn serialize(&self, seq: S) -> Result where S: ::serde::Serializer { - self.using_encoded(|bytes| ::substrate_primitives::bytes::serialize(bytes, seq)) + self.using_encoded(|bytes| seq.serialize_bytes(bytes)) } } #[cfg(feature = "std")] -impl fmt::Debug - for UncheckedExtrinsic +impl fmt::Debug + for UncheckedExtrinsic where - Address: fmt::Debug + Codec, - Index: fmt::Debug + HasCompact + Codec, - Signature: Codec, + Address: fmt::Debug, Call: fmt::Debug, + Extra: SignedExtension, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "UncheckedExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.signed, &x.index)), self.function) + write!(f, "UncheckedExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.0, &x.2, &x.3)), self.function) } } #[cfg(test)] -mod test { - use crate::codec::{Decode, Encode}; - use super::UncheckedExtrinsic; +mod tests { + use super::*; + use runtime_io::blake2_256; + use crate::codec::{Encode, Decode}; + use crate::traits::{SignedExtension, DispatchError}; + use serde::{Serialize, Deserialize}; + + struct TestContext; + impl Lookup for TestContext { + type Source = u64; + type Target = u64; + fn lookup(&self, s: u64) -> Result { Ok(s) } + } + impl CurrentHeight for TestContext { + type BlockNumber = u64; + fn current_height(&self) -> u64 { 42 } + } + impl BlockNumberToHash for TestContext { + type BlockNumber = u64; + type Hash = u64; + fn block_number_to_hash(&self, n: u64) -> Option { Some(n) } + } + + #[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)] + struct TestSig(u64, Vec); + impl traits::Verify for TestSig { + type Signer = u64; + fn verify>(&self, mut msg: L, signer: &Self::Signer) -> bool { + *signer == self.0 && msg.get() == &self.1[..] + } + } + + const DUMMY_ACCOUNTID: u64 = 0; + + #[derive(Debug, Encode, Decode, Clone, Eq, PartialEq, Ord, PartialOrd)] + struct TestExtra; + impl SignedExtension for TestExtra { + type AccountId = u64; + } + type Ex = UncheckedExtrinsic, TestSig, TestExtra>; + type CEx = CheckedExtrinsic, TestExtra>; #[test] - fn encoding_matches_vec() { - type Extrinsic = UncheckedExtrinsic; - let ex = Extrinsic::new_unsigned(42); - let encoded = ex.encode(); - let decoded = Extrinsic::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(decoded, ex); - let as_vec: Vec = Decode::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(as_vec.encode(), encoded); + fn unsigned_codec_should_work() { + let ux = Ex::new_unsigned(vec![0u8;0]); + let encoded = ux.encode(); + assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); + } + + #[test] + fn signed_codec_should_work() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal(), TestExtra); + let encoded = ux.encode(); + assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); + } + + #[test] + fn large_signed_codec_should_work() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8; 257], Era::immortal(), 0u64).using_encoded(blake2_256)[..].to_owned()), Era::immortal(), TestExtra); + let encoded = ux.encode(); + assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); + } + + #[test] + fn unsigned_check_should_work() { + let ux = Ex::new_unsigned(vec![0u8;0]); + assert!(!ux.is_signed().unwrap_or(false)); + assert!(>::check(ux, &TestContext).is_ok()); + } + + #[test] + fn badly_signed_check_should_fail() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, vec![0u8]), Era::immortal(), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); + } + + #[test] + fn immortal_signed_check_should_work() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal(), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0, TestExtra)), function: vec![0u8;0] })); } + #[test] + fn mortal_signed_check_should_work() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 42), 42u64).encode()), Era::mortal(32, 42), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0, TestExtra)), function: vec![0u8;0] })); + } #[test] - #[cfg(feature = "std")] - fn serialization_of_unchecked_extrinsics() { - type Extrinsic = UncheckedExtrinsic; - let ex = Extrinsic::new_unsigned(42); + fn later_mortal_signed_check_should_work() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 11), 11u64).encode()), Era::mortal(32, 11), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0, TestExtra)), function: vec![0u8;0] })); + } - assert_eq!(serde_json::to_string(&ex).unwrap(), "\"0x14002a000000\""); + #[test] + fn too_late_mortal_signed_check_should_fail() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 10), 10u64).encode()), Era::mortal(32, 10), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); + } + + #[test] + fn too_early_mortal_signed_check_should_fail() { + let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 43), 43u64).encode()), Era::mortal(32, 43), TestExtra); + assert!(ux.is_signed().unwrap_or(false)); + assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); + } + + #[test] + fn encoding_matches_vec() { + let ex = Ex::new_unsigned(vec![0u8;0]); + let encoded = ex.encode(); + let decoded = Ex::decode(&mut encoded.as_slice()).unwrap(); + assert_eq!(decoded, ex); + let as_vec: Vec = Decode::decode(&mut encoded.as_slice()).unwrap(); + assert_eq!(as_vec.encode(), encoded); } } diff --git a/core/sr-primitives/src/generic/unchecked_mortal_compact_extrinsic.rs b/core/sr-primitives/src/generic/unchecked_mortal_compact_extrinsic.rs deleted file mode 100644 index 36e17fc277cde..0000000000000 --- a/core/sr-primitives/src/generic/unchecked_mortal_compact_extrinsic.rs +++ /dev/null @@ -1,306 +0,0 @@ -// Copyright 2017-2019 Parity Technologies (UK) Ltd. -// This file is part of Substrate. - -// Substrate is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Substrate is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Substrate. If not, see . - -//! Generic implementation of an unchecked (pre-verification) extrinsic. - -#[cfg(feature = "std")] -use std::fmt; - -use rstd::prelude::*; -use runtime_io::blake2_256; -use crate::codec::{Decode, Encode, Input, Compact}; -use crate::traits::{self, Member, SimpleArithmetic, MaybeDisplay, CurrentHeight, BlockNumberToHash, - Lookup, Checkable, Extrinsic, SaturatedConversion}; -use super::{CheckedExtrinsic, Era}; - -const TRANSACTION_VERSION: u8 = 1; - -/// A extrinsic right from the external world. This is unchecked and so -/// can contain a signature. -#[derive(PartialEq, Eq, Clone)] -pub struct UncheckedMortalCompactExtrinsic { - /// The signature, address, number of extrinsics have come before from - /// the same signer and an era describing the longevity of this transaction, - /// if this is a signed extrinsic. - pub signature: Option<(Address, Signature, Compact, Era)>, - /// The function that should be called. - pub function: Call, -} - -impl UncheckedMortalCompactExtrinsic { - /// New instance of a signed extrinsic aka "transaction". - pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature, era: Era) -> Self { - UncheckedMortalCompactExtrinsic { - signature: Some((signed, signature, index.into(), era)), - function, - } - } - - /// New instance of an unsigned extrinsic aka "inherent". - pub fn new_unsigned(function: Call) -> Self { - UncheckedMortalCompactExtrinsic { - signature: None, - function, - } - } -} - -impl Extrinsic for UncheckedMortalCompactExtrinsic { - fn is_signed(&self) -> Option { - Some(self.signature.is_some()) - } -} - -impl Checkable - for UncheckedMortalCompactExtrinsic -where - Address: Member + MaybeDisplay, - Index: Member + MaybeDisplay + SimpleArithmetic, - Compact: Encode, - Call: Encode + Member, - Signature: Member + traits::Verify, - AccountId: Member + MaybeDisplay, - BlockNumber: SimpleArithmetic, - Hash: Encode, - Context: Lookup - + CurrentHeight - + BlockNumberToHash, -{ - type Checked = CheckedExtrinsic; - - fn check(self, context: &Context) -> Result { - Ok(match self.signature { - Some((signed, signature, index, era)) => { - let current_u64 = context.current_height().saturated_into::(); - let h = context.block_number_to_hash(era.birth(current_u64).saturated_into()) - .ok_or("transaction birth block ancient")?; - let signed = context.lookup(signed)?; - let raw_payload = (index, self.function, era, h); - if !raw_payload.using_encoded(|payload| { - if payload.len() > 256 { - signature.verify(&blake2_256(payload)[..], &signed) - } else { - signature.verify(payload, &signed) - } - }) { - return Err(crate::BAD_SIGNATURE) - } - CheckedExtrinsic { - signed: Some((signed, (raw_payload.0).0)), - function: raw_payload.1, - } - } - None => CheckedExtrinsic { - signed: None, - function: self.function, - }, - }) - } -} - -impl Decode - for UncheckedMortalCompactExtrinsic -where - Address: Decode, - Signature: Decode, - Compact: Decode, - Call: Decode, -{ - fn decode(input: &mut I) -> Option { - // This is a little more complicated than usual since the binary format must be compatible - // with substrate's generic `Vec` type. Basically this just means accepting that there - // will be a prefix of vector length (we don't need - // to use this). - let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?; - - let version = input.read_byte()?; - - let is_signed = version & 0b1000_0000 != 0; - let version = version & 0b0111_1111; - if version != TRANSACTION_VERSION { - return None - } - - Some(UncheckedMortalCompactExtrinsic { - signature: if is_signed { Some(Decode::decode(input)?) } else { None }, - function: Decode::decode(input)?, - }) - } -} - -impl Encode - for UncheckedMortalCompactExtrinsic -where - Address: Encode, - Signature: Encode, - Compact: Encode, - Call: Encode, -{ - fn encode(&self) -> Vec { - super::encode_with_vec_prefix::(|v| { - // 1 byte version id. - match self.signature.as_ref() { - Some(s) => { - v.push(TRANSACTION_VERSION | 0b1000_0000); - s.encode_to(v); - } - None => { - v.push(TRANSACTION_VERSION & 0b0111_1111); - } - } - self.function.encode_to(v); - }) - } -} - -#[cfg(feature = "std")] -impl serde::Serialize - for UncheckedMortalCompactExtrinsic - where Compact: Encode -{ - fn serialize(&self, seq: S) -> Result where S: ::serde::Serializer { - self.using_encoded(|bytes| seq.serialize_bytes(bytes)) - } -} - -#[cfg(feature = "std")] -impl fmt::Debug for UncheckedMortalCompactExtrinsic where - Address: fmt::Debug, - Index: fmt::Debug, - Call: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "UncheckedMortalCompactExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.0, &x.2)), self.function) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use runtime_io::blake2_256; - use crate::codec::{Encode, Decode}; - use serde::{Serialize, Deserialize}; - - struct TestContext; - impl Lookup for TestContext { - type Source = u64; - type Target = u64; - fn lookup(&self, s: u64) -> Result { Ok(s) } - } - impl CurrentHeight for TestContext { - type BlockNumber = u64; - fn current_height(&self) -> u64 { 42 } - } - impl BlockNumberToHash for TestContext { - type BlockNumber = u64; - type Hash = u64; - fn block_number_to_hash(&self, n: u64) -> Option { Some(n) } - } - - #[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)] - struct TestSig(u64, Vec); - impl traits::Verify for TestSig { - type Signer = u64; - fn verify>(&self, mut msg: L, signer: &Self::Signer) -> bool { - *signer == self.0 && msg.get() == &self.1[..] - } - } - - const DUMMY_ACCOUNTID: u64 = 0; - - type Ex = UncheckedMortalCompactExtrinsic, TestSig>; - type CEx = CheckedExtrinsic>; - - #[test] - fn unsigned_codec_should_work() { - let ux = Ex::new_unsigned(vec![0u8;0]); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn signed_codec_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal()); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn large_signed_codec_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8; 257], Era::immortal(), 0u64).using_encoded(blake2_256)[..].to_owned()), Era::immortal()); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn unsigned_check_should_work() { - let ux = Ex::new_unsigned(vec![0u8;0]); - assert!(!ux.is_signed().unwrap_or(false)); - assert!(>::check(ux, &TestContext).is_ok()); - } - - #[test] - fn badly_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, vec![0u8]), Era::immortal()); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn immortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal()); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn mortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 42), 42u64).encode()), Era::mortal(32, 42)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn later_mortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (Compact::from(DUMMY_ACCOUNTID), vec![0u8;0], Era::mortal(32, 11), 11u64).encode()), Era::mortal(32, 11)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn too_late_mortal_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 10), 10u64).encode()), Era::mortal(32, 10)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn too_early_mortal_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 43), 43u64).encode()), Era::mortal(32, 43)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn encoding_matches_vec() { - let ex = Ex::new_unsigned(vec![0u8;0]); - let encoded = ex.encode(); - let decoded = Ex::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(decoded, ex); - let as_vec: Vec = Decode::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(as_vec.encode(), encoded); - } -} diff --git a/core/sr-primitives/src/generic/unchecked_mortal_extrinsic.rs b/core/sr-primitives/src/generic/unchecked_mortal_extrinsic.rs deleted file mode 100644 index 7f92b20edd0c3..0000000000000 --- a/core/sr-primitives/src/generic/unchecked_mortal_extrinsic.rs +++ /dev/null @@ -1,307 +0,0 @@ -// Copyright 2017-2019 Parity Technologies (UK) Ltd. -// This file is part of Substrate. - -// Substrate is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Substrate is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Substrate. If not, see . - -//! Generic implementation of an unchecked (pre-verification) extrinsic. - -#[cfg(feature = "std")] -use std::fmt; - -use rstd::prelude::*; -use runtime_io::blake2_256; -use crate::codec::{Decode, Encode, Input}; -use crate::traits::{ - self, Member, SimpleArithmetic, MaybeDisplay, CurrentHeight, BlockNumberToHash, - Lookup, Checkable, Extrinsic, SaturatedConversion -}; -use super::{CheckedExtrinsic, Era}; - -const TRANSACTION_VERSION: u8 = 1; - -/// A extrinsic right from the external world. This is unchecked and so -/// can contain a signature. -#[derive(PartialEq, Eq, Clone)] -pub struct UncheckedMortalExtrinsic { - /// The signature, address, number of extrinsics have come before from - /// the same signer and an era describing the longevity of this transaction, - /// if this is a signed extrinsic. - pub signature: Option<(Address, Signature, Index, Era)>, - /// The function that should be called. - pub function: Call, -} - -impl UncheckedMortalExtrinsic { - /// New instance of a signed extrinsic aka "transaction". - pub fn new_signed(index: Index, function: Call, signed: Address, signature: Signature, era: Era) -> Self { - UncheckedMortalExtrinsic { - signature: Some((signed, signature, index, era)), - function, - } - } - - /// New instance of an unsigned extrinsic aka "inherent". - pub fn new_unsigned(function: Call) -> Self { - UncheckedMortalExtrinsic { - signature: None, - function, - } - } -} - -impl Extrinsic for UncheckedMortalExtrinsic { - fn is_signed(&self) -> Option { - Some(self.signature.is_some()) - } -} - -impl Checkable - for UncheckedMortalExtrinsic -where - Address: Member + MaybeDisplay, - Index: Encode + Member + MaybeDisplay + SimpleArithmetic, - Call: Encode + Member, - Signature: Member + traits::Verify, - AccountId: Member + MaybeDisplay, - BlockNumber: SimpleArithmetic, - Hash: Encode, - Context: Lookup - + CurrentHeight - + BlockNumberToHash, -{ - type Checked = CheckedExtrinsic; - - fn check(self, context: &Context) -> Result { - Ok(match self.signature { - Some((signed, signature, index, era)) => { - let current_u64 = context.current_height().saturated_into::(); - let h = context.block_number_to_hash(era.birth(current_u64).saturated_into()) - .ok_or("transaction birth block ancient")?; - let signed = context.lookup(signed)?; - let raw_payload = (index, self.function, era, h); - - if !raw_payload.using_encoded(|payload| { - if payload.len() > 256 { - signature.verify(&blake2_256(payload)[..], &signed) - } else { - signature.verify(payload, &signed) - } - }) { - return Err(crate::BAD_SIGNATURE) - } - CheckedExtrinsic { - signed: Some((signed, raw_payload.0)), - function: raw_payload.1, - } - } - None => CheckedExtrinsic { - signed: None, - function: self.function, - }, - }) - } -} - -impl Decode - for UncheckedMortalExtrinsic -where - Address: Decode, - Signature: Decode, - Index: Decode, - Call: Decode, -{ - fn decode(input: &mut I) -> Option { - // This is a little more complicated than usual since the binary format must be compatible - // with substrate's generic `Vec` type. Basically this just means accepting that there - // will be a prefix of vector length (we don't need - // to use this). - let _length_do_not_remove_me_see_above: Vec<()> = Decode::decode(input)?; - - let version = input.read_byte()?; - - let is_signed = version & 0b1000_0000 != 0; - let version = version & 0b0111_1111; - if version != TRANSACTION_VERSION { - return None - } - - Some(UncheckedMortalExtrinsic { - signature: if is_signed { Some(Decode::decode(input)?) } else { None }, - function: Decode::decode(input)?, - }) - } -} - -impl Encode - for UncheckedMortalExtrinsic -where - Address: Encode, - Signature: Encode, - Index: Encode, - Call: Encode, -{ - fn encode(&self) -> Vec { - super::encode_with_vec_prefix::(|v| { - // 1 byte version id. - match self.signature.as_ref() { - Some(s) => { - v.push(TRANSACTION_VERSION | 0b1000_0000); - s.encode_to(v); - } - None => { - v.push(TRANSACTION_VERSION & 0b0111_1111); - } - } - self.function.encode_to(v); - }) - } -} - -#[cfg(feature = "std")] -impl serde::Serialize - for UncheckedMortalExtrinsic -{ - fn serialize(&self, seq: S) -> Result where S: ::serde::Serializer { - self.using_encoded(|bytes| seq.serialize_bytes(bytes)) - } -} - -#[cfg(feature = "std")] -impl fmt::Debug for UncheckedMortalExtrinsic where - Address: fmt::Debug, - Index: fmt::Debug, - Call: fmt::Debug, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "UncheckedMortalExtrinsic({:?}, {:?})", self.signature.as_ref().map(|x| (&x.0, &x.2)), self.function) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use runtime_io::blake2_256; - use crate::codec::{Encode, Decode}; - use serde::{Serialize, Deserialize}; - - struct TestContext; - impl Lookup for TestContext { - type Source = u64; - type Target = u64; - fn lookup(&self, s: u64) -> Result { Ok(s) } - } - impl CurrentHeight for TestContext { - type BlockNumber = u64; - fn current_height(&self) -> u64 { 42 } - } - impl BlockNumberToHash for TestContext { - type BlockNumber = u64; - type Hash = u64; - fn block_number_to_hash(&self, n: u64) -> Option { Some(n) } - } - - #[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Encode, Decode)] - struct TestSig(u64, Vec); - impl traits::Verify for TestSig { - type Signer = u64; - fn verify>(&self, mut msg: L, signer: &Self::Signer) -> bool { - *signer == self.0 && msg.get() == &self.1[..] - } - } - - const DUMMY_ACCOUNTID: u64 = 0; - - type Ex = UncheckedMortalExtrinsic, TestSig>; - type CEx = CheckedExtrinsic>; - - #[test] - fn unsigned_codec_should_work() { - let ux = Ex::new_unsigned(vec![0u8;0]); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn signed_codec_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal()); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn large_signed_codec_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8; 257], Era::immortal(), 0u64).using_encoded(blake2_256)[..].to_owned()), Era::immortal()); - let encoded = ux.encode(); - assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux)); - } - - #[test] - fn unsigned_check_should_work() { - let ux = Ex::new_unsigned(vec![0u8;0]); - assert!(!ux.is_signed().unwrap_or(false)); - assert!(>::check(ux, &TestContext).is_ok()); - } - - #[test] - fn badly_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, vec![0u8]), Era::immortal()); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn immortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::immortal(), 0u64).encode()), Era::immortal()); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn mortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 42), 42u64).encode()), Era::mortal(32, 42)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn later_mortal_signed_check_should_work() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 11), 11u64).encode()), Era::mortal(32, 11)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Ok(CEx { signed: Some((DUMMY_ACCOUNTID, 0)), function: vec![0u8;0] })); - } - - #[test] - fn too_late_mortal_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 10), 10u64).encode()), Era::mortal(32, 10)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn too_early_mortal_signed_check_should_fail() { - let ux = Ex::new_signed(0, vec![0u8;0], DUMMY_ACCOUNTID, TestSig(DUMMY_ACCOUNTID, (DUMMY_ACCOUNTID, vec![0u8;0], Era::mortal(32, 43), 43u64).encode()), Era::mortal(32, 43)); - assert!(ux.is_signed().unwrap_or(false)); - assert_eq!(>::check(ux, &TestContext), Err(crate::BAD_SIGNATURE)); - } - - #[test] - fn encoding_matches_vec() { - let ex = Ex::new_unsigned(vec![0u8;0]); - let encoded = ex.encode(); - let decoded = Ex::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(decoded, ex); - let as_vec: Vec = Decode::decode(&mut encoded.as_slice()).unwrap(); - assert_eq!(as_vec.encode(), encoded); - } -} diff --git a/core/sr-primitives/src/testing.rs b/core/sr-primitives/src/testing.rs index f8df25ec596b0..76d37a0580a3a 100644 --- a/core/sr-primitives/src/testing.rs +++ b/core/sr-primitives/src/testing.rs @@ -19,12 +19,16 @@ use serde::{Serialize, Serializer, Deserialize, de::Error as DeError, Deserializer}; use std::{fmt::Debug, ops::Deref, fmt}; use crate::codec::{Codec, Encode, Decode}; -use crate::traits::{self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, TypedKey}; +use crate::traits::{ + self, Checkable, Applyable, BlakeTwo256, OpaqueKeys, TypedKey, DispatchError, DispatchResult, + ValidateUnsigned +}; use crate::{generic, KeyTypeId}; use crate::weights::{Weighable, Weight}; pub use substrate_primitives::H256; use substrate_primitives::U256; use substrate_primitives::ed25519::{Public as AuthorityId}; +use crate::transaction_validity::TransactionValidity; /// Authority Id #[derive(Default, PartialEq, Eq, Clone, Encode, Decode, Debug)] @@ -199,7 +203,7 @@ impl<'a, Xt> Deserialize<'a> for Block where Block: Decode { /// /// If sender is some then the transaction is signed otherwise it is unsigned. #[derive(PartialEq, Eq, Clone, Encode, Decode)] -pub struct TestXt(pub Option, pub u64, pub Call); +pub struct TestXt(pub Option, u64, pub Call); impl Serialize for TestXt where TestXt: Encode { @@ -227,12 +231,22 @@ impl Applyable for TestXt where Call: 'static + Sized + Send + Sync + Clone + Eq + Codec + Debug, { type AccountId = u64; - type Index = u64; type Call = Call; fn sender(&self) -> Option<&u64> { self.0.as_ref() } - fn index(&self) -> Option<&u64> { self.0.as_ref().map(|_| &self.1) } - fn deconstruct(self) -> (Self::Call, Option) { - (self.2, self.0) + + /// Checks to see if this is a valid *transaction*. It returns information on it if so. + fn validate>(&self, + _weight: crate::weights::Weight + ) -> TransactionValidity { + TransactionValidity::Valid(Default::default()) + } + + /// Executes all necessary logic needed prior to dispatch and deconstructs into function call, + /// index and sender. + fn dispatch(self, + _weight: crate::weights::Weight + ) -> Result { + Ok(Ok(())) } } impl Weighable for TestXt { diff --git a/core/sr-primitives/src/traits.rs b/core/sr-primitives/src/traits.rs index a6d94babbe577..ccd9c06ea33ec 100644 --- a/core/sr-primitives/src/traits.rs +++ b/core/sr-primitives/src/traits.rs @@ -23,7 +23,7 @@ use runtime_io; #[cfg(feature = "std")] use serde::{Serialize, Deserialize, de::DeserializeOwned}; use substrate_primitives::{self, Hasher, Blake2Hasher}; use crate::codec::{Codec, Encode, Decode, HasCompact}; -use crate::transaction_validity::TransactionValidity; +use crate::transaction_validity::{ValidTransaction, TransactionValidity}; use crate::generic::{Digest, DigestItem}; pub use substrate_primitives::crypto::TypedKey; pub use integer_sqrt::IntegerSquareRoot; @@ -749,6 +749,137 @@ impl Checkable for T { } } +/// An abstract error concerning an attempt to verify, check or dispatch the transaction. This +/// cannot be more concrete because it's designed to work reasonably well over a broad range of +/// possible transaction types. +pub enum DispatchError { + /// General error to do with the inability to pay some fees (e.g. account balance too low). + Payment, + + /// General error to do with the permissions of the sender. + NoPermission, + + /// General error to do with the state of the system in general. + BadState, + + /// General error to do with the transaction being outdated (e.g. nonce too low). + Stale, + + /// General error to do with the transaction not yet being valid (e.g. nonce too high). + Future, + + /// General error to do with the transaction's proofs (e.g. signature). + BadProof, + +/* /// General error to do with actually executing the dispatched logic. + User(&'static str),*/ +} + +impl From for i8 { + fn from(e: DispatchError) -> i8 { + match e { + DispatchError::Payment => -64, + DispatchError::NoPermission => -65, + DispatchError::BadState => -66, + DispatchError::Stale => -67, + DispatchError::Future => -68, + DispatchError::BadProof => -69, + } + } +} + +/// Result of a module function call; either nothing (functions are only called for "side effects") +/// or an error message. +pub type DispatchResult = result::Result<(), &'static str>; + +/// A lazy call (module function and argument values) that can be executed via its `dispatch` +/// method. +pub trait Dispatchable { + /// Every function call from your runtime has an origin, which specifies where the extrinsic was + /// generated from. In the case of a signed extrinsic (transaction), the origin contains an + /// identifier for the caller. The origin can be empty in the case of an inherent extrinsic. + type Origin; + /// ... + type Trait; + /// Actually dispatch this call and result the result of it. + fn dispatch(self, origin: Self::Origin) -> DispatchResult; +} + +/// Means by which a transaction may be extended. This type embodies both the data and the logic +/// that should be additionally associated with the transaction. It should be plain old data. +pub trait SignedExtension: + Codec + MaybeDebug + Sync + Send + Clone + Eq + PartialEq +{ + /// The type which encodes the sender identity. + type AccountId; + + /// Validate a signed transaction for the transaction queue. + fn validate( + &self, + _who: &Self::AccountId, + _weight: crate::weights::Weight, + ) -> Result { Ok(Default::default()) } + + /// Do any pre-flight stuff for a signed transaction. + fn pre_dispatch( + self, + who: &Self::AccountId, + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { self.validate(who, weight).map(|_| ()) } + + /// Validate an unsigned transaction for the transaction queue. Normally the default + /// implementation is fine since `ValidateUnsigned` is a better way of recognising and + /// validating unsigned transactions. + fn validate_unsigned( + _weight: crate::weights::Weight, + ) -> Result { Ok(Default::default()) } + + /// Do any pre-flight stuff for a unsigned transaction. + fn pre_dispatch_unsigned( + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { Self::validate_unsigned(weight).map(|_| ()) } +} + +impl< + AccountId, + A: SignedExtension, + B: SignedExtension, +> SignedExtension for (A, B) { + type AccountId = AccountId; + fn validate( + &self, + who: &Self::AccountId, + weight: crate::weights::Weight, + ) -> Result { + let a = self.0.validate(who, weight)?; + let b = self.1.validate(who, weight)?; + Ok(a.combine_with(b)) + } + fn pre_dispatch( + self, + who: &Self::AccountId, + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { + self.0.pre_dispatch(who, weight)?; + self.1.pre_dispatch(who, weight)?; + Ok(()) + } + fn validate_unsigned( + weight: crate::weights::Weight, + ) -> Result { + let a = A::validate_unsigned(weight)?; + let b = B::validate_unsigned(weight)?; + Ok(a.combine_with(b)) + } + fn pre_dispatch_unsigned( + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { + A::pre_dispatch_unsigned(weight)?; + B::pre_dispatch_unsigned(weight)?; + Ok(()) + } +} + /// An "executable" piece of information, used by the standard Substrate Executive in order to /// enact a piece of extrinsic information by marshalling and dispatching to a named function /// call. @@ -758,16 +889,23 @@ impl Checkable for T { pub trait Applyable: Sized + Send + Sync { /// Id of the account that is responsible for this piece of information (sender). type AccountId: Member + MaybeDisplay; - /// Index allowing to disambiguate other `Applyable`s from the same `AccountId`. - type Index: Member + MaybeDisplay + SimpleArithmetic; - /// Function call. - type Call: Member; - /// Returns a reference to the index if any. - fn index(&self) -> Option<&Self::Index>; + + /// Type by which we can dispatch. Restricts the UnsignedValidator type. + type Call; + /// Returns a reference to the sender if any. fn sender(&self) -> Option<&Self::AccountId>; - /// Deconstructs into function call and sender. - fn deconstruct(self) -> (Self::Call, Option); + + /// Checks to see if this is a valid *transaction*. It returns information on it if so. + fn validate>(&self, + weight: crate::weights::Weight + ) -> TransactionValidity; + + /// Executes all necessary logic needed prior to dispatch and deconstructs into function call, + /// index and sender. + fn dispatch(self, + weight: crate::weights::Weight + ) -> Result; } /// Auxiliary wrapper that holds an api instance and binds it to the given lifetime. diff --git a/core/sr-primitives/src/transaction_validity.rs b/core/sr-primitives/src/transaction_validity.rs index f36599b67b42c..66e66c0042db9 100644 --- a/core/sr-primitives/src/transaction_validity.rs +++ b/core/sr-primitives/src/transaction_validity.rs @@ -18,6 +18,7 @@ use rstd::prelude::*; use crate::codec::{Encode, Decode}; +use crate::traits::DispatchError; /// Priority for a transaction. Additive. Higher is better. pub type TransactionPriority = u64; @@ -36,40 +37,81 @@ pub enum TransactionValidity { /// Transaction is invalid. Details are described by the error code. Invalid(i8), /// Transaction is valid. - Valid { - /// Priority of the transaction. - /// - /// Priority determines the ordering of two transactions that have all - /// their dependencies (required tags) satisfied. - priority: TransactionPriority, - /// Transaction dependencies - /// - /// A non-empty list signifies that some other transactions which provide - /// given tags are required to be included before that one. - requires: Vec, - /// Provided tags - /// - /// A list of tags this transaction provides. Successfully importing the transaction - /// will enable other transactions that depend on (require) those tags to be included as well. - /// Provided and required tags allow Substrate to build a dependency graph of transactions - /// and import them in the right (linear) order. - provides: Vec, - /// Transaction longevity - /// - /// Longevity describes minimum number of blocks the validity is correct. - /// After this period transaction should be removed from the pool or revalidated. - longevity: TransactionLongevity, - /// A flag indicating if the transaction should be propagated to other peers. - /// - /// By setting `false` here the transaction will still be considered for - /// including in blocks that are authored on the current node, but will - /// never be sent to other peers. - propagate: bool, - }, + Valid(ValidTransaction), /// Transaction validity can't be determined. Unknown(i8), } +impl From> for TransactionValidity { + fn from(r: Result) -> Self { + match r { + Ok(v) => TransactionValidity::Valid(v), + Err(e) => TransactionValidity::Invalid(e.into()), + } + } +} + +/// Information concerning a valid transaction. +#[derive(Clone, PartialEq, Eq, Encode)] +#[cfg_attr(feature = "std", derive(Debug))] +pub struct ValidTransaction { + /// Priority of the transaction. + /// + /// Priority determines the ordering of two transactions that have all + /// their dependencies (required tags) satisfied. + pub priority: TransactionPriority, + /// Transaction dependencies + /// + /// A non-empty list signifies that some other transactions which provide + /// given tags are required to be included before that one. + pub requires: Vec, + /// Provided tags + /// + /// A list of tags this transaction provides. Successfully importing the transaction + /// will enable other transactions that depend on (require) those tags to be included as well. + /// Provided and required tags allow Substrate to build a dependency graph of transactions + /// and import them in the right (linear) order. + pub provides: Vec, + /// Transaction longevity + /// + /// Longevity describes minimum number of blocks the validity is correct. + /// After this period transaction should be removed from the pool or revalidated. + pub longevity: TransactionLongevity, + /// A flag indicating if the transaction should be propagated to other peers. + /// + /// By setting `false` here the transaction will still be considered for + /// including in blocks that are authored on the current node, but will + /// never be sent to other peers. + pub propagate: bool, +} + +impl Default for ValidTransaction { + fn default() -> Self { + ValidTransaction { + priority: 0, + requires: vec![], + provides: vec![], + longevity: TransactionLongevity::max_value(), + propagate: true, + } + } +} + +impl ValidTransaction { + /// Combine two instances into one, as a best effort. This will take the superset of each of the + /// `provides` and `requires` tags, it will sum the priorities, take the minimum longevity and + /// the logic *And* of the propagate flags. + pub fn combine_with(mut self, mut other: ValidTransaction) -> Self { + ValidTransaction { + priority: self.priority + other.priority, + requires: { self.requires.append(&mut other.requires); self.requires }, + provides: { self.provides.append(&mut other.provides); self.provides }, + longevity: self.longevity.min(other.longevity), + propagate: self.propagate && other.propagate, + } + } +} + impl Decode for TransactionValidity { fn decode(value: &mut I) -> Option { match value.read_byte()? { @@ -81,9 +123,9 @@ impl Decode for TransactionValidity { let longevity = TransactionLongevity::decode(value)?; let propagate = bool::decode(value).unwrap_or(true); - Some(TransactionValidity::Valid { + Some(TransactionValidity::Valid(ValidTransaction { priority, requires, provides, longevity, propagate, - }) + })) }, 2 => Some(TransactionValidity::Unknown(i8::decode(value)?)), _ => None, @@ -101,24 +143,24 @@ mod tests { 1, 5, 0, 0, 0, 0, 0, 0, 0, 4, 16, 1, 2, 3, 4, 4, 12, 4, 5, 6, 42, 0, 0, 0, 0, 0, 0, 0 ]; - assert_eq!(TransactionValidity::decode(&mut &*old_encoding), Some(TransactionValidity::Valid { + assert_eq!(TransactionValidity::decode(&mut &*old_encoding), Some(TransactionValidity::Valid(ValidTransaction { priority: 5, requires: vec![vec![1, 2, 3, 4]], provides: vec![vec![4, 5, 6]], longevity: 42, propagate: true, - })); + }))); } #[test] fn should_encode_and_decode() { - let v = TransactionValidity::Valid { + let v = TransactionValidity::Valid(ValidTransaction { priority: 5, requires: vec![vec![1, 2, 3, 4]], provides: vec![vec![4, 5, 6]], longevity: 42, propagate: false, - }; + }); let encoded = v.encode(); assert_eq!( diff --git a/node-template/runtime/src/lib.rs b/node-template/runtime/src/lib.rs index 865299285af52..08275f3c8d87c 100644 --- a/node-template/runtime/src/lib.rs +++ b/node-template/runtime/src/lib.rs @@ -231,7 +231,7 @@ pub type Block = generic::Block; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedMortalCompactExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 3bfb86c8cf4e7..f7fc9dcb1ea70 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -430,12 +430,18 @@ pub type Block = generic::Block; pub type SignedBlock = generic::SignedBlock; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; +/// The nonce checker. +pub use system::CheckNonce; +/// The fees-taker. +pub use balances::TakeFees; +/// The SignedExtension to the basic transaction logic. +pub type SignedExtra = (CheckNonce, TakeFees); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedMortalCompactExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. -pub type CheckedExtrinsic = generic::CheckedExtrinsic; +pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. -pub type Executive = executive::Executive, Balances, Runtime, AllModules>; +pub type Executive = executive::Executive, Runtime, AllModules>; impl_runtime_apis! { impl client_api::Core for Runtime { diff --git a/srml/balances/src/lib.rs b/srml/balances/src/lib.rs index 30fb2a48e935c..ef0b126a2d9bf 100644 --- a/srml/balances/src/lib.rs +++ b/srml/balances/src/lib.rs @@ -154,15 +154,15 @@ use rstd::{cmp, result, mem}; use parity_codec::{Codec, Encode, Decode}; use srml_support::{StorageValue, StorageMap, Parameter, decl_event, decl_storage, decl_module}; use srml_support::traits::{ - UpdateBalanceOutcome, Currency, OnFreeBalanceZero, MakePayment, OnUnbalanced, + UpdateBalanceOutcome, Currency, OnFreeBalanceZero, OnUnbalanced, WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, Imbalance, SignedImbalance, ReservableCurrency }; use srml_support::{dispatch::Result, traits::Get}; -use primitives::traits::{ +use primitives::{transaction_validity::TransactionPriority, traits::{ Zero, SimpleArithmetic, StaticLookup, Member, CheckedAdd, CheckedSub, - MaybeSerializeDebug, Saturating, Bounded -}; + MaybeSerializeDebug, Saturating, Bounded, SignedExtension +}}; use system::{IsDeadAccount, OnNewAccount, ensure_signed, ensure_root}; mod mock; @@ -1144,18 +1144,45 @@ where } } -impl, I: Instance> MakePayment for Module { - fn make_payment(transactor: &T::AccountId, encoded_len: usize) -> Result { - let encoded_len = T::Balance::from(encoded_len as u32); - let transaction_fee = T::TransactionBaseFee::get() + T::TransactionByteFee::get() * encoded_len; - let imbalance = Self::withdraw( - transactor, - transaction_fee, +/// Require the transactor pay for themselves and maybe include a tip to gain additional priority +/// in the queue. +#[derive(Encode, Decode, Clone, Eq, PartialEq)] +pub struct TakeFees, I: Instance = DefaultInstance>(pub T::Balance); + +#[cfg(feature = "std")] +impl, I: Instance> rstd::fmt::Debug for TakeFees { + fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result { + self.0.fmt(f) + } +} + +use primitives::traits::{DispatchError, SaturatedConversion}; +use primitives::transaction_validity::ValidTransaction; +use primitives::weights::Weight; + +impl, I: Instance + Clone + Eq> SignedExtension for TakeFees { + type AccountId = T::AccountId; + + fn validate( + &self, + who: &Self::AccountId, + weight: Weight, + ) -> rstd::result::Result { + let fee_x = T::Balance::from(weight as u32); + // should be weight_to_fee(weight) + let fee = T::TransactionBaseFee::get() + T::TransactionByteFee::get() * fee_x; + let fee = fee + self.0.clone(); + let imbalance = >::withdraw( + who, + fee.clone(), WithdrawReason::TransactionPayment, ExistenceRequirement::KeepAlive - )?; + ).map_err(|_| DispatchError::Payment)?; T::TransactionPayment::on_unbalanced(imbalance); - Ok(()) + + let mut r = ValidTransaction::default(); + r.priority = fee.saturated_into::(); + Ok(r) } } diff --git a/srml/collective/src/lib.rs b/srml/collective/src/lib.rs index 5f2a0ef2815da..47bff6546d869 100644 --- a/srml/collective/src/lib.rs +++ b/srml/collective/src/lib.rs @@ -424,7 +424,7 @@ mod tests { } pub type Block = primitives::generic::Block; - pub type UncheckedExtrinsic = primitives::generic::UncheckedMortalCompactExtrinsic; + pub type UncheckedExtrinsic = primitives::generic::UncheckedExtrinsic; srml_support::construct_runtime!( pub enum Test where diff --git a/srml/elections/src/lib.rs b/srml/elections/src/lib.rs index 80324ecefbdaf..18c96918558f4 100644 --- a/srml/elections/src/lib.rs +++ b/srml/elections/src/lib.rs @@ -1211,7 +1211,7 @@ mod tests { } pub type Block = primitives::generic::Block; - pub type UncheckedExtrinsic = primitives::generic::UncheckedMortalCompactExtrinsic; + pub type UncheckedExtrinsic = primitives::generic::UncheckedExtrinsic; srml_support::construct_runtime!( pub enum Test where diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index 49d4addb3bc2b..6c83f92850ab6 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -79,17 +79,18 @@ use rstd::marker::PhantomData; use rstd::result; use primitives::{generic::Digest, traits::{ self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalize, - OnInitialize, NumberFor, Block as BlockT, OffchainWorker, - ValidateUnsigned, + OnInitialize, NumberFor, Block as BlockT, OffchainWorker, ValidateUnsigned }}; -use srml_support::{Dispatchable, traits::MakePayment}; +use srml_support::Dispatchable; use parity_codec::{Codec, Encode}; use system::{extrinsics_root, DigestOf}; use primitives::{ApplyOutcome, ApplyError}; -use primitives::transaction_validity::{TransactionValidity, TransactionPriority, TransactionLongevity}; +use primitives::transaction_validity::TransactionValidity; use primitives::weights::Weighable; mod internal { + use primitives::traits::DispatchError; + pub const MAX_TRANSACTIONS_WEIGHT: u32 = 4 * 1024 * 1024; pub enum ApplyError { @@ -104,6 +105,19 @@ mod internal { Success, Fail(&'static str), } + + impl From for ApplyError { + fn from(d: DispatchError) -> Self { + match d { + DispatchError::Payment => ApplyError::CantPay, + DispatchError::NoPermission => ApplyError::CantPay, + DispatchError::BadState => ApplyError::CantPay, + DispatchError::Stale => ApplyError::Stale, + DispatchError::Future => ApplyError::Future, + DispatchError::BadProof => ApplyError::BadSignature(""), + } + } + } } /// Trait that can be used to execute a block. @@ -116,27 +130,26 @@ pub type CheckedOf = >::Checked; pub type CallOf = as Applyable>::Call; pub type OriginOf = as Dispatchable>::Origin; -pub struct Executive( - PhantomData<(System, Block, Context, Payment, UnsignedValidator, AllModules)> +pub struct Executive( + PhantomData<(System, Block, Context, UnsignedValidator, AllModules)> ); impl< System: system::Trait, Block: traits::Block, Context: Default, - Payment: MakePayment, UnsignedValidator, AllModules: OnInitialize + OnFinalize + OffchainWorker, -> ExecuteBlock for Executive +> ExecuteBlock for Executive where Block::Extrinsic: Checkable + Codec, - CheckedOf: Applyable + Weighable, + CheckedOf: Applyable + Weighable, CallOf: Dispatchable, OriginOf: From>, UnsignedValidator: ValidateUnsigned>, { fn execute_block(block: Block) { - Executive::::execute_block(block); + Executive::::execute_block(block); } } @@ -144,13 +157,12 @@ impl< System: system::Trait, Block: traits::Block, Context: Default, - Payment: MakePayment, UnsignedValidator, AllModules: OnInitialize + OnFinalize + OffchainWorker, -> Executive +> Executive where Block::Extrinsic: Checkable + Codec, - CheckedOf: Applyable + Weighable, + CheckedOf: Applyable + Weighable, CallOf: Dispatchable, OriginOf: From>, UnsignedValidator: ValidateUnsigned>, @@ -264,37 +276,27 @@ where // Verify that the signature is good. let xt = uxt.check(&Default::default()).map_err(internal::ApplyError::BadSignature)?; + // We don't need to make sure to `note_extrinsic` only after we know it's going to be + // executed to prevent it from leaking in storage since at this point, it will either + // execute or panic (and revert storage changes). + if let Some(encoded) = to_note { + >::note_extrinsic(encoded); + } + // Check the weight of the block if that extrinsic is applied. let weight = xt.weight(encoded_len); + + // TODO: Consider placing into a transaction extension. if >::all_extrinsics_weight() + weight > internal::MAX_TRANSACTIONS_WEIGHT { return Err(internal::ApplyError::FullBlock); } - if let (Some(sender), Some(index)) = (xt.sender(), xt.index()) { - // check index - let expected_index = >::account_nonce(sender); - if index != &expected_index { return Err( - if index < &expected_index { internal::ApplyError::Stale } else { internal::ApplyError::Future } - ) } - // pay any fees - Payment::make_payment(sender, encoded_len).map_err(|_| internal::ApplyError::CantPay)?; - - // AUDIT: Under no circumstances may this function panic from here onwards. - // FIXME: ensure this at compile-time (such as by not defining a panic function, forcing - // a linker error unless the compiler can prove it cannot be called). - // increment nonce in storage - >::inc_account_nonce(sender); - } - - // Make sure to `note_extrinsic` only after we know it's going to be executed - // to prevent it from leaking in storage. - if let Some(encoded) = to_note { - >::note_extrinsic(encoded); - } + // AUDIT: Under no circumstances may this function panic from here onwards. // Decode parameters and dispatch - let (f, s) = xt.deconstruct(); - let r = f.dispatch(s.into()); + let r = Applyable::dispatch(xt, weight) + .map_err(internal::ApplyError::from)?; + >::note_applied_extrinsic(&r, encoded_len as u32); r.map(|_| internal::ApplyOutcome::Success).or_else(|e| match e { @@ -332,7 +334,6 @@ where pub fn validate_transaction(uxt: Block::Extrinsic) -> TransactionValidity { // Note errors > 0 are from ApplyError const UNKNOWN_ERROR: i8 = -127; - const MISSING_SENDER: i8 = -20; const INVALID_INDEX: i8 = -10; let encoded_len = uxt.encode().len(); @@ -348,39 +349,9 @@ where Err(_) => return TransactionValidity::Invalid(UNKNOWN_ERROR), }; - match (xt.sender(), xt.index()) { - (Some(sender), Some(index)) => { - // pay any fees - if Payment::make_payment(sender, encoded_len).is_err() { - return TransactionValidity::Invalid(ApplyError::CantPay as i8) - } - - // check index - let expected_index = >::account_nonce(sender); - if index < &expected_index { - return TransactionValidity::Invalid(ApplyError::Stale as i8) - } + let weight = xt.weight(encoded_len); - let index = *index; - let provides = vec![(sender, index).encode()]; - let requires = if expected_index < index { - vec![(sender, index - One::one()).encode()] - } else { - vec![] - }; - - TransactionValidity::Valid { - priority: encoded_len as TransactionPriority, - requires, - provides, - longevity: TransactionLongevity::max_value(), - propagate: true, - } - }, - (None, None) => UnsignedValidator::validate_unsigned(&xt.deconstruct().0), - (Some(_), None) => TransactionValidity::Invalid(INVALID_INDEX), - (None, Some(_)) => TransactionValidity::Invalid(MISSING_SENDER), - } + xt.validate::(weight) } /// Start an offchain worker and generate extrinsics. diff --git a/srml/support/src/dispatch.rs b/srml/support/src/dispatch.rs index f990cbd8d5a1e..00c21a31ba47f 100644 --- a/srml/support/src/dispatch.rs +++ b/srml/support/src/dispatch.rs @@ -25,25 +25,16 @@ pub use srml_metadata::{ FunctionMetadata, DecodeDifferent, DecodeDifferentArray, FunctionArgumentMetadata, ModuleConstantMetadata, DefaultByte, DefaultByteGetter, }; -pub use sr_primitives::weights::{TransactionWeight, Weighable, Weight}; +pub use sr_primitives::{ + weights::{TransactionWeight, Weighable, Weight}, traits::{Dispatchable, DispatchResult} +}; /// A type that cannot be instantiated. pub enum Never {} /// Result of a module function call; either nothing (functions are only called for "side effects") /// or an error message. -pub type Result = result::Result<(), &'static str>; - -/// A lazy call (module function and argument values) that can be executed via its `dispatch` -/// method. -pub trait Dispatchable { - /// Every function call from your runtime has an origin, which specifies where the extrinsic was - /// generated from. In the case of a signed extrinsic (transaction), the origin contains an - /// identifier for the caller. The origin can be empty in the case of an inherent extrinsic. - type Origin; - type Trait; - fn dispatch(self, origin: Self::Origin) -> Result; -} +pub type Result = DispatchResult; /// Serializable version of Dispatchable. /// This value can be used as a "function" in an extrinsic. diff --git a/srml/support/src/traits.rs b/srml/support/src/traits.rs index 86071a37a2720..f6c09190ebad6 100644 --- a/srml/support/src/traits.rs +++ b/srml/support/src/traits.rs @@ -91,19 +91,6 @@ pub enum UpdateBalanceOutcome { AccountKilled, } -/// Simple trait designed for hooking into a transaction payment. -/// -/// It operates over a single generic `AccountId` type. -pub trait MakePayment { - /// Make transaction payment from `who` for an extrinsic of encoded length - /// `encoded_len` bytes. Return `Ok` iff the payment was successful. - fn make_payment(who: &AccountId, encoded_len: usize) -> Result<(), &'static str>; -} - -impl MakePayment for () { - fn make_payment(_: &T, _: usize) -> Result<(), &'static str> { Ok(()) } -} - /// A trait for finding the author of a block header based on the `PreRuntime` digests contained /// within it. pub trait FindAuthor { diff --git a/srml/support/test/tests/instance.rs b/srml/support/test/tests/instance.rs index 62e7263b511be..46edcd7df323b 100644 --- a/srml/support/test/tests/instance.rs +++ b/srml/support/test/tests/instance.rs @@ -269,7 +269,7 @@ srml_support::construct_runtime!( pub type Header = generic::Header; pub type Block = generic::Block; -pub type UncheckedExtrinsic = generic::UncheckedMortalCompactExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; fn new_test_ext() -> runtime_io::TestExternalities { GenesisConfig{ @@ -407,4 +407,4 @@ fn storage_with_instance_basic_operation() { DoubleMap::remove(key1, key2); assert_eq!(DoubleMap::get(key1, key2), 0); }); -} \ No newline at end of file +} diff --git a/srml/support/test/tests/issue2219.rs b/srml/support/test/tests/issue2219.rs index 185b5e24807a9..9bae9bcde5549 100644 --- a/srml/support/test/tests/issue2219.rs +++ b/srml/support/test/tests/issue2219.rs @@ -152,7 +152,7 @@ pub type BlockNumber = u64; pub type Index = u64; pub type Header = generic::Header; pub type Block = generic::Block; -pub type UncheckedExtrinsic = generic::UncheckedMortalCompactExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; impl system::Trait for Runtime { type Hash = H256; @@ -183,4 +183,4 @@ fn create_genesis_config() { enable_storage_role: true, }) }; -} \ No newline at end of file +} diff --git a/srml/system/src/lib.rs b/srml/system/src/lib.rs index a55cb9be86ea4..0d3d514396d59 100644 --- a/srml/system/src/lib.rs +++ b/srml/system/src/lib.rs @@ -76,15 +76,20 @@ use serde::Serialize; use rstd::prelude::*; #[cfg(any(feature = "std", test))] use rstd::map; -use primitives::{generic, traits::{self, CheckEqual, SimpleArithmetic, - SimpleBitOps, Hash, Member, MaybeDisplay, EnsureOrigin, CurrentHeight, BlockNumberToHash, - MaybeSerializeDebugButNotDeserialize, MaybeSerializeDebug, StaticLookup, One, Bounded, Lookup, - Zero, -}}; +use primitives::{ + generic, weights::Weight, traits::{ + self, CheckEqual, SimpleArithmetic, Zero, SignedExtension, + SimpleBitOps, Hash, Member, MaybeDisplay, EnsureOrigin, CurrentHeight, BlockNumberToHash, + MaybeSerializeDebugButNotDeserialize, MaybeSerializeDebug, StaticLookup, One, Bounded, + Lookup, DispatchError + }, transaction_validity::{ + ValidTransaction, TransactionPriority, TransactionLongevity + }, +}; use substrate_primitives::storage::well_known_keys; use srml_support::{ storage, decl_module, decl_event, decl_storage, StorageDoubleMap, StorageValue, - StorageMap, Parameter, for_each_tuple, traits::{Contains, Get}, + StorageMap, Parameter, for_each_tuple, traits::{Contains, Get} }; use safe_mix::TripletMix; use parity_codec::{Encode, Decode}; @@ -748,6 +753,62 @@ impl Module { } } +/// Nonce check and increment to give replay protection for transactions. +#[derive(Encode, Decode, Clone, Eq, PartialEq)] +pub struct CheckNonce(pub T::Index); + +#[cfg(feature = "std")] +impl rstd::fmt::Debug for CheckNonce { + fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result { + self.0.fmt(f) + } +} + +impl SignedExtension for CheckNonce { + type AccountId = T::AccountId; + fn pre_dispatch( + self, + who: &Self::AccountId, + _weight: Weight, + ) -> Result<(), DispatchError> { + let expected = >::get(who); + if self.0 != expected { + return Err( + if self.0 < expected { DispatchError::Stale } else { DispatchError::Future } + ) + } + >::insert(who, expected + T::Index::one()); + Ok(()) + } + + fn validate( + &self, + who: &Self::AccountId, + _weight: Weight, + ) -> Result { + // check index + let expected = >::get(who); + if self.0 < expected { + return Err(DispatchError::Stale) + } + + let provides = vec![Encode::encode(&(who, self.0))]; + let requires = if expected < self.0 { + vec![Encode::encode(&(who, self.0 - One::one()))] + } else { + vec![] + }; + + Ok(ValidTransaction { + priority: _weight as TransactionPriority, + requires, + provides, + longevity: TransactionLongevity::max_value(), + propagate: true, + }) + } +} + pub struct ChainContext(::rstd::marker::PhantomData); impl Default for ChainContext { fn default() -> Self { diff --git a/subkey/src/cli.yml b/subkey/src/cli.yml index 89190df3624f5..b4f99f8743fa5 100644 --- a/subkey/src/cli.yml +++ b/subkey/src/cli.yml @@ -18,6 +18,12 @@ args: takes_value: true required: false help: The password for the key + - network: + short: n + long: network + takes_value: true + required: false + help: Specify a network. One of substrate (default), polkadot and kusama. subcommands: - generate: about: Generate a random account diff --git a/subkey/src/main.rs b/subkey/src/main.rs index 7cff0d6414f26..e2f1d04606d26 100644 --- a/subkey/src/main.rs +++ b/subkey/src/main.rs @@ -18,15 +18,18 @@ #[cfg(feature = "bench")] extern crate test; -use std::{str::FromStr, io::{stdin, Read}}; +use std::{str::FromStr, io::{stdin, Read}, convert::TryInto}; use hex_literal::hex; use clap::load_yaml; use bip39::{Mnemonic, Language, MnemonicType}; -use substrate_primitives::{ed25519, sr25519, hexdisplay::HexDisplay, Pair, Public, crypto::Ss58Codec, blake2_256}; +use substrate_primitives::{ + ed25519, sr25519, hexdisplay::HexDisplay, Pair, Public, + crypto::{Ss58Codec, set_default_ss58_version}, blake2_256 +}; use parity_codec::{Encode, Decode, Compact}; use sr_primitives::generic::Era; use node_primitives::{Balance, Index, Hash}; -use node_runtime::{Call, UncheckedExtrinsic, BalancesCall}; +use node_runtime::{Call, UncheckedExtrinsic, CheckNonce, TakeFees, BalancesCall}; mod vanity; @@ -87,6 +90,12 @@ fn execute(matches: clap::ArgMatches) where <::Pair as Pair>::Public: Sized + AsRef<[u8]> + Ss58Codec + AsRef<<::Pair as Pair>::Public>, { let password = matches.value_of("password"); + let maybe_network = matches.value_of("network"); + if let Some(network) = maybe_network { + let v = network.try_into() + .expect("Invalid network name: must be polkadot/substrate/kusama"); + set_default_ss58_version(v); + } match matches.subcommand() { ("generate", Some(matches)) => { // create a new randomly generated mnemonic phrase @@ -161,11 +170,11 @@ fn execute(matches: clap::ArgMatches) where signer.sign(payload) }); let extrinsic = UncheckedExtrinsic::new_signed( - index, raw_payload.1, signer.public().into(), signature.into(), era, + (CheckNonce(index), TakeFees(0)), ); println!("0x{}", hex::encode(&extrinsic.encode())); } @@ -202,11 +211,11 @@ fn execute(matches: clap::ArgMatches) where ); let extrinsic = UncheckedExtrinsic::new_signed( - index, raw_payload.1, signer.public().into(), signature.into(), era, + (CheckNonce(index), TakeFees(0)), ); println!("0x{}", hex::encode(&extrinsic.encode()));