Skip to content
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 on GitHub? Sign in to your account

fixing benchmarks #264

Merged
merged 7 commits into from
Jul 20, 2024
Merged
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
22 changes: 22 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ frame-benchmarking = { version = "35.0.0", default-features = false }
frame-support = { version = "35.0.0", default-features = false }
frame-system = { version = "35.0.0", default-features = false }
pallet-assets = { version = "36.0.0", default-features = false }
pallet-aura = { version = "34.0.0", default-features = false }
pallet-balances = { version = "36.0.0", default-features = false }
pallet-timestamp = { version = "34.0.0", default-features = false }
pallet-vesting = { version = "35.0.0", default-features = false }
Expand All @@ -72,5 +73,6 @@ substrate-fixed = { version = "0.5.9", default-features = false }
# dev-deps [std]
sp-keyring = { version = "38.0.0", default-features = false }
sp-externalities = { version = "0.29.0", default-features = false }
sp-consensus-aura = { version = "0.39.0", default-features = false }
polkadot-runtime-parachains = { version = "14.0.0" }
xcm-simulator = { version = "14.0.0" }
12 changes: 12 additions & 0 deletions enclave-bridge/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ benchmarks! {
// and we do more thorough checks in the normal cargo tests.
assert_eq!(frame_system::Pallet::<T>::events().len(), 1);
}

purge_enclave_from_shard_status {
let accounts: Vec<T::AccountId> = generate_accounts::<T>(1);
add_sovereign_enclaves_to_registry::<T>(&accounts);

let shard = ShardIdentifier::from(EnclaveFingerprint::default());
let shard_config = ShardConfig::new(EnclaveFingerprint::default());
// initialize
let _ = Pallet::<T>::update_shard_config(RawOrigin::Signed(accounts[0].clone()).into(), shard, shard_config.clone(), 0u32.into());
<ShardConfigRegistry<T>>::insert(shard, UpgradableShardConfig::from(shard_config.clone()));

}: _(RawOrigin::Root, shard, accounts[0].clone())
}

fn add_sovereign_enclaves_to_registry<T: Config>(accounts: &[T::AccountId]) {
Expand Down
1 change: 1 addition & 0 deletions primitives/teerdays/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{traits::AtLeast32BitUnsigned, Saturating};
Expand Down
15 changes: 12 additions & 3 deletions teeracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ sp-std = { workspace = true }
# benchmarking
frame-benchmarking = { workspace = true, optional = true }
hex-literal = { workspace = true, optional = true }
pallet-aura = { workspace = true, optional = true }
pallet-timestamp = { workspace = true, optional = true }
sp-consensus-aura = { workspace = true, optional = true }
test-utils = { default-features = false, path = "../test-utils", optional = true }

[dev-dependencies]
sp-externalities = { workspace = true }
frame-benchmarking = { workspace = true, features = ["std"] }
hex-literal = { workspace = true }
sp-keyring = { workspace = true }
pallet-aura = { workspace = true }
pallet-balances = { workspace = true, features = ["std"] }
test-utils = { path = "../test-utils" }
pallet-timestamp = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-externalities = { workspace = true }
sp-keyring = { workspace = true }
test-utils = { path = "../test-utils" }


[features]
Expand All @@ -55,13 +59,15 @@ std = [
"pallet-teerex/std",
"parity-scale-codec/std",
"scale-info/std",
"sp-consensus-aura?/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"substrate-fixed/std",
"teeracle-primitives/std",
"teerex-primitives/std",
"pallet-aura?/std",
"pallet-balances/std",
"pallet-timestamp?/std",
"sp-externalities/std",
Expand All @@ -71,18 +77,21 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"hex-literal",
"pallet-timestamp/runtime-benchmarks",
"pallet-aura",
"test-utils",
"pallet-teerex/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-consensus-aura",
"sp-runtime/runtime-benchmarks",
]

try-runtime = [
"frame-support/try-runtime",
"pallet-teerex/try-runtime",
"frame-system/try-runtime",
"pallet-aura?/try-runtime",
"pallet-balances/try-runtime",
"pallet-timestamp?/try-runtime",
"sp-runtime/try-runtime",
Expand Down
12 changes: 11 additions & 1 deletion teeracle/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ use test_utils::{
};

benchmarks! {
where_clause { where T::AccountId: From<[u8; 32]>, T::Hash: From<[u8; 32]> }
where_clause {
where
T::AccountId: From<[u8; 32]>,
T::Hash: From<[u8; 32]>,
T: pallet_aura::Config,
T::Moment: CheckedConversion,
}
update_exchange_rate {
<pallet_aura::CurrentSlot<T> as StorageValue<Slot>>::put(Slot::from(TEST4_SETUP.timestamp.saturating_div(T::SlotDuration::get().checked_into().unwrap())));
pallet_timestamp::Pallet::<T>::set_timestamp(TEST4_SETUP.timestamp.checked_into().unwrap());
let signer: T::AccountId = get_signer(TEST4_SETUP.signer_pub);
let trading_pair: TradingPairString = "DOT/USD".into();
Expand All @@ -60,6 +67,7 @@ benchmarks! {
}

update_oracle {
<pallet_aura::CurrentSlot<T> as StorageValue<Slot>>::put(Slot::from(TEST4_SETUP.timestamp.saturating_div(T::SlotDuration::get().checked_into().unwrap())));
pallet_timestamp::Pallet::<T>::set_timestamp(TEST4_SETUP.timestamp.checked_into().unwrap());
let signer: T::AccountId = get_signer(TEST4_SETUP.signer_pub);
let oracle_name = OracleDataName::from("Test_Oracle_Name");
Expand Down Expand Up @@ -107,6 +115,8 @@ use crate::{Config, Pallet as PalletModule};

#[cfg(test)]
use frame_benchmarking::impl_benchmark_test_suite;
use frame_support::{traits::Get, StorageValue};
use sp_consensus_aura::Slot;

#[cfg(test)]
impl_benchmark_test_suite!(PalletModule, crate::mock::new_test_ext(), crate::mock::Test,);
18 changes: 15 additions & 3 deletions teeracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

*/
use crate as pallet_teeracle;
use frame_support::{derive_impl, parameter_types};
use frame_support::{derive_impl, parameter_types, traits::ConstBool};
use frame_system as system;
use pallet_teeracle::Config;
use sp_core::H256;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{ConstU32, H256};
use sp_keyring::AccountKeyring;
use sp_runtime::{
generic,
Expand Down Expand Up @@ -47,6 +48,7 @@ pub type SignedExtra = (
frame_support::construct_runtime!(
pub enum Test
{
Aura: pallet_aura,
System: frame_system,
Balances: pallet_balances,
Timestamp: pallet_timestamp,
Expand Down Expand Up @@ -87,6 +89,16 @@ impl frame_system::Config for Test {
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

parameter_types! {
pub const SlotDuration: u64 = 6000;
}
impl pallet_aura::Config for Test {
type AuthorityId = AuraId;
type DisabledValidators = ();
type MaxAuthorities = ConstU32<32>;
type AllowMultipleBlocksPerSlot = ConstBool<false>;
type SlotDuration = SlotDuration;
}
pub type Balance = u64;

parameter_types! {
Expand Down Expand Up @@ -117,7 +129,7 @@ pub type Moment = u64;

impl pallet_timestamp::Config for Test {
type Moment = Moment;
type OnTimestampSet = ();
type OnTimestampSet = Aura;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see. This whole PR looks like an overkill, but I guess we can remove the feature flag now in our runtimes at least 🥳

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was more involved than I hoped, but I saw no other way to make the benchmarks work. Was there a better way? What feature flag do you mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uff, I see it is gone now: https://github.com/integritee-network/integritee-node/blob/1a18046482c72620898300e04b29fa7a72e7a3ff/runtime/src/lib.rs#L289

Here we used to have a feature flag that set OnTimestamp= () in case we were running the benchmark because Aura didn't like it when we messed with time. But I guess your implementation now, is better than the runtime hack we did before anyhow.

type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
}
Expand Down
15 changes: 11 additions & 4 deletions teeracle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

*/
use crate::{mock::*, ExchangeRates};
use frame_support::{assert_err, assert_noop, assert_ok};
use frame_support::{assert_err, assert_noop, assert_ok, StorageValue};
use hex_literal::hex;
use pallet_teerex::Error;
use sp_consensus_aura::Slot;
use sp_runtime::DispatchError::BadOrigin;
use substrate_fixed::types::U32F32;
use teeracle_primitives::*;
Expand All @@ -38,7 +39,7 @@ fn get_signer(pubkey: &[u8; 32]) -> AccountId {
}

fn register_ias_enclave_and_add_oracle_to_whitelist_ok(src: &str) {
Timestamp::set_timestamp(TEST4_TIMESTAMP);
set_timestamp(TEST4_TIMESTAMP);
let signer = get_signer(TEST4_SIGNER_PUB);
assert_ok!(Teerex::register_sgx_enclave(
RuntimeOrigin::signed(signer.clone()),
Expand All @@ -60,6 +61,12 @@ fn update_exchange_rate_dot_dollars_ok(src: &str, rate: Option<U32F32>) {
));
}

fn set_timestamp(moment: u64) {
<pallet_aura::CurrentSlot<Test> as StorageValue<Slot>>::put(Slot::from(
moment / SlotDuration::get(),
));
Timestamp::set_timestamp(moment);
}
#[test]
fn update_exchange_rate_works() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -222,7 +229,7 @@ fn update_oracle_from_not_registered_enclave_fails() {
#[test]
fn update_exchange_rate_from_not_whitelisted_oracle_fails() {
new_test_ext().execute_with(|| {
Timestamp::set_timestamp(TEST4_TIMESTAMP);
set_timestamp(TEST4_TIMESTAMP);
let signer = get_signer(TEST4_SIGNER_PUB);
assert_ok!(Teerex::register_sgx_enclave(
RuntimeOrigin::signed(signer.clone()),
Expand All @@ -247,7 +254,7 @@ fn update_exchange_rate_from_not_whitelisted_oracle_fails() {
#[test]
fn update_oracle_from_not_whitelisted_oracle_fails() {
new_test_ext().execute_with(|| {
Timestamp::set_timestamp(TEST4_TIMESTAMP);
set_timestamp(TEST4_TIMESTAMP);
let signer = get_signer(TEST4_SIGNER_PUB);
assert_ok!(Teerex::register_sgx_enclave(
RuntimeOrigin::signed(signer.clone()),
Expand Down
62 changes: 53 additions & 9 deletions teeracle/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.

*/
use frame_support::weights::Weight;
use frame_support::{traits::Get, weights::Weight};
use sp_std::marker::PhantomData;

/// Weight functions needed for pallet_exchange.
Expand All @@ -27,17 +27,61 @@ pub trait WeightInfo {

pub struct IntegriteeWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for IntegriteeWeight<T> {
fn add_to_whitelist() -> Weight {
Weight::from_parts(46_200_000, 0u64)
}
fn remove_from_whitelist() -> Weight {
Weight::from_parts(46_200_000, 0u64)
}
/// Storage: `Teerex::SovereignEnclaves` (r:1 w:0)
/// Proof: `Teerex::SovereignEnclaves` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Teeracle::Whitelists` (r:1 w:0)
/// Proof: `Teeracle::Whitelists` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Teeracle::ExchangeRates` (r:1 w:1)
/// Proof: `Teeracle::ExchangeRates` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn update_exchange_rate() -> Weight {
Weight::from_parts(46_200_000, 0u64)
// Proof Size summary in bytes:
// Measured: `454`
// Estimated: `3919`
// Minimum execution time: 44_730_000 picoseconds.
Weight::from_parts(49_230_000, 0)
.saturating_add(Weight::from_parts(0, 3919))
.saturating_add(T::DbWeight::get().reads(3))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Teerex::SovereignEnclaves` (r:1 w:0)
/// Proof: `Teerex::SovereignEnclaves` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Teeracle::Whitelists` (r:1 w:0)
/// Proof: `Teeracle::Whitelists` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Teeracle::OracleData` (r:0 w:1)
/// Proof: `Teeracle::OracleData` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn update_oracle() -> Weight {
Weight::from_parts(46_200_000, 0u64)
// Proof Size summary in bytes:
// Measured: `445`
// Estimated: `3910`
// Minimum execution time: 37_526_000 picoseconds.
Weight::from_parts(41_294_000, 0)
.saturating_add(Weight::from_parts(0, 3910))
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Teeracle::Whitelists` (r:1 w:1)
/// Proof: `Teeracle::Whitelists` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn add_to_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `6`
// Estimated: `3471`
// Minimum execution time: 17_640_000 picoseconds.
Weight::from_parts(19_529_000, 0)
.saturating_add(Weight::from_parts(0, 3471))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
/// Storage: `Teeracle::Whitelists` (r:1 w:1)
/// Proof: `Teeracle::Whitelists` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn remove_from_whitelist() -> Weight {
// Proof Size summary in bytes:
// Measured: `107`
// Estimated: `3572`
// Minimum execution time: 20_741_000 picoseconds.
Weight::from_parts(21_866_000, 0)
.saturating_add(Weight::from_parts(0, 3572))
.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1))
}
}
// For tests
Expand Down
1 change: 1 addition & 0 deletions teerdays/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#![cfg_attr(not(feature = "std"), no_std)]

pub use crate::weights::WeightInfo;
use frame_support::traits::{
Currency, InspectLockableCurrency, LockIdentifier, LockableCurrency, WithdrawReasons,
};
Expand Down
Loading
Loading