From 635961989355e836457ac3c2be91aba391f7cfbd Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Mon, 27 Jan 2025 16:52:26 +0530 Subject: [PATCH 1/3] runtime: Add entries to list of benchmarking pallets Signed-off-by: Shreevatsa N --- Cargo.toml | 1 + runtimes/braid/Cargo.toml | 1 + runtimes/braid/src/lib.rs | 2 ++ runtimes/loom/Cargo.toml | 1 + runtimes/loom/src/lib.rs | 2 ++ runtimes/weave/Cargo.toml | 1 + runtimes/weave/src/lib.rs | 2 ++ 7 files changed, 10 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 221f20664..98dfede9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,7 @@ members = [ "test-utils/runtime/client", "test-utils/runtime/transaction-pool", "test-utils/service", + "pallets/entries", ] default-members = ["node/cli"] diff --git a/runtimes/braid/Cargo.toml b/runtimes/braid/Cargo.toml index c66a467c1..deb630542 100644 --- a/runtimes/braid/Cargo.toml +++ b/runtimes/braid/Cargo.toml @@ -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", ] diff --git a/runtimes/braid/src/lib.rs b/runtimes/braid/src/lib.rs index 71e1cf2bc..397f15d60 100644 --- a/runtimes/braid/src/lib.rs +++ b/runtimes/braid/src/lib.rs @@ -1188,6 +1188,8 @@ mod benches { [pallet_network_membership, NetworkMembership] [pallet_network_score, NetworkScore] [pallet_sudo, Sudo] + [pallet_registries, Registries] + [pallet_entries, Entries] ); } diff --git a/runtimes/loom/Cargo.toml b/runtimes/loom/Cargo.toml index 594e98414..abd8e050a 100644 --- a/runtimes/loom/Cargo.toml +++ b/runtimes/loom/Cargo.toml @@ -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", ] diff --git a/runtimes/loom/src/lib.rs b/runtimes/loom/src/lib.rs index f908cbcdb..941b363b6 100644 --- a/runtimes/loom/src/lib.rs +++ b/runtimes/loom/src/lib.rs @@ -1354,6 +1354,8 @@ mod benches { [pallet_network_membership, NetworkMembership] [pallet_network_score, NetworkScore] [pallet_sudo, Sudo] + [pallet_registries, Registries] + [pallet_entries, Entries] ); } diff --git a/runtimes/weave/Cargo.toml b/runtimes/weave/Cargo.toml index edc309f8f..479afa96a 100644 --- a/runtimes/weave/Cargo.toml +++ b/runtimes/weave/Cargo.toml @@ -247,6 +247,7 @@ runtime-benchmarks = [ "pallet-config/runtime-benchmarks", "pallet-treasury/runtime-benchmarks", "pallet-registries/runtime-benchmarks", + "pallet-entries/runtime-benchmarks", ] try-runtime = [ diff --git a/runtimes/weave/src/lib.rs b/runtimes/weave/src/lib.rs index d2b538c0f..f8fdf326e 100644 --- a/runtimes/weave/src/lib.rs +++ b/runtimes/weave/src/lib.rs @@ -1348,6 +1348,8 @@ mod benches { [pallet_network_membership, NetworkMembership] [pallet_network_score, NetworkScore] [pallet_sudo, Sudo] + [pallet_registries, Registries] + [pallet_entries, Entries] ); } From 56fe65486646a85ddfdb7949e894165367a290fb Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Mon, 27 Jan 2025 16:52:49 +0530 Subject: [PATCH 2/3] scripts: Add entries into list of benchmarks Signed-off-by: Shreevatsa N --- scripts/run_benches_for_pallets.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/run_benches_for_pallets.sh b/scripts/run_benches_for_pallets.sh index bb9e5b2f8..95fbfb0d3 100755 --- a/scripts/run_benches_for_pallets.sh +++ b/scripts/run_benches_for_pallets.sh @@ -51,14 +51,17 @@ done if [ "$skip_build" != true ]; then echo "[+] Compiling CORD benchmarks..." - cargo build --profile=production --locked --features=runtime-benchmarks --bin cord + cargo build --profile=release --locked --features=runtime-benchmarks --bin cord fi # The executable to use. -CORD=./target/production/cord +CORD=./target/release/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" @@ -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" ;; From ea4d42cfc827d6ea38888f27693f0da61fbeb71f Mon Sep 17 00:00:00 2001 From: Shreevatsa N Date: Mon, 27 Jan 2025 16:53:58 +0530 Subject: [PATCH 3/3] entries: Changes for add weights Signed-off-by: Shreevatsa N --- pallets/entries/Cargo.toml | 3 +- pallets/entries/src/benchmarking.rs | 182 ++++++++++++++++++++++++++++ pallets/entries/src/lib.rs | 3 + scripts/run_benches_for_pallets.sh | 4 +- 4 files changed, 189 insertions(+), 3 deletions(-) create mode 100644 pallets/entries/src/benchmarking.rs diff --git a/pallets/entries/Cargo.toml b/pallets/entries/Cargo.toml index 9116238c1..7a3939edb 100644 --- a/pallets/entries/Cargo.toml +++ b/pallets/entries/Cargo.toml @@ -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 } @@ -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", diff --git a/pallets/entries/src/benchmarking.rs b/pallets/entries/src/benchmarking.rs new file mode 100644 index 000000000..fbe5c6a67 --- /dev/null +++ b/pallets/entries/src/benchmarking.rs @@ -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(generic_event: ::RuntimeEvent) { + frame_system::Pallet::::assert_last_event(generic_event.into()); +} + +pub fn generate_registry_id(digest: &RegistryHashOf) -> RegistryIdOf { + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Registries).unwrap() +} + +pub fn generate_authorization_id( + digest: &RegistryHashOf, +) -> RegistryAuthorizationIdOf { + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::RegistryAuthorization) + .unwrap() +} + +pub fn generate_schema_id(digest: &SchemaHashOf) -> SchemaIdOf { + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::SchemaAccounts) + .unwrap() +} + +pub fn generate_namespace_id(digest: &NameSpaceCodeOf) -> NameSpaceIdOf { + Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::NameSpace).unwrap() +} + +pub fn generate_namespace_authorization_id( + digest: &NameSpaceCodeOf, +) -> NamespaceAuthorizationIdOf { + Ss58Identifier::create_identifier( + &(digest).encode()[..], + IdentifierType::NameSpaceAuthorization, + ) + .unwrap() +} + +pub fn generate_registry_entry_id(digest: &RegistryEntryHashOf) -> 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 = ::Hashing::hash(&namespace.encode()[..]); + + let id_digest = ::Hashing::hash( + &[&namespace_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_id: NameSpaceIdOf = generate_namespace_id::(&id_digest); + + let namespace_auth_id_digest = ::Hashing::hash( + &[&namespace_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + let namespace_authorization_id: NamespaceAuthorizationIdOf = generate_namespace_authorization_id::(&namespace_auth_id_digest); + + let registry = [2u8; 256].to_vec(); + + let raw_blob = [2u8; 256].to_vec(); + let blob: RegistryBlobOf = BoundedVec::try_from(raw_blob) + .expect("Test blob should fit into the expected input length for the test runtime."); + + let registry_digest = ::Hashing::hash(®istry.encode()[..]); + + let id_digest = ::Hashing::hash( + &[®istry_digest.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let registry_id: RegistryIdOf = generate_registry_id::(&id_digest); + + let auth_id_digest = ::Hashing::hash( + &[®istry_id.encode()[..], &creator.encode()[..], &creator.encode()[..]].concat()[..], + ); + + let authorization_id: RegistryAuthorizationIdOf = generate_authorization_id::(&auth_id_digest); + + let raw_schema = [2u8; 256].to_vec(); + let schema: InputSchemaOf = BoundedVec::try_from(raw_schema) + .expect("Test schema should fit into the expected input length for the test runtime."); + let schema_id_digest = ::Hashing::hash(&schema.encode()[..]); + let schema_id: SchemaIdOf = generate_schema_id::(&schema_id_digest); + + let registry_entry_json_object = json!({ + "name": "Alice", + "age": 25, + "email": "alice@dhiway.com", + "isActive": true, + "address": { + "street": "Koramangala", + "city": "Bengaluru", + "zipcode": "560001" + }, + "phoneNumbers": [ + "+91-234787324", + "+91-283746823" + ] + }); + + let registry_entry_json_string = + serde_json::to_string(®istry_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 = + 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 = + ::Hashing::hash(®istry_entry_raw_bytes.encode()[..]); + + let registry_entry_id_digest = ::Hashing::hash( + &[ + ®istry_entry_digest.encode()[..], + ®istry_id.encode()[..], + &creator.encode()[..], + ] + .concat()[..], + ); + + let registry_entry_id: RegistryEntryIdOf = + generate_registry_entry_id::(®istry_entry_id_digest); + + pallet_namespace::Pallet::::create( + RawOrigin::Signed(creator.clone()).into(), + namespace_digest, + None + )?; + + pallet_registries::Pallet::::create( + RawOrigin::Signed(creator.clone()).into(), + registry_digest, + namespace_authorization_id.clone(), + Some(schema_id.clone()), + Some(blob), + )?; + + }: _( + RawOrigin::Signed(creator.clone()).into(), + registry_entry_id.clone(), + authorization_id.clone(), + registry_entry_digest, + None + ) + + verify { + assert_last_event::( + 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); +} diff --git a/pallets/entries/src/lib.rs b/pallets/entries/src/lib.rs index 891eb68ce..793b41abb 100644 --- a/pallets/entries/src/lib.rs +++ b/pallets/entries/src/lib.rs @@ -39,6 +39,9 @@ mod types; +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; + #[cfg(any(feature = "mock", test))] pub mod mock; diff --git a/scripts/run_benches_for_pallets.sh b/scripts/run_benches_for_pallets.sh index 95fbfb0d3..837248097 100755 --- a/scripts/run_benches_for_pallets.sh +++ b/scripts/run_benches_for_pallets.sh @@ -51,11 +51,11 @@ done if [ "$skip_build" != true ]; then echo "[+] Compiling CORD benchmarks..." - cargo build --profile=release --locked --features=runtime-benchmarks --bin cord + cargo build --profile=production --locked --features=runtime-benchmarks --bin cord fi # The executable to use. -CORD=./target/release/cord +CORD=./target/production/cord # Manually exclude some pallets. # TODO: Add namespace after its benchmarking.rs file is ready