|
| 1 | +// Copyright 2019-2020 Plug New Zealand Limited |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//! Attestation benchmarking. |
| 16 | +
|
| 17 | +#![cfg(feature = "runtime-benchmarks")] |
| 18 | + |
| 19 | +use super::*; |
| 20 | + |
| 21 | +use frame_benchmarking::{account, benchmarks, whitelisted_caller}; |
| 22 | +use frame_system::RawOrigin; |
| 23 | + |
| 24 | +use crate::Module as Attestation; |
| 25 | + |
| 26 | +const SEED: u32 = 0; |
| 27 | + |
| 28 | +benchmarks! { |
| 29 | + _{ } |
| 30 | + |
| 31 | + set_claim { |
| 32 | + let issuer: T::AccountId = whitelisted_caller(); |
| 33 | + let holder: T::AccountId = account("holder", 0, SEED); |
| 34 | + let topic = AttestationTopic::from(0xf00d); |
| 35 | + let value = AttestationValue::from(0xb33f); |
| 36 | + }: set_claim(RawOrigin::Signed(issuer.clone()), holder.clone(), topic.clone(), value.clone()) |
| 37 | + verify { |
| 38 | + let issuers: Vec<<T as frame_system::Trait>::AccountId> = vec![issuer.clone()]; |
| 39 | + assert_eq!(Attestation::<T>::issuers(holder.clone()), issuers); |
| 40 | + assert_eq!(Attestation::<T>::topics((holder.clone(), issuer.clone())), [topic.clone()]); |
| 41 | + assert_eq!(Attestation::<T>::value((holder, issuer, topic)), value); |
| 42 | + } |
| 43 | + |
| 44 | + remove_claim { |
| 45 | + let issuer1: T::AccountId = whitelisted_caller(); |
| 46 | + let issuer2: T::AccountId = account("issuer2", 0, SEED); |
| 47 | + let issuer3: T::AccountId = account("issuer3", 0, SEED); |
| 48 | + |
| 49 | + let holder: T::AccountId = account("holder", 0, SEED); |
| 50 | + |
| 51 | + let topic1 = AttestationTopic::from(0xf00d); |
| 52 | + let topic2 = AttestationTopic::from(0xf00e); |
| 53 | + let topic3 = AttestationTopic::from(0xf00f); |
| 54 | + |
| 55 | + let value = AttestationValue::from(0xb33f); |
| 56 | + |
| 57 | + let _ = Attestation::<T>::set_claim(RawOrigin::Signed(issuer2.clone()).into(), holder.clone(), topic2.clone(), value.clone()); |
| 58 | + let _ = Attestation::<T>::set_claim(RawOrigin::Signed(issuer1.clone()).into(), holder.clone(), topic1.clone(), value.clone()); |
| 59 | + let _ = Attestation::<T>::set_claim(RawOrigin::Signed(issuer3.clone()).into(), holder.clone(), topic3.clone(), value.clone()); |
| 60 | + |
| 61 | + }: remove_claim(RawOrigin::Signed(issuer1.clone()), holder.clone(), topic1.clone()) |
| 62 | + verify { |
| 63 | + let issuers: Vec<<T as frame_system::Trait>::AccountId> = vec![issuer2.clone(), issuer3.clone()]; |
| 64 | + assert_eq!(Attestation::<T>::issuers(holder.clone()), issuers); |
| 65 | + assert_ne!(Attestation::<T>::value((holder, issuer1, topic1)), value); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use super::*; |
| 72 | + use crate::mock::{ExtBuilder, Test}; |
| 73 | + use frame_support::assert_ok; |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn set_claim() { |
| 77 | + ExtBuilder::build().execute_with(|| { |
| 78 | + assert_ok!(test_benchmark_set_claim::<Test>()); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + fn remove_claim() { |
| 84 | + ExtBuilder::build().execute_with(|| { |
| 85 | + assert_ok!(test_benchmark_remove_claim::<Test>()); |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments