8000 Digital twin remove and benchmarks by dkuanyshbaev · Pull Request #368 · airalab/robonomics · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Digital twin remove and benchmarks #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already o 8000 n GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion Cargo.lock

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

10 changes: 9 additions & 1 deletion frame/digital-twin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "pallet-robonomics-digital-twin"
description = "Robonomics Network digital twin runtime module"
version = "0.3.0"
version = "3.1.0"
authors = ["Airalab <research@aira.life>"]
edition = "2021"

Expand All @@ -13,6 +13,7 @@ sp-core = { workspace = true }
sp-runtime = { workspace = true }
frame-system = { workspace = true }
frame-support = { workspace = true }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
sp-io = { workspace = true }
Expand All @@ -26,5 +27,12 @@ std = [
"sp-runtime/std",
"frame-system/std",
"frame-support/std",
"frame-benchmarking/std",
"scale-info/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
80 changes: 80 additions & 0 deletions frame/digital-twin/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2024 Robonomics Network <research@robonomics.network>
//
// 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 Digital Twin Pallet

use super::{Pallet as DigitalTwin, *};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_system::RawOrigin;
use sp_core::H256;
use sp_std::prelude::*;

const SEED: u32 = 0;

benchmarks! {

create {
let caller: T::AccountId = account("caller", 1, SEED);

DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;
}: _(RawOrigin::Signed(caller))

set_source {
let caller: T::AccountId = account("caller", 1, SEED);
let id: u32 = 0;
let topic: H256 = Default::default();
let source: T::AccountId = account("caller", 2, SEED);

DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;
DigitalTwin::<T>::set_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;
}: _(RawOrigin::Signed(caller), id, topic, source)

remove_source {
let caller: T::AccountId = account("caller", 1, SEED);
let id: u32 = 0;
let topic: H256 = Default::default();
let source: T::AccountId = account("caller", 2, SEED);

DigitalTwin::<T>::create(RawOrigin::Signed(caller.clone()).into())?;
DigitalTwin::<T>::set_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;
DigitalTwin::<T>::remove_source(
RawOrigin::Signed(caller.clone()).into(),
id,
topic,
source.clone(),
)?;
}: _(RawOrigin::Signed(caller), id, topic, source)

verify {
}
}

impl_benchmark_test_suite!(
DigitalTwin,
crate::tests::new_test_ext(),
crate::tests::Runtime,
);
70 changes: 67 additions & 3 deletions frame/digital-twin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
//! Digital twin runtime module. This can be compiled with `#[no_std]`, ready for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;

pub use pallet::*;
pub use weights::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_core::H256;
Expand All @@ -31,6 +37,8 @@ pub mod pallet {
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -70,7 +78,8 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create new digital twin.
#[pallet::weight(50_000)]
#[pallet::weight(T::WeightInfo::create())]
#[pallet::call_index(0)]
pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let id = <Total<T>>::get().unwrap_or(0);
Expand All @@ -81,7 +90,8 @@ pub mod pallet {
}

/// Set data source account for difital twin.
#[pallet::weight(50_000)]
#[pallet::weight(T::WeightInfo::set_source())]
#[pallet::call_index(1)]
pub fn set_source(
origin: OriginFor<T>,
id: u32,
Expand All @@ -106,6 +116,29 @@ pub mod pallet {
});
Ok(().into())
}

/// Remove data source account for difital twin.
#[pallet::weight(T::WeightInfo::remove_source())]
#[pallet::call_index(2)]
pub fn remove_source(
origin: OriginFor<T>,
id: u32,
topic: H256,
source: T::AccountId,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
ensure!(
<Owner<T>>::get(id) == Some(sender.clone()),
"sender should be a twin owner"
);
Self::deposit_event(Event::TopicChanged(sender, id, topic, source.clone()));
<DigitalTwin<T>>::mutate(id, |m| {
if let Some(map) = m {
map.remove(&topic);
}
});
Ok(().into())
}
}
}

Expand Down Expand Up @@ -159,7 +192,7 @@ mod tests {
type RuntimeEvent = RuntimeEvent;
}

fn new_test_ext() -> sp_io::TestExternalities {
pub fn new_test_ext() -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();
Expand Down Expand Up @@ -201,6 +234,37 @@ mod tests {
})
}

#[test]
fn test_remove_source() {
new_test_ext().execute_with(|| {
let sender = 1;
let bad_sender = 2;
let source = 3;
assert_ok!(DigitalTwin::create(RuntimeOrigin::signed(sender)));
assert_ok!(DigitalTwin::set_source(
RuntimeOrigin::signed(sender),
0,
Default::default(),
source
));
assert_err!(
DigitalTwin::remove_source(
RuntimeOrigin::signed(bad_sender),
0,
Default::default(),
source
),
DispatchError::Other("sender should be a twin owner")
);
assert_ok!(DigitalTwin::remove_source(
RuntimeOrigin::signed(sender),
0,
Default::default(),
source
));
})
}

#[test]
fn test_bad_origin() {
new_test_ext().execute_with(|| {
Expand Down
86 changes: 86 additions & 0 deletions frame/digital-twin/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! Autogenerated weights for `robonomics_digital_twin`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2024-10-06, STEPS: `20`, REPEAT: `10`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! CPU: `11th Gen Intel(R) Core(TM) i5-1130G7 @ 1.10GHz`
//! EXECUTION: ``, WASM-EXECUTION: `Compiled`, CHAIN: `Some("dev")`, DB CACHE: 1024

// Executed Command:
// ./target/release/robonomics
// benchmark
// pallet
// --chain
// dev
// --pallet
// robonomics_digital_twin
// --extrinsic
// *
// --steps
// 20
// --repeat
// 10
// --output
// ~/weights.rs

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

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

/// Weight functions.
pub trait WeightInfo {
fn create() -> Weight;
fn set_source() -> Weight;
fn remove_source() -> Weight;
}

/// Weight functions for `robonomics_digital_twin`.
pub struct RobonomicsWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for RobonomicsWeight<T> {
/// Storage: `DigitalTwin::Total` (r:1 w:1)
/// Proof: `DigitalTwin::Total` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::Owner` (r:0 w:1)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn create() -> Weight {
// Proof Size summary in bytes:
// Measured: `103`
// Estimated: `1588`
// Minimum execution time: 13_684_000 picoseconds.
Weight::from_parts(14_382_000, 0)
.saturating_add(Weight::from_parts(0, 1588))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(2))
}
/// Storage: `DigitalTwin::Owner` (r:1 w:0)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::DigitalTwin` (r:1 w:1)
/// Proof: `DigitalTwin::DigitalTwin` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn set_source() -> Weight {
// Proof Size summary in bytes:
// Measured: `234`
// Estimated: `3699`
// Minimum execution time: 17_211_000 picoseconds.
Weight::from_parts(17_931_000, 0)
.saturating_add(Weight::from_parts(0, 3699))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `DigitalTwin::Owner` (r:1 w:0)
/// Proof: `DigitalTwin::Owner` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `DigitalTwin::DigitalTwin` (r:1 w:1)
/// Proof: `DigitalTwin::DigitalTwin` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_source() -> Weight {
// Proof Size summary in bytes:
// Measured: `166`
// Estimated: `3631`
// Minimum execution time: 16_309_000 picoseconds.
Weight::from_parts(16_437_000, 0)
.saturating_add(Weight::from_parts(0, 3631))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
}
1 change: 1 addition & 0 deletions runtime/dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
"pallet-robonomics-launch/runtime-benchmarks",
"pallet-robonomics-datalog/runtime-benchmarks",
"pallet-robonomics-digital-twin/runtime-benchmarks",
"frame-system-benchmarking",
"hex-literal",
]
2 changes: 2 additions & 0 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl pallet_robonomics_rws::Config for Runtime {

impl pallet_robonomics_digital_twin::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_robonomics_digital_twin::weights::RobonomicsWeight<Runtime>;
}

impl pallet_robonomics_liability::Config for Runtime {
Expand Down Expand Up @@ -667,6 +668,7 @@ mod benches {
// Robonomics pallets
[robonomics_datalog, Datalog]
[robonomics_launch, Launch]
[robonomics_digital_twin, DigitalTwin]
);
}

Expand Down
1 change: 1 addition & 0 deletions runtime/main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ runtime-benchmarks = [
"pallet-timestamp/runtime-benchmarks",
"pallet-robonomics-launch/runtime-benchmarks",
"pallet-robonomics-datalog/runtime-benchmarks",
"pallet-robonomics-digital-twin/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
"frame-system-benchmarking",
Expand Down
2 changes: 2 additions & 0 deletions runtime/main/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ impl pallet_robonomics_rws::Config for Runtime {

impl pallet_robonomics_digital_twin::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = pallet_robonomics_digital_twin::weights::RobonomicsWeight<Runtime>;
}

impl pallet_robonomics_liability::Config for Runtime {
Expand Down Expand Up @@ -729,6 +730,7 @@ mod benches {
// Robonomics pallets
[robonomics_datalog, Datalog]
[robonomics_launch, Launch]
[robonomics_digital_twin, DigitalTwin]
);
}

Expand Down
0