Associated type Hasher for QueryPreimage, StorePreimage and Bounded (#1720)

I hope it's enough to fix #1701 
the only solution I found to make it happen is to put an associated type
to the `Bounded` enum as well.
@liamaharon @kianenigma @bkchr 

Polkadot address: 12poSUQPtcF1HUPQGY3zZu2P8emuW9YnsPduA4XG3oCEfJVp

---------

Signed-off-by: muraca <mmuraca247@gmail.com>
Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Matteo Muraca
2023-09-27 14:46:14 +02:00
committed by GitHub
parent 7cbe0c76ef
commit 8b061a5c5d
20 changed files with 189 additions and 159 deletions
@@ -25,7 +25,6 @@ use frame_support::{
traits::{Currency, EnsureOrigin, Get, OnInitialize, UnfilteredDispatchable},
};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use sp_core::H256;
use sp_runtime::{traits::Bounded, BoundedVec};
use crate::Pallet as Democracy;
@@ -46,7 +45,7 @@ fn make_proposal<T: Config>(n: u32) -> BoundedCallOf<T> {
<T as Config>::Preimages::bound(call).unwrap()
}
fn add_proposal<T: Config>(n: u32) -> Result<H256, &'static str> {
fn add_proposal<T: Config>(n: u32) -> Result<T::Hash, &'static str> {
let other = funded_account::<T>("proposer", n);
let value = T::MinimumDeposit::get();
let proposal = make_proposal::<T>(n);
@@ -55,7 +54,7 @@ fn add_proposal<T: Config>(n: u32) -> Result<H256, &'static str> {
}
// add a referendum with a metadata.
fn add_referendum<T: Config>(n: u32) -> (ReferendumIndex, H256, PreimageHash) {
fn add_referendum<T: Config>(n: u32) -> (ReferendumIndex, T::Hash, T::Hash) {
let vote_threshold = VoteThreshold::SimpleMajority;
let proposal = make_proposal::<T>(n);
let hash = proposal.hash();
@@ -85,7 +84,7 @@ fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
}
// note a new preimage.
fn note_preimage<T: Config>() -> PreimageHash {
fn note_preimage<T: Config>() -> T::Hash {
use core::sync::atomic::{AtomicU8, Ordering};
use sp_std::borrow::Cow;
// note a new preimage on every function invoke.
+29 -25
View File
@@ -159,9 +159,8 @@ use frame_support::{
traits::{
defensive_prelude::*,
schedule::{v3::Named as ScheduleNamed, DispatchTime},
Bounded, Currency, EnsureOrigin, Get, Hash as PreimageHash, LockIdentifier,
LockableCurrency, OnUnbalanced, QueryPreimage, ReservableCurrency, StorePreimage,
WithdrawReasons,
Bounded, Currency, EnsureOrigin, Get, LockIdentifier, LockableCurrency, OnUnbalanced,
QueryPreimage, ReservableCurrency, StorePreimage, WithdrawReasons,
},
weights::Weight,
};
@@ -203,7 +202,7 @@ type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
pub type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
pub type BoundedCallOf<T> = Bounded<CallOf<T>>;
pub type BoundedCallOf<T> = Bounded<CallOf<T>, <T as frame_system::Config>::Hashing>;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
#[frame_support::pallet]
@@ -211,7 +210,6 @@ pub mod pallet {
use super::{DispatchResult, *};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_core::H256;
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
@@ -226,10 +224,15 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The Scheduler.
type Scheduler: ScheduleNamed<BlockNumberFor<Self>, CallOf<Self>, Self::PalletsOrigin>;
type Scheduler: ScheduleNamed<
BlockNumberFor<Self>,
CallOf<Self>,
Self::PalletsOrigin,
Hasher = Self::Hashing,
>;
/// The Preimage provider.
type Preimages: QueryPreimage + StorePreimage;
type Preimages: QueryPreimage<H = Self::Hashing> + StorePreimage;
/// Currency type for this pallet.
type Currency: ReservableCurrency<Self::AccountId>
@@ -421,22 +424,22 @@ pub mod pallet {
pub type Blacklist<T: Config> = StorageMap<
_,
Identity,
H256,
T::Hash,
(BlockNumberFor<T>, BoundedVec<T::AccountId, T::MaxBlacklisted>),
>;
/// Record of all proposals that have been subject to emergency cancellation.
#[pallet::storage]
pub type Cancellations<T: Config> = StorageMap<_, Identity, H256, bool, ValueQuery>;
pub type Cancellations<T: Config> = StorageMap<_, Identity, T::Hash, bool, ValueQuery>;
/// General information concerning any proposal or referendum.
/// The `PreimageHash` refers to the preimage of the `Preimages` provider which can be a JSON
/// The `Hash` refers to the preimage of the `Preimages` provider which can be a JSON
/// dump or IPFS hash of a JSON file.
///
/// Consider a garbage collection for a metadata of finished referendums to `unrequest` (remove)
/// large preimages.
#[pallet::storage]
pub type MetadataOf<T: Config> = StorageMap<_, Blake2_128Concat, MetadataOwner, PreimageHash>;
pub type MetadataOf<T: Config> = StorageMap<_, Blake2_128Concat, MetadataOwner, T::Hash>;
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
@@ -476,9 +479,9 @@ pub mod pallet {
/// An account has cancelled a previous delegation operation.
Undelegated { account: T::AccountId },
/// An external proposal has been vetoed.
Vetoed { who: T::AccountId, proposal_hash: H256, until: BlockNumberFor<T> },
Vetoed { who: T::AccountId, proposal_hash: T::Hash, until: BlockNumberFor<T> },
/// A proposal_hash has been blacklisted permanently.
Blacklisted { proposal_hash: H256 },
Blacklisted { proposal_hash: T::Hash },
/// An account has voted in a referendum
Voted { voter: T::AccountId, ref_index: ReferendumIndex, vote: AccountVote<BalanceOf<T>> },
/// An account has secconded a proposal
@@ -490,14 +493,14 @@ pub mod pallet {
/// Metadata owner.
owner: MetadataOwner,
/// Preimage hash.
hash: PreimageHash,
hash: T::Hash,
},
/// Metadata for a proposal or a referendum has been cleared.
MetadataCleared {
/// Metadata owner.
owner: MetadataOwner,
/// Preimage hash.
hash: PreimageHash,
hash: T::Hash,
},
/// Metadata has been transferred to new owner.
MetadataTransferred {
@@ -506,7 +509,7 @@ pub mod pallet {
/// New metadata owner.
owner: MetadataOwner,
/// Preimage hash.
hash: PreimageHash,
hash: T::Hash,
},
}
@@ -775,7 +778,7 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::fast_track())]
pub fn fast_track(
origin: OriginFor<T>,
proposal_hash: H256,
proposal_hash: T::Hash,
voting_period: BlockNumberFor<T>,
delay: BlockNumberFor<T>,
) -> DispatchResult {
@@ -827,7 +830,7 @@ pub mod pallet {
/// Weight: `O(V + log(V))` where V is number of `existing vetoers`
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::veto_external())]
pub fn veto_external(origin: OriginFor<T>, proposal_hash: H256) -> DispatchResult {
pub fn veto_external(origin: OriginFor<T>, proposal_hash: T::Hash) -> DispatchResult {
let who = T::VetoOrigin::ensure_origin(origin)?;
if let Some((ext_proposal, _)) = NextExternal::<T>::get() {
@@ -1042,7 +1045,7 @@ pub mod pallet {
#[pallet::weight((T::WeightInfo::blacklist(), DispatchClass::Operational))]
pub fn blacklist(
origin: OriginFor<T>,
proposal_hash: H256,
proposal_hash: T::Hash,
maybe_ref_index: Option<ReferendumIndex>,
) -> DispatchResult {
T::BlacklistOrigin::ensure_origin(origin)?;
@@ -1139,7 +1142,7 @@ pub mod pallet {
pub fn set_metadata(
origin: OriginFor<T>,
owner: MetadataOwner,
maybe_hash: Option<PreimageHash>,
maybe_hash: Option<T::Hash>,
) -> DispatchResult {
match owner {
MetadataOwner::External => {
@@ -1173,15 +1176,16 @@ pub mod pallet {
}
pub trait EncodeInto: Encode {
fn encode_into<T: AsMut<[u8]> + Default>(&self) -> T {
fn encode_into<T: AsMut<[u8]> + Default, H: sp_core::Hasher>(&self) -> T {
let mut t = T::default();
self.using_encoded(|data| {
if data.len() <= t.as_mut().len() {
t.as_mut()[0..data.len()].copy_from_slice(data);
} else {
// encoded self is too big to fit into a T. hash it and use the first bytes of that
// instead.
let hash = sp_io::hashing::blake2_256(data);
// encoded self is too big to fit into a T.
// hash it and use the first bytes of that instead.
let hash = H::hash(&data);
let hash = hash.as_ref();
let l = t.as_mut().len().min(hash.len());
t.as_mut()[0..l].copy_from_slice(&hash[0..l]);
}
@@ -1610,7 +1614,7 @@ impl<T: Config> Pallet<T> {
// Earliest it can be scheduled for is next block.
let when = now.saturating_add(status.delay.max(One::one()));
if T::Scheduler::schedule_named(
(DEMOCRACY_ID, index).encode_into(),
(DEMOCRACY_ID, index).encode_into::<_, T::Hashing>(),
DispatchTime::At(when),
None,
63,
+1 -1
View File
@@ -277,7 +277,7 @@ fn tally(r: ReferendumIndex) -> Tally<u64> {
}
/// note a new preimage without registering.
fn note_preimage(who: u64) -> PreimageHash {
fn note_preimage(who: u64) -> <Test as frame_system::Config>::Hash {
use std::sync::atomic::{AtomicU8, Ordering};
// note a new preimage on every function invoke.
static COUNTER: AtomicU8 = AtomicU8::new(0);
@@ -22,9 +22,8 @@ use super::*;
#[test]
fn set_external_metadata_works() {
new_test_ext().execute_with(|| {
use frame_support::traits::Hash as PreimageHash;
// invalid preimage hash.
let invalid_hash: PreimageHash = [1u8; 32].into();
let invalid_hash: <Test as frame_system::Config>::Hash = [1u8; 32].into();
// metadata owner is an external proposal.
let owner = MetadataOwner::External;
// fails to set metadata if an external proposal does not exist.
@@ -83,9 +82,8 @@ fn clear_metadata_works() {
#[test]
fn set_proposal_metadata_works() {
new_test_ext().execute_with(|| {
use frame_support::traits::Hash as PreimageHash;
// invalid preimage hash.
let invalid_hash: PreimageHash = [1u8; 32].into();
let invalid_hash: <Test as frame_system::Config>::Hash = [1u8; 32].into();
// create an external proposal.
assert_ok!(propose_set_balance(1, 2, 5));
// metadata owner is a public proposal.