Skip to content

Commit 200b6ab

Browse files
anonymousGigaentemajm
authored
Add benchmarks for octopus-pallets (#96)
* add benchmarks for octopus-pallets * Apply suggestions from code review Co-authored-by: Artem <[email protected]> * Update appchain/benchmarking/src/lib.rs Co-authored-by: Artem <[email protected]> Co-authored-by: Yuanchao Sun <[email protected]> Co-authored-by: Artem <[email protected]>
1 parent 24449c1 commit 200b6ab

File tree

19 files changed

+2090
-1389
lines changed

19 files changed

+2090
-1389
lines changed

appchain/benchmarking/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ std = [
5757
"scale-info/std",
5858
"codec/std",
5959
]
60+
61+
runtime-benchmarks = []

appchain/benchmarking/src/lib.rs

Lines changed: 169 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,169 @@
1-
//#![cfg_attr(not(feature = "std"), no_std)]
2-
//mod mock;
3-
//
4-
//use codec::Decode;
5-
//use frame_benchmarking::{benchmarks, whitelisted_caller, Vec};
6-
//use frame_system::{
7-
// offchain::{AppCrypto, SigningTypes},
8-
// RawOrigin,
9-
//};
10-
//use pallet_octopus_appchain::{
11-
// types::{BurnEvent, LockAssetEvent, Observation, ObservationsPayload, Validator, ValidatorSet},
12-
// Config as AppchainConfig, Pallet as AppchainPallet,
13-
//};
14-
//use pallet_octopus_support::traits::AppchainInterface;
15-
//use scale_info::prelude::{format, string::ToString, vec};
16-
//use sp_runtime::{traits::StaticLookup, RuntimeAppPublic};
17-
//
18-
//pub struct Pallet<T: Config>(pallet_octopus_appchain::Pallet<T>);
19-
//pub trait Config: AppchainConfig + pallet_assets::Config<pallet_assets::Instance1> {}
20-
//
21-
//fn create_default_asset<T: pallet_assets::Config<I>, I: 'static>(
22-
// is_sufficient: bool,
23-
//) -> (T::AccountId, <T::Lookup as StaticLookup>::Source) {
24-
// let caller: T::AccountId = whitelisted_caller();
25-
// let caller_lookup = T::Lookup::unlookup(caller.clone());
26-
// let data: Vec<u8> = vec![1, 0, 0, 0];
27-
// let asset_id = T::AssetId::decode(&mut &*data).unwrap();
28-
// let root = RawOrigin::Root.into();
29-
// assert!(pallet_assets::Pallet::<T, I>::force_create(
30-
// root,
31-
// asset_id,
32-
// caller_lookup.clone(),
33-
// is_sufficient,
34-
// 1u32.into(),
35-
// )
36-
// .is_ok());
37-
// (caller, caller_lookup)
38-
//}
39-
//
40-
//fn produce_validator_set<T: AppchainConfig>(index: u32) -> Observation<T::AccountId> {
41-
// let receiver: T::AccountId = whitelisted_caller();
42-
// let validator = Validator { validator_id_in_appchain: receiver, total_stake: 100 };
43-
// Observation::UpdateValidatorSet(ValidatorSet { set_id: index, validators: vec![validator] })
44-
//}
45-
//
46-
//fn produce_burn_notification<T: AppchainConfig>(index: u32) -> Observation<T::AccountId> {
47-
// let receiver: T::AccountId = whitelisted_caller();
48-
// Observation::Burn(BurnEvent {
49-
// index,
50-
// sender_id: "pallet-test.testnet".to_string().as_bytes().to_vec(),
51-
// receiver,
52-
// amount: 10000000000000000000,
53-
// })
54-
//}
55-
//
56-
//fn produce_lock_asset_notification<T: AppchainConfig>(
57-
// index: u32,
58-
// token_id: Vec<u8>,
59-
//) -> Observation<T::AccountId> {
60-
// let receiver: T::AccountId = whitelisted_caller();
61-
// Observation::LockAsset(LockAssetEvent {
62-
// index,
63-
// token_id,
64-
// sender_id: "pallet-test.testnet".to_string().as_bytes().to_vec(),
65-
// receiver,
66-
// amount: 100000,
67-
// })
68-
//}
69-
//
70-
//fn get_submitter_information<T: AppchainConfig>(
71-
//) -> (<T as SigningTypes>::Public, <T as SigningTypes>::Signature, Vec<u8>) {
72-
// const PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
73-
// let key = <T::AppCrypto as AppCrypto<
74-
// <T as SigningTypes>::Public,
75-
// <T as SigningTypes>::Signature,
76-
// >>::RuntimeAppPublic::generate_pair(Some(
77-
// format!("{}//Alice", PHRASE).as_bytes().to_vec(),
78-
// ));
79-
// let key_data = key.to_raw_vec();
80-
//
81-
// let generic_public = <T::AppCrypto as AppCrypto<
82-
// <T as SigningTypes>::Public,
83-
// <T as SigningTypes>::Signature,
84-
// >>::GenericPublic::from(key);
85-
// let public: <T as SigningTypes>::Public = generic_public.into();
86-
// let sig = <T::AppCrypto as AppCrypto<
87-
// <T as SigningTypes>::Public,
88-
// <T as SigningTypes>::Signature,
89-
// >>::sign(&frame_benchmarking::vec![1, 2, 3], public.clone())
90-
// .unwrap();
91-
// (public, sig, key_data)
92-
//}
93-
//
94-
//benchmarks! {
95-
// force_set_is_activated {
96-
// let mut is_activated: bool = true;
97-
// for i in 0 .. 100 {
98-
// if i%2 == 0 {
99-
// is_activated = true;
100-
// } else {
101-
// is_activated = false;
102-
// }
103-
// }
104-
// }: {
105-
// let _ = AppchainPallet::<T>::force_set_is_activated(RawOrigin::Root.into(), is_activated);
106-
// }
107-
// verify {
108-
// assert_eq!(AppchainPallet::<T>::is_activated(), is_activated);
109-
// }
110-
//
111-
// force_set_next_set_id {
112-
// let next_set_id:u32 = 1;
113-
// }: {
114-
// let _ = AppchainPallet::<T>::force_set_next_set_id(RawOrigin::Root.into(), next_set_id);
115-
// }
116-
// verify {
117-
// assert_eq!(AppchainPallet::<T>::next_set_id(), next_set_id);
118-
// }
119-
//
120-
// force_set_planned_validators {
121-
// let b in 2 .. 100u32;
122-
// let mut validators: Vec<(T::AccountId, u128)> = Vec::new();
123-
// for i in 1..b {
124-
// validators.push((whitelisted_caller(), 100));
125-
// }
126-
// }: {
127-
// let _ = AppchainPallet::<T>::force_set_planned_validators(RawOrigin::Root.into(), validators);
128-
// }
129-
// verify {
130-
// assert_eq!(AppchainPallet::<T>::planned_validators().len(), (b-1) as usize);
131-
// }
132-
//
133-
// force_set_next_notification_id {
134-
// let next_notification_id:u32 = 1;
135-
// }: {
136-
// let ret = AppchainPallet::<T>::force_set_next_notification_id(RawOrigin::Root.into(), next_notification_id);
137-
// assert!(ret.is_ok());
138-
// }
139-
//
140-
// submit_observations {
141-
// let b in 2 .. 10u32;
142-
// let mut obs: Vec<Observation<<T as frame_system::Config>::AccountId>> = Vec::new();
143-
// obs.push(produce_validator_set::<T>(1u32));
144-
// create_default_asset::<T, pallet_assets::Instance1>(true);
145-
//
146-
// for i in 1..b {
147-
// if i%2 == 1 {
148-
// obs.push(produce_lock_asset_notification::<T>(i, "usdn.testnet".to_string().as_bytes().to_vec()));
149-
// } else {
150-
// obs.push(produce_burn_notification::<T>(i));
151-
// }
152-
// }
153-
//
154-
// let (public, signature, key_data) = get_submitter_information::<T>();
155-
// let payload = ObservationsPayload {
156-
// public,
157-
// key_data,
158-
// block_number: 1u32.into(),
159-
// observations: obs,
160-
// };
161-
//
162-
// }: {
163-
// let ret = AppchainPallet::<T>::submit_observations(RawOrigin::None.into(), payload, signature);
164-
// assert!(ret.is_ok());
165-
// }
166-
//
167-
// impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test, extra = false);
168-
//}
169-
//
1+
#![cfg(feature = "runtime-benchmarks")]
2+
#![cfg_attr(not(feature = "std"), no_std)]
3+
mod mock;
4+
5+
use codec::Decode;
6+
use frame_benchmarking::{benchmarks, whitelisted_caller, Vec};
7+
use frame_system::{
8+
offchain::{AppCrypto, SigningTypes},
9+
RawOrigin,
10+
};
11+
use pallet_octopus_appchain::{
12+
types::{BurnEvent, LockAssetEvent, Observation, ObservationsPayload, Validator, ValidatorSet},
13+
Config as AppchainConfig, Pallet as AppchainPallet,
14+
};
15+
use pallet_octopus_support::traits::AppchainInterface;
16+
use scale_info::prelude::{format, string::ToString, vec};
17+
use sp_runtime::{traits::StaticLookup, RuntimeAppPublic};
18+
19+
pub struct Pallet<T: Config>(pallet_octopus_appchain::Pallet<T>);
20+
pub trait Config: AppchainConfig + pallet_assets::Config<pallet_assets::Instance1> {}
21+
22+
fn create_default_asset<T: pallet_assets::Config<I>, I: 'static>(
23+
is_sufficient: bool,
24+
) -> (T::AccountId, <T::Lookup as StaticLookup>::Source) {
25+
let caller: T::AccountId = whitelisted_caller();
26+
let caller_lookup = T::Lookup::unlookup(caller.clone());
27+
let data: Vec<u8> = vec![1, 0, 0, 0];
28+
let asset_id = T::AssetId::decode(&mut &*data).unwrap();
29+
let root = RawOrigin::Root.into();
30+
assert!(pallet_assets::Pallet::<T, I>::force_create(
31+
root,
32+
asset_id,
33+
caller_lookup.clone(),
34+
is_sufficient,
35+
1u32.into(),
36+
)
37+
.is_ok());
38+
(caller, caller_lookup)
39+
}
40+
41+
fn produce_validator_set<T: AppchainConfig>(index: u32) -> Observation<T::AccountId> {
42+
let receiver: T::AccountId = whitelisted_caller();
43+
let validator = Validator { validator_id_in_appchain: receiver, total_stake: 100 };
44+
Observation::UpdateValidatorSet(ValidatorSet { set_id: index, validators: vec![validator] })
45+
}
46+
47+
fn produce_burn_notification<T: AppchainConfig>(index: u32) -> Observation<T::AccountId> {
48+
let receiver: T::AccountId = whitelisted_caller();
49+
Observation::Burn(BurnEvent {
50+
index,
51+
sender_id: "pallet-test.testnet".to_string().as_bytes().to_vec(),
52+
receiver,
53+
amount: 10_000_000_000_000_000_000,
54+
})
55+
}
56+
57+
fn produce_lock_asset_notification<T: AppchainConfig>(
58+
index: u32,
59+
token_id: Vec<u8>,
60+
) -> Observation<T::AccountId> {
61+
let receiver: T::AccountId = whitelisted_caller();
62+
Observation::LockAsset(LockAssetEvent {
63+
index,
64+
token_id,
65+
sender_id: "pallet-test.testnet".to_string().as_bytes().to_vec(),
66+
receiver,
67+
amount: 100_000,
68+
})
69+
}
70+
71+
fn get_submitter_information<T: AppchainConfig>(
72+
) -> (<T as SigningTypes>::Public, <T as SigningTypes>::Signature, Vec<u8>) {
73+
const PHRASE: &str = "bottom drive obey lake curtain smoke basket hold race lonely fit walk";
74+
let key = <T::AppCrypto as AppCrypto<
75+
<T as SigningTypes>::Public,
76+
<T as SigningTypes>::Signature,
77+
>>::RuntimeAppPublic::generate_pair(Some(
78+
format!("{}//Alice", PHRASE).as_bytes().to_vec(),
79+
));
80+
let key_data = key.to_raw_vec();
81+
82+
let generic_public = <T::AppCrypto as AppCrypto<
83+
<T as SigningTypes>::Public,
84+
<T as SigningTypes>::Signature,
85+
>>::GenericPublic::from(key);
86+
let public: <T as SigningTypes>::Public = generic_public.into();
87+
let sig = <T::AppCrypto as AppCrypto<
88+
<T as SigningTypes>::Public,
89+
<T as SigningTypes>::Signature,
90+
>>::sign(&frame_benchmarking::vec![1, 2, 3], public.clone())
91+
.unwrap();
92+
(public, sig, key_data)
93+
}
94+
95+
benchmarks! {
96+
force_set_is_activated {
97+
let mut is_activated: bool = true;
98+
for i in 0 .. 100 {
99+
if i%2 == 0 {
100+
is_activated = true;
101+
} else {
102+
is_activated = false;
103+
}
104+
}
105+
}: {
106+
let _ = AppchainPallet::<T>::force_set_is_activated(RawOrigin::Root.into(), is_activated);
107+
}
108+
verify {
109+
assert_eq!(AppchainPallet::<T>::is_activated(), is_activated);
110+
}
111+
112+
force_set_next_set_id {
113+
let next_set_id:u32 = 1;
114+
}: {
115+
let _ = AppchainPallet::<T>::force_set_next_set_id(RawOrigin::Root.into(), next_set_id);
116+
}
117+
verify {
118+
assert_eq!(AppchainPallet::<T>::next_set_id(), next_set_id);
119+
}
120+
121+
force_set_planned_validators {
122+
let b in 2 .. 100u32;
123+
let mut validators: Vec<(T::AccountId, u128)> = Vec::new();
124+
for i in 1..b {
125+
validators.push((whitelisted_caller(), 100));
126+
}
127+
}: {
128+
let _ = AppchainPallet::<T>::force_set_planned_validators(RawOrigin::Root.into(), validators);
129+
}
130+
verify {
131+
assert_eq!(AppchainPallet::<T>::planned_validators().len(), (b-1) as usize);
132+
}
133+
134+
force_set_next_notification_id {
135+
let next_notification_id:u32 = 1;
136+
}: {
137+
let ret = AppchainPallet::<T>::force_set_next_notification_id(RawOrigin::Root.into(), next_notification_id);
138+
assert!(ret.is_ok());
139+
}
140+
141+
submit_observations {
142+
let b in 2 .. 10u32;
143+
let mut obs: Vec<Observation<<T as frame_system::Config>::AccountId>> = Vec::new();
144+
obs.push(produce_validator_set::<T>(1u32));
145+
create_default_asset::<T, pallet_assets::Instance1>(true);
146+
147+
for i in 1..b {
148+
if i%2 == 1 {
149+
obs.push(produce_lock_asset_notification::<T>(i, "usdn.testnet".to_string().as_bytes().to_vec()));
150+
} else {
151+
obs.push(produce_burn_notification::<T>(i));
152+
}
153+
}
154+
155+
let (public, signature, key_data) = get_submitter_information::<T>();
156+
let payload = ObservationsPayload {
157+
public,
158+
key_data,
159+
block_number: 1u32.into(),
160+
observations: obs,
161+
};
162+
163+
}: {
164+
let ret = AppchainPallet::<T>::submit_observations(RawOrigin::None.into(), payload, signature);
165+
assert!(ret.is_ok());
166+
}
167+
168+
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test, extra = false);
169+
}

0 commit comments

Comments
 (0)