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

feat: DID support for service endpoints #276

Merged
merged 50 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
5bf70b9
wip: add support for service endpoints
ntn-x2 Oct 20, 2021
77760db
feat: add support for service endpoints in DID pallet
ntn-x2 Oct 20, 2021
03339c1
chore: refactor
ntn-x2 Oct 20, 2021
46d1523
wip: tests and benchmarks for new feature
ntn-x2 Oct 20, 2021
ea9bbfe
test: add unit test cases for new features
ntn-x2 Oct 21, 2021
24953c5
bench: add benchmarks for service endpoints
ntn-x2 Oct 21, 2021
ab3df16
chore: refactoring
ntn-x2 Oct 21, 2021
29077a9
chore: add some more checks
ntn-x2 Oct 21, 2021
e665543
wip: add checks for ASCII characters
ntn-x2 Oct 21, 2021
68db99a
feat: add input check for ASCII characters
ntn-x2 Oct 21, 2021
e96be02
chore: unused errors
ntn-x2 Oct 21, 2021
56fe178
feat: migrate everything to bounded vectors
ntn-x2 Oct 21, 2021
571c1bb
chore: --all build working
ntn-x2 Oct 21, 2021
7099f46
chore: build warning in delegation pallet
ntn-x2 Oct 21, 2021
162d0eb
wip: benchmark weights
ntn-x2 Oct 21, 2021
851fa90
wip: using the new weights
ntn-x2 Oct 21, 2021
2609392
chore: rename DID pallet configuration parameters
ntn-x2 Oct 22, 2021
b3b2a5c
bench: temporary remove benchmarks from runtime
ntn-x2 Oct 22, 2021
bc027d9
bench: re-run benchmarks with step=1
ntn-x2 Oct 22, 2021
ab4a925
feat: new storage design
ntn-x2 Oct 22, 2021
a37b0fa
chore: minor improvements
ntn-x2 Oct 22, 2021
711fad9
Merge commit 'a32b631449f4b7079e36c12576a95f7fcde8dc40' into aa-new-d…
ntn-x2 Oct 22, 2021
359f943
chore: refactoring
ntn-x2 Oct 22, 2021
6f51968
chore: improving benchmarks
ntn-x2 Oct 22, 2021
6fdfba1
chore: improving benchmarks pt. 2
ntn-x2 Oct 22, 2021
c00a3e9
chore: improving benchmarks pt. 3
ntn-x2 Oct 22, 2021
0bbe221
fix: Box DidCreationDetails to stay within call limits
ntn-x2 Oct 22, 2021
5d74ee0
bench: update default benchmarks
ntn-x2 Oct 22, 2021
9a81ca2
chore: comments
ntn-x2 Oct 22, 2021
6f24492
chore: reword the failure comment
ntn-x2 Oct 25, 2021
c6301e5
chore: make DidEndpointDetails fields public
ntn-x2 Oct 25, 2021
9323dab
chore: rename DidEndpointDetails to DidEndpoint
ntn-x2 Oct 25, 2021
14dc907
feat: add parameter for benchmarks
ntn-x2 Oct 25, 2021
37f45db
chore: replace OptionQuery with ValueQuery for DidServicesCount stora…
ntn-x2 Oct 25, 2021
847e4ea
chore: use .is_zero()
ntn-x2 Oct 25, 2021
b387043
chore: fmt
ntn-x2 Oct 25, 2021
96c0e17
chore: mark all safe sections
ntn-x2 Oct 25, 2021
36c92e3
test: add tests + move DID removal after storage prefix deletion
ntn-x2 Oct 25, 2021
d3908ac
Merge branch 'develop' into aa-new-did-service-endpoints
ntn-x2 Oct 27, 2021
fccda30
replace function in try_for_each
ntn-x2 Oct 27, 2021
29c7e33
chore: replace try_for_each to make it easier to read for some 👴
ntn-x2 Oct 27, 2021
a7ddb29
fix: remove endpoint count parameter from service endpoint removal ex…
ntn-x2 Oct 27, 2021
a1f1e1a
bench: re-run benchmarks without parameter for remove_service_endpoin…
ntn-x2 Oct 27, 2021
278369b
Merge branch 'develop' into aa-new-did-service-endpoints
ntn-x2 Oct 27, 2021
bb7f2df
Merge branch 'develop' into aa-new-did-service-endpoints
ntn-x2 Oct 27, 2021
68b11f5
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Oct 27, 2021
0761ad2
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Oct 27, 2021
889784b
cargo run --quiet --release -p kilt-parachain --features=runtime-benc…
Oct 27, 2021
01d44fd
Revert "cargo run --quiet --release -p kilt-parachain --features=runt…
ntn-x2 Oct 27, 2021
2f8ff41
Merge branch 'develop' into aa-new-did-service-endpoints
weichweich Oct 28, 2021
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 pallets/did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ runtime-benchmarks = [
"frame-benchmarking",
"pallet-balances/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"codec/std",
Expand Down
190 changes: 184 additions & 6 deletions pallets/did/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use sp_std::{convert::TryInto, vec::Vec};

use crate::{
did_details::*,
mock_utils::{generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys},
mock_utils::{
generate_base_did_creation_details, generate_base_did_details, get_key_agreement_keys, get_service_endpoints,
},
*,
};

Expand Down Expand Up @@ -100,6 +102,13 @@ fn generate_base_did_call_operation<T: Config>(
}
}

fn save_service_endpoints<T: Config>(did_subject: &DidIdentifierOf<T>, endpoints: &[DidEndpoint<T>]) {
for endpoint in endpoints.iter() {
ServiceEndpoints::<T>::insert(&did_subject, &endpoint.id, endpoint.clone());
}
DidEndpointsCount::<T>::insert(&did_subject, endpoints.len().saturated_into::<u32>());
}

benchmarks! {
where_clause {
where
Expand All @@ -110,6 +119,9 @@ benchmarks! {
/* create extrinsic */
create_ed25519_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
// We only calculate weights based on how many endpoints are specified. For each endpoint, we use the max possible length and count for its components.
// This makes weight computation easier at runtime, at the cost of always having worst-case weights for any # of endpoints c.
let c in 1 .. T::MaxNumberOfServicesPerDid::get();

let submitter: AccountIdOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);

Expand All @@ -120,14 +132,23 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys::<T>(n);
let did_public_att_key = get_ed25519_public_attestation_key();
let did_public_del_key = get_ed25519_public_delegation_key();
let service_endpoints = get_service_endpoints::<T>(
c,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);

let mut did_creation_details = generate_base_did_creation_details::<T>(did_subject.clone(), submitter.clone());
did_creation_details.new_key_agreement_keys = did_key_agreement_keys;
did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key));
did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key));
did_creation_details.new_service_details = service_endpoints.clone();

let did_creation_signature = ed25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ed25519 signature.");
}: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature))
}: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature))
verify {
let stored_did = Did::<T>::get(&did_subject).expect("New DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.key_agreement_keys;
Expand All @@ -152,11 +173,20 @@ benchmarks! {
stored_did.attestation_key,
Some(expected_attestation_key_id)
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject).saturated_into::<usize>(),
service_endpoints.len()
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
service_endpoints.len()
);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

create_sr25519_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
let c in 1 .. T::MaxNumberOfServicesPerDid::get();

let submitter: AccountIdOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);
make_free_for_did::<T>(&submitter);
Expand All @@ -166,14 +196,23 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys::<T>(n);
let did_public_att_key = get_sr25519_public_attestation_key();
let did_public_del_key = get_sr25519_public_delegation_key();
let service_endpoints = get_service_endpoints::<T>(
c,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);

let mut did_creation_details = generate_base_did_creation_details::<T>(did_subject.clone(), submitter.clone());
did_creation_details.new_key_agreement_keys = did_key_agreement_keys;
did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key));
did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key));
did_creation_details.new_service_details = service_endpoints.clone();

let did_creation_signature = sr25519_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw sr25519 signature.");
}: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature))
}: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature))
verify {
let stored_did = Did::<T>::get(&did_subject).expect("New DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.key_agreement_keys;
Expand All @@ -198,11 +237,20 @@ benchmarks! {
stored_did.attestation_key,
Some(expected_attestation_key_id)
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject).saturated_into::<usize>(),
service_endpoints.len()
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
service_endpoints.len()
);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

create_ecdsa_keys {
let n in 1 .. T::MaxNewKeyAgreementKeys::get();
let c in 1 .. T::MaxNumberOfServicesPerDid::get();

let submitter: AccountIdOf<T> = account(DEFAULT_ACCOUNT_ID, 0, DEFAULT_ACCOUNT_SEED);
make_free_for_did::<T>(&submitter);
Expand All @@ -212,14 +260,23 @@ benchmarks! {
let did_key_agreement_keys = get_key_agreement_keys::<T>(n);
let did_public_att_key = get_ecdsa_public_attestation_key();
let did_public_del_key = get_ecdsa_public_delegation_key();
let service_endpoints = get_service_endpoints::<T>(
c,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);

let mut did_creation_details = generate_base_did_creation_details::<T>(did_subject.clone(), submitter.clone());
did_creation_details.new_key_agreement_keys = did_key_agreement_keys;
did_creation_details.new_attestation_key = Some(DidVerificationKey::from(did_public_att_key.clone()));
did_creation_details.new_delegation_key = Some(DidVerificationKey::from(did_public_del_key.clone()));
did_creation_details.new_service_details = service_endpoints.clone();

let did_creation_signature = ecdsa_sign(AUTHENTICATION_KEY_ID, &did_public_auth_key, did_creation_details.encode().as_ref()).expect("Failed to create DID signature from raw ecdsa signature.");
}: create(RawOrigin::Signed(submitter), did_creation_details.clone(), DidSignature::from(did_creation_signature))
}: create(RawOrigin::Signed(submitter), Box::new(did_creation_details.clone()), DidSignature::from(did_creation_signature))
verify {
let stored_did = Did::<T>::get(&did_subject).expect("New DID should be stored on chain.");
let stored_key_agreement_keys_ids = stored_did.key_agreement_keys;
Expand All @@ -244,33 +301,79 @@ benchmarks! {
stored_did.attestation_key,
Some(expected_attestation_key_id)
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject).saturated_into::<usize>(),
service_endpoints.len()
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
service_endpoints.len()
);
assert_eq!(stored_did.last_tx_counter, 0u64);
}

delete {
let c in 1 .. T::MaxNumberOfServicesPerDid::get();

let did_public_auth_key = get_ed25519_public_authentication_key();
let did_subject: DidIdentifierOf<T> = MultiSigner::from(did_public_auth_key).into_account().into();

let did_details = generate_base_did_details::<T>(DidVerificationKey::from(did_public_auth_key));
let service_endpoints = get_service_endpoints::<T>(
c,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);

Did::<T>::insert(&did_subject, did_details);
}: _(RawOrigin::Signed(did_subject.clone()))
save_service_endpoints(&did_subject, &service_endpoints);
}: _(RawOrigin::Signed(did_subject.clone()), c)
verify {
assert!(
Did::<T>::get(&did_subject).is_none()
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject), 0
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
0
);
}

reclaim_deposit {
let c in 1 .. T::MaxNumberOfServicesPerDid::get();

let did_public_auth_key = get_ed25519_public_authentication_key();
let did_subject: DidIdentifierOf<T> = MultiSigner::from(did_public_auth_key).into_account().into();

let did_details = generate_base_did_details::<T>(DidVerificationKey::from(did_public_auth_key));
let service_endpoints = get_service_endpoints::<T>(
c,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);

Did::<T>::insert(&did_subject, did_details.clone());
}: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone())
save_service_endpoints(&did_subject, &service_endpoints);
}: _(RawOrigin::Signed(did_details.deposit.owner.clone()), did_subject.clone(), c)
verify {
assert!(
Did::<T>::get(&did_subject).is_none()
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject), 0
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
0
);
}

/* submit_did_call extrinsic */
Expand Down Expand Up @@ -764,6 +867,81 @@ benchmarks! {
assert!(!Did::<T>::get(&did_subject).unwrap().key_agreement_keys.contains(&key_agreement_key_id));
}

add_service_endpoint {
let public_auth_key = get_ecdsa_public_authentication_key();
let did_subject: DidIdentifierOf<T> = MultiSigner::from(public_auth_key.clone()).into_account().into();
// Max allowed - 1.
let old_service_endpoints = get_service_endpoints::<T>(
T::MaxNumberOfServicesPerDid::get() - 1,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);
// New endpoint with max length and count for all the properties.
let mut new_service_endpoint = get_service_endpoints::<T>(
1,
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
)[0].clone();
// Changing from the default ID otherwise it would be the same as the one first one in `old_service_endpoints`.
new_service_endpoint.id = b"new_id".to_vec().try_into().unwrap();

let did_details = generate_base_did_details::<T>(DidVerificationKey::from(public_auth_key));
Did::<T>::insert(&did_subject, did_details);
save_service_endpoints(&did_subject, &old_service_endpoints);
}: _(RawOrigin::Signed(did_subject.clone()), new_service_endpoint.clone())
verify {
assert_eq!(
ServiceEndpoints::<T>::get(&did_subject, &new_service_endpoint.id),
Some(new_service_endpoint)
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject),
T::MaxNumberOfServicesPerDid::get()
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
T::MaxNumberOfServicesPerDid::get().saturated_into::<usize>()
);
}

remove_service_endpoint {
let public_auth_key = get_ecdsa_public_authentication_key();
let did_subject: DidIdentifierOf<T> = MultiSigner::from(public_auth_key.clone()).into_account().into();
// All set to max.
let old_service_endpoints = get_service_endpoints::<T>(
T::MaxNumberOfServicesPerDid::get(),
T::MaxServiceIdLength::get(),
T::MaxNumberOfTypesPerService::get(),
T::MaxServiceTypeLength::get(),
T::MaxNumberOfUrlsPerService::get(),
T::MaxServiceUrlLength::get(),
);
let endpoint_id = old_service_endpoints[0].id.clone();

let did_details = generate_base_did_details::<T>(DidVerificationKey::from(public_auth_key));
Did::<T>::insert(&did_subject, did_details);
save_service_endpoints(&did_subject, &old_service_endpoints);
}: _(RawOrigin::Signed(did_subject.clone()), endpoint_id.clone())
verify {
assert!(
ServiceEndpoints::<T>::get(&did_subject, &endpoint_id).is_none()
);
assert_eq!(
DidEndpointsCount::<T>::get(&did_subject),
T::MaxNumberOfServicesPerDid::get() - 1
);
assert_eq!(
ServiceEndpoints::<T>::iter_prefix(&did_subject).count(),
T::MaxNumberOfServicesPerDid::get().saturated_into::<usize>() - 1
);
}

signature_verification_sr25519 {
let l in 1 .. MAX_PAYLOAD_BYTE_LENGTH;

Expand Down
Loading