Skip to content

Commit 1bccaf6

Browse files
authored
change: ETCM-12424 remove dependency on slots from pallet-block-producer-fees (#1085)
1 parent d8df494 commit 1bccaf6

File tree

8 files changed

+44
-30
lines changed

8 files changed

+44
-30
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ runtime APIs `sp_sidechain::GetEpochDurationApi` instead of deprecated `sp_sidec
3939
implement the new API in their runtime. Backward compatibility option can be switched on by enabling `legacy-slotapi-compat`
4040
feature in the crate, which will cause legacy chain nodes to still use `SlotApi` where present and include the
4141
`sidechain.slot` as an optional field.
42+
* `pallet-block-producer-fees` no longer uses slots and is instead configured with a `Moment` type used for identifying
43+
when an SPO's fee configuration has changed
4244

4345
## Removed
4446

demo/runtime/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ impl pallet_block_producer_fees::Config for Runtime {
556556

557557
type HistoricalChangesPerProducer = ConstU16<5>;
558558

559-
fn current_slot() -> sp_consensus_slots::Slot {
559+
type Moment = Slot;
560+
561+
fn current_moment() -> Slot {
560562
let slot: u64 = pallet_aura::CurrentSlot::<Runtime>::get().into();
561563
sp_consensus_slots::Slot::from(slot)
562564
}

toolkit/block-producer-fees/pallet/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ frame-system = { workspace = true }
1717
parity-scale-codec = { workspace = true }
1818
scale-info = { workspace = true }
1919
sp-block-producer-fees = { workspace = true }
20-
sp-consensus-slots = { workspace = true }
2120
sp-std = { workspace = true }
2221

2322
[dev-dependencies]

toolkit/block-producer-fees/pallet/src/benchmarking.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ pub trait BenchmarkHelper<T> {
1111
fn account_id(i: u8) -> T;
1212
}
1313

14-
#[benchmarks]
14+
#[benchmarks(where <T as crate::Config>::Moment: From<u64>)]
1515
mod benchmarks {
1616
use super::*;
1717
use frame_support::traits::Get;
1818
use frame_system::RawOrigin;
1919
use frame_system::pallet_prelude::OriginFor;
20-
use sp_consensus_slots::Slot;
2120
use sp_std::collections::vec_deque::VecDeque;
2221

2322
// Pessimistic storage for the ID
24-
fn setup_storage<T: Config>() {
23+
fn setup_storage<T: Config>()
24+
where
25+
<T as crate::Config>::Moment: From<u64>,
26+
{
2527
let size = T::HistoricalChangesPerProducer::get();
2628
// Pessimistic storage content for is full, because it requires additional removal from the vecdeque.
27-
let data = (0..size).into_iter().map(|_| (Slot::from(0), 0u16)).collect::<VecDeque<_>>();
29+
let data = (0..size)
30+
.into_iter()
31+
.map(|_| (T::Moment::from(0u64), 0u16))
32+
.collect::<VecDeque<_>>();
2833
for i in 0u8..100u8 {
2934
let id = T::BenchmarkHelper::account_id(i);
3035
FeesChanges::<T>::insert(id, data.clone());

toolkit/block-producer-fees/pallet/src/lib.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! distributing the rest of rewards to his stakers. Precision of the margin fee setting is bounded
55
//! to 1/100 of a percent.
66
//!
7-
//! The margin fees are stored together with the slot at which change occurred, so this data can be
7+
//! The margin fees are stored together with the time at which change occurred, so this data can be
88
//! exposed to rewards calculation.
99
//!
1010
//! Log of changes per account is bounded. The oldest entries are dropped when new ones are added.
@@ -31,7 +31,6 @@ pub mod pallet {
3131
use frame_support::pallet_prelude::*;
3232
use frame_system::pallet_prelude::*;
3333
use sp_block_producer_fees::PerTenThousands;
34-
use sp_consensus_slots::Slot;
3534
use sp_std::collections::vec_deque::VecDeque;
3635

3736
/// Current version of the pallet
@@ -49,23 +48,26 @@ pub mod pallet {
4948
/// Weight information on extrinsic in the pallet. For convenience weights in [weights] module can be used.
5049
type WeightInfo: WeightInfo;
5150

52-
/// Should provide the slot number of the current block.
53-
fn current_slot() -> Slot;
51+
/// Moment in time when a change in fees occured
52+
type Moment: Parameter + Default + MaxEncodedLen + PartialOrd;
53+
54+
/// Should provide the moment for the current block.
55+
fn current_moment() -> Self::Moment;
5456

5557
#[cfg(feature = "runtime-benchmarks")]
5658
/// Benchmark helper type used for running benchmarks
5759
type BenchmarkHelper: benchmarking::BenchmarkHelper<Self::AccountId>;
5860
}
5961

60-
type FeeChange = (Slot, PerTenThousands);
62+
type FeeChangeOf<T> = (<T as Config>::Moment, PerTenThousands);
6163

6264
/// Stores bounded amount of fee changes per account
6365
#[pallet::storage]
6466
#[pallet::unbounded]
6567
pub type FeesChanges<T: Config> = StorageMap<
6668
Hasher = Twox64Concat,
6769
Key = T::AccountId,
68-
Value = VecDeque<FeeChange>,
70+
Value = VecDeque<FeeChangeOf<T>>,
6971
QueryKind = ValueQuery,
7072
>;
7173

@@ -83,7 +85,7 @@ pub mod pallet {
8385
if fees_log.len() > T::HistoricalChangesPerProducer::get().into() {
8486
let _ = fees_log.pop_back();
8587
}
86-
fees_log.push_front((T::current_slot(), fee_numerator));
88+
fees_log.push_front((T::current_moment(), fee_numerator));
8789
});
8890
Ok(())
8991
}
@@ -96,25 +98,28 @@ pub mod pallet {
9698
}
9799

98100
/// Retrieves all stored block producer fees settings. The most recent fees are in front of vecdeque.
99-
pub fn get_all() -> impl Iterator<Item = (T::AccountId, VecDeque<FeeChange>)> {
101+
pub fn get_all() -> impl Iterator<Item = (T::AccountId, VecDeque<FeeChangeOf<T>>)> {
100102
FeesChanges::<T>::iter()
101103
}
102104

103105
/// Retrieves the latest fee settings for all accounts.
104-
pub fn get_all_latest() -> impl Iterator<Item = (T::AccountId, FeeChange)> {
106+
pub fn get_all_latest() -> impl Iterator<Item = (T::AccountId, FeeChangeOf<T>)> {
105107
Self::get_all().map(|(account_id, changes)| {
106-
(account_id, *changes.front().expect("There are no empty collections in storage"))
108+
(
109+
account_id,
110+
changes.front().expect("There are no empty collections in storage").clone(),
111+
)
107112
})
108113
}
109114

110115
/// Retrieves fees settings for the given account id.
111116
/// Empty collection is returned if there are no settings stored for given id.
112-
pub fn get(id: T::AccountId) -> VecDeque<FeeChange> {
117+
pub fn get(id: T::AccountId) -> VecDeque<FeeChangeOf<T>> {
113118
FeesChanges::<T>::get(id)
114119
}
115120

116121
/// Gets the latest fee setting for the given account.
117-
pub fn get_latest(id: T::AccountId) -> Option<FeeChange> {
122+
pub fn get_latest(id: T::AccountId) -> Option<FeeChangeOf<T>> {
118123
FeesChanges::<T>::get(id).front().cloned()
119124
}
120125
}

toolkit/block-producer-fees/pallet/src/mock.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use frame_support::{
44
traits::{ConstU16, ConstU32, ConstU64, Everything},
55
};
66
use frame_system::mocking::MockBlock;
7-
use sp_consensus_slots::Slot;
87
use sp_io::TestExternalities;
98
use sp_runtime::{
109
AccountId32, BuildStorage,
1110
traits::{BlakeTwo256, IdentityLookup},
1211
};
1312

13+
type Moment = u64;
14+
1415
#[frame_support::pallet]
1516
pub mod mock_pallet {
1617
use super::*;
@@ -22,7 +23,7 @@ pub mod mock_pallet {
2223
pub trait Config: frame_system::Config {}
2324

2425
#[pallet::storage]
25-
pub type CurrentSlot<T: Config> = StorageValue<_, Slot, ValueQuery>;
26+
pub type CurrentMoment<T: Config> = StorageValue<_, Moment, ValueQuery>;
2627
}
2728

2829
construct_runtime! {
@@ -84,8 +85,10 @@ impl crate::pallet::Config for Test {
8485
// Stores the current and one historical value, two in total
8586
type HistoricalChangesPerProducer = ConstU16<1>;
8687

87-
fn current_slot() -> Slot {
88-
mock_pallet::CurrentSlot::<Test>::get()
88+
type Moment = Moment;
89+
90+
fn current_moment() -> Moment {
91+
mock_pallet::CurrentMoment::<Test>::get()
8992
}
9093

9194
#[cfg(feature = "runtime-benchmarks")]

toolkit/block-producer-fees/pallet/src/tests.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::*;
22
use frame_support::{assert_err, assert_ok};
33
use frame_system::pallet_prelude::OriginFor;
44
use mock::*;
5-
use sp_consensus_slots::Slot;
65
use sp_runtime::{AccountId32, DispatchError};
76
use sp_std::collections::vec_deque::VecDeque;
87

@@ -13,21 +12,21 @@ fn stores_the_configured_number_of_fee_changes() {
1312
let bob = AccountId32::new([2u8; 32]);
1413
let charlie = AccountId32::new([3u8; 32]);
1514

16-
mock_pallet::CurrentSlot::<Test>::set(Slot::from(1));
15+
mock_pallet::CurrentMoment::<Test>::set(1);
1716
assert_ok!(Pallet::<Test>::set_fee(OriginFor::<Test>::signed(alice.clone()), 101));
1817
assert_ok!(Pallet::<Test>::set_fee(OriginFor::<Test>::signed(bob.clone()), 201));
1918

20-
mock_pallet::CurrentSlot::<Test>::set(Slot::from(2));
19+
mock_pallet::CurrentMoment::<Test>::set(2);
2120
assert_ok!(Pallet::<Test>::set_fee(OriginFor::<Test>::signed(alice.clone()), 102));
2221

23-
mock_pallet::CurrentSlot::<Test>::set(Slot::from(3));
22+
mock_pallet::CurrentMoment::<Test>::set(3);
2423
// Setting third fee causes drop of the first one
2524
assert_ok!(Pallet::<Test>::set_fee(OriginFor::<Test>::signed(alice.clone()), 103));
2625

27-
let alice_entry_2 = (Slot::from(2), 102);
28-
let alice_entry_3 = (Slot::from(3), 103);
26+
let alice_entry_2 = (2, 102);
27+
let alice_entry_3 = (3, 103);
2928
let alice_entries = VecDeque::from(vec![alice_entry_3, alice_entry_2]);
30-
let bob_entry_1 = (Slot::from(1), 201);
29+
let bob_entry_1 = (1, 201);
3130
let bob_entries = VecDeque::from(vec![bob_entry_1]);
3231

3332
assert_eq!(Pallet::<Test>::get(alice.clone()), alice_entries);

0 commit comments

Comments
 (0)