This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Extract frame_system SignedExtensions into separate files. #6474
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f92f8e
Split the code.
tomusdrw 09a53fe
Restructure.
tomusdrw e71d2c5
Split tests.
tomusdrw 56156c4
Merge branch 'master' into td-split-check
tomusdrw 5eacccb
Self-review.
tomusdrw 738c2b9
Break lines.
tomusdrw 45b2132
Move tests out.
tomusdrw 89ed7eb
Rename CheckEra -> CheckMortality but keep backwards compatibility
tomusdrw d6d21e2
Update frame/system/src/extensions/check_mortality.rs
bkchr 0616c45
Don't rename the IDENTIFIER for now.
tomusdrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use codec::{Encode, Decode}; | ||
use crate::{Trait, Module, BlockHash}; | ||
use frame_support::StorageMap; | ||
use sp_runtime::{ | ||
generic::Era, | ||
traits::{SignedExtension, DispatchInfoOf, SaturatedConversion}, | ||
transaction_validity::{ | ||
ValidTransaction, TransactionValidityError, InvalidTransaction, TransactionValidity, | ||
}, | ||
|
||
}; | ||
|
||
/// Check for transaction mortality. | ||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct CheckEra<T: Trait + Send + Sync>(Era, sp_std::marker::PhantomData<T>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we move this, can we may directly rename this? And call it |
||
|
||
impl<T: Trait + Send + Sync> CheckEra<T> { | ||
/// utility constructor. Used only in client/factory code. | ||
pub fn from(era: Era) -> Self { | ||
Self(era, sp_std::marker::PhantomData) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> sp_std::fmt::Debug for CheckEra<T> { | ||
#[cfg(feature = "std")] | ||
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
write!(f, "CheckEra({:?})", self.0) | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> SignedExtension for CheckEra<T> { | ||
type AccountId = T::AccountId; | ||
type Call = T::Call; | ||
type AdditionalSigned = T::Hash; | ||
type Pre = (); | ||
const IDENTIFIER: &'static str = "CheckEra"; | ||
|
||
fn validate( | ||
&self, | ||
_who: &Self::AccountId, | ||
_call: &Self::Call, | ||
_info: &DispatchInfoOf<Self::Call>, | ||
_len: usize, | ||
) -> TransactionValidity { | ||
let current_u64 = <Module<T>>::block_number().saturated_into::<u64>(); | ||
let valid_till = self.0.death(current_u64); | ||
Ok(ValidTransaction { | ||
longevity: valid_till.saturating_sub(current_u64), | ||
..Default::default() | ||
}) | ||
} | ||
|
||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> { | ||
let current_u64 = <Module<T>>::block_number().saturated_into::<u64>(); | ||
let n = self.0.birth(current_u64).saturated_into::<T::BlockNumber>(); | ||
if !<BlockHash<T>>::contains_key(n) { | ||
Err(InvalidTransaction::AncientBirthBlock.into()) | ||
} else { | ||
Ok(<Module<T>>::block_hash(n)) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::mock::{Test, new_test_ext, System, CALL}; | ||
use frame_support::weights::{DispatchClass, DispatchInfo, Pays}; | ||
use sp_core::H256; | ||
|
||
#[test] | ||
fn signed_ext_check_era_should_work() { | ||
new_test_ext().execute_with(|| { | ||
// future | ||
assert_eq!( | ||
CheckEra::<Test>::from(Era::mortal(4, 2)).additional_signed().err().unwrap(), | ||
InvalidTransaction::AncientBirthBlock.into(), | ||
); | ||
|
||
// correct | ||
System::set_block_number(13); | ||
<BlockHash<Test>>::insert(12, H256::repeat_byte(1)); | ||
assert!(CheckEra::<Test>::from(Era::mortal(4, 12)).additional_signed().is_ok()); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn signed_ext_check_era_should_change_longevity() { | ||
new_test_ext().execute_with(|| { | ||
let normal = DispatchInfo { weight: 100, class: DispatchClass::Normal, pays_fee: Pays::Yes }; | ||
let len = 0_usize; | ||
let ext = ( | ||
crate::CheckWeight::<Test>::default(), | ||
CheckEra::<Test>::from(Era::mortal(16, 256)), | ||
); | ||
System::set_block_number(17); | ||
<BlockHash<Test>>::insert(16, H256::repeat_byte(1)); | ||
|
||
assert_eq!(ext.validate(&1, CALL, &normal, len).unwrap().longevity, 15); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use codec::{Encode, Decode}; | ||
use crate::{Trait, Module}; | ||
use sp_runtime::{ | ||
traits::{SignedExtension, Zero}, | ||
transaction_validity::TransactionValidityError, | ||
}; | ||
|
||
/// Genesis hash check to provide replay protection between different networks. | ||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct CheckGenesis<T: Trait + Send + Sync>(sp_std::marker::PhantomData<T>); | ||
|
||
impl<T: Trait + Send + Sync> sp_std::fmt::Debug for CheckGenesis<T> { | ||
#[cfg(feature = "std")] | ||
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
write!(f, "CheckGenesis") | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> CheckGenesis<T> { | ||
/// Creates new `SignedExtension` to check genesis hash. | ||
pub fn new() -> Self { | ||
Self(sp_std::marker::PhantomData) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> SignedExtension for CheckGenesis<T> { | ||
type AccountId = T::AccountId; | ||
type Call = <T as Trait>::Call; | ||
type AdditionalSigned = T::Hash; | ||
type Pre = (); | ||
const IDENTIFIER: &'static str = "CheckGenesis"; | ||
|
||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> { | ||
Ok(<Module<T>>::block_hash(T::BlockNumber::zero())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use codec::{Encode, Decode}; | ||
use crate::Trait; | ||
use frame_support::{ | ||
weights::DispatchInfo, | ||
StorageMap, | ||
}; | ||
use sp_runtime::{ | ||
traits::{SignedExtension, DispatchInfoOf, Dispatchable, One}, | ||
transaction_validity::{ | ||
ValidTransaction, TransactionValidityError, InvalidTransaction, TransactionValidity, | ||
TransactionLongevity, TransactionPriority, | ||
}, | ||
}; | ||
use sp_std::vec; | ||
|
||
/// Nonce check and increment to give replay protection for transactions. | ||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct CheckNonce<T: Trait>(#[codec(compact)] T::Index); | ||
|
||
impl<T: Trait> CheckNonce<T> { | ||
/// utility constructor. Used only in client/factory code. | ||
pub fn from(nonce: T::Index) -> Self { | ||
Self(nonce) | ||
} | ||
} | ||
|
||
impl<T: Trait> sp_std::fmt::Debug for CheckNonce<T> { | ||
#[cfg(feature = "std")] | ||
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
write!(f, "CheckNonce({})", self.0) | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Trait> SignedExtension for CheckNonce<T> where | ||
T::Call: Dispatchable<Info=DispatchInfo> | ||
{ | ||
type AccountId = T::AccountId; | ||
type Call = T::Call; | ||
type AdditionalSigned = (); | ||
type Pre = (); | ||
const IDENTIFIER: &'static str = "CheckNonce"; | ||
|
||
fn additional_signed(&self) -> sp_std::result::Result<(), TransactionValidityError> { Ok(()) } | ||
|
||
fn pre_dispatch( | ||
self, | ||
who: &Self::AccountId, | ||
_call: &Self::Call, | ||
_info: &DispatchInfoOf<Self::Call>, | ||
_len: usize, | ||
) -> Result<(), TransactionValidityError> { | ||
let mut account = crate::Account::<T>::get(who); | ||
if self.0 != account.nonce { | ||
return Err( | ||
if self.0 < account.nonce { | ||
InvalidTransaction::Stale | ||
} else { | ||
InvalidTransaction::Future | ||
}.into() | ||
) | ||
} | ||
account.nonce += T::Index::one(); | ||
crate::Account::<T>::insert(who, account); | ||
Ok(()) | ||
} | ||
|
||
fn validate( | ||
&self, | ||
who: &Self::AccountId, | ||
_call: &Self::Call, | ||
info: &DispatchInfoOf<Self::Call>, | ||
_len: usize, | ||
) -> TransactionValidity { | ||
// check index | ||
let account = crate::Account::<T>::get(who); | ||
if self.0 < account.nonce { | ||
return InvalidTransaction::Stale.into() | ||
} | ||
|
||
let provides = vec![Encode::encode(&(who, self.0))]; | ||
let requires = if account.nonce < self.0 { | ||
vec![Encode::encode(&(who, self.0 - One::one()))] | ||
} else { | ||
vec![] | ||
}; | ||
|
||
Ok(ValidTransaction { | ||
priority: info.weight as TransactionPriority, | ||
requires, | ||
provides, | ||
longevity: TransactionLongevity::max_value(), | ||
propagate: true, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::mock::{Test, new_test_ext, CALL}; | ||
|
||
#[test] | ||
fn signed_ext_check_nonce_works() { | ||
new_test_ext().execute_with(|| { | ||
crate::Account::<Test>::insert(1, crate::AccountInfo { | ||
nonce: 1, | ||
refcount: 0, | ||
data: 0, | ||
}); | ||
let info = DispatchInfo::default(); | ||
let len = 0_usize; | ||
// stale | ||
assert!(CheckNonce::<Test>(0).validate(&1, CALL, &info, len).is_err()); | ||
assert!(CheckNonce::<Test>(0).pre_dispatch(&1, CALL, &info, len).is_err()); | ||
// correct | ||
assert!(CheckNonce::<Test>(1).validate(&1, CALL, &info, len).is_ok()); | ||
assert!(CheckNonce::<Test>(1).pre_dispatch(&1, CALL, &info, len).is_ok()); | ||
// future | ||
assert!(CheckNonce::<Test>(5).validate(&1, CALL, &info, len).is_ok()); | ||
assert!(CheckNonce::<Test>(5).pre_dispatch(&1, CALL, &info, len).is_err()); | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use crate::{Trait, Module}; | ||
use codec::{Encode, Decode}; | ||
use sp_runtime::{ | ||
traits::SignedExtension, | ||
transaction_validity::TransactionValidityError, | ||
}; | ||
|
||
/// Ensure the runtime version registered in the transaction is the same as at present. | ||
#[derive(Encode, Decode, Clone, Eq, PartialEq)] | ||
pub struct CheckSpecVersion<T: Trait + Send + Sync>(sp_std::marker::PhantomData<T>); | ||
|
||
impl<T: Trait + Send + Sync> sp_std::fmt::Debug for CheckSpecVersion<T> { | ||
#[cfg(feature = "std")] | ||
fn fmt(&self, f: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
write!(f, "CheckSpecVersion") | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
fn fmt(&self, _: &mut sp_std::fmt::Formatter) -> sp_std::fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> CheckSpecVersion<T> { | ||
/// Create new `SignedExtension` to check runtime version. | ||
pub fn new() -> Self { | ||
Self(sp_std::marker::PhantomData) | ||
} | ||
} | ||
|
||
impl<T: Trait + Send + Sync> SignedExtension for CheckSpecVersion<T> { | ||
type AccountId = T::AccountId; | ||
type Call = <T as Trait>::Call; | ||
type AdditionalSigned = u32; | ||
type Pre = (); | ||
const IDENTIFIER: &'static str = "CheckSpecVersion"; | ||
|
||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError> { | ||
Ok(<Module<T>>::runtime_version().spec_version) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.