Skip to content

Commit

Permalink
ADD: pallet digital twin v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuanyshbaev committed Jun 26, 2024
1 parent f1b3b02 commit 45e94f7
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ members = [
"frame/liability",
"frame/lighthouse",
"frame/digital-twin",
"frame/digital-twin-v2",
"primitives",
"runtime/dev",
"runtime/main",
Expand Down Expand Up @@ -139,7 +140,7 @@ sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkad
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
try-runtime-cli = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }

# Substrate pallets
# Substrate pallets
# (wasm)
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
Expand Down Expand Up @@ -190,11 +191,11 @@ cumulus-primitives-timestamp = { git = "https://github.com/paritytech/cumulus",
cumulus-primitives-utility = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v1.0.0", default-features = false }
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v1.0.0", default-features = false }

# Polkadot
# Polkadot
# (native)
polkadot-cli = { git = "https://github.com/paritytech/polkadot", branch = "release-v1.0.0" }

# Polkadot
# Polkadot
# (wasm)
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v1.0.0", default-features = false }
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v1.0.0", default-features = false }
Expand Down
30 changes: 30 additions & 0 deletions frame/digital-twin-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "pallet-robonomics-digital-twin-v2"
description = "Robonomics Network digital twin runtime module"
version = "0.1.0"
authors = ["Airalab <[email protected]>"]
edition = "2021"

[dependencies]
scale-info = { workspace = true }
parity-scale-codec = { workspace = true }
sp-std = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
frame-system = { workspace = true }
frame-support = { workspace = true }

[dev-dependencies]
sp-io = { workspace = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"sp-std/std",
"sp-core/std",
"sp-runtime/std",
"frame-system/std",
"frame-support/std",
"scale-info/std",
]
203 changes: 203 additions & 0 deletions frame/digital-twin-v2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2023 Robonomics Network <[email protected]>
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////
//! Digital twin runtime module. This can be compiled with `#[no_std]`, ready for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_core::H256;

#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// New digital twin was registered: [sender, id].
NewDigitalTwin(T::AccountId, u32),
/// Digital twin topic was changed: [sender, id, topic, source]
TopicChanged(T::AccountId, u32, H256, T::AccountId),
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::storage]
#[pallet::getter(fn total)]
/// Total count of registered digital twins.
pub(super) type Total<T> = StorageValue<_, u32>;

#[pallet::storage]
#[pallet::getter(fn owner)]
/// Get owner of digital twin with given id.
pub(super) type Owner<T> =
StorageMap<_, Twox64Concat, u32, <T as frame_system::Config>::AccountId>;

#[pallet::storage]
#[pallet::getter(fn digital_twin)]
/// Get internal structure of difital twin in format: source account -> topic hash.
pub(super) type DigitalTwin<T> =
StorageMap<_, Twox64Concat, u32, (<T as frame_system::Config>::AccountId, H256)>;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]

Check warning on line 65 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated struct `pallet::_::Store`: Use of `#[pallet::generate_store(pub(super) trait Store)]` will be removed after July 2023. Check https://github.com/paritytech/substrate/pull/13535 for more details.

warning: use of deprecated struct `pallet::_::Store`: Use of `#[pallet::generate_store(pub(super) trait Store)]` will be removed after July 2023. Check https://github.com/paritytech/substrate/pull/13535 for more details. --> frame/digital-twin-v2/src/lib.rs:65:6 | 65 | #[pallet::generate_store(pub(super) trait Store)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(deprecated)]` on by default
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create new digital twin.
#[pallet::weight(50_000)]

Check warning on line 72 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin-v2/src/lib.rs:72:26 | 72 | #[pallet::weight(50_000)] | ^^^^^^ help: omit the `let` binding: `50_000;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 72 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798>

warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798> --> frame/digital-twin-v2/src/lib.rs:72:26 | 72 | #[pallet::weight(50_000)] | ^^^^^^
pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo {

Check warning on line 73 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin-v2/src/lib.rs:73:16 | 73 | pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo { | ^^^^^^ help: omit the `let` binding: `create;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `#[warn(clippy::let_unit_value)]` on by default

Check warning on line 73 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381>

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381> --> frame/digital-twin-v2/src/lib.rs:73:16 | 73 | pub fn create(origin: OriginFor<T>) -> DispatchResultWithPostInfo { | ^^^^^^
let sender = ensure_signed(origin)?;
let id = <Total<T>>::get().unwrap_or(0);
<Total<T>>::put(id + 1);
<Owner<T>>::insert(id, sender.clone());
Self::deposit_event(Event::NewDigitalTwin(sender, id));
Ok(().into())
}

/// Set data source account for difital twin.
#[pallet::weight(50_000)]

Check warning on line 83 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin-v2/src/lib.rs:83:26 | 83 | #[pallet::weight(50_000)] | ^^^^^^ help: omit the `let` binding: `50_000;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 83 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_1::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798>

warning: use of deprecated constant `pallet::warnings::ConstantWeight_1::_w`: It is deprecated to use hard-coded constant as call weight. Please instead benchmark all calls or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/13798> --> frame/digital-twin-v2/src/lib.rs:83:26 | 83 | #[pallet::weight(50_000)] | ^^^^^^
pub fn set_source(

Check warning on line 84 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this let-binding has unit value

warning: this let-binding has unit value --> frame/digital-twin-v2/src/lib.rs:84:16 | 84 | pub fn set_source( | ^^^^^^^^^^ help: omit the `let` binding: `set_source;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

Check warning on line 84 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_1::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381>

warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_1::_w`: It is deprecated to use implicit call indices. Please instead ensure that all calls have a `pallet::call_index` attribute or put the pallet into `dev` mode. For more info see: <https://github.com/paritytech/substrate/pull/12891> <https://github.com/paritytech/substrate/pull/11381> --> frame/digital-twin-v2/src/lib.rs:84:16 | 84 | pub fn set_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>>::insert(id, (source, topic));
Ok(().into())
}
}
}

#[cfg(test)]
mod tests {
use crate::{self as digital_twin, *};

use frame_support::{assert_err, assert_ok, parameter_types};
use sp_runtime::{traits::IdentityLookup, BuildStorage, DispatchError};

type Block = frame_system::mocking::MockBlock<Runtime>;

frame_support::construct_runtime!(
pub enum Runtime {
System: frame_system,
DigitalTwin: digital_twin,
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
}

impl frame_system::Config for Runtime {
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type Block = Block;
type RuntimeCall = RuntimeCall;
type Hash = sp_core::H256;
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type DbWeight = ();
type BaseCallFilter = frame_support::traits::Everything;
type SystemWeightInfo = ();
type BlockWeights = ();
type BlockLength = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

fn new_test_ext() -> sp_io::TestExternalities {
let storage = frame_system::GenesisConfig::<Runtime>::default()
.build_storage()
.unwrap();
storage.into()
}

#[test]
fn test_create() {
new_test_ext().execute_with(|| {
assert_eq!(DigitalTwin::total(), None);
let sender = 1;
assert_ok!(DigitalTwin::create(RuntimeOrigin::signed(sender)));
assert_eq!(DigitalTwin::total(), Some(1));
assert_eq!(DigitalTwin::owner(0), Some(sender));
})
}

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

#[test]
fn test_bad_origin() {
new_test_ext().execute_with(|| {
assert_err!(
DigitalTwin::create(RuntimeOrigin::none()),
DispatchError::BadOrigin
);
})
}
}
2 changes: 2 additions & 0 deletions runtime/dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pallet-robonomics-launch = { path = "../../frame/launch", default-features = fal
pallet-robonomics-datalog = { path = "../../frame/datalog", default-features = false }
pallet-robonomics-liability = { path = "../../frame/liability", default-features = false }
pallet-robonomics-digital-twin = { path = "../../frame/digital-twin", default-features = false }
pallet-robonomics-digital-twin-v2 = { path = "../../frame/digital-twin-v2", default-features = false }

[build-dependencies]
substrate-wasm-builder = { workspace = true }
Expand Down Expand Up @@ -106,6 +107,7 @@ std = [
"pallet-robonomics-datalog/std",
"pallet-robonomics-liability/std",
"pallet-robonomics-digital-twin/std",
"pallet-robonomics-digital-twin-v2/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand Down
5 changes: 5 additions & 0 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ impl pallet_robonomics_digital_twin::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_robonomics_digital_twin_v2::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_robonomics_liability::Config for Runtime {
type Agreement = pallet_robonomics_liability::SignedAgreement<
pallet_robonomics_liability::technics::IPFS,
Expand Down Expand Up @@ -610,6 +614,7 @@ construct_runtime!(
Launch: pallet_robonomics_launch,
RWS: pallet_robonomics_rws,
DigitalTwin: pallet_robonomics_digital_twin,
DigitalTwinV2: pallet_robonomics_digital_twin_v2,
Liability: pallet_robonomics_liability,

// Sudo. Usable initially.
Expand Down
2 changes: 2 additions & 0 deletions runtime/main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pallet-transaction-payment-rpc-runtime-api = { workspace = true }
# robonomics dependencies
pallet-robonomics-datalog = { path = "../../frame/datalog", default-features = false }
pallet-robonomics-digital-twin = { path = "../../frame/digital-twin", default-features = false }
pallet-robonomics-digital-twin-v2 = { path = "../../frame/digital-twin-v2", default-features = false }
pallet-robonomics-launch = { path = "../../frame/launch", default-features = false }
pallet-robonomics-liability = { path = "../../frame/liability", default-features = false }
pallet-robonomics-lighthouse = { path = "../../frame/lighthouse", default-features = false }
Expand Down Expand Up @@ -117,6 +118,7 @@ std = [
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-robonomics-datalog/std",
"pallet-robonomics-digital-twin/std",
"pallet-robonomics-digital-twin-v2/std",
"pallet-robonomics-launch/std",
"pallet-robonomics-liability/std",
"pallet-robonomics-lighthouse/std",
Expand Down
5 changes: 5 additions & 0 deletions runtime/main/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@ impl pallet_robonomics_digital_twin::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_robonomics_digital_twin_v2::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}

impl pallet_robonomics_liability::Config for Runtime {
type Agreement = pallet_robonomics_liability::SignedAgreement<
pallet_robonomics_liability::technics::IPFS,
Expand Down Expand Up @@ -661,6 +665,7 @@ construct_runtime! {
Datalog: pallet_robonomics_datalog = 51,
Launch: pallet_robonomics_launch = 52,
DigitalTwin: pallet_robonomics_digital_twin = 54,
DigitalTwinV2: pallet_robonomics_digital_twin_v2 = 53,
RWS: pallet_robonomics_rws = 55,
Liability: pallet_robonomics_liability = 56,

Expand Down

0 comments on commit 45e94f7

Please sign in to comment.