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
+1
View File
@@ -4148,6 +4148,7 @@ dependencies = [
"sp-runtime", "sp-runtime",
"sp-std", "sp-std",
"sp-storage", "sp-storage",
"substrate-test-utils",
] ]
[[package]] [[package]]
+70 -77
View File
@@ -21,12 +21,10 @@ use frame_support::{
traits::Currency, traits::Currency,
weights::{GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight}, weights::{GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight},
}; };
use sp_core::{ use sp_core::{NeverNativeValue, traits::Externalities, storage::well_known_keys};
NeverNativeValue, map, traits::Externalities, storage::{well_known_keys, Storage},
};
use sp_runtime::{ use sp_runtime::{
ApplyExtrinsicResult, Fixed128, ApplyExtrinsicResult, Fixed128,
traits::{Hash as HashT, Convert, BlakeTwo256}, traits::{Hash as HashT, Convert},
transaction_validity::InvalidTransaction, transaction_validity::InvalidTransaction,
}; };
use pallet_contracts::ContractAddressFor; use pallet_contracts::ContractAddressFor;
@@ -159,20 +157,13 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec<u8>, Hash) {
#[test] #[test]
fn panic_execution_with_foreign_code_gives_error() { fn panic_execution_with_foreign_code_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage { let mut t = new_test_ext(BLOATY_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(69u128, 0u8, 0u128, 0u128, 0u128).encode() (69u128, 0u8, 0u128, 0u128, 0u128).encode()
}, );
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69_u128.encode());
69_u128.encode() t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => {
vec![0u8; 32]
}
],
children_default: map![],
});
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -195,20 +186,13 @@ fn panic_execution_with_foreign_code_gives_error() {
#[test] #[test]
fn bad_extrinsic_with_native_equivalent_code_gives_error() { fn bad_extrinsic_with_native_equivalent_code_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage { let mut t = new_test_ext(COMPACT_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode() (0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode()
}, );
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69_u128.encode());
69_u128.encode() t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => {
vec![0u8; 32]
}
],
children_default: map![],
});
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -231,18 +215,20 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() {
#[test] #[test]
fn successful_execution_with_native_equivalent_code_gives_ok() { fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage { let mut t = new_test_ext(COMPACT_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
}, );
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { t.insert(
(111 * DOLLARS).encode() <frame_system::Account<Runtime>>::hashed_key_for(bob()),
}, (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32] );
], t.insert(
children_default: map![], <pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(),
}); (111 * DOLLARS).encode()
);
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -273,18 +259,20 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {
#[test] #[test]
fn successful_execution_with_foreign_code_gives_ok() { fn successful_execution_with_foreign_code_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage { let mut t = new_test_ext(BLOATY_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
}, );
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { t.insert(
(111 * DOLLARS).encode() <frame_system::Account<Runtime>>::hashed_key_for(bob()),
}, (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32] );
], t.insert(
children_default: map![], <pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(),
}); (111 * DOLLARS).encode()
);
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -708,15 +696,13 @@ fn native_big_block_import_fails_on_fallback() {
#[test] #[test]
fn panic_execution_gives_error() { fn panic_execution_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage { let mut t = new_test_ext(BLOATY_CODE, false);
top: map![ t.insert(
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
0_u128.encode() (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
}, );
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32] t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 0_u128.encode());
], t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
children_default: map![],
});
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -739,18 +725,20 @@ fn panic_execution_gives_error() {
#[test] #[test]
fn successful_execution_gives_ok() { fn successful_execution_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage { let mut t = new_test_ext(COMPACT_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode() (0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
}, );
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { t.insert(
(111 * DOLLARS).encode() <frame_system::Account<Runtime>>::hashed_key_for(bob()),
}, (0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32] );
], t.insert(
children_default: map![], <pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(),
}); (111 * DOLLARS).encode()
);
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
@@ -760,7 +748,12 @@ fn successful_execution_gives_ok() {
None, None,
).0; ).0;
assert!(r.is_ok()); assert!(r.is_ok());
t.execute_with(|| {
assert_eq!(Balances::total_balance(&alice()), 111 * DOLLARS);
});
let fm = t.execute_with(TransactionPayment::next_fee_multiplier); let fm = t.execute_with(TransactionPayment::next_fee_multiplier);
let r = executor_call::<NeverNativeValue, fn() -> _>( let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t, &mut t,
"BlockBuilder_apply_extrinsic", "BlockBuilder_apply_extrinsic",
+16 -17
View File
@@ -21,8 +21,8 @@ use frame_support::{
traits::Currency, traits::Currency,
weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight}, weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight},
}; };
use sp_core::{NeverNativeValue, map, storage::Storage}; use sp_core::NeverNativeValue;
use sp_runtime::{Fixed128, Perbill, traits::{Convert, BlakeTwo256}}; use sp_runtime::{Fixed128, Perbill, traits::Convert};
use node_runtime::{ use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment,
TransactionByteFee, WeightFeeCoefficient, TransactionByteFee, WeightFeeCoefficient,
@@ -131,21 +131,20 @@ fn transaction_fee_is_correct_ultimate() {
// - 1 MILLICENTS in substrate node. // - 1 MILLICENTS in substrate node.
// - 1 milli-dot based on current polkadot runtime. // - 1 milli-dot based on current polkadot runtime.
// (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`) // (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`)
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage { let mut t = new_test_ext(COMPACT_CODE, false);
top: map![ t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => { <frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() (0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
}, );
<frame_system::Account<Runtime>>::hashed_key_for(bob()) => { t.insert(
(0u32, 0u8, 10 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() <frame_system::Account<Runtime>>::hashed_key_for(bob()),
}, (0u32, 0u8, 10 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => { );
(110 * DOLLARS).encode() t.insert(
}, <pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(),
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32] (110 * DOLLARS).encode()
], );
children_default: map![], t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);
});
let tip = 1_000_000; let tip = 1_000_000;
let xt = sign(CheckedExtrinsic { let xt = sign(CheckedExtrinsic {
+2
View File
@@ -344,6 +344,7 @@ parameter_types! {
pub const CooloffPeriod: BlockNumber = 28 * 24 * 60 * MINUTES; pub const CooloffPeriod: BlockNumber = 28 * 24 * 60 * MINUTES;
// One cent: $10,000 / MB // One cent: $10,000 / MB
pub const PreimageByteDeposit: Balance = 1 * CENTS; pub const PreimageByteDeposit: Balance = 1 * CENTS;
pub const MaxVotes: u32 = 100;
} }
impl pallet_democracy::Trait for Runtime { impl pallet_democracy::Trait for Runtime {
@@ -376,6 +377,7 @@ impl pallet_democracy::Trait for Runtime {
type PreimageByteDeposit = PreimageByteDeposit; type PreimageByteDeposit = PreimageByteDeposit;
type Slash = Treasury; type Slash = Treasury;
type Scheduler = Scheduler; type Scheduler = Scheduler;
type MaxVotes = MaxVotes;
} }
parameter_types! { parameter_types! {
+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-balances = { version = "2.0.0-dev", path = "../balances" }
pallet-scheduler = { version = "2.0.0-dev", path = "../scheduler" } pallet-scheduler = { version = "2.0.0-dev", path = "../scheduler" }
sp-storage = { version = "2.0.0-dev", path = "../../primitives/storage" } 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" hex-literal = "0.2.1"
[features] [features]
@@ -43,5 +44,6 @@ std = [
runtime-benchmarks = [ runtime-benchmarks = [
"frame-benchmarking", "frame-benchmarking",
"frame-system/runtime-benchmarks", "frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/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_REFERENDUMS: u32 = 100;
const MAX_PROPOSALS: u32 = 100; const MAX_PROPOSALS: u32 = 100;
const MAX_SECONDERS: u32 = 100; const MAX_SECONDERS: u32 = 100;
const MAX_VETOERS: u32 = 100;
const MAX_BYTES: u32 = 16_384; const MAX_BYTES: u32 = 16_384;
fn assert_last_event<T: Trait>(generic_event: <T as Trait>::Event) { 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 value = T::MinimumDeposit::get();
let proposal_hash: T::Hash = T::Hashing::hash_of(&n); 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) Ok(proposal_hash)
} }
@@ -134,15 +137,15 @@ benchmarks! {
// Create s existing "seconds" // Create s existing "seconds"
for i in 0 .. s { for i in 0 .. s {
let seconder = funded_account::<T>("seconder", i); 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")?; let deposits = Democracy::<T>::deposit_of(0).ok_or("Proposal not created")?;
assert_eq!(deposits.1.len(), (s + 1) as usize, "Seconds not recorded"); assert_eq!(deposits.0.len(), (s + 1) as usize, "Seconds not recorded");
}: _(RawOrigin::Signed(caller), 0) }: _(RawOrigin::Signed(caller), 0, u32::max_value())
verify { verify {
let deposits = Democracy::<T>::deposit_of(0).ok_or("Proposal not created")?; 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 { vote_new {
@@ -300,7 +303,7 @@ benchmarks! {
// Worst case scenario, we external propose a previously blacklisted proposal // Worst case scenario, we external propose a previously blacklisted proposal
external_propose { external_propose {
let p in 1 .. MAX_PROPOSALS; 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 origin = T::ExternalOrigin::successful_origin();
let proposal_hash = T::Hashing::hash_of(&p); let proposal_hash = T::Hashing::hash_of(&p);
@@ -361,7 +364,7 @@ benchmarks! {
veto_external { veto_external {
// Existing veto-ers // 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); let proposal_hash: T::Hash = T::Hashing::hash_of(&v);
@@ -685,7 +688,7 @@ benchmarks! {
assert!(Preimages::<T>::contains_key(proposal_hash)); assert!(Preimages::<T>::contains_key(proposal_hash));
let caller = funded_account::<T>("caller", 0); let caller = funded_account::<T>("caller", 0);
}: _(RawOrigin::Signed(caller), proposal_hash.clone()) }: _(RawOrigin::Signed(caller), proposal_hash.clone(), u32::max_value())
verify { verify {
let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); let proposal_hash = T::Hashing::hash(&encoded_proposal[..]);
assert!(!Preimages::<T>::contains_key(proposal_hash)); 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 public_proposals;
mod scheduling; mod scheduling;
mod voting; mod voting;
mod migration;
mod decoders;
const AYE: Vote = Vote { aye: true, conviction: Conviction::None }; const AYE: Vote = Vote { aye: true, conviction: Conviction::None };
const NAY: Vote = Vote { aye: false, 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 MinimumDeposit: u64 = 1;
pub const EnactmentPeriod: u64 = 2; pub const EnactmentPeriod: u64 = 2;
pub const CooloffPeriod: u64 = 2; pub const CooloffPeriod: u64 = 2;
pub const MaxVotes: u32 = 100;
} }
ord_parameter_types! { ord_parameter_types! {
pub const One: u64 = 1; pub const One: u64 = 1;
@@ -182,6 +185,7 @@ impl super::Trait for Test {
type InstantOrigin = EnsureSignedBy<Six, u64>; type InstantOrigin = EnsureSignedBy<Six, u64>;
type InstantAllowed = InstantAllowed; type InstantAllowed = InstantAllowed;
type Scheduler = Scheduler; type Scheduler = Scheduler;
type MaxVotes = MaxVotes;
} }
pub fn new_test_ext() -> sp_io::TestExternalities { 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( Democracy::propose(
Origin::signed(who), Origin::signed(who),
set_balance_proposal_hash(value), 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( Democracy::propose(
Origin::signed(who), Origin::signed(who),
set_balance_proposal_hash_and_note(value), set_balance_proposal_hash_and_note(value),
delay delay,
) )
} }
fn next_block() { fn next_block() {
System::set_block_number(System::block_number() + 1); System::set_block_number(System::block_number() + 1);
Scheduler::on_initialize(System::block_number()); 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) { 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(); next_block();
assert_noop!( 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 Error::<Test>::TooEarly
); );
next_block(); 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::free_balance(6), 60);
assert_eq!(Balances::reserved_balance(6), 0); 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() { fn preimage_deposit_should_be_reapable() {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
assert_noop!( 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 Error::<Test>::PreimageMissing
); );
@@ -104,12 +104,12 @@ fn preimage_deposit_should_be_reapable() {
next_block(); next_block();
next_block(); next_block();
assert_noop!( 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 Error::<Test>::TooEarly
); );
next_block(); 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::reserved_balance(6), 0);
assert_eq!(Balances::free_balance(6), 48); assert_eq!(Balances::free_balance(6), 48);
assert_eq!(Balances::free_balance(5), 62); 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))); assert_ok!(Democracy::vote(Origin::signed(1), r, aye(1)));
next_block(); next_block();
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() { fn deposit_for_proposals_should_be_taken() {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(1, 2, 5)); assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0)); assert_ok!(Democracy::second(Origin::signed(2), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_eq!(Balances::free_balance(1), 5); assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::free_balance(2), 15); assert_eq!(Balances::free_balance(2), 15);
assert_eq!(Balances::free_balance(5), 35); 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() { fn deposit_for_proposals_should_be_returned() {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(1, 2, 5)); assert_ok!(propose_set_balance_and_note(1, 2, 5));
assert_ok!(Democracy::second(Origin::signed(2), 0)); assert_ok!(Democracy::second(Origin::signed(2), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0, u32::max_value()));
fast_forward_to(3); fast_forward_to(3);
assert_eq!(Balances::free_balance(1), 10); assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 20); assert_eq!(Balances::free_balance(2), 20);
@@ -78,7 +78,21 @@ fn poor_proposer_should_not_work() {
fn poor_seconder_should_not_work() { fn poor_seconder_should_not_work() {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
assert_ok!(propose_set_balance_and_note(2, 2, 11)); 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
);
}); });
} }