Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit 9e0a9c1

Browse files
author
Alex Sed
authored
Implement benchmarking for Attestation and set weightInfo (#148)
Adds basic benchmarking and weightInfo of Attestation.
1 parent 9036fb1 commit 9e0a9c1

File tree

8 files changed

+145
-0
lines changed

8 files changed

+145
-0
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/runtime/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pallet-utility = { version = "2.0.0", default-features = false, path = "../../..
7979
pallet-transaction-payment = { version = "2.0.0", default-features = false, path = "../../../frame/transaction-payment" }
8080
pallet-transaction-payment-rpc-runtime-api = { version = "2.0.0", default-features = false, path = "../../../frame/transaction-payment/rpc/runtime-api/" }
8181
pallet-vesting = { version = "2.0.0", default-features = false, path = "../../../frame/vesting" }
82+
prml-attestation = { version = "2.0.0", default-features = false, path = "../../../prml/attestation" }
8283

8384
[build-dependencies]
8485
wasm-builder-runner = { version = "1.0.5", package = "substrate-wasm-builder-runner", path = "../../../utils/wasm-builder-runner" }
@@ -144,6 +145,7 @@ std = [
144145
"pallet-society/std",
145146
"pallet-recovery/std",
146147
"pallet-vesting/std",
148+
"prml-attestation/std",
147149
]
148150
runtime-benchmarks = [
149151
"frame-benchmarking",
@@ -173,4 +175,5 @@ runtime-benchmarks = [
173175
"pallet-session-benchmarking",
174176
"frame-system-benchmarking",
175177
"hex-literal",
178+
"prml-attestation/runtime-benchmarks",
176179
]

bin/node/runtime/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,11 @@ impl pallet_vesting::Trait for Runtime {
887887
type WeightInfo = weights::pallet_vesting::WeightInfo;
888888
}
889889

890+
impl prml_attestation::Trait for Runtime {
891+
type Event = Event;
892+
type WeightInfo = weights::prml_attestation::WeightInfo;
893+
}
894+
890895
construct_runtime!(
891896
pub enum Runtime where
892897
Block = Block,
@@ -925,6 +930,7 @@ construct_runtime!(
925930
Scheduler: pallet_scheduler::{Module, Call, Storage, Event<T>},
926931
Proxy: pallet_proxy::{Module, Call, Storage, Event<T>},
927932
Multisig: pallet_multisig::{Module, Call, Storage, Event<T>},
933+
Attestation: prml_attestation::{Module, Call, Storage, Event<T>},
928934
}
929935
);
930936

@@ -1221,6 +1227,7 @@ impl_runtime_apis! {
12211227
add_benchmark!(params, batches, pallet_treasury, Treasury);
12221228
add_benchmark!(params, batches, pallet_utility, Utility);
12231229
add_benchmark!(params, batches, pallet_vesting, Vesting);
1230+
add_benchmark!(params, batches, prml_attestation, Attestation);
12241231

12251232
if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) }
12261233
Ok(batches)

bin/node/runtime/src/weights/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ pub mod pallet_timestamp;
3232
pub mod pallet_utility;
3333
pub mod pallet_vesting;
3434
pub mod pallet_elections_phragmen;
35+
pub mod prml_attestation;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of Plug.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd. and Plug New Zealand.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0
19+
20+
#![allow(unused_parens)]
21+
#![allow(unused_imports)]
22+
23+
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
24+
25+
pub struct WeightInfo;
26+
impl prml_attestation::WeightInfo for WeightInfo {
27+
fn set_claim() -> Weight {
28+
(89_000_000 as Weight)
29+
.saturating_add(DbWeight::get().reads(2 as Weight))
30+
.saturating_add(DbWeight::get().writes(3 as Weight))
31+
}
32+
fn remove_claim() -> Weight {
33+
(101_000_000 as Weight)
34+
.saturating_add(DbWeight::get().reads(2 as Weight))
35+
.saturating_add(DbWeight::get().writes(3 as Weight))
36+
}
37+
}

prml/attestation/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ authors = ["Centrality Developers <support@centrality.ai>"]
55
edition = "2018"
66

77
[dependencies]
8+
frame-benchmarking = { version = "2.0.0", default-features = false, path = "../../frame/benchmarking", optional = true }
89
codec = { package = "parity-scale-codec", version = "1.3.5", default-features = false, features = ["derive"] }
910
sp-core = { path = "../../primitives/core", default-features = false }
1011
sp-io = { path = "../../primitives/io", default-features = false }
1112
sp-runtime = { path = "../../primitives/runtime", default-features = false }
13+
sp-std = { path = "../../primitives/std", default-features = false }
1214
frame-support = { path = "../../frame/support", default-features = false }
1315
frame-system = { path = "../../frame/system", default-features = false }
1416

@@ -18,6 +20,9 @@ std = [
1820
"codec/std",
1921
"sp-core/std",
2022
"sp-runtime/std",
23+
"sp-std/std",
24+
"frame-benchmarking/std",
2125
"frame-support/std",
2226
"frame-system/std",
2327
]
28+
runtime-benchmarks = ["frame-benchmarking"]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}

prml/attestation/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
3636
#![cfg_attr(not(feature = "std"), no_std)]
3737

38+
mod benchmarking;
3839
mod default_weight;
3940
mod mock;
4041

0 commit comments

Comments
 (0)