Skip to content

Commit

Permalink
add collective pallet. (#105)
Browse files Browse the repository at this point in the history
* add collective pallet.

* finished setting members for council.

* store council members to subtensor concilmember storgaemap.

* add storagemap to store concil members inside subtensor pallet.

* add a council call.

* add council_set_max_registrations_per_block for council members.

* Begin switching to pallet_membership for council mgnmt

* Add test random collective

* Add pallet-membership to Cargo.toml

* Add pallet-membership config

* EnsureRootOrHalfCouncil -> EnsureRootOrMajorityCouncil

* Add pallet-membership to chainspec.rs

* Remove --execution native flag from localnet

* Enforce specific toolchain for project

* Allow localnet to discover local validators instead of bootnode

* Re-add pallet-collective from git over fs

* Properly insert properties map into chainspec

* Add SudoRuntimeCall, CouncilOrigin to subtensor::Config

* Add sudo extrinsic to subtensor

* Add Sudid event to subtensor

* Fix testing for subtensor by removing collective, membership from runtime

* Create EnsureCouncilMajority origin in runtime

* Remove pallet-sudo from runtime, remove pallet-collective from dev-deps

* CouncilMaxMembers -> 3, CouncilMaxProposals -> 10

* Remove sudo pallet from construct_runtime!

* Remove sudo pallet config from runtime

* Remove vestiges of sudo from chain_spec

* Add localnet chain-spec for testing

* Fix tabulation in subtensor::sudo comment

* Re-add sudo to runtime

* Ensure root for membership mngmt

* Return DispatchResult of call from sudo

* Add sudoUncheckedWeight extrinsic to subtensor

* CouncilMotionDuration -> 1 DAY

* Move collective pallet into repository

* Add SenateCollective, SenateMembers to manage senate votes

* 🤖 🧈 Reformat source code

* Add VoteOrigin, ProposalOrigin to collective pallet

* Add more test accounts to localnet_genesis

* Fix removed finney testnet sudo key

* Add CanPropose, CanVote, GetVotingMembers traits

* Add CanPropose, CanVote implementations to runtime for collective

* Remove threshold argument from collective::propose

* Add max members for senate

* Remove threshold argument from collective::benchmarks::propose

* Add GetVotingMembers implementation for Collective

* Remove unused pallet-collective in subtensor

* Remove pallet_collective imports from subtensor

* Add MemberManagement trait to subtensor, implementation in runtime

* Add swap_member, members to MemberManagement trait

* Implement new MemberManagement interface methods

* Add senate module, do_join_senate, do_leave_senate extrinsics to subtensor

* Add max_members to MemberManagement, fix vec includes for interface

* Add subtensor::SenateRequiredStakePercentage storage

* Add benchmarks for join_senate, leave_senate

* Fix all broken benchmarks

* Reverse sort stake comparison in join_senate for descending order

* Fix all benchmarks properly this time

* Speed up benchmarking process for subtensor

* Use a full senate for senate benchmarking

* Update weights and DB reads for join_senate, leave_senate

* Remove senate member if required stake drops below threshold

* Update remove_stake benchmark to include senate removal threshold

* Update remove_stake weight

* Fix division by zero case in join_senate

* Remove cast votes of senate member when leaving

* CouncilMotionDuration 7200 blocks -> 100 blocks

* Use coldkey as signing origin for senate actions

Add add_vote to TriumvirateInterface
Update benchmarks for new signing scheme
Fix some tabulation differences in benchmarks.rs

* Update Cargo.lock for subtensor

* Add collective, membership pallets to subtensor Cargo.toml

* Update collective testing to support modifications

* Add triumvirate+senate to subtensor testing

* Up spec version 121 -> 122

* Handle result in CollectiveInterface::remove_votes

* Begin implementing subtensor-specific senate testing

* Add verbose errors for senate actions

* Fix staking removal tests broken by weight updates

* Add pallet-collective and pallet-membership to subtensor std for testing

* Fix division by zero in remove_stake for senate removal

* Add has_voted helper to pallet-collective

* Add complete test suite for senate activities in subtensor

* Fix errors from resolved merge conflicts

* Update Cargo.toml authors and remove collective/migrations

* Remove migrations testing in pallet-collective

---------

Co-authored-by: Rubberbandits <dalegribble@riseup.net>
  • Loading branch information
SaMotlagh and Rubberbandits committed Jun 28, 2023
1 parent 4412494 commit 54f3350
Show file tree
Hide file tree
Showing 19 changed files with 5,316 additions and 148 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

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

216 changes: 136 additions & 80 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use node_subtensor_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, Signature, SudoConfig,
SystemConfig, WASM_BINARY, SubtensorModuleConfig
SystemConfig, WASM_BINARY, SubtensorModuleConfig, TriumvirateConfig, TriumvirateMembersConfig,
SenateConfig, SenateMembersConfig
};
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{sr25519, Pair, Public};
use sp_core::{sr25519, Pair, Public, bounded_vec};
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{IdentifyAccount, Verify};
use sp_core::crypto::Ss58Codec;
Expand Down Expand Up @@ -286,84 +287,6 @@ pub fn finney_testnet_config() -> Result<ChainSpec, String> {
))
}

// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
_endowed_accounts: Vec<AccountId>,
_enable_println: bool,
_stakes: Vec<(AccountId, Vec<(AccountId, (u64, u16))>)>,
_balances: Vec<(AccountId, u64)>,
_balances_issuance: u64
) -> GenesisConfig {
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
},
balances: BalancesConfig {
// Configure sudo balance
balances: vec![
(Ss58Codec::from_ss58check("5GpzQgpiAKHMWNSH3RN4GLf96GVTDct9QxYEFAY7LWcVzTbx").unwrap(),1000000000000)
]
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
},
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
transaction_payment: Default::default(),
subtensor_module: Default::default(),
}
}


// Configure initial storage state for FRAME modules.
fn finney_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
_endowed_accounts: Vec<AccountId>,
_enable_println: bool,
stakes: Vec<(AccountId, Vec<(AccountId, (u64, u16))>)>,
balances: Vec<(AccountId, u64)>,
balances_issuance: u64

) -> GenesisConfig {
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
},
balances: BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
//balances: balances.iter().cloned().map(|k| k).collect(),
balances: balances.iter().cloned().map(|k| k).collect(),
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
},
sudo: SudoConfig {
// Assign network admin rights.
key: Some(root_key),
},
transaction_payment: Default::default(),
subtensor_module: SubtensorModuleConfig {
stakes: stakes,
balances_issuance: balances_issuance
},
}
}

pub fn localnet_config() -> Result<ChainSpec, String> {
let path: PathBuf = std::path::PathBuf::from("./snapshot.json");
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Expand Down Expand Up @@ -483,5 +406,138 @@ fn localnet_genesis(
},
transaction_payment: Default::default(),
subtensor_module: Default::default(),
triumvirate: TriumvirateConfig {
members: Default::default(),
phantom: Default::default(),
},
triumvirate_members: TriumvirateMembersConfig {
members: bounded_vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
get_account_id_from_seed::<sr25519::Public>("Charlie"),
],
phantom: Default::default()
},
senate: SenateConfig {
members: Default::default(),
phantom: Default::default(),
},
senate_members: SenateMembersConfig {
members: bounded_vec![
get_account_id_from_seed::<sr25519::Public>("Dave"),
get_account_id_from_seed::<sr25519::Public>("Eve"),
get_account_id_from_seed::<sr25519::Public>("Ferdie"),
],
phantom: Default::default()
}
}
}


// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
_endowed_accounts: Vec<AccountId>,
_enable_println: bool,
_stakes: Vec<(AccountId, Vec<(AccountId, (u64, u16))>)>,
_balances: Vec<(AccountId, u64)>,
_balances_issuance: u64
) -> GenesisConfig {
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
},
balances: BalancesConfig {
// Configure sudo balance
balances: vec![
(Ss58Codec::from_ss58check("5GpzQgpiAKHMWNSH3RN4GLf96GVTDct9QxYEFAY7LWcVzTbx").unwrap(),1000000000000)
]
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
},
sudo: SudoConfig {
key: Some(Ss58Codec::from_ss58check("5GpzQgpiAKHMWNSH3RN4GLf96GVTDct9QxYEFAY7LWcVzTbx").unwrap()),
},
transaction_payment: Default::default(),
subtensor_module: Default::default(),
triumvirate: TriumvirateConfig { // Add initial authorities as collective members
members: Default::default(),//initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
phantom: Default::default(),
},
triumvirate_members: TriumvirateMembersConfig {
members: Default::default(),
phantom: Default::default()
},
senate: SenateConfig {
members: Default::default(),
phantom: Default::default(),
},
senate_members: SenateMembersConfig {
members: Default::default(),
phantom: Default::default()
}
}
}


// Configure initial storage state for FRAME modules.
fn finney_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
_endowed_accounts: Vec<AccountId>,
_enable_println: bool,
stakes: Vec<(AccountId, Vec<(AccountId, (u64, u16))>)>,
balances: Vec<(AccountId, u64)>,
balances_issuance: u64

) -> GenesisConfig {
GenesisConfig {
system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
},
balances: BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
//balances: balances.iter().cloned().map(|k| k).collect(),
balances: balances.iter().cloned().map(|k| k).collect(),
},
aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
},
grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
},
sudo: SudoConfig {
key: Some(Ss58Codec::from_ss58check("5FCM3DBXWiGcwYYQtT8z4ZD93TqYpYxjaAfgv6aMStV1FTCT").unwrap()),
},
transaction_payment: Default::default(),
subtensor_module: SubtensorModuleConfig {
stakes: stakes,
balances_issuance: balances_issuance
},
triumvirate: TriumvirateConfig { // Add initial authorities as collective members
members: Default::default(),//initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
phantom: Default::default(),
},
triumvirate_members: TriumvirateMembersConfig {
members: Default::default(),
phantom: Default::default()
},
senate: SenateConfig {
members: Default::default(),
phantom: Default::default(),
},
senate_members: SenateMembersConfig {
members: Default::default(),
phantom: Default::default()
}
}
}
47 changes: 47 additions & 0 deletions pallets/collective/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "pallet-collective"
version = "4.0.0-dev"
authors = ["Parity Technologies <admin@parity.io>, Opentensor Technologies"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://bittensor.com"
repository = "https://github.com/opentensor/subtensor"
description = "Collective system: Members of a set of account IDs can make their collective feelings known through dispatched calls from one of two specialized origins."
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.39" }
sp-io = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
sp-runtime = { version = "7.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.39" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.39" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
25 changes: 25 additions & 0 deletions pallets/collective/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Collective system: Members of a set of account IDs can make their collective feelings known
through dispatched calls from one of two specialized origins.

The membership can be provided in one of two ways: either directly, using the Root-dispatchable
function `set_members`, or indirectly, through implementing the `ChangeMembers`.
The pallet assumes that the amount of members stays at or below `MaxMembers` for its weight
calculations, but enforces this neither in `set_members` nor in `change_members_sorted`.

A "prime" member may be set to help determine the default vote behavior based on chain
config. If `PrimeDefaultVote` is used, the prime vote acts as the default vote in case of any
abstentions after the voting period. If `MoreThanMajorityThenPrimeDefaultVote` is used, then
abstentations will first follow the majority of the collective voting, and then the prime
member.

Voting happens through motions comprising a proposal (i.e. a dispatchable) plus a
number of approvals required for it to pass and be called. Motions are open for members to
vote on for a minimum period given by `MotionDuration`. As soon as the required number of
approvals is given, the motion is closed and executed. If the number of approvals is not reached
during the voting period, then `close` may be called by any account in order to force the end
the motion explicitly. If a prime member is defined, then their vote is used instead of any
abstentions and the proposal is executed if there are enough approvals counting the new votes.

If there are not, or if no prime member is set, then the motion is dropped without being executed.

License: Apache-2.0
Loading

0 comments on commit 54f3350

Please sign in to comment.