Skip to content

Commit

Permalink
Gilts Pallet (paritytech#8139)
Browse files Browse the repository at this point in the history
* Initial draft

* Enlarge function drafted.

* Thaw draft

* Retract_bid draft

* Final bits of draft impl.

* Test mockup

* Tests

* Docs

* Add benchmark scaffold

* Integrate weights

* All benchmarks done

* Missing file

* Remove stale comments

* Fixes

* Fixes

* Allow for priority queuing.

* Another test and a fix

* Fixes

* Fixes

* cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_gilt --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/gilt/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Grumble

* Update frame/gilt/src/tests.rs

Co-authored-by: Shawn Tabrizi <[email protected]>

* Update frame/gilt/src/tests.rs

Co-authored-by: Shawn Tabrizi <[email protected]>

* Grumble

* Update frame/gilt/src/tests.rs

Co-authored-by: Shawn Tabrizi <[email protected]>

* Update frame/gilt/src/lib.rs

Co-authored-by: Shawn Tabrizi <[email protected]>

* Update frame/gilt/src/lib.rs

Co-authored-by: Shawn Tabrizi <[email protected]>

* Fix unreserve ordering

* Grumble

* Fixes

Co-authored-by: Parity Benchmarking Bot <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
3 people authored and jam10o-new committed Feb 28, 2021
1 parent b95822e commit 4cdc230
Show file tree
Hide file tree
Showing 14 changed files with 1,626 additions and 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ members = [
"frame/example-offchain-worker",
"frame/example-parallel",
"frame/executive",
"frame/gilt",
"frame/grandpa",
"frame/identity",
"frame/im-online",
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub fn testnet_genesis(
max_members: 999,
}),
pallet_vesting: Some(Default::default()),
pallet_gilt: Some(Default::default()),
}
}

Expand Down
5 changes: 4 additions & 1 deletion bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ pallet-contracts = { version = "3.0.0", default-features = false, path = "../../
pallet-contracts-primitives = { version = "3.0.0", default-features = false, path = "../../../frame/contracts/common/" }
pallet-contracts-rpc-runtime-api = { version = "3.0.0", default-features = false, path = "../../../frame/contracts/rpc/runtime-api/" }
pallet-democracy = { version = "3.0.0", default-features = false, path = "../../../frame/democracy" }
pallet-elections-phragmen = { version = "3.0.0", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-election-provider-multi-phase = { version = "3.0.0", default-features = false, path = "../../../frame/election-provider-multi-phase" }
pallet-elections-phragmen = { version = "3.0.0", default-features = false, path = "../../../frame/elections-phragmen" }
pallet-gilt = { version = "3.0.0", default-features = false, path = "../../../frame/gilt" }
pallet-grandpa = { version = "3.0.0", default-features = false, path = "../../../frame/grandpa" }
pallet-im-online = { version = "3.0.0", default-features = false, path = "../../../frame/im-online" }
pallet-indices = { version = "3.0.0", default-features = false, path = "../../../frame/indices" }
Expand Down Expand Up @@ -112,6 +113,7 @@ std = [
"pallet-democracy/std",
"pallet-elections-phragmen/std",
"frame-executive/std",
"pallet-gilt/std",
"pallet-grandpa/std",
"pallet-im-online/std",
"pallet-indices/std",
Expand Down Expand Up @@ -170,6 +172,7 @@ runtime-benchmarks = [
"pallet-contracts/runtime-benchmarks",
"pallet-democracy/runtime-benchmarks",
"pallet-elections-phragmen/runtime-benchmarks",
"pallet-gilt/runtime-benchmarks",
"pallet-grandpa/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-im-online/runtime-benchmarks",
Expand Down
30 changes: 29 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,32 @@ impl pallet_assets::Config for Runtime {
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const QueueCount: u32 = 300;
pub const MaxQueueLen: u32 = 1000;
pub const FifoQueueLen: u32 = 500;
pub const Period: BlockNumber = 30 * DAYS;
pub const MinFreeze: Balance = 100 * DOLLARS;
pub const IntakePeriod: BlockNumber = 10;
pub const MaxIntakeBids: u32 = 10;
}

impl pallet_gilt::Config for Runtime {
type Event = Event;
type Currency = Balances;
type AdminOrigin = frame_system::EnsureRoot<AccountId>;
type Deficit = ();
type Surplus = ();
type QueueCount = QueueCount;
type MaxQueueLen = MaxQueueLen;
type FifoQueueLen = FifoQueueLen;
type Period = Period;
type MinFreeze = MinFreeze;
type IntakePeriod = IntakePeriod;
type MaxIntakeBids = MaxIntakeBids;
type WeightInfo = pallet_gilt::weights::SubstrateWeight<Runtime>;
}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand Down Expand Up @@ -1090,6 +1116,7 @@ construct_runtime!(
Assets: pallet_assets::{Module, Call, Storage, Event<T>},
Mmr: pallet_mmr::{Module, Storage},
Lottery: pallet_lottery::{Module, Call, Storage, Event<T>},
Gilt: pallet_gilt::{Module, Call, Storage, Event<T>, Config},
}
);

Expand Down Expand Up @@ -1427,8 +1454,9 @@ impl_runtime_apis! {
add_benchmark!(params, batches, pallet_collective, Council);
add_benchmark!(params, batches, pallet_contracts, Contracts);
add_benchmark!(params, batches, pallet_democracy, Democracy);
add_benchmark!(params, batches, pallet_elections_phragmen, Elections);
add_benchmark!(params, batches, pallet_election_provider_multi_phase, ElectionProviderMultiPhase);
add_benchmark!(params, batches, pallet_elections_phragmen, Elections);
add_benchmark!(params, batches, pallet_gilt, Gilt);
add_benchmark!(params, batches, pallet_grandpa, Grandpa);
add_benchmark!(params, batches, pallet_identity, Identity);
add_benchmark!(params, batches, pallet_im_online, ImOnline);
Expand Down
1 change: 1 addition & 0 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ pub fn config_endowed(
max_members: 999,
}),
pallet_vesting: Some(Default::default()),
pallet_gilt: Some(Default::default()),
}
}
46 changes: 46 additions & 0 deletions frame/gilt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "pallet-gilt"
version = "3.0.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
homepage = "https://substrate.dev"
repository = "https://github.com/paritytech/substrate/"
description = "FRAME pallet for rewarding account freezing."
readme = "README.md"

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

[dependencies]
serde = { version = "1.0.101", optional = true }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
sp-std = { version = "3.0.0", default-features = false, path = "../../primitives/std" }
sp-runtime = { version = "3.0.0", default-features = false, path = "../../primitives/runtime" }
sp-arithmetic = { version = "3.0.0", default-features = false, path = "../../primitives/arithmetic" }
frame-benchmarking = { version = "3.0.0", default-features = false, path = "../benchmarking", optional = true }
frame-support = { version = "3.0.0", default-features = false, path = "../support" }
frame-system = { version = "3.0.0", default-features = false, path = "../system" }

[dev-dependencies]
sp-io = { version = "3.0.0", path = "../../primitives/io" }
sp-core = { version = "3.0.0", path = "../../primitives/core" }
pallet-balances = { version = "3.0.0", path = "../balances" }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sp-std/std",
"sp-runtime/std",
"sp-arithmetic/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
2 changes: 2 additions & 0 deletions frame/gilt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

License: Apache-2.0
136 changes: 136 additions & 0 deletions frame/gilt/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// This file is part of Substrate.

// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmarks for Gilt Pallet
#![cfg(feature = "runtime-benchmarks")]

use sp_std::prelude::*;
use super::*;
use sp_runtime::traits::{Zero, Bounded};
use sp_arithmetic::Perquintill;
use frame_system::RawOrigin;
use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use frame_support::{traits::{Currency, Get, EnsureOrigin}, dispatch::UnfilteredDispatchable};

use crate::Pallet as Gilt;

type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

benchmarks! {
place_bid {
let l in 0..(T::MaxQueueLen::get() - 1);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..l {
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
}
}: _(RawOrigin::Signed(caller.clone()), T::MinFreeze::get() * BalanceOf::<T>::from(2u32), 1)
verify {
assert_eq!(QueueTotals::<T>::get()[0], (l + 1, T::MinFreeze::get() * BalanceOf::<T>::from(l + 2)));
}

place_bid_max {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..T::MaxQueueLen::get() {
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
}
}: {
Gilt::<T>::place_bid(
RawOrigin::Signed(caller.clone()).into(),
T::MinFreeze::get() * BalanceOf::<T>::from(2u32),
1,
)?
}
verify {
assert_eq!(QueueTotals::<T>::get()[0], (
T::MaxQueueLen::get(),
T::MinFreeze::get() * BalanceOf::<T>::from(T::MaxQueueLen::get() + 1),
));
}

retract_bid {
let l in 1..T::MaxQueueLen::get();
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
for i in 0..l {
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
}
}: _(RawOrigin::Signed(caller.clone()), T::MinFreeze::get(), 1)
verify {
assert_eq!(QueueTotals::<T>::get()[0], (l - 1, T::MinFreeze::get() * BalanceOf::<T>::from(l - 1)));
}

set_target {
let call = Call::<T>::set_target(Default::default());
let origin = T::AdminOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }

thaw {
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, T::MinFreeze::get() * BalanceOf::<T>::from(3u32));
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
Gilt::<T>::enlarge(T::MinFreeze::get() * BalanceOf::<T>::from(2u32), 2);
Active::<T>::mutate(0, |m_g| if let Some(ref mut g) = m_g { g.expiry = Zero::zero() });
}: _(RawOrigin::Signed(caller.clone()), 0)
verify {
assert!(Active::<T>::get(0).is_none());
}

pursue_target_noop {
}: { Gilt::<T>::pursue_target(0) }

pursue_target_per_item {
// bids taken
let b in 1..T::MaxQueueLen::get();

let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, T::MinFreeze::get() * BalanceOf::<T>::from(b + 1));

for _ in 0..b {
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), 1)?;
}

Call::<T>::set_target(Perquintill::from_percent(100))
.dispatch_bypass_filter(T::AdminOrigin::successful_origin())?;

}: { Gilt::<T>::pursue_target(b) }

pursue_target_per_queue {
// total queues hit
let q in 1..T::QueueCount::get();

let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, T::MinFreeze::get() * BalanceOf::<T>::from(q + 1));

for i in 0..q {
Gilt::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinFreeze::get(), i + 1)?;
}

Call::<T>::set_target(Perquintill::from_percent(100))
.dispatch_bypass_filter(T::AdminOrigin::successful_origin())?;

}: { Gilt::<T>::pursue_target(q) }
}

impl_benchmark_test_suite!(
Gilt,
crate::mock::new_test_ext(),
crate::mock::Test,
);
Loading

0 comments on commit 4cdc230

Please sign in to comment.