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

Data Preservers Profiles #551

Merged
merged 14 commits into from
May 23, 2024
14 changes: 8 additions & 6 deletions container-chains/nodes/frontier/src/rpc/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>.

use fc_rpc::frontier_backend_client::{self, is_canon};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use sp_blockchain::HeaderBackend;
use sp_core::H256;
use sp_runtime::traits::Block;
use std::{marker::PhantomData, sync::Arc};
use {
fc_rpc::frontier_backend_client::{self, is_canon},
jsonrpsee::{core::RpcResult, proc_macros::rpc},
sp_blockchain::HeaderBackend,
sp_core::H256,
sp_runtime::traits::Block,
std::{marker::PhantomData, sync::Arc},
};

#[rpc(server)]
#[async_trait::async_trait]
Expand Down
2 changes: 1 addition & 1 deletion container-chains/nodes/frontier/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ where
C::Api: RuntimeApiCollection,
P: TransactionPool<Block = Block> + 'static,
{
use finality::{FrontierFinality, FrontierFinalityApiServer};
use {
fc_rpc::{
Eth, EthApiServer, EthFilter, EthFilterApiServer, EthPubSub, EthPubSubApiServer, Net,
NetApiServer, Web3, Web3ApiServer,
},
finality::{FrontierFinality, FrontierFinalityApiServer},
substrate_frame_rpc_system::{System, SystemApiServer},
};

Expand Down
12 changes: 7 additions & 5 deletions container-chains/runtime-templates/frontier/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

use {
super::{
currency::MICROUNIT, precompiles::FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX, weights,
weights::xcm::XcmWeight as XcmGenericWeights, AccountId, AllPalletsWithSystem, AssetRate,
Balance, Balances, ForeignAssetsCreator, MaintenanceMode, MessageQueue, ParachainInfo,
ParachainSystem, PolkadotXcm, Runtime, RuntimeBlockWeights, RuntimeCall, RuntimeEvent,
RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue,
currency::MICROUNIT,
precompiles::FOREIGN_ASSET_PRECOMPILE_ADDRESS_PREFIX,
weights::{self, xcm::XcmWeight as XcmGenericWeights},
AccountId, AllPalletsWithSystem, AssetRate, Balance, Balances, ForeignAssetsCreator,
MaintenanceMode, MessageQueue, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime,
RuntimeBlockWeights, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee,
WeightToFee, XcmpQueue,
},
ccp_xcm::SignedToAccountKey20,
cumulus_primitives_core::{AggregateMessageOrigin, ParaId},
Expand Down
10 changes: 6 additions & 4 deletions container-chains/runtime-templates/simple/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

use {
super::{
currency::MICROUNIT, weights, weights::xcm::XcmWeight as XcmGenericWeights, AccountId,
AllPalletsWithSystem, AssetRate, Balance, Balances, ForeignAssetsCreator, MaintenanceMode,
MessageQueue, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeBlockWeights,
RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee, WeightToFee, XcmpQueue,
currency::MICROUNIT,
weights::{self, xcm::XcmWeight as XcmGenericWeights},
AccountId, AllPalletsWithSystem, AssetRate, Balance, Balances, ForeignAssetsCreator,
MaintenanceMode, MessageQueue, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime,
RuntimeBlockWeights, RuntimeCall, RuntimeEvent, RuntimeOrigin, TransactionByteFee,
WeightToFee, XcmpQueue,
},
cumulus_primitives_core::{AggregateMessageOrigin, ParaId},
frame_support::{
Expand Down
233 changes: 229 additions & 4 deletions pallets/data-preservers/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,39 @@

//! Benchmarking
use {
crate::{Call, Config, Pallet},
crate::{
Call, Config, Pallet, ParaIdsFilter, Profile, ProfileDeposit, ProfileMode, Profiles,
RegisteredProfile,
},
frame_benchmarking::v2::*,
frame_support::{
traits::{EnsureOriginWithArg, OriginTrait},
traits::{
fungible::{Inspect, Mutate},
EnsureOrigin, EnsureOriginWithArg, OriginTrait,
},
BoundedVec,
},
frame_system::RawOrigin,
sp_runtime::traits::Zero,
sp_std::vec,
tp_traits::ParaId,
};

#[benchmarks]
const SEED: u32 = 0;

fn create_funded_user<T: Config>(string: &'static str, n: u32, balance_factor: u32) -> T::AccountId
where
T::Currency: Mutate<T::AccountId>,
{
let user = account(string, n, SEED);
let balance = <T::Currency>::minimum_balance() * balance_factor.into();
let _ = <T::Currency>::set_balance(&user, balance);
user
}

#[benchmarks(
where T::Currency: Mutate<T::AccountId>, T::ProfileId: Zero
)]
mod benchmarks {
use super::*;

Expand All @@ -55,5 +76,209 @@ mod benchmarks {
assert_eq!(Pallet::<T>::boot_nodes(para_id), boot_nodes);
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
#[benchmark]
fn create_profile(x: Linear<1, 200>, y: Linear<1, 10>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the weights, none of the benchmarks depend on x or y, that's great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the runtime weigts it seems to use x and y but with very small multipliers. Should we simplify and remove those parameters?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would leave it, or maybe use one param which is "profile size", but it's fine

// x: url len, y: para ids len
let url = BoundedVec::try_from(vec![b'A'; x as usize]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); y as usize]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let deposit = T::ProfileDeposit::profile_deposit(&profile).expect("deposit to be computed");

let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);

#[extrinsic_call]
Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()), profile.clone());

assert_eq!(
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit,
profile
})
);
}

#[benchmark]
fn force_create_profile(x: Linear<1, 200>, y: Linear<1, 10>) {
// x: url len, y: para ids len
let url = BoundedVec::try_from(vec![b'A'; x as usize]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); y as usize]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let owner = create_funded_user::<T>("owner", 1, 1_000_000_000u32);
let origin_force = T::ForceSetProfileOrigin::try_successful_origin()
.expect("failed to create ForceSetProfileOrigin");

#[extrinsic_call]
Pallet::<T>::force_create_profile(
origin_force as T::RuntimeOrigin,
profile.clone(),
owner.clone(),
);

assert_eq!(
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: owner,
deposit: 0u32.into(),
profile
})
);
}

#[benchmark]
fn update_profile(x: Linear<1, 200>, y: Linear<1, 10>) {
let url = BoundedVec::try_from(vec![b'A'; 10]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); 2]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);

Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()).into(), profile)
.expect("to create profile");

// x: url len, y: para ids len
let url = BoundedVec::try_from(vec![b'B'; x as usize]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(43); y as usize]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let deposit = T::ProfileDeposit::profile_deposit(&profile).expect("deposit to be computed");

#[extrinsic_call]
Pallet::<T>::update_profile(
RawOrigin::Signed(caller.clone()),
T::ProfileId::zero(),
profile.clone(),
);

assert_eq!(
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit,
profile
})
);
}

#[benchmark]
fn force_update_profile(x: Linear<1, 200>, y: Linear<1, 10>) {
let url = BoundedVec::try_from(vec![b'A'; 10]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); 2]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);

Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()).into(), profile)
.expect("to create profile");

// x: url len, y: para ids len
let url = BoundedVec::try_from(vec![b'B'; x as usize]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(43); y as usize]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let origin_force = T::ForceSetProfileOrigin::try_successful_origin()
.expect("failed to create ForceSetProfileOrigin");

#[extrinsic_call]
Pallet::<T>::force_update_profile(
origin_force as T::RuntimeOrigin,
T::ProfileId::zero(),
profile.clone(),
);

assert_eq!(
Profiles::<T>::get(T::ProfileId::zero()),
Some(RegisteredProfile {
account: caller,
deposit: 0u32.into(),
profile
})
);
}

#[benchmark]
fn delete_profile() {
let url = BoundedVec::try_from(vec![b'A'; 10]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); 2]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);

Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()).into(), profile)
.expect("to create profile");

#[extrinsic_call]
Pallet::<T>::delete_profile(RawOrigin::Signed(caller.clone()), T::ProfileId::zero());

assert_eq!(Profiles::<T>::get(T::ProfileId::zero()), None);
}

#[benchmark]
fn force_delete_profile() {
let url = BoundedVec::try_from(vec![b'A'; 10]).unwrap();
let para_ids = BoundedVec::try_from(vec![ParaId::from(42); 2]).unwrap();

let profile = Profile {
url,
para_ids: ParaIdsFilter::Whitelist(para_ids),
mode: ProfileMode::Bootnode,
};

let caller = create_funded_user::<T>("caller", 1, 1_000_000_000u32);

Pallet::<T>::create_profile(RawOrigin::Signed(caller.clone()).into(), profile)
.expect("to create profile");

let origin_force = T::ForceSetProfileOrigin::try_successful_origin()
.expect("failed to create ForceSetProfileOrigin");

#[extrinsic_call]
Pallet::<T>::force_delete_profile(origin_force as T::RuntimeOrigin, T::ProfileId::zero());

assert_eq!(Profiles::<T>::get(T::ProfileId::zero()), None);
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::ExtBuilder::default().build(),
crate::mock::Test
);
}
Loading
Loading