From 05d36e3c9e5d2aba7a39dfc72d697663463d90b5 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Sun, 14 Jul 2019 17:39:18 +0200 Subject: [PATCH 1/6] Weight signed extension. --- Cargo.lock | 1 + core/sr-primitives/src/traits.rs | 94 +++++++++++-------- .../sr-primitives/src/transaction_validity.rs | 2 +- node-template/runtime/src/lib.rs | 11 ++- node-template/runtime/src/template.rs | 3 + node/cli/src/factory_impl.rs | 13 ++- node/cli/src/service.rs | 3 +- node/executor/src/lib.rs | 29 +++--- node/runtime/src/lib.rs | 9 +- srml/assets/src/lib.rs | 2 + srml/aura/src/mock.rs | 2 + srml/authorship/src/lib.rs | 2 + srml/balances/src/lib.rs | 1 + srml/balances/src/mock.rs | 2 + srml/balances/src/tests.rs | 17 ++++ srml/collective/src/lib.rs | 2 + srml/contracts/src/tests.rs | 2 + srml/council/src/lib.rs | 2 + srml/democracy/src/lib.rs | 2 + srml/elections/src/lib.rs | 2 + srml/example/src/lib.rs | 2 + srml/executive/Cargo.toml | 1 + srml/executive/src/lib.rs | 31 +++--- srml/finality-tracker/src/lib.rs | 2 + srml/grandpa/src/mock.rs | 2 + srml/indices/src/mock.rs | 2 + srml/session/src/mock.rs | 2 + srml/staking/src/mock.rs | 2 + srml/system/src/lib.rs | 78 ++++++++++++++- srml/timestamp/src/lib.rs | 2 + srml/treasury/src/lib.rs | 2 + subkey/src/main.rs | 6 +- 32 files changed, 245 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d925435e138c4..33d34db349ec1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3738,6 +3738,7 @@ name = "srml-executive" version = "2.0.0" dependencies = [ "hex-literal 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "node-runtime 2.0.0", "parity-codec 4.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)", "sr-io 2.0.0", diff --git a/core/sr-primitives/src/traits.rs b/core/sr-primitives/src/traits.rs index 91667ecce4ae7..4866619dc1303 100644 --- a/core/sr-primitives/src/traits.rs +++ b/core/sr-primitives/src/traits.rs @@ -840,46 +840,64 @@ pub trait SignedExtension: ) -> 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(()) - } +macro_rules! tuple_impl_indexed { + ($first:ident, $($rest:ident,)+ ; $first_index:tt, $($rest_index:tt,)+) => { + tuple_impl_indexed!([$first] [$($rest)+] ; [$first_index,] [$($rest_index,)+]); + }; + ([$($direct:ident)+] ; [$($index:tt,)+]) => { + impl< + AccountId, + $($direct: SignedExtension),+ + > SignedExtension for ($($direct),+,) { + type AccountId = AccountId; + fn validate( + &self, + who: &Self::AccountId, + weight: crate::weights::Weight, + ) -> Result { + let aggregator = vec![$(<$direct as SignedExtension>::validate(&self.$index, who, weight)?),+]; + Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a))) + } + fn pre_dispatch( + self, + who: &Self::AccountId, + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { + $(self.$index.pre_dispatch(who, weight)?;)+ + Ok(()) + } + fn validate_unsigned( + weight: crate::weights::Weight, + ) -> Result { + let aggregator = vec![$($direct::validate_unsigned(weight)?),+]; + Ok(aggregator.into_iter().fold(ValidTransaction::default(), |acc, a| acc.combine_with(a))) + } + fn pre_dispatch_unsigned( + weight: crate::weights::Weight, + ) -> Result<(), DispatchError> { + $($direct::pre_dispatch_unsigned(weight)?;)+ + Ok(()) + } + } + + }; + ([$($direct:ident)+] [] ; [$($index:tt,)+] []) => { + tuple_impl_indexed!([$($direct)+] ; [$($index,)+]); + }; + ( + [$($direct:ident)+] [$first:ident $($rest:ident)*] + ; + [$($index:tt,)+] [$first_index:tt, $($rest_index:tt,)*] + ) => { + tuple_impl_indexed!([$($direct)+] ; [$($index,)+]); + tuple_impl_indexed!([$($direct)+ $first] [$($rest)*] ; [$($index,)+ $first_index,] [$($rest_index,)*]); + }; } +// TODO TODO: merge this into `tuple_impl` once codec supports `trait Codec` for longer tuple lengths. +#[allow(non_snake_case)] +tuple_impl_indexed!(A, B, C, D, E, F, G, H, I, J, ; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,); + /// To be used only for testing. #[cfg(feature = "std")] impl SignedExtension for () { diff --git a/core/sr-primitives/src/transaction_validity.rs b/core/sr-primitives/src/transaction_validity.rs index 66e66c0042db9..a6cc43c5a365d 100644 --- a/core/sr-primitives/src/transaction_validity.rs +++ b/core/sr-primitives/src/transaction_validity.rs @@ -103,7 +103,7 @@ impl ValidTransaction { /// the logic *And* of the propagate flags. pub fn combine_with(mut self, mut other: ValidTransaction) -> Self { ValidTransaction { - priority: self.priority + other.priority, + priority: self.priority.saturating_add(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), diff --git a/node-template/runtime/src/lib.rs b/node-template/runtime/src/lib.rs index 1dcc65d00b5e4..bed67b33f23b6 100644 --- a/node-template/runtime/src/lib.rs +++ b/node-template/runtime/src/lib.rs @@ -17,7 +17,7 @@ use primitives::bytes; use primitives::{ed25519, sr25519, OpaqueMetadata}; use runtime_primitives::{ ApplyResult, transaction_validity::TransactionValidity, generic, create_runtime_str, - traits::{self, NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify} + traits::{self, NumberFor, BlakeTwo256, Block as BlockT, StaticLookup, Verify}, weights::Weight, }; use client::{ block_builder::api::{CheckInherentsResult, InherentData, self as block_builder_api}, @@ -113,6 +113,7 @@ pub fn native_version() -> NativeVersion { parameter_types! { pub const BlockHashCount: BlockNumber = 250; + pub const MaximumBlockWeight: Weight = 4 * 1024 * 1024; } impl system::Trait for Runtime { @@ -136,6 +137,8 @@ impl system::Trait for Runtime { type Origin = Origin; /// Maximum number of block number to block hash mappings to keep (oldest pruned first). type BlockHashCount = BlockHashCount; + /// Maximum weight of each block. With a default weight system of 1byte == 1weight, 4mb is ok. + type MaximumBlockWeight = MaximumBlockWeight; } impl aura::Trait for Runtime { @@ -228,11 +231,11 @@ pub type Block = generic::Block; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; /// The SignedExtension to the basic transaction logic. -pub type SignedExtra = (system::CheckNonce, balances::TakeFees); +pub type SignedExtra = (system::CheckNonce, system::CheckWeight, balances::TakeFees); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; +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; diff --git a/node-template/runtime/src/template.rs b/node-template/runtime/src/template.rs index 05257d2a53341..97466d2b6dce5 100644 --- a/node-template/runtime/src/template.rs +++ b/node-template/runtime/src/template.rs @@ -73,6 +73,7 @@ mod tests { use primitives::{H256, Blake2Hasher}; use support::{impl_outer_origin, assert_ok, parameter_types}; use runtime_primitives::{traits::{BlakeTwo256, IdentityLookup}, testing::Header}; + use runtime_primitives::weights::Weight; impl_outer_origin! { pub enum Origin for Test {} @@ -85,6 +86,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: Weight = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -97,6 +99,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Test { type Event = (); diff --git a/node/cli/src/factory_impl.rs b/node/cli/src/factory_impl.rs index 40f4f602ce712..5df7f3064692e 100644 --- a/node/cli/src/factory_impl.rs +++ b/node/cli/src/factory_impl.rs @@ -24,7 +24,7 @@ use rand::rngs::StdRng; use parity_codec::Decode; use keyring::sr25519::Keyring; use node_primitives::Hash; -use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, BalancesCall}; +use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, BalancesCall, Runtime}; use primitives::sr25519; use primitives::crypto::Pair; use parity_codec::Encode; @@ -54,6 +54,12 @@ pub struct FactoryState { type Number = <::Header as HeaderT>::Number; +impl FactoryState { + fn build_extra(index: node_primitives::Index) -> node_runtime::SignedExtra { + (system::CheckNonce::from(index), system::CheckWeight::from(), balances::TakeFees::from(0)) + } +} + impl RuntimeAdapter for FactoryState { type AccountId = node_primitives::AccountId; type Balance = node_primitives::Balance; @@ -130,11 +136,8 @@ impl RuntimeAdapter for FactoryState { ) -> ::Extrinsic { let index = self.extract_index(&sender, prior_block_hash); let phase = self.extract_phase(*prior_block_hash); - let check_nonce = system::CheckNonce::from(index); - let take_fees = balances::TakeFees::from(0); - sign::(CheckedExtrinsic { - signed: Some((sender.clone(), (check_nonce, take_fees))), + signed: Some((sender.clone(), Self::build_extra(index))), function: Call::Balances( BalancesCall::transfer( indices::address::Address::Id(destination.clone().into()), diff --git a/node/cli/src/service.rs b/node/cli/src/service.rs index ce054f9f798a2..947cadc25cfc8 100644 --- a/node/cli/src/service.rs +++ b/node/cli/src/service.rs @@ -359,8 +359,9 @@ mod tests { let function = Call::Balances(BalancesCall::transfer(to.into(), amount)); let era = Era::immortal(); let check_nonce = system::CheckNonce::from(index); + let check_weight = system::CheckWeight::from(); let take_fees = balances::TakeFees::from(0); - let extra = (check_nonce, take_fees); + let extra = (check_nonce, check_weight, take_fees); let raw_payload = (function, era, genesis_hash, extra.clone()); let signature = raw_payload.using_encoded(|payload| if payload.len() > 256 { diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index 87c4d4a88e4bc..cf40b28cebdb8 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -80,8 +80,10 @@ mod tests { type TestExternalities = CoreTestExternalities; - fn transfer_fee(bytes: Balance) -> Balance { - >::get() + >::get() * bytes + fn transfer_fee(extrinsic: &E) -> Balance { + >::get() + + >::get() * + (extrinsic.encode().len() as Balance) } fn creation_fee() -> Balance { @@ -137,8 +139,8 @@ mod tests { } } - fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra { - (system::CheckNonce::from(nonce), balances::TakeFees::from(extra_fee)) + fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra { + (system::CheckNonce::from(nonce), system::CheckWeight::from(), balances::TakeFees::from(extra_fee)) } fn xt() -> UncheckedExtrinsic { @@ -259,7 +261,7 @@ mod tests { assert!(r.is_ok()); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee()); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } @@ -295,7 +297,7 @@ mod tests { assert!(r.is_ok()); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee()); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } @@ -490,7 +492,6 @@ mod tests { // session change => consensus authorities change => authorities change digest item appears let digest = Header::decode(&mut &block2.0[..]).unwrap().digest; assert_eq!(digest.logs().len(), 0); -// assert!(digest.logs()[0].as_consensus().is_some()); (block1, block2) } @@ -528,7 +529,7 @@ mod tests { ).0.unwrap(); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee()); assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS); let events = vec![ EventRecord { @@ -565,8 +566,8 @@ mod tests { runtime_io::with_externalities(&mut t, || { // TODO TODO: this needs investigating: why are we deducting creation fee twice here? and why bob also pays it? - assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(169) - 2 * creation_fee()); - assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(&xt()) - 2 * creation_fee()); + assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - transfer_fee(&xt()) - creation_fee()); let events = vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -621,15 +622,15 @@ mod tests { WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block1.0).unwrap(); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt()) - creation_fee()); assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS); }); WasmExecutor::new().call(&mut t, 8, COMPACT_CODE, "Core_execute_block", &block2.0).unwrap(); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(169) - 2 * creation_fee()); - assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - 1 * transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 32 * DOLLARS - 2 * transfer_fee(&xt()) - 2 * creation_fee()); + assert_eq!(Balances::total_balance(&bob()), 179 * DOLLARS - 1 * transfer_fee(&xt()) - creation_fee()); }); } @@ -875,7 +876,7 @@ mod tests { assert_eq!(r, Ok(ApplyOutcome::Success)); runtime_io::with_externalities(&mut t, || { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(169) - creation_fee()); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(&xt()) - creation_fee()); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 71784959e5ef8..b79421154e590 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -36,6 +36,7 @@ use client::{ }; use runtime_primitives::{ApplyResult, impl_opaque_keys, generic, create_runtime_str, key_types}; use runtime_primitives::transaction_validity::TransactionValidity; +use runtime_primitives::weights::Weight; use runtime_primitives::traits::{ BlakeTwo256, Block as BlockT, DigestFor, NumberFor, StaticLookup, Convert, }; @@ -111,6 +112,7 @@ pub const DAYS: Moment = HOURS * 24; parameter_types! { pub const BlockHashCount: BlockNumber = 250; + pub const MaximumBlockWeight: Weight = 4 * 1024 * 1024; } impl system::Trait for Runtime { @@ -124,6 +126,7 @@ impl system::Trait for Runtime { type Header = generic::Header; type Event = Event; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl aura::Trait for Runtime { @@ -436,11 +439,11 @@ pub type SignedBlock = generic::SignedBlock; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; /// The SignedExtension to the basic transaction logic. -pub type SignedExtra = (system::CheckNonce, balances::TakeFees); +pub type SignedExtra = (system::CheckNonce, system::CheckWeight, balances::TakeFees); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; +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, Runtime, AllModules>; diff --git a/srml/assets/src/lib.rs b/srml/assets/src/lib.rs index 19159bf60fba3..1e4c06700abe1 100644 --- a/srml/assets/src/lib.rs +++ b/srml/assets/src/lib.rs @@ -257,6 +257,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -269,6 +270,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Test { type Event = (); diff --git a/srml/aura/src/mock.rs b/srml/aura/src/mock.rs index fad511baba331..ba6a66cdace68 100644 --- a/srml/aura/src/mock.rs +++ b/srml/aura/src/mock.rs @@ -37,6 +37,7 @@ pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { @@ -50,6 +51,7 @@ impl system::Trait for Test { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl timestamp::Trait for Test { diff --git a/srml/authorship/src/lib.rs b/srml/authorship/src/lib.rs index 08a0279284871..42fa0630a4855 100644 --- a/srml/authorship/src/lib.rs +++ b/srml/authorship/src/lib.rs @@ -337,6 +337,7 @@ mod tests { parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { @@ -350,6 +351,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Test { diff --git a/srml/balances/src/lib.rs b/srml/balances/src/lib.rs index 493090ed0b3c2..6f0872d37ad98 100644 --- a/srml/balances/src/lib.rs +++ b/srml/balances/src/lib.rs @@ -747,6 +747,7 @@ impl, I: Instance> system::Trait for ElevatedTrait { type Header = T::Header; type Event = (); type BlockHashCount = T::BlockHashCount; + type MaximumBlockWeight = T::MaximumBlockWeight; } impl, I: Instance> Trait for ElevatedTrait { type Balance = T::Balance; diff --git a/srml/balances/src/mock.rs b/srml/balances/src/mock.rs index 26f4439f4608d..274f093386823 100644 --- a/srml/balances/src/mock.rs +++ b/srml/balances/src/mock.rs @@ -67,6 +67,7 @@ impl Get for TransactionByteFee { pub struct Runtime; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Runtime { type Origin = Origin; @@ -79,6 +80,7 @@ impl system::Trait for Runtime { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Runtime { type Balance = u64; diff --git a/srml/balances/src/tests.rs b/srml/balances/src/tests.rs index 10e6f94693682..c5e36ed6571f2 100644 --- a/srml/balances/src/tests.rs +++ b/srml/balances/src/tests.rs @@ -658,3 +658,20 @@ fn extra_balance_should_transfer() { } ); } + +#[test] +fn signed_extension_take_fees_work() { + with_externalities( + &mut ExtBuilder::default() + .existential_deposit(10) + .transaction_fees(10, 1) + .monied(true) + .build(), + || { + assert!(TakeFees::::from(0).validate(&1, 10).is_ok()); + assert_eq!(Balances::free_balance(&1), 100 - 20); + assert!(TakeFees::::from(5 /* tipped */).validate(&1, 10).is_ok()); + assert_eq!(Balances::free_balance(&1), 100 - 20 - 25); + } + ); +} diff --git a/srml/collective/src/lib.rs b/srml/collective/src/lib.rs index 23b156c718e7f..a8303501d46ea 100644 --- a/srml/collective/src/lib.rs +++ b/srml/collective/src/lib.rs @@ -402,6 +402,7 @@ mod tests { parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -414,6 +415,7 @@ mod tests { type Header = Header; type Event = Event; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Test { type Origin = Origin; diff --git a/srml/contracts/src/tests.rs b/srml/contracts/src/tests.rs index b195a74bf429b..c3c6a595a3442 100644 --- a/srml/contracts/src/tests.rs +++ b/srml/contracts/src/tests.rs @@ -96,6 +96,7 @@ impl Get for BlockGasLimit { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -108,6 +109,7 @@ impl system::Trait for Test { type Header = Header; type Event = MetaEvent; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const BalancesTransactionBaseFee: u64 = 0; diff --git a/srml/council/src/lib.rs b/srml/council/src/lib.rs index 8a52f9ec004a9..1baeb6fdfd34b 100644 --- a/srml/council/src/lib.rs +++ b/srml/council/src/lib.rs @@ -98,6 +98,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -110,6 +111,7 @@ mod tests { type Header = Header; type Event = Event; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; diff --git a/srml/democracy/src/lib.rs b/srml/democracy/src/lib.rs index d13feb4db531d..5c49f9519013d 100644 --- a/srml/democracy/src/lib.rs +++ b/srml/democracy/src/lib.rs @@ -998,6 +998,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -1010,6 +1011,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; diff --git a/srml/elections/src/lib.rs b/srml/elections/src/lib.rs index 77597591775d7..324c1a78fbf1e 100644 --- a/srml/elections/src/lib.rs +++ b/srml/elections/src/lib.rs @@ -1108,6 +1108,7 @@ mod tests { parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -1120,6 +1121,7 @@ mod tests { type Header = Header; type Event = Event; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; diff --git a/srml/example/src/lib.rs b/srml/example/src/lib.rs index 20ee1c6ba114f..b2bb0a0af47e4 100644 --- a/srml/example/src/lib.rs +++ b/srml/example/src/lib.rs @@ -525,6 +525,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -537,6 +538,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; diff --git a/srml/executive/Cargo.toml b/srml/executive/Cargo.toml index 27057fe523ab3..3f9abb28cc63a 100644 --- a/srml/executive/Cargo.toml +++ b/srml/executive/Cargo.toml @@ -17,6 +17,7 @@ system = { package = "srml-system", path = "../system", default-features = false hex-literal = "0.2.0" substrate-primitives = { path = "../../core/primitives" } srml-indices = { path = "../indices" } +node-runtime = { path = "../../node/runtime" } balances = { package = "srml-balances", path = "../balances" } [features] diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index 512b9086c9cbb..135b3647cfd75 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -91,8 +91,6 @@ use primitives::weights::Weighable; mod internal { use primitives::traits::DispatchError; - pub const MAX_TRANSACTIONS_WEIGHT: u32 = 4 * 1024 * 1024; - pub enum ApplyError { BadSignature(&'static str), Stale, @@ -283,17 +281,10 @@ where >::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); - } - // AUDIT: Under no circumstances may this function panic from here onwards. // Decode parameters and dispatch + let weight = xt.weight(encoded_len); let r = Applyable::dispatch(xt, weight) .map_err(internal::ApplyError::from)?; @@ -369,7 +360,7 @@ mod tests { use primitives::traits::{Header as HeaderT, BlakeTwo256, IdentityLookup}; use primitives::testing::{Digest, Header, Block}; use srml_support::{impl_outer_event, impl_outer_origin, parameter_types}; - use srml_support::traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason}; + use srml_support::traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons, WithdrawReason, Get}; use system; use hex_literal::hex; @@ -389,6 +380,7 @@ mod tests { pub struct Runtime; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Runtime { type Origin = Origin; @@ -401,6 +393,7 @@ mod tests { type Header = Header; type Event = MetaEvent; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; @@ -435,12 +428,12 @@ mod tests { } } - type SignedExtra = (system::CheckNonce, balances::TakeFees); + type SignedExtra = node_runtime::SignedExtra; type TestXt = primitives::testing::TestXt, SignedExtra>; type Executive = super::Executive, system::ChainContext, Runtime, ()>; fn extra(nonce: u64, fee: u64) -> SignedExtra { - (system::CheckNonce::from(nonce), balances::TakeFees::from(fee)) + (system::CheckNonce::from(nonce), system::CheckWeight::from(), balances::TakeFees::from(fee)) } #[test] @@ -551,7 +544,7 @@ mod tests { let xt = primitives::testing::TestXt(Some(1), Call::transfer(33, 69), extra(0, 0)); let xt2 = primitives::testing::TestXt(Some(1), Call::transfer(33, 69), extra(1, 0)); let encoded = xt2.encode(); - let len = if should_fail { (internal::MAX_TRANSACTIONS_WEIGHT - 1) as usize } else { encoded.len() }; + let len = if should_fail { ( ::MaximumBlockWeight::get() - 1) as usize } else { encoded.len() }; let encoded_len = encoded.len() as u32; with_externalities(&mut t, || { Executive::initialize_block(&Header::new( @@ -583,14 +576,16 @@ mod tests { } #[test] - fn default_block_weight() { - let xt = primitives::testing::TestXt(None, Call::set_balance(33, 69, 69), extra(0, 0)); + fn default_block_weight_is_stored() { + let xt = primitives::testing::TestXt(Some(1), Call::transfer(33, 0), extra(0, 0)); + let x1 = primitives::testing::TestXt(Some(1), Call::transfer(33, 0), extra(1, 0)); + let x2 = primitives::testing::TestXt(Some(1), Call::transfer(33, 0), extra(2, 0)); let len = xt.clone().encode().len() as u32; let mut t = new_test_ext(); with_externalities(&mut t, || { Executive::apply_extrinsic(xt.clone()).unwrap(); - Executive::apply_extrinsic(xt.clone()).unwrap(); - Executive::apply_extrinsic(xt.clone()).unwrap(); + Executive::apply_extrinsic(x1.clone()).unwrap(); + Executive::apply_extrinsic(x2.clone()).unwrap(); assert_eq!( >::all_extrinsics_weight(), 3 * (0 /*base*/ + len /*len*/ * 1 /*byte*/) diff --git a/srml/finality-tracker/src/lib.rs b/srml/finality-tracker/src/lib.rs index f9ccc36346222..2e17d9688f1c9 100644 --- a/srml/finality-tracker/src/lib.rs +++ b/srml/finality-tracker/src/lib.rs @@ -299,6 +299,7 @@ mod tests { parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -311,6 +312,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const WindowSize: u64 = 11; diff --git a/srml/grandpa/src/mock.rs b/srml/grandpa/src/mock.rs index 733b2deaf1493..1420d65eb5576 100644 --- a/srml/grandpa/src/mock.rs +++ b/srml/grandpa/src/mock.rs @@ -43,6 +43,7 @@ impl Trait for Test { } parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -55,6 +56,7 @@ impl system::Trait for Test { type Header = Header; type Event = TestEvent; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } mod grandpa { diff --git a/srml/indices/src/mock.rs b/srml/indices/src/mock.rs index 53e8f314c94bb..938aec2bc3311 100644 --- a/srml/indices/src/mock.rs +++ b/srml/indices/src/mock.rs @@ -66,6 +66,7 @@ impl ResolveHint for TestResolveHint { pub struct Runtime; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Runtime { type Origin = Origin; @@ -78,6 +79,7 @@ impl system::Trait for Runtime { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Runtime { type AccountIndex = u64; diff --git a/srml/session/src/mock.rs b/srml/session/src/mock.rs index adb37720519a8..e6228e21042ed 100644 --- a/srml/session/src/mock.rs +++ b/srml/session/src/mock.rs @@ -109,6 +109,7 @@ pub fn set_next_validators(next: Vec) { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -121,6 +122,7 @@ impl system::Trait for Test { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl timestamp::Trait for Test { type Moment = u64; diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index abad1752a6ec6..b767f446df1e8 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -87,6 +87,7 @@ impl_outer_origin!{ pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -99,6 +100,7 @@ impl system::Trait for Test { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const TransferFee: u64 = 0; diff --git a/srml/system/src/lib.rs b/srml/system/src/lib.rs index bc2b217a49eeb..3ab8c83a40271 100644 --- a/srml/system/src/lib.rs +++ b/srml/system/src/lib.rs @@ -76,6 +76,7 @@ use serde::Serialize; use rstd::prelude::*; #[cfg(any(feature = "std", test))] use rstd::map; +use rstd::marker::PhantomData; use primitives::{ generic, weights::Weight, traits::{ self, CheckEqual, SimpleArithmetic, Zero, SignedExtension, @@ -190,6 +191,9 @@ pub trait Trait: 'static + Eq + Clone { /// Maximum number of block number to block hash mappings to keep (oldest pruned first). type BlockHashCount: Get; + + /// The maximum weight of a block. + type MaximumBlockWeight: Get; } pub type DigestOf = generic::Digest<::Hash>; @@ -725,17 +729,15 @@ impl Module { } /// To be called immediately after an extrinsic has been applied. - pub fn note_applied_extrinsic(r: &Result<(), &'static str>, encoded_len: u32) { + pub fn note_applied_extrinsic(r: &Result<(), &'static str>, _encoded_len: u32) { Self::deposit_event(match r { Ok(_) => Event::ExtrinsicSuccess, Err(_) => Event::ExtrinsicFailed, }.into()); let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32; - let total_length = encoded_len.saturating_add(Self::all_extrinsics_weight()); storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &next_extrinsic_index); - AllExtrinsicsWeight::put(&total_length); } /// To be called immediately after `note_applied_extrinsic` of the last extrinsic of the block @@ -753,6 +755,42 @@ impl Module { } } +/// Weight limit check and increment. +#[derive(Encode, Decode, Clone, Eq, PartialEq)] +pub struct CheckWeight(PhantomData); + +impl SignedExtension for CheckWeight { + type AccountId = T::AccountId; + // TODO TODO: make sure that this has to be here and not in validate. + fn pre_dispatch( + self, + _who: &Self::AccountId, + weight: Weight, + ) -> Result<(), DispatchError> { + let current_weight = Module::::all_extrinsics_weight(); + let next_weight = current_weight.saturating_add(weight); + if next_weight > T::MaximumBlockWeight::get() { + return Err(DispatchError::Payment) + } + AllExtrinsicsWeight::put(next_weight); + Ok(()) + } +} + +#[cfg(feature = "std")] +impl CheckWeight { + pub fn from() -> Self { + Self(PhantomData) + } +} + +#[cfg(feature = "std")] +impl rstd::fmt::Debug for CheckWeight { + fn fmt(&self, f: &mut rstd::fmt::Formatter) -> rstd::fmt::Result { + write!(f, "CheckWeight") + } +} + /// Nonce check and increment to give replay protection for transactions. #[derive(Encode, Decode, Clone, Eq, PartialEq)] pub struct CheckNonce(#[codec(compact)] T::Index); @@ -864,6 +902,7 @@ mod tests { parameter_types! { pub const BlockHashCount: u64 = 10; + pub const MaximumBlockWeight: Weight = 1024; } impl Trait for Test { @@ -877,6 +916,7 @@ mod tests { type Header = Header; type Event = u16; type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl From for u16 { @@ -1027,4 +1067,36 @@ mod tests { } }) } + + #[test] + fn signed_ext_check_nonce_works() { + with_externalities(&mut new_test_ext(), || { + >::insert(1, 1); + // stale + assert!(CheckNonce::(0).validate(&1, 0).is_err()); + assert!(CheckNonce::(0).pre_dispatch(&1, 0).is_err()); + // correct + assert!(CheckNonce::(1).validate(&1, 0).is_ok()); + assert!(CheckNonce::(1).pre_dispatch(&1, 0).is_ok()); + // future + assert!(CheckNonce::(5).validate(&1, 0).is_ok()); + assert!(CheckNonce::(5).pre_dispatch(&1, 0).is_err()); + }) + } + + #[test] + fn signed_ext_check_weight_works() { + with_externalities(&mut new_test_ext(), || { + // small + AllExtrinsicsWeight::put(512); + assert!(CheckWeight::(PhantomData).pre_dispatch(&1, 100).is_ok()); + // almost + AllExtrinsicsWeight::put(512); + assert!(CheckWeight::(PhantomData).pre_dispatch(&1, 512).is_ok()); + // big + AllExtrinsicsWeight::put(512); + assert!(CheckWeight::(PhantomData).pre_dispatch(&1, 513).is_err()); + + }) + } } diff --git a/srml/timestamp/src/lib.rs b/srml/timestamp/src/lib.rs index 7bffac0db9007..a1c0b2f69fcaa 100644 --- a/srml/timestamp/src/lib.rs +++ b/srml/timestamp/src/lib.rs @@ -343,6 +343,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -355,6 +356,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } impl Trait for Test { type Moment = u64; diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 7c4d7b10f21cf..aa49190d5abb2 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -370,6 +370,7 @@ mod tests { pub struct Test; parameter_types! { pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: u32 = 1024; } impl system::Trait for Test { type Origin = Origin; @@ -382,6 +383,7 @@ mod tests { type Header = Header; type Event = (); type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; } parameter_types! { pub const ExistentialDeposit: u64 = 0; diff --git a/subkey/src/main.rs b/subkey/src/main.rs index dfd923e993149..ce9ababeceb7d 100644 --- a/subkey/src/main.rs +++ b/subkey/src/main.rs @@ -87,7 +87,11 @@ fn execute(matches: clap::ArgMatches) where <::Pair as Pair>::Public: Sized + AsRef<[u8]> + Ss58Codec + AsRef<<::Pair as Pair>::Public>, { let extra = |i: Index, f: Balance| { - (system::CheckNonce::::from(i), balances::TakeFees::::from(f)) + ( + system::CheckNonce::::from(i), + system::CheckWeight::::from(), + balances::TakeFees::::from(f), + ) }; let password = matches.value_of("password"); match matches.subcommand() { From cd6147dd517dfa2fbbefe9c9656243b592da6417 Mon Sep 17 00:00:00 2001 From: kianenigma Date: Tue, 16 Jul 2019 11:46:32 +0200 Subject: [PATCH 2/6] Revert a bit + test for check era. --- .../src/generic/unchecked_extrinsic.rs | 2 +- core/sr-primitives/src/traits.rs | 5 +++-- node-template/runtime/src/lib.rs | 6 +++--- node/cli/src/factory_impl.rs | 6 +++--- node/executor/src/lib.rs | 2 +- node/runtime/src/lib.rs | 14 ++++++------- srml/balances/src/lib.rs | 1 + srml/executive/src/lib.rs | 7 ++++++- srml/system/src/lib.rs | 20 +++++++++++++++++++ 9 files changed, 45 insertions(+), 18 deletions(-) diff --git a/core/sr-primitives/src/generic/unchecked_extrinsic.rs b/core/sr-primitives/src/generic/unchecked_extrinsic.rs index 60488cbee4d0c..65c0f20182e5e 100644 --- a/core/sr-primitives/src/generic/unchecked_extrinsic.rs +++ b/core/sr-primitives/src/generic/unchecked_extrinsic.rs @@ -199,7 +199,6 @@ mod tests { use super::*; use runtime_io::blake2_256; use crate::codec::{Encode, Decode}; - use crate::generic::Era; use crate::traits::{SignedExtension, BlockNumberToHash, Lookup, CurrentHeight}; use serde::{Serialize, Deserialize}; @@ -239,6 +238,7 @@ mod tests { impl SignedExtension for TestExtra { type AccountId = u64; type AdditionalSigned = (); + fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) } } type Ex = UncheckedExtrinsic; diff --git a/core/sr-primitives/src/traits.rs b/core/sr-primitives/src/traits.rs index af2f4341cfa92..1afe65fa274b2 100644 --- a/core/sr-primitives/src/traits.rs +++ b/core/sr-primitives/src/traits.rs @@ -815,11 +815,11 @@ pub trait SignedExtension: /// Any additional data that will go into the signed payload. This may be created dynamically /// from the transaction using the `additional_signed` function. - type AdditionalSigned: Encode + Default; + type AdditionalSigned: Encode; /// Construct any additional data that should be in the signed payload of the transaction. Can /// also perform any pre-signature-verification checks and return an error if needed. - fn additional_signed(&self) -> Result { Ok(Default::default()) } + fn additional_signed(&self) -> Result; /// Validate a signed transaction for the transaction queue. fn validate( @@ -915,6 +915,7 @@ tuple_impl_indexed!(A, B, C, D, E, F, G, H, I, J, ; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 impl SignedExtension for () { type AccountId = u64; type AdditionalSigned = (); + fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) } } /// An "executable" piece of information, used by the standard Substrate Executive in order to diff --git a/node-template/runtime/src/lib.rs b/node-template/runtime/src/lib.rs index bed67b33f23b6..3a6eb1886fc34 100644 --- a/node-template/runtime/src/lib.rs +++ b/node-template/runtime/src/lib.rs @@ -231,11 +231,11 @@ pub type Block = generic::Block; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; /// The SignedExtension to the basic transaction logic. -pub type SignedExtra = (system::CheckNonce, system::CheckWeight, balances::TakeFees); +pub type SignedExtra = (system::CheckNonce, system::CheckWeight, balances::TakeFees); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic>; +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; diff --git a/node/cli/src/factory_impl.rs b/node/cli/src/factory_impl.rs index 34b079e5dc0d7..3dc50744682e6 100644 --- a/node/cli/src/factory_impl.rs +++ b/node/cli/src/factory_impl.rs @@ -23,7 +23,7 @@ use rand::rngs::StdRng; use parity_codec::Decode; use keyring::sr25519::Keyring; -use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall, Runtime}; +use node_runtime::{Call, CheckedExtrinsic, UncheckedExtrinsic, SignedExtra, BalancesCall}; use primitives::{sr25519, crypto::Pair}; use parity_codec::Encode; use sr_primitives::{generic::Era, traits::{Block as BlockT, Header as HeaderT, SignedExtension}}; @@ -52,7 +52,7 @@ pub struct FactoryState { type Number = <::Header as HeaderT>::Number; impl FactoryState { - fn build_extra(index: node_primitives::Index, phase: u64) -> node_runtime::SignedExtra { + fn build_extra(index: node_primitives::Index, phase: u64) -> node_runtime::SignedExtra { ( system::CheckEra::from(Era::mortal(256, phase)), system::CheckNonce::from(index), @@ -234,7 +234,7 @@ fn gen_seed_bytes(seed: u64) -> [u8; 32] { fn sign( xt: CheckedExtrinsic, key: &sr25519::Pair, - additional_signed: as SignedExtension>::AdditionalSigned, + additional_signed: ::AdditionalSigned, ) -> ::Extrinsic { let s = match xt.signed { Some((signed, extra)) => { diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index f5b7af0bd5c88..4b9654e287e33 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -139,7 +139,7 @@ mod tests { } } - fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra { + fn signed_extra(nonce: Index, extra_fee: Balance) -> SignedExtra { ( system::CheckEra::from(Era::mortal(256, 0)), system::CheckNonce::from(nonce), diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index ddfd0e3cf9de8..a8130dee0ea17 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -439,16 +439,16 @@ pub type SignedBlock = generic::SignedBlock; /// BlockId type as expected by this runtime. pub type BlockId = generic::BlockId; /// The SignedExtension to the basic transaction logic. -pub type SignedExtra = ( - system::CheckEra, - system::CheckNonce, - system::CheckWeight, - balances::TakeFees +pub type SignedExtra = ( + system::CheckEra, + system::CheckNonce, + system::CheckWeight, + balances::TakeFees ); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic>; +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, Runtime, AllModules>; diff --git a/srml/balances/src/lib.rs b/srml/balances/src/lib.rs index b49d5c77ec5ff..004ea41a035e9 100644 --- a/srml/balances/src/lib.rs +++ b/srml/balances/src/lib.rs @@ -1158,6 +1158,7 @@ use primitives::weights::Weight; impl, I: Instance + Clone + Eq> SignedExtension for TakeFees { type AccountId = T::AccountId; type AdditionalSigned = (); + fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) } fn validate( &self, diff --git a/srml/executive/src/lib.rs b/srml/executive/src/lib.rs index 4822bfe8807c8..fa08a1017b94f 100644 --- a/srml/executive/src/lib.rs +++ b/srml/executive/src/lib.rs @@ -429,7 +429,12 @@ mod tests { } } - type SignedExtra = node_runtime::SignedExtra; + type SignedExtra = ( + system::CheckEra, + system::CheckNonce, + system::CheckWeight, + balances::TakeFees + ); type TestXt = primitives::testing::TestXt, SignedExtra>; type Executive = super::Executive, system::ChainContext, Runtime, ()>; diff --git a/srml/system/src/lib.rs b/srml/system/src/lib.rs index cae8deac5145f..be7a3ec492252 100644 --- a/srml/system/src/lib.rs +++ b/srml/system/src/lib.rs @@ -775,6 +775,8 @@ impl SignedExtension for CheckWeight { type AccountId = T::AccountId; type AdditionalSigned = (); + fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) } + fn pre_dispatch( self, _who: &Self::AccountId, @@ -831,6 +833,8 @@ impl SignedExtension for CheckNonce { type AccountId = T::AccountId; type AdditionalSigned = (); + fn additional_signed(&self) -> rstd::result::Result<(), &'static str> { Ok(()) } + fn pre_dispatch( self, who: &Self::AccountId, @@ -1148,4 +1152,20 @@ mod tests { }) } + + #[test] + fn signed_ext_check_era_should_work() { + with_externalities(&mut new_test_ext(), || { + // future + assert_eq!( + CheckEra::::from(Era::mortal(4, 2)).additional_signed().err().unwrap(), + "transaction birth block ancient" + ); + + // correct + System::set_block_number(13); + >::insert(12, H256::repeat_byte(1)); + assert!(CheckEra::::from(Era::mortal(4, 12)).additional_signed().is_ok()); + }) + } } From a3fbb3302b38c366ebddeb64ff01b46fb59597b1 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Tue, 16 Jul 2019 19:39:35 +0900 Subject: [PATCH 3/6] Update Cargo.toml --- srml/executive/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/srml/executive/Cargo.toml b/srml/executive/Cargo.toml index 3f9abb28cc63a..27057fe523ab3 100644 --- a/srml/executive/Cargo.toml +++ b/srml/executive/Cargo.toml @@ -17,7 +17,6 @@ system = { package = "srml-system", path = "../system", default-features = false hex-literal = "0.2.0" substrate-primitives = { path = "../../core/primitives" } srml-indices = { path = "../indices" } -node-runtime = { path = "../../node/runtime" } balances = { package = "srml-balances", path = "../balances" } [features] From 7209a6518e9e15440cf3f7ec009b66aaee11c2ff Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Tue, 16 Jul 2019 19:42:18 +0900 Subject: [PATCH 4/6] Update node/cli/src/factory_impl.rs --- node/cli/src/factory_impl.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/node/cli/src/factory_impl.rs b/node/cli/src/factory_impl.rs index 3dc50744682e6..6f2ef00ad74a6 100644 --- a/node/cli/src/factory_impl.rs +++ b/node/cli/src/factory_impl.rs @@ -57,7 +57,8 @@ impl FactoryState { system::CheckEra::from(Era::mortal(256, phase)), system::CheckNonce::from(index), system::CheckWeight::from(), - balances::TakeFees::from(0)) + balances::TakeFees::from(0) + ) } } From a1c50ca8c3d75f29c4a877a188e5a86b4f635eb3 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Tue, 16 Jul 2019 19:42:27 +0900 Subject: [PATCH 5/6] Update node/executor/src/lib.rs --- node/executor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index 4b9654e287e33..836cf3f22f698 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -83,7 +83,7 @@ mod tests { // TODO: fix for being charged based on weight now. fn transfer_fee(extrinsic: &E) -> Balance { >::get() + - >::get() * + >::get() * (extrinsic.encode().len() as Balance) } From c1345f8d7b99fd7ad6c3228e008a63883ace1d5e Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Tue, 16 Jul 2019 19:42:34 +0900 Subject: [PATCH 6/6] Update node/executor/src/lib.rs --- node/executor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index 836cf3f22f698..e0617ff9d1324 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -84,7 +84,7 @@ mod tests { fn transfer_fee(extrinsic: &E) -> Balance { >::get() + >::get() * - (extrinsic.encode().len() as Balance) + (extrinsic.encode().len() as Balance) } fn creation_fee() -> Balance {