Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Companion for #8949 #3216

Merged
8 commits merged into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 155 additions & 155 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ construct_runtime!(
BridgeRialtoGrandpa: pallet_bridge_grandpa::{Pallet, Call, Storage},
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Aura: pallet_aura::{Pallet, Config<T>},
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event},
Expand Down
2 changes: 1 addition & 1 deletion bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ construct_runtime!(
BridgeDispatch: pallet_bridge_dispatch::{Pallet, Event<T>},
BridgeMillauMessages: pallet_bridge_messages::{Pallet, Call, Storage, Event<T>},
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Aura: pallet_aura::{Pallet, Config<T>},
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event},
Expand Down
102 changes: 53 additions & 49 deletions runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use sp_runtime::traits::Zero;
use sp_runtime::{
traits::{CheckedSub, SignedExtension, DispatchInfoOf}, RuntimeDebug,
transaction_validity::{
TransactionLongevity, TransactionValidity, ValidTransaction, InvalidTransaction,
TransactionSource, TransactionValidityError,
TransactionValidity, ValidTransaction, InvalidTransaction, TransactionValidityError,
},
};
use primitives::v1::ValidityError;
Expand Down Expand Up @@ -429,6 +428,53 @@ pub mod pallet {
Ok(Pays::No.into())
}
}

#[pallet::validate_unsigned]
impl<T: Config> ValidateUnsigned for Pallet<T> {
type Call = Call<T>;

fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
const PRIORITY: u64 = 100;

let (maybe_signer, maybe_statement) = match call {
// <weight>
// The weight of this logic is included in the `claim` dispatchable.
// </weight>
Call::claim(account, ethereum_signature) => {
let data = account.using_encoded(to_ascii_hex);
(Self::eth_recover(&ethereum_signature, &data, &[][..]), None)
}
// <weight>
// The weight of this logic is included in the `claim_attest` dispatchable.
// </weight>
Call::claim_attest(account, ethereum_signature, statement) => {
let data = account.using_encoded(to_ascii_hex);
(Self::eth_recover(&ethereum_signature, &data, &statement), Some(statement.as_slice()))
}
_ => return Err(InvalidTransaction::Call.into()),
};

let signer = maybe_signer
.ok_or(InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()))?;

let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
ensure!(<Claims<T>>::contains_key(&signer), e);

let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
match Signing::<T>::get(signer) {
None => ensure!(maybe_statement.is_none(), e),
Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
}

Ok(ValidTransaction {
priority: PRIORITY,
requires: vec![],
provides: vec![("claims", signer).encode()],
longevity: TransactionLongevity::max_value(),
propagate: true,
})
}
}
}

/// Converts the given binary data into ASCII-encoded hex. It will be twice the length.
Expand Down Expand Up @@ -503,52 +549,6 @@ impl<T: Config> Pallet<T> {
}
}

impl<T: Config> sp_runtime::traits::ValidateUnsigned for Pallet<T> {
type Call = Call<T>;

fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
const PRIORITY: u64 = 100;

let (maybe_signer, maybe_statement) = match call {
// <weight>
// The weight of this logic is included in the `claim` dispatchable.
// </weight>
Call::claim(account, ethereum_signature) => {
let data = account.using_encoded(to_ascii_hex);
(Self::eth_recover(&ethereum_signature, &data, &[][..]), None)
}
// <weight>
// The weight of this logic is included in the `claim_attest` dispatchable.
// </weight>
Call::claim_attest(account, ethereum_signature, statement) => {
let data = account.using_encoded(to_ascii_hex);
(Self::eth_recover(&ethereum_signature, &data, &statement), Some(statement.as_slice()))
}
_ => return Err(InvalidTransaction::Call.into()),
};

let signer = maybe_signer
.ok_or(InvalidTransaction::Custom(ValidityError::InvalidEthereumSignature.into()))?;

let e = InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into());
ensure!(<Claims<T>>::contains_key(&signer), e);

let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
match Signing::<T>::get(signer) {
None => ensure!(maybe_statement.is_none(), e),
Some(s) => ensure!(Some(s.to_text()) == maybe_statement, e),
}

Ok(ValidTransaction {
priority: PRIORITY,
requires: vec![],
provides: vec![("claims", signer).encode()],
longevity: TransactionLongevity::max_value(),
propagate: true,
})
}
}

/// Validate `attest` calls prior to execution. Needed to avoid a DoS attack since they are
/// otherwise free to place on chain.
#[derive(Encode, Decode, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -649,7 +649,11 @@ mod tests {
use parity_scale_codec::Encode;
// The testing primitives are very useful for avoiding having to work with signatures
// or public keys. `u64` is used as the `AccountId` and no `Signature`s are required.
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup, Identity}, testing::Header};
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup, Identity},
transaction_validity::TransactionLongevity,
testing::Header,
};
use frame_support::{
assert_ok, assert_err, assert_noop, parameter_types,
ord_parameter_types, weights::{Pays, GetDispatchInfo}, traits::{ExistenceRequirement, GenesisBuild},
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/src/slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ mod tests {
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Slots: slots::{Pallet, Call, Storage, Event<T>},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Call, Storage},
RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage},
}
);

Expand Down
6 changes: 2 additions & 4 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,9 @@ impl InstanceFilter<Call> for ProxyType {
// Specifically omitting the entire Balances pallet
Call::Authorship(..) |
Call::Staking(..) |
Call::Offences(..) |
Call::Session(..) |
Call::Grandpa(..) |
Call::ImOnline(..) |
Call::AuthorityDiscovery(..) |
Call::Democracy(..) |
Call::Council(..) |
Call::TechnicalCommittee(..) |
Expand Down Expand Up @@ -1401,12 +1399,12 @@ construct_runtime! {
// Consensus support.
Authorship: pallet_authorship::{Pallet, Call, Storage} = 5,
Staking: pallet_staking::{Pallet, Call, Storage, Config<T>, Event<T>} = 6,
Offences: pallet_offences::{Pallet, Call, Storage, Event} = 7,
Offences: pallet_offences::{Pallet, Storage, Event} = 7,
Historical: session_historical::{Pallet} = 34,
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 8,
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 10,
ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 11,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Call, Config} = 12,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 12,

// Governance stuff; uncallable initially.
Democracy: pallet_democracy::{Pallet, Call, Storage, Config<T>, Event<T>} = 13,
Expand Down
9 changes: 3 additions & 6 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ impl Filter<Call> for BaseFilter {
Call::TechnicalMembership(_) | Call::Treasury(_) | Call::PhragmenElection(_) |
Call::System(_) | Call::Scheduler(_) | Call::Indices(_) |
Call::Babe(_) | Call::Timestamp(_) | Call::Balances(_) |
Call::Authorship(_) | Call::Staking(_) | Call::Offences(_) |
Call::Authorship(_) | Call::Staking(_) |
Call::Session(_) | Call::Grandpa(_) | Call::ImOnline(_) |
Call::AuthorityDiscovery(_) |
Call::Utility(_) | Call::Claims(_) | Call::Vesting(_) |
Call::Identity(_) | Call::Proxy(_) | Call::Multisig(_) |
Call::Bounties(_) | Call::Tips(_) | Call::ElectionProviderMultiPhase(_)
Expand Down Expand Up @@ -904,11 +903,9 @@ impl InstanceFilter<Call> for ProxyType {
// Specifically omitting the entire Balances pallet
Call::Authorship(..) |
Call::Staking(..) |
Call::Offences(..) |
Call::Session(..) |
Call::Grandpa(..) |
Call::ImOnline(..) |
Call::AuthorityDiscovery(..) |
Call::Democracy(..) |
Call::Council(..) |
Call::TechnicalCommittee(..) |
Expand Down Expand Up @@ -998,12 +995,12 @@ construct_runtime! {
// Consensus support.
Authorship: pallet_authorship::{Pallet, Call, Storage} = 6,
Staking: pallet_staking::{Pallet, Call, Storage, Config<T>, Event<T>} = 7,
Offences: pallet_offences::{Pallet, Call, Storage, Event} = 8,
Offences: pallet_offences::{Pallet, Storage, Event} = 8,
Historical: session_historical::{Pallet} = 33,
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 9,
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 11,
ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 12,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Call, Config} = 13,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 13,

// Governance stuff.
Democracy: pallet_democracy::{Pallet, Call, Storage, Config<T>, Event<T>} = 14,
Expand Down
4 changes: 2 additions & 2 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ construct_runtime! {

// Consensus support.
Authorship: pallet_authorship::{Pallet, Call, Storage},
Offences: pallet_offences::{Pallet, Call, Storage, Event},
Offences: pallet_offences::{Pallet, Storage, Event},
Historical: session_historical::{Pallet},
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned},
ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>},
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Call, Config},
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config},

// Parachains modules.
ParachainsOrigin: parachains_origin::{Pallet, Origin},
Expand Down
4 changes: 2 additions & 2 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ construct_runtime! {
// Consensus support.
Authorship: pallet_authorship::{Pallet, Call, Storage},
Staking: pallet_staking::{Pallet, Call, Storage, Config<T>, Event<T>},
Offences: pallet_offences::{Pallet, Call, Storage, Event},
Offences: pallet_offences::{Pallet, Storage, Event},
Historical: session_historical::{Pallet},
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>},
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event},
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Call, Config},
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config},

// Claims. Usable initially.
Claims: claims::{Pallet, Call, Storage, Event<T>, Config<T>, ValidateUnsigned},
Expand Down
6 changes: 2 additions & 4 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,9 @@ impl InstanceFilter<Call> for ProxyType {
// Specifically omitting the entire Balances pallet
Call::Authorship(..) |
Call::Staking(..) |
Call::Offences(..) |
Call::Session(..) |
Call::Grandpa(..) |
Call::ImOnline(..) |
Call::AuthorityDiscovery(..) |
Call::Utility(..) |
Call::Identity(..) |
Call::Recovery(pallet_recovery::Call::as_recovered(..)) |
Expand Down Expand Up @@ -1018,12 +1016,12 @@ construct_runtime! {
// Consensus support.
Authorship: pallet_authorship::{Pallet, Call, Storage} = 5,
Staking: pallet_staking::{Pallet, Call, Storage, Config<T>, Event<T>} = 6,
Offences: pallet_offences::{Pallet, Call, Storage, Event} = 7,
Offences: pallet_offences::{Pallet, Storage, Event} = 7,
Historical: session_historical::{Pallet} = 27,
Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 8,
Grandpa: pallet_grandpa::{Pallet, Call, Storage, Config, Event, ValidateUnsigned} = 10,
ImOnline: pallet_im_online::{Pallet, Call, Storage, Event<T>, ValidateUnsigned, Config<T>} = 11,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Call, Config} = 12,
AuthorityDiscovery: pallet_authority_discovery::{Pallet, Config} = 12,

// Utility module.
Utility: pallet_utility::{Pallet, Call, Event} = 16,
Expand Down