Democracy weight (#5828)

This commit is contained in:
thiolliere
2020-05-15 13:25:43 +02:00
committed by GitHub
parent be8c96adec
commit 1dbd761192
12 changed files with 742 additions and 240 deletions
+2
View File
@@ -26,6 +26,7 @@ sp-core = { version = "2.0.0-dev", path = "../../primitives/core" }
pallet-balances = { version = "2.0.0-dev", path = "../balances" }
pallet-scheduler = { version = "2.0.0-dev", path = "../scheduler" }
sp-storage = { version = "2.0.0-dev", path = "../../primitives/storage" }
substrate-test-utils = { version = "2.0.0-dev", path = "../../test-utils" }
hex-literal = "0.2.1"
[features]
@@ -43,5 +44,6 @@ std = [
runtime-benchmarks = [
"frame-benchmarking",
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
+12 -9
View File
@@ -34,7 +34,6 @@ 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 assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) {
@@ -56,7 +55,11 @@ fn add_proposal<T: Trait>(n: u32) -> Result<T::Hash, &'static str> {
let value = T::MinimumDeposit::get();
let proposal_hash: T::Hash = T::Hashing::hash_of(&n);
Democracy::<T>::propose(RawOrigin::Signed(other).into(), proposal_hash, value.into())?;
Democracy::<T>::propose(
RawOrigin::Signed(other).into(),
proposal_hash,
value.into(),
)?;
Ok(proposal_hash)
}
@@ -134,15 +137,15 @@ benchmarks! {
// Create s existing "seconds"
for i in 0 .. s {
let seconder = funded_account::<T>("seconder", i);
Democracy::<T>::second(RawOrigin::Signed(seconder).into(), 0)?;
Democracy::<T>::second(RawOrigin::Signed(seconder).into(), 0, u32::max_value())?;
}
let deposits = Democracy::<T>::deposit_of(0).ok_or("Proposal not created")?;
assert_eq!(deposits.1.len(), (s + 1) as usize, "Seconds not recorded");
}: _(RawOrigin::Signed(caller), 0)
assert_eq!(deposits.0.len(), (s + 1) as usize, "Seconds not recorded");
}: _(RawOrigin::Signed(caller), 0, u32::max_value())
verify {
let deposits = Democracy::<T>::deposit_of(0).ok_or("Proposal not created")?;
assert_eq!(deposits.1.len(), (s + 2) as usize, "`second` benchmark did not work");
assert_eq!(deposits.0.len(), (s + 2) as usize, "`second` benchmark did not work");
}
vote_new {
@@ -300,7 +303,7 @@ benchmarks! {
// Worst case scenario, we external propose a previously blacklisted proposal
external_propose {
let p in 1 .. MAX_PROPOSALS;
let v in 1 .. MAX_VETOERS;
let v in 1 .. MAX_VETOERS as u32;
let origin = T::ExternalOrigin::successful_origin();
let proposal_hash = T::Hashing::hash_of(&p);
@@ -361,7 +364,7 @@ benchmarks! {
veto_external {
// Existing veto-ers
let v in 0 .. MAX_VETOERS;
let v in 0 .. MAX_VETOERS as u32;
let proposal_hash: T::Hash = T::Hashing::hash_of(&v);
@@ -685,7 +688,7 @@ benchmarks! {
assert!(Preimages::<T>::contains_key(proposal_hash));
let caller = funded_account::<T>("caller", 0);
}: _(RawOrigin::Signed(caller), proposal_hash.clone())
}: _(RawOrigin::Signed(caller), proposal_hash.clone(), u32::max_value())
verify {
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);
assert!(!Preimages::<T>::contains_key(proposal_hash));
File diff suppressed because it is too large Load Diff
+7 -3
View File
@@ -42,6 +42,8 @@ mod proxying;
mod public_proposals;
mod scheduling;
mod voting;
mod migration;
mod decoders;
const AYE: Vote = Vote { aye: true, conviction: Conviction::None };
const NAY: Vote = Vote { aye: false, conviction: Conviction::None };
@@ -132,6 +134,7 @@ parameter_types! {
pub const MinimumDeposit: u64 = 1;
pub const EnactmentPeriod: u64 = 2;
pub const CooloffPeriod: u64 = 2;
pub const MaxVotes: u32 = 100;
}
ord_parameter_types! {
pub const One: u64 = 1;
@@ -182,6 +185,7 @@ impl super::Trait for Test {
type InstantOrigin = EnsureSignedBy<Six, u64>;
type InstantAllowed = InstantAllowed;
type Scheduler = Scheduler;
type MaxVotes = MaxVotes;
}
pub fn new_test_ext() -> sp_io::TestExternalities {
@@ -232,7 +236,7 @@ fn propose_set_balance(who: u64, value: u64, delay: u64) -> DispatchResult {
Democracy::propose(
Origin::signed(who),
set_balance_proposal_hash(value),
delay
delay,
)
}
@@ -240,14 +244,14 @@ fn propose_set_balance_and_note(who: u64, value: u64, delay: u64) -> DispatchRes
Democracy::propose(
Origin::signed(who),
set_balance_proposal_hash_and_note(value),
delay
delay,
)
}
fn next_block() {
System::set_block_number(System::block_number() + 1);
Scheduler::on_initialize(System::block_number());
assert_eq!(Democracy::begin_block(System::block_number()), Ok(()));
assert!(Democracy::begin_block(System::block_number()).is_ok());
}
fn fast_forward_to(n: u64) {
@@ -0,0 +1,81 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The for various partial storage decoders
use super::*;
use frame_support::storage::{migration, StorageMap, unhashed};
#[test]
fn test_decode_compact_u32_at() {
new_test_ext().execute_with(|| {
let v = codec::Compact(u64::max_value());
migration::put_storage_value(b"test", b"", &[], v);
assert_eq!(decode_compact_u32_at(b"test"), None);
for v in vec![0, 10, u32::max_value()] {
let compact_v = codec::Compact(v);
unhashed::put(b"test", &compact_v);
assert_eq!(decode_compact_u32_at(b"test"), Some(v));
}
unhashed::kill(b"test");
assert_eq!(decode_compact_u32_at(b"test"), None);
})
}
#[test]
fn len_of_deposit_of() {
new_test_ext().execute_with(|| {
for l in vec![0, 1, 200, 1000] {
let value: (Vec<u64>, u64) = ((0..l).map(|_| Default::default()).collect(), 3u64);
DepositOf::<Test>::insert(2, value);
assert_eq!(Democracy::len_of_deposit_of(2), Some(l));
}
DepositOf::<Test>::remove(2);
assert_eq!(Democracy::len_of_deposit_of(2), None);
})
}
#[test]
fn pre_image() {
new_test_ext().execute_with(|| {
let key = Default::default();
let missing = PreimageStatus::Missing(0);
Preimages::<Test>::insert(key, missing);
assert!(Democracy::pre_image_data_len(key).is_err());
assert_eq!(Democracy::check_pre_image_is_missing(key), Ok(()));
Preimages::<Test>::remove(key);
assert!(Democracy::pre_image_data_len(key).is_err());
assert!(Democracy::check_pre_image_is_missing(key).is_err());
for l in vec![0, 10, 100, 1000u32] {
let available = PreimageStatus::Available{
data: (0..l).map(|i| i as u8).collect(),
provider: 0,
deposit: 0,
since: 0,
expiry: None,
};
Preimages::<Test>::insert(key, available);
assert_eq!(Democracy::pre_image_data_len(key), Ok(l));
assert!(Democracy::check_pre_image_is_missing(key).is_err());
}
})
}
@@ -0,0 +1,45 @@
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.
// Substrate is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Substrate is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
//! The tests for migration.
use super::*;
use frame_support::{storage::migration, Hashable, traits::OnRuntimeUpgrade};
use substrate_test_utils::assert_eq_uvec;
#[test]
fn migration() {
new_test_ext().execute_with(|| {
for i in 0..3 {
let k = i.twox_64_concat();
let v: (BalanceOf<Test>, Vec<u64>) = (i * 1000, vec![i]);
migration::put_storage_value(b"Democracy", b"DepositOf", &k, v);
}
StorageVersion::kill();
Democracy::on_runtime_upgrade();
assert_eq!(StorageVersion::get(), Some(Releases::V1));
assert_eq_uvec!(
DepositOf::<Test>::iter().collect::<Vec<_>>(),
vec![
(0, (vec![0u64], <BalanceOf<Test>>::from(0u32))),
(1, (vec![1u64], <BalanceOf<Test>>::from(1000u32))),
(2, (vec![2u64], <BalanceOf<Test>>::from(2000u32))),
]
);
})
}
@@ -77,11 +77,11 @@ fn preimage_deposit_should_be_reapable_earlier_by_owner() {
next_block();
assert_noop!(
Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2)),
Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2), u32::max_value()),
Error::<Test>::TooEarly
);
next_block();
assert_ok!(Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2)));
assert_ok!(Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2), u32::max_value()));
assert_eq!(Balances::free_balance(6), 60);
assert_eq!(Balances::reserved_balance(6), 0);
@@ -92,7 +92,7 @@ fn preimage_deposit_should_be_reapable_earlier_by_owner() {
fn preimage_deposit_should_be_reapable() {
new_test_ext().execute_with(|| {
assert_noop!(
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)),
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2), u32::max_value()),
Error::<Test>::PreimageMissing
);
@@ -104,12 +104,12 @@ fn preimage_deposit_should_be_reapable() {
next_block();
next_block();
assert_noop!(
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)),
Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2), u32::max_value()),
Error::<Test>::TooEarly
);
next_block();
assert_ok!(Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2)));
assert_ok!(Democracy::reap_preimage(Origin::signed(5), set_balance_proposal_hash(2), u32::max_value()));
assert_eq!(Balances::reserved_balance(6), 0);
assert_eq!(Balances::free_balance(6), 48);
assert_eq!(Balances::free_balance(5), 62);
@@ -153,6 +153,6 @@ fn reaping_imminent_preimage_should_fail() {
assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
next_block();
next_block();
assert_noop!(Democracy::reap_preimage(Origin::signed(6), h), Error::<Test>::Imminent);
assert_noop!(Democracy::reap_preimage(Origin::signed(6), h, u32::max_value()), Error::<Test>::Imminent);
});
}
@@ -35,10 +35,10 @@ fn backing_for_should_work() {
fn deposit_for_proposals_should_be_taken() {
new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(2), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::free_balance(2), 15);
assert_eq!(Balances::free_balance(5), 35);
@@ -49,10 +49,10 @@ fn deposit_for_proposals_should_be_taken() {
fn deposit_for_proposals_should_be_returned() {
new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(5), 0));
assert_ok!(Democracy::second(Origin::signed(2), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
fast_forward_to(3);
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 20);
@@ -78,7 +78,21 @@ fn poor_proposer_should_not_work() {
fn poor_seconder_should_not_work() {
new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(2, 2, 11));
assert_noop!(Democracy::second(Origin::signed(1), 0), BalancesError::<Test, _>::InsufficientBalance);
assert_noop!(
Democracy::second(Origin::signed(1), 0, u32::max_value()),
BalancesError::<Test, _>::InsufficientBalance
);
});
}
#[test]
fn invalid_seconds_upper_bound_should_not_work() {
new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_noop!(
Democracy::second(Origin::signed(2), 0, 0),
Error::<Test>::WrongUpperBound
);
});
}