Skip to content

Commit

Permalink
ADD: remove function for digital twin v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuanyshbaev committed Jun 28, 2024
1 parent 45e94f7 commit f6b46b6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions frame/digital-twin-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub mod pallet {
NewDigitalTwin(T::AccountId, u32),
/// Digital twin topic was changed: [sender, id, topic, source]
TopicChanged(T::AccountId, u32, H256, T::AccountId),
/// Digital twin topic was removed: [sender, id, source]
TopicRemoved(T::AccountId, u32, T::AccountId),
}

#[pallet::hooks]
Expand Down Expand Up @@ -96,6 +98,23 @@ pub mod pallet {
<DigitalTwin<T>>::insert(id, (source, topic));
Ok(().into())
}

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

Check warning on line 103 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:103:26 | 103 | #[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 103 in frame/digital-twin-v2/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ConstantWeight_2::_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_2::_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:103:26 | 103 | #[pallet::weight(50_000)] | ^^^^^^
pub fn remove_source(

Check warning on line 104 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:104:16 | 104 | pub fn remove_source( | ^^^^^^^^^^^^^ help: omit the `let` binding: `remove_source;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value

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

View workflow job for this annotation

GitHub Actions / clippy

use of deprecated constant `pallet::warnings::ImplicitCallIndex_2::_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_2::_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:104:16 | 104 | pub fn remove_source( | ^^^^^^^^^^^^^
origin: OriginFor<T>,
id: u32,
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::TopicRemoved(sender, id, source.clone()));
<DigitalTwin<T>>::remove(id);
Ok(().into())
}
}
}

Expand Down Expand Up @@ -191,6 +210,24 @@ mod tests {
})
}

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

#[test]
fn test_bad_origin() {
new_test_ext().execute_with(|| {
Expand Down

0 comments on commit f6b46b6

Please sign in to comment.