Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Democracy weight #5828

Merged
merged 34 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
74bd838
WIP democracy weight
gui1117 Apr 29, 2020
f676fab
Apply suggestions from code review
gui1117 May 4, 2020
d9139bf
Apply suggestions from code review
gui1117 May 4, 2020
87a64cd
fix node
gui1117 May 4, 2020
2386428
fix doc
gui1117 May 4, 2020
3d0643e
WIP
gui1117 May 4, 2020
f7e4280
implement refund for costy extrinsics
gui1117 May 4, 2020
7b767a8
new bench machine
gui1117 May 5, 2020
3eefb9a
use compact
gui1117 May 5, 2020
f72eb85
Merge remote-tracking branch 'origin/master' into gui-democracy-weight
gui1117 May 5, 2020
1a7e7fd
Apply suggestions from code review
gui1117 May 5, 2020
ef5f609
doc
gui1117 May 5, 2020
bac6a08
refactor a bit
gui1117 May 6, 2020
8c03436
remove todo for potential additional PR on top of this one
gui1117 May 7, 2020
d6a1e03
Merge remote-tracking branch 'origin/master' into gui-democracy-weight
gui1117 May 7, 2020
7b6ed82
add new benchmark information
gui1117 May 7, 2020
64bb6d5
Merge remote-tracking branch 'origin/master' into gui-democracy-weight
gui1117 May 7, 2020
d83293d
Update weights and add raw data
shawntabrizi May 8, 2020
49dce0c
adress issues
gui1117 May 10, 2020
2d0b5ae
Merge remote-tracking branch 'origin/master' into gui-democracy-weight
gui1117 May 10, 2020
f80b35c
add even more value in tests
gui1117 May 11, 2020
3d0caab
add baseweight for on_initialize
gui1117 May 11, 2020
edff293
Apply suggestions from code review
gui1117 May 11, 2020
2452f05
no use of default version
gui1117 May 11, 2020
e0a8022
fix storage version
gui1117 May 11, 2020
cefce53
use genesis config in tests
gui1117 May 11, 2020
0408f77
refactor
gui1117 May 11, 2020
dfa9a7e
remove arg for propose
gui1117 May 12, 2020
63b4a4d
Merge remote-tracking branch 'origin/master' into gui-democracy-weight
gui1117 May 12, 2020
db093e1
doc
gui1117 May 12, 2020
fd1e142
fix doc
gui1117 May 12, 2020
cb5f01f
remove configurable max vetoers
gui1117 May 12, 2020
fe879df
Update frame/democracy/src/lib.rs
gui1117 May 14, 2020
926f53b
Merge branch 'master' into gui-democracy-weight
shawntabrizi May 14, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

147 changes: 70 additions & 77 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ use frame_support::{
traits::Currency,
weights::{GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight},
};
use sp_core::{
NeverNativeValue, map, traits::Externalities, storage::{well_known_keys, Storage},
};
use sp_core::{NeverNativeValue, traits::Externalities, storage::well_known_keys};
use sp_runtime::{
ApplyExtrinsicResult, Fixed128,
traits::{Hash as HashT, Convert, BlakeTwo256},
traits::{Hash as HashT, Convert},
transaction_validity::InvalidTransaction,
};
use pallet_contracts::ContractAddressFor;
Expand Down Expand Up @@ -158,20 +156,13 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec<u8>, Hash) {

#[test]
fn panic_execution_with_foreign_code_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(69u128, 0u8, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
69_u128.encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => {
vec![0u8; 32]
}
],
children_default: map![],
});
let mut t = new_test_ext(BLOATY_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(69u128, 0u8, 0u128, 0u128, 0u128).encode()
);
t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69_u128.encode());
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);

let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t,
Expand All @@ -194,20 +185,13 @@ fn panic_execution_with_foreign_code_gives_error() {

#[test]
fn bad_extrinsic_with_native_equivalent_code_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
69_u128.encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => {
vec![0u8; 32]
}
],
children_default: map![],
});
let mut t = new_test_ext(COMPACT_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 69u128, 0u128, 0u128, 0u128).encode()
);
t.insert(<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(), 69_u128.encode());
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);

let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t,
Expand All @@ -230,18 +214,20 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() {

#[test]
fn successful_execution_with_native_equivalent_code_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
(111 * DOLLARS).encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
],
children_default: map![],
});
let mut t = new_test_ext(COMPACT_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(bob()),
(0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<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() -> _>(
&mut t,
Expand Down Expand Up @@ -272,18 +258,20 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {

#[test]
fn successful_execution_with_foreign_code_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
(111 * DOLLARS).encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
],
children_default: map![],
});
let mut t = new_test_ext(BLOATY_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(bob()),
(0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<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() -> _>(
&mut t,
Expand Down Expand Up @@ -707,15 +695,13 @@ fn native_big_block_import_fails_on_fallback() {

#[test]
fn panic_execution_gives_error() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(BLOATY_CODE, Storage {
top: map![
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
0_u128.encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
],
children_default: map![],
});
let mut t = new_test_ext(BLOATY_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
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]);

let r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t,
Expand All @@ -738,18 +724,20 @@ fn panic_execution_gives_error() {

#[test]
fn successful_execution_gives_ok() {
let mut t = TestExternalities::<BlakeTwo256>::new_with_code(COMPACT_CODE, Storage {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
(111 * DOLLARS).encode()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
],
children_default: map![],
});
let mut t = new_test_ext(COMPACT_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 111 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(bob()),
(0u32, 0u8, 0 * DOLLARS, 0u128, 0u128, 0u128).encode()
);
t.insert(
<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() -> _>(
&mut t,
Expand All @@ -759,7 +747,12 @@ fn successful_execution_gives_ok() {
None,
).0;
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 r = executor_call::<NeverNativeValue, fn() -> _>(
&mut t,
"BlockBuilder_apply_extrinsic",
Expand Down
33 changes: 16 additions & 17 deletions bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use frame_support::{
traits::Currency,
weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight},
};
use sp_core::{NeverNativeValue, map, storage::Storage};
use sp_runtime::{Fixed128, Perbill, traits::{Convert, BlakeTwo256}};
use sp_core::NeverNativeValue;
use sp_runtime::{Fixed128, Perbill, traits::Convert};
use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment,
TransactionByteFee, WeightFeeCoefficient,
Expand Down Expand Up @@ -130,21 +130,20 @@ fn transaction_fee_is_correct_ultimate() {
// - 1 MILLICENTS in substrate node.
// - 1 milli-dot based on current polkadot runtime.
// (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 {
top: map![
<frame_system::Account<Runtime>>::hashed_key_for(alice()) => {
(0u32, 0u8, 100 * 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()
},
<frame_system::BlockHash<Runtime>>::hashed_key_for(0) => vec![0u8; 32]
],
children_default: map![],
});
let mut t = new_test_ext(COMPACT_CODE, false);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(alice()),
(0u32, 0u8, 100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
);
t.insert(
<frame_system::Account<Runtime>>::hashed_key_for(bob()),
(0u32, 0u8, 10 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
);
t.insert(
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec(),
(110 * DOLLARS).encode()
);
t.insert(<frame_system::BlockHash<Runtime>>::hashed_key_for(0), vec![0u8; 32]);

let tip = 1_000_000;
let xt = sign(CheckedExtrinsic {
Expand Down
4 changes: 4 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ parameter_types! {
pub const CooloffPeriod: BlockNumber = 28 * 24 * 60 * MINUTES;
// One cent: $10,000 / MB
pub const PreimageByteDeposit: Balance = 1 * CENTS;
pub const MaxVotes: u32 = 100;
pub const MaxVetoers: u32 = 20;
}

impl pallet_democracy::Trait for Runtime {
Expand Down Expand Up @@ -373,6 +375,8 @@ impl pallet_democracy::Trait for Runtime {
type PreimageByteDeposit = PreimageByteDeposit;
type Slash = Treasury;
type Scheduler = Scheduler;
type MaxVotes = MaxVotes;
type MaxVetoers = MaxVetoers;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions frame/democracy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -43,5 +44,6 @@ std = [
runtime-benchmarks = [
"frame-benchmarking",
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
16 changes: 10 additions & 6 deletions frame/democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,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)
}
Expand Down Expand Up @@ -133,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 {
Expand Down Expand Up @@ -684,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));
Expand Down
Loading