Skip to content

Commit

Permalink
Remove system.remark, add weights and remove event
Browse files Browse the repository at this point in the history
  • Loading branch information
dastansam committed Sep 14, 2024
1 parent 8fdb8d8 commit 17c23ba
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 51 deletions.
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.

12 changes: 10 additions & 2 deletions crates/pallet-history-seeding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ include = [

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.12", default-features = false, features = ["derive"] }
scale-info = { version = "2.11.2", default-features = false, features = ["derive"] }
frame-benchmarking = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631", optional = true }
frame-support = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
frame-system = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }
scale-info = { version = "2.11.2", default-features = false, features = ["derive"] }
sp-std = { default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "5626154d0781ac9a6ffd5a6207ed237f425ae631" }

[dev-dependencies]
Expand All @@ -30,9 +31,16 @@ sp-runtime = { default-features = false, git = "https://github.com/subspace/polk
default = ["std"]
std = [
"codec/std",
"scale-info/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-std/std",
]
try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
37 changes: 37 additions & 0 deletions crates/pallet-history-seeding/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Benchmarking for the pallet-history-seeding

use super::*;
use frame_benchmarking::v2::*;

#[benchmarks]
mod benchmarks {
use super::*;
use crate::Pallet;
use frame_support::pallet_prelude::*;
use frame_system::RawOrigin;
use sp_std::vec;

#[benchmark]
fn seed_history(
b: Linear<0, { *T::BlockLength::get().max.get(DispatchClass::Normal) }>,
) -> Result<(), BenchmarkError> {
let remark_message = vec![1; b as usize];
let seeder: T::AccountId = account("HistorySeeder", 1, 0);

Pallet::<T>::set_history_seeder(RawOrigin::Root.into(), seeder.clone()).unwrap();

#[extrinsic_call]
_(RawOrigin::Signed(seeder), remark_message);

Ok(())
}

#[benchmark]
fn set_history_seeder() {
let seeder = account("HistorySeeder", 1, 0);
#[extrinsic_call]
_(RawOrigin::Root, seeder);
}

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}
42 changes: 12 additions & 30 deletions crates/pallet-history-seeding/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::pallet_prelude::*;
use frame_support::traits::{BuildGenesisConfig, Get};
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
mod tests;
pub mod weights;

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use crate::weights::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_support::traits::BuildGenesisConfig;
use frame_system::pallet_prelude::*;
use frame_system::{RawOrigin, WeightInfo};
use scale_info::prelude::vec::Vec;

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type WeightInfo: WeightInfo;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// History was seeded. [who, remark_size]
HistorySeeded { who: T::AccountId, remark_size: u32 },
}

#[pallet::error]
pub enum Error<T> {
/// The sender is not authorized to seed history
Expand All @@ -38,41 +33,28 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn history_seeder)]
pub type HistorySeeder<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;
pub(super) type HistorySeeder<T: Config> = StorageValue<_, T::AccountId, OptionQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Seed history with a remark
/// TODO: add proper weight
#[pallet::call_index(0)]
#[pallet::weight((<T as frame_system::Config>::SystemWeightInfo::remark(remark.len() as u32) + T::DbWeight::get().reads(1), Pays::No))]
#[pallet::weight((T::WeightInfo::seed_history(remark.len() as u32), Pays::No))]
pub fn seed_history(origin: OriginFor<T>, remark: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin.clone())?;

// Ensure the sender is the authorized history seeder
ensure!(
Some(who.clone()) == Self::history_seeder(),
Error::<T>::NotAuthorized
);

// Add the remark to the block
frame_system::Pallet::<T>::remark(
RawOrigin::Signed(who.clone()).into(),
remark.clone(),
)
.map_err(|e| e.error)?;

// Emit an event
Self::deposit_event(Event::HistorySeeded {
who,
remark_size: remark.len() as u32,
});
let _ = remark;

Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(T::DbWeight::get().writes(1))]
#[pallet::weight(T::WeightInfo::set_history_seeder())]
pub fn set_history_seeder(
origin: OriginFor<T>,
new_seeder: T::AccountId,
Expand All @@ -92,7 +74,7 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
if let Some(ref seeder) = self.history_seeder {
if let Some(seeder) = &self.history_seeder {
HistorySeeder::<T>::put(seeder);
}
}
Expand Down
21 changes: 4 additions & 17 deletions crates/pallet-history-seeding/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::{self as pallet_history_seeding};
use crate::{self as pallet_history_seeding, Error};
use frame_support::traits::BuildGenesisConfig;
use frame_support::{assert_noop, assert_ok, construct_runtime, derive_impl};
use frame_system as system;
use sp_runtime::BuildStorage;
Expand All @@ -9,7 +9,6 @@ type Block = frame_system::mocking::MockBlock<Test>;
construct_runtime!(
pub struct Test {
System: frame_system,
Sudo: pallet_sudo,
HistorySeeding: pallet_history_seeding,
}
);
Expand All @@ -19,16 +18,10 @@ impl frame_system::Config for Test {
type Block = Block;
}

impl pallet_sudo::Config for Test {
type RuntimeEvent = RuntimeEvent;
type RuntimeCall = RuntimeCall;
impl pallet_history_seeding::Config for Test {
type WeightInfo = ();
}

impl pallet::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = system::GenesisConfig::<Test>::default()
.build_storage()
Expand All @@ -39,7 +32,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
#[test]
fn genesis_config_works() {
new_test_ext().execute_with(|| {
let genesis_config = pallet::GenesisConfig::<Test> {
let genesis_config = pallet_history_seeding::GenesisConfig::<Test> {
history_seeder: Some(1),
};
genesis_config.build();
Expand Down Expand Up @@ -76,12 +69,6 @@ fn seed_history_works() {
remark.clone()
));

// Check if the event was emitted
System::assert_has_event(RuntimeEvent::HistorySeeding(Event::HistorySeeded {
who: 1,
remark_size: 3,
}));

// Ensure unauthorized account cannot seed history
assert_noop!(
HistorySeeding::seed_history(RuntimeOrigin::signed(2), remark),
Expand Down
93 changes: 93 additions & 0 deletions crates/pallet-history-seeding/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

//! Autogenerated weights for pallet_history_seeding
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 42.0.0
//! DATE: 2024-09-12, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024

// Executed Command:
// /Users/dastansamat/.cargo/target/release/subspace-node
// benchmark
// pallet
// --runtime
// /Users/dastansamat/.cargo/target/release/wbuild/subspace-runtime/subspace_runtime.compact.compressed.wasm
// --steps=50
// --repeat=20
// --pallet=pallet_history_seeding
// --extrinsic
// *
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./crates/pallet-history-seeding/src/weights.rs
// --template
// ./frame-weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::ParityDbWeight}};
use core::marker::PhantomData;

/// Weight functions needed for pallet_history_seeding.
pub trait WeightInfo {
fn seed_history(b: u32, ) -> Weight;
fn set_history_seeder() -> Weight;
}

/// Weights for pallet_history_seeding using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: `HistorySeeding::HistorySeeder` (r:1 w:0)
/// Proof: `HistorySeeding::HistorySeeder` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// The range of component `b` is `[0, 3932160]`.
fn seed_history(b: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `37`
// Estimated: `1517`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 1517)
// Standard Error: 0
.saturating_add(Weight::from_parts(211, 0).saturating_mul(b.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
}
/// Storage: `HistorySeeding::HistorySeeder` (r:0 w:1)
/// Proof: `HistorySeeding::HistorySeeder` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
fn set_history_seeder() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 1_000_000 picoseconds.
Weight::from_parts(2_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: `HistorySeeding::HistorySeeder` (r:1 w:0)
/// Proof: `HistorySeeding::HistorySeeder` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
/// The range of component `b` is `[0, 3932160]`.
fn seed_history(b: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `37`
// Estimated: `1517`
// Minimum execution time: 2_000_000 picoseconds.
Weight::from_parts(2_000_000, 1517)
// Standard Error: 0
.saturating_add(Weight::from_parts(211, 0).saturating_mul(b.into()))
.saturating_add(ParityDbWeight::get().reads(1_u64))
}
/// Storage: `HistorySeeding::HistorySeeder` (r:0 w:1)
/// Proof: `HistorySeeding::HistorySeeder` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`)
fn set_history_seeder() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 1_000_000 picoseconds.
Weight::from_parts(2_000_000, 0)
.saturating_add(ParityDbWeight::get().writes(1_u64))
}
}
1 change: 1 addition & 0 deletions crates/subspace-node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub fn gemini_3h_compiled() -> Result<GenericChainSpec, String> {
],
},
CouncilDemocracyConfigParams::<BlockNumber>::production_params(),
// TODO: Proper value here
sudo_account.clone(),
)?)
.map_err(|error| format!("Failed to serialize genesis config: {error}"))?,
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-collective/runtime-benchmarks",
"pallet-domains/runtime-benchmarks",
"pallet-history-seeding/runtime-benchmarks",
"pallet-mmr/runtime-benchmarks",
"pallet-messenger/runtime-benchmarks",
"pallet-rewards/runtime-benchmarks",
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl pallet_democracy::Config for Runtime {
}

impl pallet_history_seeding::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_history_seeding::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -970,7 +970,6 @@ construct_runtime!(
Democracy: pallet_democracy = 83,
Preimage: pallet_preimage = 84,

// history seeding
HistorySeeding: pallet_history_seeding = 91,

// Reserve some room for other pallets as we'll remove sudo pallet eventually.
Expand Down Expand Up @@ -1119,6 +1118,7 @@ mod benches {
[pallet_timestamp, Timestamp]
[pallet_messenger, Messenger]
[pallet_transporter, Transporter]
[pallet_history_seeding, HistorySeeding]
);
}

Expand Down

0 comments on commit 17c23ba

Please sign in to comment.