Skip to content

Commit

Permalink
store proposals using preimages
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <[email protected]>
  • Loading branch information
muraca committed Sep 18, 2023
1 parent 2eb441e commit 7acdbe5
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 604 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use crate::OriginCaller;
use frame_support::{
dispatch::DispatchResultWithPostInfo,
traits::{Currency, Get, Imbalance, OnUnbalanced, OriginTrait, PrivilegeCmp},
traits::{
Currency, Get, Hash as PreimageHash, Imbalance, OnUnbalanced, OriginTrait, PrivilegeCmp,
},
weights::Weight,
};
use log;
Expand All @@ -30,8 +32,6 @@ type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

type ProposalOf<T, I> = <T as pallet_collective::Config<I>>::Proposal;

type HashOf<T> = <T as frame_system::Config>::Hash;

/// Type alias to conveniently refer to the `Currency::Balance` associated type.
pub type BalanceOf<T> =
<pallet_balances::Pallet<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -85,7 +85,7 @@ where
/// Adapter from collective pallet to alliance proposal provider trait.
pub struct AllianceProposalProvider<T, I = ()>(PhantomData<(T, I)>);

impl<T, I> ProposalProvider<AccountIdOf<T>, HashOf<T>, ProposalOf<T, I>>
impl<T, I> ProposalProvider<AccountIdOf<T>, PreimageHash, ProposalOf<T, I>>
for AllianceProposalProvider<T, I>
where
T: pallet_collective::Config<I> + frame_system::Config,
Expand All @@ -107,15 +107,15 @@ where

fn vote_proposal(
who: AccountIdOf<T>,
proposal: HashOf<T>,
proposal: PreimageHash,
index: ProposalIndex,
approve: bool,
) -> Result<bool, DispatchError> {
pallet_collective::Pallet::<T, I>::do_vote(who, proposal, index, approve)
}

fn close_proposal(
proposal_hash: HashOf<T>,
proposal_hash: PreimageHash,
proposal_index: ProposalIndex,
proposal_weight_bound: Weight,
length_bound: u32,
Expand All @@ -128,8 +128,8 @@ where
)
}

fn proposal_of(proposal_hash: HashOf<T>) -> Option<ProposalOf<T, I>> {
pallet_collective::Pallet::<T, I>::proposal_of(proposal_hash)
fn proposal_of(proposal_hash: PreimageHash) -> Option<ProposalOf<T, I>> {
pallet_collective::Pallet::<T, I>::proposal_of(&proposal_hash)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {
type SetMembersOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::pallet_collective::WeightInfo<Runtime>;
type MaxProposalWeight = MaxProposalWeight;
type Preimages = Preimage;
}

pub const MAX_FELLOWS: u32 = ALLIANCE_MAX_MEMBERS;
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {
type SetMembersOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::pallet_collective_council::WeightInfo<Runtime>;
type MaxProposalWeight = MaxProposalWeight;
type Preimages = Preimage;
}

parameter_types! {
Expand Down Expand Up @@ -520,6 +521,7 @@ impl pallet_collective::Config<TechnicalCollective> for Runtime {
type SetMembersOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::pallet_collective_technical_committee::WeightInfo<Runtime>;
type MaxProposalWeight = MaxProposalWeight;
type Preimages = Preimage;
}

type MoreThanHalfCouncil = EitherOfDiverse<
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl ProposalProvider<AccountId, Hash, RuntimeCall> for AllianceProposalProvider
}

fn proposal_of(proposal_hash: Hash) -> Option<RuntimeCall> {
AllianceMotion::proposal_of(proposal_hash)
AllianceMotion::proposal_of(&proposal_hash)
}
}

Expand Down
3 changes: 3 additions & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
type Preimages = Preimage;
}

parameter_types! {
Expand Down Expand Up @@ -1150,6 +1151,7 @@ impl pallet_collective::Config<TechnicalCollective> for Runtime {
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
type Preimages = Preimage;
}

type EnsureRootOrHalfCouncil = EitherOfDiverse<
Expand Down Expand Up @@ -1874,6 +1876,7 @@ impl pallet_collective::Config<AllianceCollective> for Runtime {
type WeightInfo = pallet_collective::weights::SubstrateWeight<Runtime>;
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxCollectivesProposalWeight;
type Preimages = Preimage;
}

parameter_types! {
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/alliance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ array-bytes = "6.1"
sp-core-hashing = { path = "../../primitives/core/hashing", default-features = false}
pallet-balances = { path = "../balances" }
pallet-collective = { path = "../collective" }
pallet-preimage = { path = "../preimage" }

[features]
default = [ "std" ]
Expand All @@ -49,6 +50,7 @@ std = [
"pallet-balances/std",
"pallet-collective?/std",
"pallet-identity/std",
"pallet-preimage/std",
"scale-info/std",
"sp-core-hashing?/std",
"sp-core/std",
Expand All @@ -64,6 +66,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"sp-core-hashing",
"sp-runtime/runtime-benchmarks",
]
Expand All @@ -73,5 +76,6 @@ try-runtime = [
"pallet-balances/try-runtime",
"pallet-collective?/try-runtime",
"pallet-identity/try-runtime",
"pallet-preimage/try-runtime",
"sp-runtime/try-runtime",
]
12 changes: 11 additions & 1 deletion substrate/frame/alliance/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ impl pallet_collective::Config<AllianceCollective> for Test {
type WeightInfo = ();
type SetMembersOrigin = EnsureRoot<Self::AccountId>;
type MaxProposalWeight = MaxProposalWeight;
type Preimages = Preimage;
}

impl pallet_preimage::Config for Test {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type Currency = ();
type ManagerOrigin = EnsureRoot<Self::AccountId>;
type Consideration = ();
}

parameter_types! {
Expand Down Expand Up @@ -199,7 +208,7 @@ impl ProposalProvider<AccountId, H256, RuntimeCall> for AllianceProposalProvider
}

fn proposal_of(proposal_hash: H256) -> Option<RuntimeCall> {
AllianceMotion::proposal_of(proposal_hash)
AllianceMotion::proposal_of(&proposal_hash)
}
}

Expand Down Expand Up @@ -246,6 +255,7 @@ frame_support::construct_runtime!(
Identity: pallet_identity,
AllianceMotion: pallet_collective::<Instance1>,
Alliance: pallet_alliance,
Preimage: pallet_preimage,
}
);

Expand Down
141 changes: 63 additions & 78 deletions substrate/frame/alliance/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
//! Tests for the alliance pallet.
use frame_support::{assert_noop, assert_ok, error::BadOrigin};
use frame_system::{EventRecord, Phase};

use super::*;
use crate::mock::*;
Expand Down Expand Up @@ -186,19 +185,14 @@ fn propose_works() {
));
assert_eq!(*AllianceMotion::proposals(), vec![hash]);
assert_eq!(AllianceMotion::proposal_of(&hash), Some(proposal));
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3,
}),
topics: vec![],
}]
);
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(
AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3,
},
));
});
}

Expand All @@ -214,25 +208,21 @@ fn vote_works() {
));
assert_ok!(Alliance::vote(RuntimeOrigin::signed(2), hash, 0, true));

let record = |event| EventRecord { phase: Phase::Initialization, event, topics: vec![] };
assert_eq!(
System::events(),
vec![
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 2,
proposal_hash: hash,
voted: true,
yes: 1,
no: 0,
})),
]
);
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(
AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3,
},
));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 2,
proposal_hash: hash,
voted: true,
yes: 1,
no: 0,
}));
});
}

Expand All @@ -258,51 +248,46 @@ fn close_works() {
proposal_len
));

let record = |event| EventRecord { phase: Phase::Initialization, event, topics: vec![] };
assert_eq!(
System::events(),
vec![
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 1,
proposal_hash: hash,
voted: true,
yes: 1,
no: 0,
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 2,
proposal_hash: hash,
voted: true,
yes: 2,
no: 0,
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 3,
proposal_hash: hash,
voted: true,
yes: 3,
no: 0,
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Closed {
proposal_hash: hash,
yes: 3,
no: 0,
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Approved {
proposal_hash: hash
})),
record(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Executed {
proposal_hash: hash,
result: Ok(()),
}))
]
);
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(
AllianceMotionEvent::Proposed {
account: 1,
proposal_index: 0,
proposal_hash: hash,
threshold: 3,
},
));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 1,
proposal_hash: hash,
voted: true,
yes: 1,
no: 0,
}));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 2,
proposal_hash: hash,
voted: true,
yes: 2,
no: 0,
}));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Voted {
account: 3,
proposal_hash: hash,
voted: true,
yes: 3,
no: 0,
}));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(AllianceMotionEvent::Closed {
proposal_hash: hash,
yes: 3,
no: 0,
}));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(
AllianceMotionEvent::Approved { proposal_hash: hash },
));
System::assert_has_event(mock::RuntimeEvent::AllianceMotion(
AllianceMotionEvent::Executed { proposal_hash: hash, result: Ok(()) },
));
});
}

Expand Down
Loading

0 comments on commit 7acdbe5

Please sign in to comment.