Skip to content

Commit

Permalink
identifier and pallet updates (#262)
Browse files Browse the repository at this point in the history
* sdk update - v1.5.0

* sub-second block time

* node re-org

* node re-org modules

* test utils and tests

* clippy fixes - test utils and tests

* cargo workspace defaults

* runtime blocktime updates

* rustfmt +nightly

* rustfmt +nightly fix

* revert to rust nightly for ci

* fmt fixes

* minor constant fix

* Remove caching from CI and see if build passes

Signed-off-by: Amar Tumballi <amar@dhiway.com>

* rust nightly toolchain

* more tests with workflow file

Signed-off-by: Amar Tumballi <amar@dhiway.com>

* add rust-src as per feedback of failure

Signed-off-by: Amar Tumballi <amar@dhiway.com>

* Revert and Fix the github workflow

Signed-off-by: Amar Tumballi <amar@dhiway.com>

* Revert "Remove caching from CI and see if build passes"

This reverts commit 38da10b.

* fix clippy errors

Signed-off-by: Amar Tumballi <amar@dhiway.com>

* rust toolchain update

* rust toolchain updates

* rust fmt fixes

* runtime dir re-org

* DID try-state and toolchain defaults

* DID try-state and toolchain defaults -2

* clippy fixes

* skip rustfmt checks on runtime DID calls

* identifier updates

* identifier updates - pallets integration

* build fixes

---------

Signed-off-by: Amar Tumballi <amar@dhiway.com>
Co-authored-by: Amar Tumballi <amar@dhiway.com>
  • Loading branch information
smohan-dw and amarts authored Dec 29, 2023
1 parent d37bb29 commit 9218523
Show file tree
Hide file tree
Showing 21 changed files with 275 additions and 203 deletions.
7 changes: 2 additions & 5 deletions Cargo.lock

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

17 changes: 11 additions & 6 deletions pallets/asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use sp_runtime::traits::UniqueSaturatedInto;
#[frame_support::pallet]
pub mod pallet {
use super::*;
pub use cord_primitives::{curi::Ss58Identifier, CountOf, RatingOf};
pub use cord_primitives::{CountOf, RatingOf};
use frame_support::{pallet_prelude::*, Twox64Concat};
use frame_system::pallet_prelude::*;
pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier};
use sp_runtime::{
traits::{Hash, IdentifyAccount, Verify, Zero},
BoundedVec,
Expand Down Expand Up @@ -221,8 +222,9 @@ pub mod pallet {
&[&digest.encode()[..], &issuer.encode()[..]].concat()[..],
);

let identifier = Ss58Identifier::to_asset_id(&(id_digest).encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let identifier =
Ss58Identifier::create_identifier(&(id_digest).encode()[..], IdentifierType::Asset)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(!<Assets<T>>::contains_key(&identifier), Error::<T>::AssetIdAlreadyExists);

Expand Down Expand Up @@ -288,8 +290,11 @@ pub mod pallet {
.concat()[..],
);

let instance_id = Ss58Identifier::to_asset_instance_id(&(id_digest).encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let instance_id = Ss58Identifier::create_identifier(
&(id_digest).encode()[..],
IdentifierType::AssetInstance,
)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

let block_number = frame_system::Pallet::<T>::block_number();

Expand Down Expand Up @@ -391,7 +396,7 @@ impl<T: Config> Pallet<T> {
let tx_moment = Self::timepoint();

let tx_entry = EventEntryOf { action: tx_action, location: tx_moment };
let _ = identifier::Pallet::<T>::update_timeline(tx_id, IdentifierTypeOf::Asset, tx_entry);
let _ = IdentifierTimeline::update_timeline::<T>(tx_id, IdentifierTypeOf::Asset, tx_entry);
Ok(())
}

Expand Down
14 changes: 7 additions & 7 deletions pallets/chain-space/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use super::*;
use codec::Encode;
use cord_primitives::curi::Ss58Identifier;
use cord_utilities::traits::GenerateBenchmarkOrigin;
use frame_benchmarking::{account, benchmarks};
use frame_support::{pallet_prelude::EnsureOrigin, sp_runtime::traits::Hash};
use frame_support::sp_runtime::traits::Hash;
use frame_system::RawOrigin;
use identifier::{IdentifierType, Ss58Identifier};

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_space_id<T: Config>(digest: &SpaceCodeOf<T>) -> SpaceIdOf {
Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap()
}

pub fn generate_authorization_id<T: Config>(digest: &SpaceCodeOf<T>) -> AuthorizationIdOf {
Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization)
.unwrap()
}

const SEED: u32 = 0;
Expand All @@ -26,9 +27,8 @@ benchmarks! {
where_clause {
where
<T as Config>::EnsureOrigin: GenerateBenchmarkOrigin<T::RuntimeOrigin, T::AccountId, T::SpaceCreatorId>,
T::ChainSpaceOrigin: EnsureOrigin<T::RuntimeOrigin>,
// T::ChainSpaceOrigin: EnsureOrigin<T::RuntimeOrigin>,
}

add_delegate {
let caller: T::AccountId = account("caller", 0, SEED);
let did: T::SpaceCreatorId = account("did", 0, SEED);
Expand Down
22 changes: 14 additions & 8 deletions pallets/chain-space/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ pub type SpaceAuthorizationOf<T> = SpaceAuthorization<SpaceIdOf, SpaceCreatorOf<
#[frame_support::pallet]
pub mod pallet {
use super::*;
pub use cord_primitives::{curi::Ss58Identifier, StatusOf};
pub use cord_primitives::StatusOf;
use cord_utilities::traits::CallSources;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier};

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
Expand Down Expand Up @@ -515,8 +516,9 @@ pub mod pallet {
&[&space_code.encode()[..], &creator.encode()[..]].concat()[..],
);

let identifier = Ss58Identifier::to_space_id(&id_digest.encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let identifier =
Ss58Identifier::create_identifier(&id_digest.encode()[..], IdentifierType::Space)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(!<Spaces<T>>::contains_key(&identifier), Error::<T>::SpaceAlreadyAnchored);

Expand All @@ -526,8 +528,11 @@ pub mod pallet {
let auth_id_digest =
T::Hashing::hash(&[&identifier.encode()[..], &creator.encode()[..]].concat()[..]);

let authorization_id = Ss58Identifier::to_authorization_id(&auth_id_digest.encode())
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let authorization_id = Ss58Identifier::create_identifier(
&auth_id_digest.encode(),
IdentifierType::Authorization,
)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

let mut delegates: BoundedVec<SpaceCreatorOf<T>, T::MaxSpaceDelegates> =
BoundedVec::default();
Expand Down Expand Up @@ -933,8 +938,9 @@ impl<T: Config> Pallet<T> {
&[&space_id.encode()[..], &delegate.encode()[..], &creator.encode()[..]].concat()[..],
);

let delegate_authorization_id = Ss58Identifier::to_authorization_id(&id_digest.encode())
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let delegate_authorization_id =
Ss58Identifier::create_identifier(&id_digest.encode(), IdentifierType::Authorization)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(
!Authorizations::<T>::contains_key(&delegate_authorization_id),
Expand Down Expand Up @@ -1253,7 +1259,7 @@ impl<T: Config> Pallet<T> {
let tx_moment = Self::timepoint();

let tx_entry = EventEntryOf { action: tx_action, location: tx_moment };
let _ = identifier::Pallet::<T>::update_timeline(tx_id, tx_type, tx_entry);
let _ = IdentifierTimeline::update_timeline::<T>(tx_id, tx_type, tx_entry);
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions pallets/chain-space/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use sp_runtime::{traits::Hash, AccountId32};
use sp_std::prelude::*;

pub fn generate_space_id<T: Config>(digest: &SpaceCodeOf<T>) -> SpaceIdOf {
Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap()
}

pub fn generate_authorization_id<T: Config>(digest: &SpaceCodeOf<T>) -> AuthorizationIdOf {
Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization)
.unwrap()
}

pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32]));
Expand Down
31 changes: 17 additions & 14 deletions pallets/network-score/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@

use super::*;
use codec::Encode;
use cord_primitives::curi::Ss58Identifier;
use cord_utilities::traits::GenerateBenchmarkOrigin;
use frame_benchmarking::{account, benchmarks};
use frame_support::{sp_runtime::traits::Hash, BoundedVec};
use frame_system::RawOrigin;
use identifier::{IdentifierType, Ss58Identifier};
use pallet_chain_space::SpaceCodeOf;

const SEED: u32 = 0;
const MAX_PAYLOAD_BYTE_LENGTH: u32 = 15 * 1024;

pub fn generate_space_id<T: Config>(other_digest: &SpaceCodeOf<T>) -> SpaceIdOf {
Ss58Identifier::to_space_id(&(other_digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(other_digest).encode()[..], IdentifierType::Space).unwrap()
}

pub fn generate_rating_id<T: Config>(other_digest: &RatingEntryHashOf<T>) -> RatingEntryIdOf {
Ss58Identifier::to_scoring_id(&(other_digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(other_digest).encode()[..], IdentifierType::Rating).unwrap()
}

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

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
Expand Down Expand Up @@ -75,8 +80,7 @@ benchmarks! {
let auth_digest = <T as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &did1.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap();
let authorization_id: AuthorizationIdOf = generate_authorization_id::<T>(&auth_digest);

let origin = <T as pallet::Config>::EnsureOrigin::generate_origin(caller.clone(), did1.clone());

Expand All @@ -85,7 +89,7 @@ benchmarks! {
let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap();
let identifier = generate_rating_id::<T>(&id_digest);

let chain_space_origin = RawOrigin::Root.into();

Expand Down Expand Up @@ -129,18 +133,18 @@ benchmarks! {
let id_digest_add = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap();
let identifier_add = generate_rating_id::<T>(&id_digest_add);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did1.encode()[..]].concat()[..]
);
let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap();
let identifier_revoke = generate_rating_id::<T>(&id_digest);

let auth_digest = <T as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &did1.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap();
generate_authorization_id::<T>(&auth_digest);

let origin = <T as pallet::Config>::EnsureOrigin::generate_origin(caller.clone(), did1.clone());
let chain_space_origin = RawOrigin::Root.into();
Expand Down Expand Up @@ -202,23 +206,22 @@ benchmarks! {
let id_digest_add = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_add.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_add = Ss58Identifier::to_scoring_id(&id_digest_add.encode()[..]).unwrap();
let identifier_add = generate_rating_id::<T>(&id_digest_add);

let id_digest = <T as frame_system::Config>::Hashing::hash(
&[&(entry_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revoke.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_revoke = Ss58Identifier::to_scoring_id(&id_digest.encode()[..]).unwrap();
let identifier_revoke = generate_rating_id::<T>(&id_digest);

let id_digest_revise = <T as frame_system::Config>::Hashing::hash(
&[&(entry_revise_digest.clone()).encode()[..], &entity_uid.encode()[..], &message_id_revise.encode()[..], &space_id.encode()[..], &did.encode()[..]].concat()[..]
);
let identifier_revise = Ss58Identifier::to_scoring_id(&id_digest_revise.encode()[..]).unwrap();
let identifier_revise = generate_rating_id::<T>(&id_digest_revise);

let auth_digest = <T as frame_system::Config>::Hashing::hash(
&[&space_id.encode()[..], &did.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap();
let authorization_id: AuthorizationIdOf = generate_authorization_id::<T>(&auth_digest);

let origin = <T as pallet::Config>::EnsureOrigin::generate_origin(caller.clone(), did.clone());
let chain_space_origin = RawOrigin::Root.into();
Expand Down
26 changes: 18 additions & 8 deletions pallets/network-score/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ use sp_runtime::traits::UniqueSaturatedInto;
#[frame_support::pallet]
pub mod pallet {
use super::*;
pub use cord_primitives::{curi::Ss58Identifier, CountOf, RatingOf};
pub use cord_primitives::{CountOf, RatingOf};
use cord_utilities::traits::CallSources;
use frame_support::{pallet_prelude::*, Twox64Concat};
use frame_system::pallet_prelude::*;
pub use identifier::{IdentifierCreator, IdentifierTimeline, IdentifierType, Ss58Identifier};
use sp_runtime::traits::Hash;
use sp_std::{prelude::Clone, str};

Expand Down Expand Up @@ -404,8 +405,11 @@ pub mod pallet {
.concat()[..],
);

let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let identifier = Ss58Identifier::create_identifier(
&(id_digest).encode()[..],
IdentifierType::Rating,
)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(
!<RatingEntries<T>>::contains_key(&identifier),
Expand Down Expand Up @@ -528,8 +532,11 @@ pub mod pallet {
.concat()[..],
);

let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let identifier = Ss58Identifier::create_identifier(
&(id_digest).encode()[..],
IdentifierType::Rating,
)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(
!<RatingEntries<T>>::contains_key(&identifier),
Expand Down Expand Up @@ -683,8 +690,11 @@ pub mod pallet {
.concat()[..],
);

let identifier = Ss58Identifier::to_scoring_id(&(id_digest).encode()[..])
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;
let identifier = Ss58Identifier::create_identifier(
&(id_digest).encode()[..],
IdentifierType::Rating,
)
.map_err(|_| Error::<T>::InvalidIdentifierLength)?;

ensure!(
!<RatingEntries<T>>::contains_key(&identifier),
Expand Down Expand Up @@ -817,7 +827,7 @@ impl<T: Config> Pallet<T> {
let tx_moment = Self::timepoint();

let tx_entry = EventEntryOf { action: tx_action, location: tx_moment };
let _ = identifier::Pallet::<T>::update_timeline(tx_id, IdentifierTypeOf::Rating, tx_entry);
let _ = IdentifierTimeline::update_timeline::<T>(tx_id, IdentifierTypeOf::Rating, tx_entry);
Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions pallets/network-score/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ use sp_runtime::{traits::Hash, AccountId32};
use sp_std::prelude::*;

pub fn generate_rating_id<T: Config>(digest: &RatingEntryHashOf<T>) -> RatingEntryIdOf {
Ss58Identifier::to_scoring_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Rating).unwrap()
}

pub fn generate_space_id<T: Config>(digest: &SpaceCodeOf<T>) -> SpaceIdOf {
Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap()
}

pub(crate) const DID_00: SubjectId = SubjectId(AccountId32::new([1u8; 32]));
Expand Down Expand Up @@ -68,7 +68,8 @@ fn check_successful_rating_creation() {
&[&space_id.encode()[..], &creator.encode()[..]].concat()[..],
);
let authorization_id: AuthorizationIdOf =
Ss58Identifier::to_authorization_id(&auth_digest.encode()[..]).unwrap();
Ss58Identifier::create_identifier(&auth_digest.encode()[..], IdentifierType::Authorization)
.unwrap();

new_test_ext().execute_with(|| {
System::set_block_number(1);
Expand Down
7 changes: 4 additions & 3 deletions pallets/schema/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
}

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

/// Generates a space ID from a digest.
pub fn generate_space_id<T: Config>(digest: &SchemaHashOf<T>) -> SpaceIdOf {
Ss58Identifier::to_space_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Space).unwrap()
}

/// Generates an authorization ID from a digest.
pub fn generate_authorization_id<T: Config>(digest: &SchemaHashOf<T>) -> AuthorizationIdOf {
Ss58Identifier::to_authorization_id(&(digest).encode()[..]).unwrap()
Ss58Identifier::create_identifier(&(digest).encode()[..], IdentifierType::Authorization)
.unwrap()
}

benchmarks! {
Expand Down
Loading

0 comments on commit 9218523

Please sign in to comment.