Skip to content

Commit

Permalink
Introduce scheduler and use it for the democracy dispatch queue (pari…
Browse files Browse the repository at this point in the history
…tytech#5412)

* Initial draft of the logic

* Build and tests

* Make work with new initialize infratructure.

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Fix test

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

* Rejig interface to make it more useful for democracy.

* Try to get democraxy module to make use of scheduler.

* Make democracy use scheduler.

* Use actual max weight for enactent

* Remove TODO

* Fix runtime build

* Minor cleanup

* Fix scheduler.

* Fix benchmarks

* Fix

* Fix

* Fix

* More bench fixes

* Fix

* Fix.

* Add more bench constants.

* Fix cancel_queued bench.

* Fix test comment.

* Update frame/scheduler/src/lib.rs

Co-Authored-By: Marcio Diaz <marcio.diaz@gmail.com>

Co-authored-by: Marcio Diaz <marcio.diaz@gmail.com>
  • Loading branch information
gavofyork and marcio-diaz committed Apr 1, 2020
1 parent 176b19f commit 2449d21
Show file tree
Hide file tree
Showing 16 changed files with 826 additions and 122 deletions.
18 changes: 18 additions & 0 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
Expand Up @@ -86,6 +86,7 @@ members = [
"frame/offences",
"frame/randomness-collective-flip",
"frame/recovery",
"frame/scheduler",
"frame/scored-pool",
"frame/session",
"frame/session/benchmarking",
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/Cargo.toml
Expand Up @@ -16,7 +16,7 @@ repository = "https://github.com/paritytech/substrate/"
wasm-opt = false

[badges]
travis-ci = { repository = "paritytech/substrate", branch = "master" }
travis-ci = { repository = "paritytech/substrate" }
maintenance = { status = "actively-developed" }
is-it-maintained-issue-resolution = { repository = "paritytech/substrate" }
is-it-maintained-open-issues = { repository = "paritytech/substrate" }
Expand Down
3 changes: 2 additions & 1 deletion bin/node/runtime/Cargo.toml
Expand Up @@ -61,8 +61,9 @@ pallet-session = { version = "2.0.0-alpha.5", features = ["historical"], path =
pallet-session-benchmarking = { version = "2.0.0-alpha.5", path = "../../../frame/session/benchmarking", default-features = false, optional = true }
pallet-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/staking" }
pallet-staking-reward-curve = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/staking/reward-curve" }
pallet-sudo = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/sudo" }
pallet-scheduler = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/scheduler" }
pallet-society = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/society" }
pallet-sudo = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/sudo" }
pallet-timestamp = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/timestamp" }
pallet-treasury = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/treasury" }
pallet-utility = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/utility" }
Expand Down
14 changes: 14 additions & 0 deletions bin/node/runtime/src/lib.rs
Expand Up @@ -207,6 +207,17 @@ impl pallet_utility::Trait for Runtime {
type MaxSignatories = MaxSignatories;
}

parameter_types! {
pub const MaximumWeight: Weight = 2_000_000;
}

impl pallet_scheduler::Trait for Runtime {
type Event = Event;
type Origin = Origin;
type Call = Call;
type MaximumWeight = MaximumWeight;
}

parameter_types! {
pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS;
pub const ExpectedBlockTime: Moment = MILLISECS_PER_BLOCK;
Expand Down Expand Up @@ -262,6 +273,7 @@ impl pallet_transaction_payment::Trait for Runtime {
parameter_types! {
pub const MinimumPeriod: Moment = SLOT_DURATION / 2;
}

impl pallet_timestamp::Trait for Runtime {
type Moment = Moment;
type OnTimestampSet = Babe;
Expand Down Expand Up @@ -392,6 +404,7 @@ impl pallet_democracy::Trait for Runtime {
type CooloffPeriod = CooloffPeriod;
type PreimageByteDeposit = PreimageByteDeposit;
type Slash = Treasury;
type Scheduler = Scheduler;
}

parameter_types! {
Expand Down Expand Up @@ -670,6 +683,7 @@ construct_runtime!(
Society: pallet_society::{Module, Call, Storage, Event<T>, Config<T>},
Recovery: pallet_recovery::{Module, Call, Storage, Event<T>},
Vesting: pallet_vesting::{Module, Call, Storage, Event<T>, Config<T>},
Scheduler: pallet_scheduler::{Module, Call, Storage, Event<T>},
}
);

Expand Down
1 change: 1 addition & 0 deletions frame/democracy/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "..
[dev-dependencies]
sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" }
pallet-balances = { version = "2.0.0-alpha.5", path = "../balances" }
pallet-scheduler = { version = "2.0.0-alpha.5", path = "../scheduler" }
sp-storage = { version = "2.0.0-alpha.5", path = "../../primitives/storage" }
hex-literal = "0.2.1"

Expand Down
74 changes: 32 additions & 42 deletions frame/democracy/src/benchmarking.rs
Expand Up @@ -29,6 +29,9 @@ const SEED: u32 = 0;
const MAX_USERS: u32 = 1000;
const MAX_REFERENDUMS: u32 = 100;
const MAX_PROPOSALS: u32 = 100;
const MAX_SECONDERS: u32 = 100;
const MAX_VETOERS: u32 = 100;
const MAX_BYTES: u32 = 16_384;

fn funded_account<T: Trait>(name: &'static str, index: u32) -> T::AccountId {
let caller: T::AccountId = account(name, index, SEED);
Expand Down Expand Up @@ -57,6 +60,13 @@ fn add_referendum<T: Trait>(n: u32) -> Result<ReferendumIndex, &'static str> {
0.into(),
);
let referendum_index: ReferendumIndex = ReferendumCount::get() - 1;
let _ = T::Scheduler::schedule_named(
(DEMOCRACY_ID, referendum_index),
0.into(),
None,
63,
Call::enact_proposal(proposal_hash, referendum_index).into(),
);
Ok(referendum_index)
}

Expand Down Expand Up @@ -89,7 +99,7 @@ benchmarks! {
let p in 1 .. MAX_PROPOSALS;

// Add p proposals
for i in 0..p {
for i in 0 .. p {
add_proposal::<T>(i)?;
}

Expand All @@ -99,10 +109,10 @@ benchmarks! {
}: _(RawOrigin::Signed(caller), proposal_hash, value.into())

second {
let s in 0 .. 100;
let s in 0 .. MAX_SECONDERS;

// Create s existing "seconds"
for i in 0..s {
for i in 0 .. s {
let seconder = funded_account::<T>("seconder", i);
Democracy::<T>::second(RawOrigin::Signed(seconder).into(), 0)?;
}
Expand Down Expand Up @@ -202,15 +212,15 @@ benchmarks! {

veto_external {
// Existing veto-ers
let v in 0 .. 100;
let v in 0 .. MAX_VETOERS;

let proposal_hash: T::Hash = T::Hashing::hash_of(&v);

let origin_propose = T::ExternalDefaultOrigin::successful_origin();
Democracy::<T>::external_propose_default(origin_propose, proposal_hash.clone())?;

let mut vetoers: Vec<T::AccountId> = Vec::new();
for i in 0..v {
for i in 0 .. v {
vetoers.push(account("vetoer", i, SEED));
}
Blacklist::<T>::insert(proposal_hash, (T::BlockNumber::zero(), vetoers));
Expand All @@ -228,12 +238,9 @@ benchmarks! {
}: _(RawOrigin::Root, referendum_index)

cancel_queued {
let d in 0 .. 100;
let u in 1 .. MAX_USERS;

let referendum_index = add_referendum::<T>(d)?;
let block_number: T::BlockNumber = 0.into();
let hash: T::Hash = T::Hashing::hash_of(&d);
<DispatchQueue<T>>::put(vec![(block_number, hash, referendum_index.clone()); d as usize]);
let referendum_index = add_referendum::<T>(u)?;
}: _(RawOrigin::Root, referendum_index)

open_proxy {
Expand Down Expand Up @@ -297,67 +304,50 @@ benchmarks! {
}: _(RawOrigin::Signed(delegator))

clear_public_proposals {
let p in 0 .. 100;
let p in 0 .. MAX_PROPOSALS;

for i in 0 .. p {
add_proposal::<T>(i)?;
}

}: _(RawOrigin::Root)

note_preimage {
// Num of bytes in encoded proposal
let b in 0 .. 16_384;
let b in 0 .. MAX_BYTES;

let caller = funded_account::<T>("caller", b);
let encoded_proposal = vec![0; b as usize];
}: _(RawOrigin::Signed(caller), encoded_proposal)

note_imminent_preimage {
// Num of bytes in encoded proposal
let b in 0 .. 16_384;
// Length of dispatch queue
let d in 0 .. 100;
let b in 0 .. MAX_BYTES;

let mut dispatch_queue = Vec::new();
// d + 1 to include the one we are testing
for i in 0 .. d + 1 {
let encoded_proposal = vec![0; i as usize];
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);
let block_number = T::BlockNumber::zero();
let referendum_index: ReferendumIndex = 0;
dispatch_queue.push((block_number, proposal_hash, referendum_index))
}
<DispatchQueue<T>>::put(dispatch_queue);
let encoded_proposal = vec![0; b as usize];
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);
let block_number = T::BlockNumber::one();
Preimages::<T>::insert(&proposal_hash, PreimageStatus::Missing(block_number));

let caller = funded_account::<T>("caller", b);
let encoded_proposal = vec![0; d as usize];
let encoded_proposal = vec![0; b as usize];
}: _(RawOrigin::Signed(caller), encoded_proposal)

reap_preimage {
// Num of bytes in encoded proposal
let b in 0 .. 16_384;
// Length of dispatch queue
let d in 0 .. 100;

let mut dispatch_queue = Vec::new();
for i in 0 .. d {
let encoded_proposal = vec![0; i as usize];
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);
let block_number = T::BlockNumber::zero();
let referendum_index: ReferendumIndex = 0;
dispatch_queue.push((block_number, proposal_hash, referendum_index))
}
<DispatchQueue<T>>::put(dispatch_queue);
let b in 0 .. MAX_BYTES;

let caller = funded_account::<T>("caller", d);
let encoded_proposal = vec![0; d as usize];
let encoded_proposal = vec![0; b as usize];
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);

let caller = funded_account::<T>("caller", b);
Democracy::<T>::note_preimage(RawOrigin::Signed(caller.clone()).into(), encoded_proposal.clone())?;

// We need to set this otherwise we get `Early` error.
let block_number = T::VotingPeriod::get() + T::EnactmentPeriod::get() + T::BlockNumber::one();
System::<T>::set_block_number(block_number.into());

let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);

}: _(RawOrigin::Signed(caller), proposal_hash)

unlock {
Expand Down

0 comments on commit 2449d21

Please sign in to comment.