Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add benchmarking and weights for twin transfer operations
  • Loading branch information
sameh-farouk committed Sep 10, 2025
commit bc9e4a52c712b1a5b8fca16a3d45b0dfaaff9caf
99 changes: 99 additions & 0 deletions substrate-node/pallets/pallet-tfgrid/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,98 @@ benchmarks! {
assert!(TfgridModule::<T>::users_terms_and_condition(caller).is_some());
}

// request_twin_transfer(new_account)
request_twin_transfer {
let old_owner: T::AccountId = whitelisted_caller();
// old owner must have signed T&C and have a twin
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(old_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();
assert_ok!(TfgridModule::<T>::create_twin(
RawOrigin::Signed(old_owner.clone()).into(),
get_relay_input(b"relay"),
get_public_key_input(b"0x0102030405060708090001020304050607080900010203040506070809000102"),
));

// new account: T&C accepted, no twin
let new_owner: T::AccountId = account("new", 0, 0);
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(new_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();
}: _(RawOrigin::Signed(old_owner.clone()), new_owner.clone())
verify {
assert!(TfgridModule::<T>::pending_transfer_by_twin(1).is_some());
assert_has_event::<T>(Event::TwinTransferRequested { twin_id: 1, old_account: old_owner, new_account: new_owner }.into());
}

// accept_twin_transfer(request_id)
accept_twin_transfer {
let old_owner: T::AccountId = whitelisted_caller();
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(old_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();
assert_ok!(TfgridModule::<T>::create_twin(
RawOrigin::Signed(old_owner.clone()).into(),
get_relay_input(b"relay"),
get_public_key_input(b"0x0102030405060708090001020304050607080900010203040506070809000103"),
));

let new_owner: T::AccountId = account("new", 0, 0);
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(new_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();

assert_ok!(TfgridModule::<T>::request_twin_transfer(
RawOrigin::Signed(old_owner.clone()).into(),
new_owner.clone(),
));
let request_id: u64 = 1;
}: _(RawOrigin::Signed(new_owner.clone()), request_id)
verify {
assert!(TfgridModule::<T>::pending_transfer_by_twin(1).is_none());
assert_has_event::<T>(Event::TwinOwnershipTransferred { twin_id: 1, old_account: old_owner, new_account: new_owner }.into());
}

// cancel_twin_transfer(request_id)
cancel_twin_transfer {
let old_owner: T::AccountId = whitelisted_caller();
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(old_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();
assert_ok!(TfgridModule::<T>::create_twin(
RawOrigin::Signed(old_owner.clone()).into(),
get_relay_input(b"relay"),
get_public_key_input(b"0x0102030405060708090001020304050607080900010203040506070809000104"),
));

let new_owner: T::AccountId = account("new", 0, 0);
TfgridModule::<T>::user_accept_tc(
RawOrigin::Signed(new_owner.clone()).into(),
get_document_link_input(b"some_link"),
get_document_hash_input(b"some_hash"),
).unwrap();

assert_ok!(TfgridModule::<T>::request_twin_transfer(
RawOrigin::Signed(old_owner.clone()).into(),
new_owner.clone(),
));
let request_id: u64 = 1;
}: _(RawOrigin::Signed(old_owner.clone()), request_id)
verify {
assert!(TfgridModule::<T>::pending_transfer_by_twin(1).is_none());
assert_has_event::<T>(Event::TwinTransferCanceled { twin_id: 1, old_account: old_owner, new_account: account("new", 0, 0) }.into());
}

// delete_node_farm()
delete_node_farm {
let caller: T::AccountId = whitelisted_caller();
Expand Down Expand Up @@ -749,6 +841,13 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
assert_eq!(event, &system_event);
}

fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = System::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let found = events.iter().any(|ev| ev.event == system_event);
assert!(found, "Expected event not found in events list");
}

pub fn _prepare_farm_with_node<T: Config>(source: T::AccountId) {
_prepare_farm::<T>(source.clone());
_create_node::<T>(source);
Expand Down
6 changes: 3 additions & 3 deletions substrate-node/pallets/pallet-tfgrid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ pub mod pallet {

// Twin ownership transfer: request by current owner (specify new_account)
#[pallet::call_index(40)]
#[pallet::weight(100_000_000 + T::DbWeight::get().writes(4).ref_time() + T::DbWeight::get().reads(5).ref_time())]
#[pallet::weight(<T as Config>::WeightInfo::request_twin_transfer())]
pub fn request_twin_transfer(
origin: OriginFor<T>,
new_account: T::AccountId,
Expand All @@ -1291,7 +1291,7 @@ pub mod pallet {

// Twin ownership transfer: accept by new account
#[pallet::call_index(41)]
#[pallet::weight(150_000_000 + T::DbWeight::get().writes(6).ref_time() + T::DbWeight::get().reads(7).ref_time())]
#[pallet::weight(<T as Config>::WeightInfo::accept_twin_transfer())]
pub fn accept_twin_transfer(
origin: OriginFor<T>,
request_id: u64,
Expand All @@ -1301,7 +1301,7 @@ pub mod pallet {

// Twin ownership transfer: cancel by current owner
#[pallet::call_index(42)]
#[pallet::weight(80_000_000 + T::DbWeight::get().writes(3).ref_time() + T::DbWeight::get().reads(3).ref_time())]
#[pallet::weight(<T as Config>::WeightInfo::cancel_twin_transfer())]
pub fn cancel_twin_transfer(
origin: OriginFor<T>,
request_id: u64,
Expand Down
39 changes: 39 additions & 0 deletions substrate-node/pallets/pallet-tfgrid/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub trait WeightInfo {
fn change_power_target() -> Weight;
fn bond_twin_account() -> Weight;
fn report_uptime_v2() -> Weight;
fn request_twin_transfer() -> Weight;
fn accept_twin_transfer() -> Weight;
fn cancel_twin_transfer() -> Weight;
}

/// Weights for pallet_tfgrid using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -548,6 +551,24 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(18_465_000, 3919)
.saturating_add(T::DbWeight::get().reads(4_u64))
}

fn request_twin_transfer() -> Weight {
Weight::from_parts(100_000_000, 0)
.saturating_add(T::DbWeight::get().reads(5_u64))
.saturating_add(T::DbWeight::get().writes(4_u64))
}

fn accept_twin_transfer() -> Weight {
Weight::from_parts(150_000_000, 0)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}

fn cancel_twin_transfer() -> Weight {
Weight::from_parts(100_000_000, 0)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -1030,4 +1051,22 @@ impl WeightInfo for () {
Weight::from_parts(18_465_000, 3919)
.saturating_add(RocksDbWeight::get().reads(4_u64))
}

fn request_twin_transfer() -> Weight {
Weight::from_parts(100_000_000, 0)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(4_u64))
}

fn accept_twin_transfer() -> Weight {
Weight::from_parts(150_000_000, 0)
.saturating_add(RocksDbWeight::get().reads(7_u64))
.saturating_add(RocksDbWeight::get().writes(6_u64))
}

fn cancel_twin_transfer() -> Weight {
Weight::from_parts(100_000_000, 0)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
}
Loading