Skip to content

Commit

Permalink
[MGX-1443] Feature/foundation membership (#819)
Browse files Browse the repository at this point in the history
add membership pallet used to manage foundation members

only allowed method is for the member to change his key
  • Loading branch information
vanderian authored Oct 1, 2024
1 parent 9b4af73 commit 0b86689
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 44 deletions.
27 changes: 23 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pallet-balances = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "
pallet-collective-mangata = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-grandpa = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-identity = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-membership = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-proxy = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-session = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
pallet-sudo-mangata = { git = "https://github.com/gasp-xyz/polkadot-sdk", branch = "eth-rollup-develop", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions rollup/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ clap = { version = "4.4.2", features = ["derive", "env"] }
codec = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
hex-literal = { workspace = true }
jsonrpsee = { workspace = true, features = ["server"] }
log = { workspace = true }
serde = { workspace = true, features = ["derive"] }
Expand Down
15 changes: 15 additions & 0 deletions rollup/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,20 @@ fn rollup_genesis(
decode_url,
_phantom: Default::default(),
},
foundation_members: rollup_runtime::FoundationMembersConfig {
members: BoundedVec::truncate_from(
[
// TODO AccountId20
// Change the following
hex_literal::hex!["c8d02dfbff5ce2fda651c7dd7719bc5b17b9c104"],
hex_literal::hex!["c4690c56c36cec7ed5f6ed5d5eebace0c317073a"],
hex_literal::hex!["fc741134c82b81b7ab7efbf334b0c90ff8dbf22c"],
]
.iter()
.map(|acc| sp_runtime::AccountId20::from(*acc))
.collect::<Vec<_>>(),
),
phantom: Default::default(),
},
}
}
5 changes: 4 additions & 1 deletion rollup/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ substrate-wasm-builder = { workspace = true }
[dependencies]
codec = { workspace = true, default-features = false, features = ["derive"] }
scale-info = { workspace = true, default-features = false, features = ["derive"] }
hex-literal = { workspace = true }
log = { workspace = true, default-features = false }
array-bytes = { workspace = true }
smallvec = "1.6.1"
Expand Down Expand Up @@ -81,6 +80,7 @@ pallet-grandpa = { workspace = true, default-features = false }
pallet-authorship = { workspace = true, default-features = false }
pallet-collective-mangata = { workspace = true, default-features = false }
pallet-identity = { workspace = true, default-features = false }
pallet-membership = { workspace = true, default-features = false }
pallet-proxy = { workspace = true, default-features = false }
pallet-session = { workspace = true, default-features = false }
pallet-sudo-mangata = { workspace = true, default-features = false }
Expand Down Expand Up @@ -121,6 +121,7 @@ std = [
"pallet-identity/std",
"pallet-issuance/std",
"pallet-maintenance/std",
"pallet-membership/std",
"pallet-metamask-signature/std",
"pallet-multipurpose-liquidity/std",
"pallet-proof-of-stake/std",
Expand Down Expand Up @@ -180,6 +181,7 @@ try-runtime = [
"pallet-identity/try-runtime",
"pallet-issuance/try-runtime",
"pallet-maintenance/try-runtime",
"pallet-membership/try-runtime",
"pallet-multipurpose-liquidity/try-runtime",
"pallet-proof-of-stake/try-runtime",
"pallet-proxy/try-runtime",
Expand Down Expand Up @@ -217,6 +219,7 @@ runtime-benchmarks = [
"pallet-identity/runtime-benchmarks",
"pallet-issuance/runtime-benchmarks",
"pallet-maintenance/runtime-benchmarks",
"pallet-membership/runtime-benchmarks",
"pallet-multipurpose-liquidity/runtime-benchmarks",
"pallet-proof-of-stake/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
Expand Down
1 change: 1 addition & 0 deletions rollup/runtime/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mangata-support = { workspace = true, default-features = false }
mangata-types = { workspace = true }
pallet-balances = { workspace = true }
pallet-identity = { workspace = true }
pallet-membership = { workspace = true }
pallet-proxy = { workspace = true }
pallet-session = { workspace = true }
sp-io = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions rollup/runtime/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ mod proxy;
mod identity;

mod maintenance;

mod membership;
16 changes: 4 additions & 12 deletions rollup/runtime/integration-test/src/maintenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ fn system_set_code_works_with_maintenance_mode() {

assert_ok!(Maintenance::switch_maintenance_mode_on(
RuntimeOrigin::signed(
FoundationAccountsProvider::<Runtime>::get()
.pop()
.expect("There atleast 1 F acc")
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc")
)
.into()
));
Expand All @@ -30,9 +28,7 @@ fn system_set_code_works_with_maintenance_mode() {

assert_ok!(Maintenance::switch_upgradability_in_maintenance_mode_on(
RuntimeOrigin::signed(
FoundationAccountsProvider::<Runtime>::get()
.pop()
.expect("There atleast 1 F acc")
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc")
)
.into()
));
Expand All @@ -43,9 +39,7 @@ fn system_set_code_works_with_maintenance_mode() {

assert_ok!(Maintenance::switch_upgradability_in_maintenance_mode_off(
RuntimeOrigin::signed(
FoundationAccountsProvider::<Runtime>::get()
.pop()
.expect("There atleast 1 F acc")
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc")
)
.into()
));
Expand All @@ -56,9 +50,7 @@ fn system_set_code_works_with_maintenance_mode() {

assert_ok!(Maintenance::switch_maintenance_mode_off(
RuntimeOrigin::signed(
FoundationAccountsProvider::<Runtime>::get()
.pop()
.expect("There atleast 1 F acc")
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc")
)
.into()
));
Expand Down
76 changes: 76 additions & 0 deletions rollup/runtime/integration-test/src/membership.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::setup::*;
use frame_support::traits::Contains;
use sp_runtime::testing::H256;

fn test_env() -> TestExternalities {
ExtBuilder { ..ExtBuilder::default() }.build()
}

#[test]
fn change_key_works() {
let mut ext = test_env();
ext.execute_with(|| {
System::set_block_number(1);

let alice = AccountId::from(ALICE);
let bob = AccountId::from(BOB);

assert_err!(
FoundationMembers::change_key(RuntimeOrigin::signed(bob.clone()), alice),
pallet_membership::Error::<Runtime>::NotMember,
);

assert!(<FoundationMembers as Contains<_>>::contains(&alice));

assert_ok!(FoundationMembers::change_key(
RuntimeOrigin::signed(
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc")
)
.into(),
bob,
));

assert!(!<FoundationMembers as Contains<_>>::contains(&alice));
assert!(<FoundationMembers as Contains<_>>::contains(&bob));
});
}

#[test]
fn other_fn_doesnt_work_for_root() {
let mut ext = test_env();
ext.execute_with(|| {
System::set_block_number(1);

let alice = AccountId::from(ALICE);
let bob = AccountId::from(BOB);
let origin: RuntimeOrigin = frame_system::RawOrigin::Root.into();

assert_err!(FoundationMembers::add_member(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::remove_member(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::swap_member(origin.clone(), alice, bob), BadOrigin,);
assert_err!(FoundationMembers::reset_members(origin.clone(), vec![]), BadOrigin,);
assert_err!(FoundationMembers::set_prime(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::clear_prime(origin.clone()), BadOrigin,);
});
}

#[test]
fn other_fn_doesnt_work_for_foundation() {
let mut ext = test_env();
ext.execute_with(|| {
System::set_block_number(1);

let alice = AccountId::from(ALICE);
let bob = AccountId::from(BOB);
let origin: RuntimeOrigin = RuntimeOrigin::signed(
FoundationAccountsProvider::get().pop().expect("There atleast 1 F acc"),
);

assert_err!(FoundationMembers::add_member(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::remove_member(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::swap_member(origin.clone(), alice, bob), BadOrigin,);
assert_err!(FoundationMembers::reset_members(origin.clone(), vec![]), BadOrigin,);
assert_err!(FoundationMembers::set_prime(origin.clone(), bob), BadOrigin,);
assert_err!(FoundationMembers::clear_prime(origin.clone()), BadOrigin,);
});
}
16 changes: 12 additions & 4 deletions rollup/runtime/integration-test/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ mod rollup_imports {
pub use rollup_runtime::{
consts::UNIT,
runtime_config::config::{
orml_asset_registry::AssetMetadataOf, pallet_maintenance::FoundationAccountsProvider,
orml_asset_registry::AssetMetadataOf,
pallet_membership::{FoundationAccountsProvider, MaxMembers},
pallet_proxy::ProxyType,
},
AccountId, AssetRegistry, Balance, Bootstrap, CustomMetadata, Identity, Maintenance,
ProofOfStake, Proxy, Rolldown, Runtime, RuntimeCall, RuntimeOrigin, System, TokenId,
Tokens, UncheckedExtrinsic, Xyk, XykMetadata,
AccountId, AssetRegistry, Balance, Bootstrap, CustomMetadata, FoundationMembers, Identity,
Maintenance, ProofOfStake, Proxy, Rolldown, Runtime, RuntimeCall, RuntimeOrigin, System,
TokenId, Tokens, UncheckedExtrinsic, Xyk, XykMetadata,
};

pub const NATIVE_ASSET_ID: TokenId = rollup_runtime::runtime_config::tokens::RX_TOKEN_ID;
Expand Down Expand Up @@ -105,6 +106,13 @@ impl ExtBuilder {
.assimilate_storage(&mut t)
.unwrap();

pallet_membership::GenesisConfig::<Runtime> {
members: BoundedVec::truncate_from([AccountId::from(ALICE)].to_vec()),
..Default::default()
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
Expand Down
25 changes: 21 additions & 4 deletions rollup/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ pub use frame_support::{
},
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Contains, EitherOfDiverse, EnsureOrigin,
EnsureOriginWithArg, Everything, ExistenceRequirement, FindAuthor, Get, Imbalance,
InstanceFilter, KeyOwnerProofSystem, Randomness, StorageInfo, WithdrawReasons,
InstanceFilter, KeyOwnerProofSystem, NeverEnsureOrigin, Randomness, StorageInfo,
WithdrawReasons,
},
unsigned::TransactionValidityError,
weights::{
Expand Down Expand Up @@ -266,7 +267,7 @@ impl pallet_treasury::Config for Runtime {
type Paymaster = PayFromAccount<Self::Currency, TreasuryAccount>;
type BalanceConverter = UnityAssetBalanceConversion;
#[cfg(not(feature = "runtime-benchmarks"))]
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u128>;
type SpendOrigin = NeverEnsureOrigin<u128>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
#[cfg(feature = "runtime-benchmarks")]
Expand Down Expand Up @@ -551,7 +552,7 @@ impl pallet_collective_mangata::Config<CouncilCollective> for Runtime {
type ProposalCloseDelay = cfg::pallet_collective_mangata::CouncilProposalCloseDelay;
type MaxProposals = cfg::pallet_collective_mangata::CouncilMaxProposals;
type MaxMembers = cfg::pallet_collective_mangata::CouncilMaxMembers;
type FoundationAccountsProvider = cfg::pallet_maintenance::FoundationAccountsProvider<Runtime>;
type FoundationAccountsProvider = cfg::pallet_membership::FoundationAccountsProvider;
type DefaultVote = pallet_collective_mangata::PrimeDefaultVote;
type WeightInfo = weights::pallet_collective_mangata_weights::ModuleWeight<Runtime>;
type SetMembersOrigin = cfg::pallet_collective_mangata::SetMembersOrigin<Self::AccountId>;
Expand Down Expand Up @@ -756,9 +757,24 @@ impl pallet_identity::Config for Runtime {
type MaxUsernameLength = cfg::pallet_identity::MaxUsernameLength;
}

/// membership pallets is used to maintain Foundation accounts
/// only a `change_key` of existing -> new member account id is allowed
impl pallet_membership::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AddOrigin = NeverEnsureOrigin<()>;
type RemoveOrigin = NeverEnsureOrigin<()>;
type SwapOrigin = NeverEnsureOrigin<()>;
type ResetOrigin = NeverEnsureOrigin<()>;
type PrimeOrigin = NeverEnsureOrigin<()>;
type MembershipInitialized = ();
type MembershipChanged = ();
type MaxMembers = cfg::pallet_membership::MaxMembers;
type WeightInfo = pallet_membership::weights::SubstrateWeight<Runtime>;
}

impl pallet_maintenance::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type FoundationAccountsProvider = cfg::pallet_maintenance::FoundationAccountsProvider<Runtime>;
type FoundationAccountsProvider = cfg::pallet_membership::FoundationAccountsProvider;
}

impl pallet_rolldown::Config for Runtime {
Expand Down Expand Up @@ -859,6 +875,7 @@ construct_runtime!(
SudoOrigin: pallet_sudo_origin = 62,
Council: pallet_collective_mangata::<Instance1> = 63,
Identity: pallet_identity = 64,
FoundationMembers: pallet_membership = 65,
}
);

Expand Down
Loading

0 comments on commit 0b86689

Please sign in to comment.