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

entries, runtimes: Updates to have weights generated for entries #573

Merged
merged 3 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ members = [
"test-utils/runtime/client",
"test-utils/runtime/transaction-pool",
"test-utils/service",
"pallets/entries",

]
default-members = ["node/cli"]
Expand Down
3 changes: 2 additions & 1 deletion pallets/entries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ log = { workspace = true }
serde_json = { workspace = true }

frame-benchmarking = { optional = true, workspace = true }
pallet-namespace = { workspace = true }
pallet-registries = { workspace = true }
pallet-schema-accounts = { workspace = true }

Expand All @@ -56,7 +57,7 @@ std = [
"identifier/std",
"cord-primitives/std",
"enumflags2/std",
"frame-benchmarking?/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
Expand Down
182 changes: 182 additions & 0 deletions pallets/entries/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#![cfg(feature = "runtime-benchmarks")]

use super::*;
use codec::Encode;
use frame_benchmarking::{account, benchmarks};
use frame_support::sp_runtime::traits::Hash;
use frame_system::RawOrigin;
use identifier::{IdentifierType, Ss58Identifier};
use pallet_namespace::{NameSpaceCodeOf, NameSpaceIdOf};
use pallet_registries::{RegistryBlobOf, RegistryHashOf, RegistryIdOf};
use pallet_schema_accounts::{InputSchemaOf, SchemaHashOf, SchemaIdOf};
use serde_json::json;
use sp_std::prelude::*;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

pub fn generate_registry_id<T: Config>(digest: &RegistryHashOf<T>) -> RegistryIdOf {
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Registries).unwrap()
}

pub fn generate_authorization_id<T: Config>(
digest: &RegistryHashOf<T>,
) -> RegistryAuthorizationIdOf {
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::RegistryAuthorization)
.unwrap()
}

pub fn generate_schema_id<T: Config>(digest: &SchemaHashOf<T>) -> SchemaIdOf {
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::SchemaAccounts)
.unwrap()
}

pub fn generate_namespace_id<T: Config>(digest: &NameSpaceCodeOf<T>) -> NameSpaceIdOf {
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::NameSpace).unwrap()
}

pub fn generate_namespace_authorization_id<T: Config>(
digest: &NameSpaceCodeOf<T>,
) -> NamespaceAuthorizationIdOf {
Ss58Identifier::create_identifier(
&(digest).encode()[..],
IdentifierType::NameSpaceAuthorization,
)
.unwrap()
}

pub fn generate_registry_entry_id<T: Config>(digest: &RegistryEntryHashOf<T>) -> RegistryEntryIdOf {
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Entries).unwrap()
}

const SEED: u32 = 0;

benchmarks! {
where_clause {
where
T: pallet_namespace::Config,
T: pallet_registries::Config,
T: pallet_schema_accounts::Config,
T: frame_system::Config,
}


create {
let creator: T::AccountId = account("creator", 0, SEED);

let namespace = [1u8; 256].to_vec();
let namespace_digest = <T as frame_system::Config>::Hashing::hash(&namespace.encode()[..]);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..],
);
let namespace_id: NameSpaceIdOf = generate_namespace_id::<T>(&id_digest);

let namespace_auth_id_digest = <T as frame_system::Config>::Hashing::hash(
&[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);
let namespace_authorization_id: NamespaceAuthorizationIdOf = generate_namespace_authorization_id::<T>(&namespace_auth_id_digest);

let registry = [2u8; 256].to_vec();

let raw_blob = [2u8; 256].to_vec();
let blob: RegistryBlobOf<T> = BoundedVec::try_from(raw_blob)
.expect("Test blob should fit into the expected input length for the test runtime.");

let registry_digest = <T as frame_system::Config>::Hashing::hash(&registry.encode()[..]);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&registry_digest.encode()[..], &creator.encode()[..]].concat()[..],
);

let registry_id: RegistryIdOf = generate_registry_id::<T>(&id_digest);

let auth_id_digest = <T as frame_system::Config>::Hashing::hash(
&[&registry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..],
);

let authorization_id: RegistryAuthorizationIdOf = generate_authorization_id::<T>(&auth_id_digest);

let raw_schema = [2u8; 256].to_vec();
let schema: InputSchemaOf<T> = BoundedVec::try_from(raw_schema)
.expect("Test schema should fit into the expected input length for the test runtime.");
let schema_id_digest = <T as frame_system::Config>::Hashing::hash(&schema.encode()[..]);
let schema_id: SchemaIdOf = generate_schema_id::<T>(&schema_id_digest);

let registry_entry_json_object = json!({
"name": "Alice",
"age": 25,
"email": "[email protected]",
"isActive": true,
"address": {
"street": "Koramangala",
"city": "Bengaluru",
"zipcode": "560001"
},
"phoneNumbers": [
"+91-234787324",
"+91-283746823"
]
});

let registry_entry_json_string =
serde_json::to_string(&registry_entry_json_object).expect("Failed to serialize JSON");

let registry_entry_raw_bytes = registry_entry_json_string.as_bytes().to_vec();

let registry_entry_blob: RegistryEntryBlobOf<T> =
BoundedVec::try_from(registry_entry_raw_bytes.clone()).expect(
"Test Blob should fit into the expected input length of BLOB for the test runtime.",
);

let registry_entry_digest: RegistryHashOf<T> =
<T as frame_system::Config>::Hashing::hash(&registry_entry_raw_bytes.encode()[..]);

let registry_entry_id_digest = <T as frame_system::Config>::Hashing::hash(
&[
&registry_entry_digest.encode()[..],
&registry_id.encode()[..],
&creator.encode()[..],
]
.concat()[..],
);

let registry_entry_id: RegistryEntryIdOf =
generate_registry_entry_id::<T>(&registry_entry_id_digest);

pallet_namespace::Pallet::<T>::create(
RawOrigin::Signed(creator.clone()).into(),
namespace_digest,
None
)?;

pallet_registries::Pallet::<T>::create(
RawOrigin::Signed(creator.clone()).into(),
registry_digest,
namespace_authorization_id.clone(),
Some(schema_id.clone()),
Some(blob),
)?;

}: _<T::RuntimeOrigin>(
RawOrigin::Signed(creator.clone()).into(),
registry_entry_id.clone(),
authorization_id.clone(),
registry_entry_digest,
None
)

verify {
assert_last_event::<T>(
Event::RegistryEntryCreated {
creator: creator,
registry_id: registry_id,
registry_entry_id: registry_entry_id,
}
.into()
);
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
3 changes: 3 additions & 0 deletions pallets/entries/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

mod types;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

#[cfg(any(feature = "mock", test))]
pub mod mock;

Expand Down
1 change: 1 addition & 0 deletions runtimes/braid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ runtime-benchmarks = [
"pallet-asset-conversion/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-config/runtime-benchmarks",
"pallet-entries/runtime-benchmarks",
"pallet-registries/runtime-benchmarks",
]

Expand Down
2 changes: 2 additions & 0 deletions runtimes/braid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ mod benches {
[pallet_network_membership, NetworkMembership]
[pallet_network_score, NetworkScore]
[pallet_sudo, Sudo]
[pallet_registries, Registries]
[pallet_entries, Entries]
);
}

Expand Down
1 change: 1 addition & 0 deletions runtimes/loom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ runtime-benchmarks = [
"pallet-assets/runtime-benchmarks",
"pallet-config/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-entries/runtime-benchmarks",
"pallet-registries/runtime-benchmarks",
]

Expand Down
2 changes: 2 additions & 0 deletions runtimes/loom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,8 @@ mod benches {
[pallet_network_membership, NetworkMembership]
[pallet_network_score, NetworkScore]
[pallet_sudo, Sudo]
[pallet_registries, Registries]
[pallet_entries, Entries]
);
}

Expand Down
1 change: 1 addition & 0 deletions runtimes/weave/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ runtime-benchmarks = [
"pallet-config/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-registries/runtime-benchmarks",
"pallet-entries/runtime-benchmarks",
]

try-runtime = [
Expand Down
2 changes: 2 additions & 0 deletions runtimes/weave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,8 @@ mod benches {
[pallet_network_membership, NetworkMembership]
[pallet_network_score, NetworkScore]
[pallet_sudo, Sudo]
[pallet_registries, Registries]
[pallet_entries, Entries]
);
}

Expand Down
9 changes: 9 additions & 0 deletions scripts/run_benches_for_pallets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ fi
CORD=./target/production/cord

# Manually exclude some pallets.
# TODO: Add namespace after its benchmarking.rs file is ready
PALLETS=(
"pallet_registries"
"pallet_entries"
"pallet_chain_space"
"pallet_collective"
"pallet_did"
Expand Down Expand Up @@ -90,6 +93,12 @@ for PALLET in "${PALLETS[@]}"; do
fi

case $PALLET in
pallet_registries)
FOLDER="registries"
;;
pallet_entries)
FOLDER="entries"
;;
pallet_chain_space)
FOLDER="chain-space"
;;
Expand Down
Loading