Skip to content

Commit 4adad21

Browse files
authored
chore: improve benchmark logic (#612)
Fixes https://github.com/KILTprotocol/ticket/issues/3104, based on top of #611. It fixes the logic for the `dip-consumer` pallet, by delegating the generation of a proof worst case to the proof verifier, which must make sure the proof is indeed the one that requires the most weight to verify, and that it verifies successfully. This also means that each consumer runtime is responsible to implement this method, as there cannot be a "universal" worst proof, as that depends on the use case. The pallet benchmarking logic is now generic enough to make this possible and flexible ✨✨✨
1 parent 476c221 commit 4adad21

File tree

31 files changed

+826
-381
lines changed

31 files changed

+826
-381
lines changed

Cargo.lock

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

crates/kilt-dip-primitives/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ version.workspace = true
1414
# External dependencies
1515
hash-db.workspace = true
1616
log.workspace = true
17-
cfg-if.workspace = true
1817

1918
# Internal dependencies
2019
did.workspace = true
@@ -78,5 +77,5 @@ std = [
7877
runtime-benchmarks = [
7978
"kilt-support/runtime-benchmarks",
8079
"pallet-dip-consumer/runtime-benchmarks",
81-
"pallet-dip-provider/runtime-benchmarks",
80+
"pallet-dip-provider/runtime-benchmarks"
8281
]

crates/kilt-dip-primitives/src/merkle_proofs/v0/dip_subject_state/mod.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use did::{
2020
did_details::{DidPublicKey, DidPublicKeyDetails},
2121
DidSignature,
2222
};
23+
use frame_support::ensure;
2324
use sp_core::ConstU32;
2425
use sp_runtime::{traits::SaturatedConversion, BoundedVec};
2526
use sp_std::vec::Vec;
@@ -29,7 +30,6 @@ use crate::{
2930
input_common::TimeBoundDidSignature,
3031
output_common::{DidKeyRelationship, DipOriginInfo, RevealedDidKey, RevealedDidMerkleProofLeaf},
3132
},
32-
traits::BenchmarkDefault,
3333
Error,
3434
};
3535

@@ -106,13 +106,7 @@ impl<
106106
>,
107107
Error,
108108
> {
109-
cfg_if::cfg_if! {
110-
if #[cfg(feature = "runtime-benchmarks")] {
111-
let _ = self.signature.valid_until >= *block_number;
112-
} else {
113-
frame_support::ensure!(self.signature.valid_until >= *block_number, Error::InvalidSignatureTime);
114-
}
115-
}
109+
ensure!(self.signature.valid_until >= *block_number, Error::InvalidSignatureTime);
116110
Ok(DipRevealedDetailsAndVerifiedDidSignatureFreshness {
117111
revealed_leaves: self.revealed_leaves,
118112
signature: self.signature.signature,
@@ -166,9 +160,7 @@ impl<
166160
KiltWeb3Name,
167161
KiltLinkableAccountId,
168162
MAX_REVEALED_LEAVES_COUNT,
169-
> where
170-
KiltDidKeyId: BenchmarkDefault,
171-
KiltBlockNumber: BenchmarkDefault,
163+
>
172164
{
173165
/// Iterates over the revealed DID leaves to find the ones that generated a
174166
/// valid signature for the provided payload.
@@ -214,15 +206,7 @@ impl<
214206
.map(|(index, _)| u32::saturated_from(index))
215207
.collect();
216208

217-
if signing_leaves_indices.is_empty() {
218-
cfg_if::cfg_if! {
219-
if #[cfg(feature = "runtime-benchmarks")] {
220-
return Ok(DipOriginInfo::default());
221-
} else {
222-
return Err(Error::InvalidDidKeyRevealed);
223-
}
224-
}
225-
}
209+
ensure!(!signing_leaves_indices.is_empty(), Error::InvalidDidKeyRevealed);
226210

227211
let signing_leaves_indices_vector = signing_leaves_indices.try_into().map_err(|_| {
228212
log::error!("Should never fail to convert vector of signing leaf indices into BoundedVec.");

crates/kilt-dip-primitives/src/merkle_proofs/v0/input_common.rs

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use parity_scale_codec::{Decode, Encode};
2121
use scale_info::TypeInfo;
2222
use sp_std::vec::Vec;
2323

24-
use crate::{merkle_proofs::v0::output_common::RevealedDidMerkleProofLeaf, utils::BoundedBlindedValue};
24+
use crate::merkle_proofs::v0::output_common::RevealedDidMerkleProofLeaf;
2525

2626
/// The state proof for a parachain head.
2727
///
@@ -31,31 +31,26 @@ use crate::{merkle_proofs::v0::output_common::RevealedDidMerkleProofLeaf, utils:
3131
#[cfg_attr(test, derive(Default))]
3232
pub struct ProviderHeadStateProof<RelayBlockNumber> {
3333
pub(crate) relay_block_number: RelayBlockNumber,
34-
pub(crate) proof: BoundedBlindedValue<u8>,
34+
pub(crate) proof: Vec<Vec<u8>>,
3535
}
3636

37-
#[cfg(feature = "runtime-benchmarks")]
38-
impl<RelayBlockNumber, Context> kilt_support::traits::GetWorstCase<Context> for ProviderHeadStateProof<RelayBlockNumber>
39-
where
40-
RelayBlockNumber: Default,
41-
{
42-
fn worst_case(context: Context) -> Self {
37+
impl<RelayBlockNumber> ProviderHeadStateProof<RelayBlockNumber> {
38+
pub fn new(relay_block_number: RelayBlockNumber, proof: Vec<Vec<u8>>) -> Self {
4339
Self {
44-
relay_block_number: RelayBlockNumber::default(),
45-
proof: BoundedBlindedValue::worst_case(context),
40+
proof,
41+
relay_block_number,
4642
}
4743
}
4844
}
4945

5046
/// The state proof for a DIP commitment.
5147
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, TypeInfo)]
5248
#[cfg_attr(test, derive(Default))]
53-
pub struct DipCommitmentStateProof(pub(crate) BoundedBlindedValue<u8>);
49+
pub struct DipCommitmentStateProof(pub(crate) Vec<Vec<u8>>);
5450

55-
#[cfg(feature = "runtime-benchmarks")]
56-
impl<Context> kilt_support::traits::GetWorstCase<Context> for DipCommitmentStateProof {
57-
fn worst_case(context: Context) -> Self {
58-
Self(BoundedBlindedValue::worst_case(context))
51+
impl DipCommitmentStateProof {
52+
pub fn new(proof: Vec<Vec<u8>>) -> Self {
53+
Self(proof)
5954
}
6055
}
6156

@@ -76,7 +71,7 @@ pub struct DidMerkleProof<
7671
ProviderWeb3Name,
7772
ProviderLinkableAccountId,
7873
> {
79-
pub(crate) blinded: BoundedBlindedValue<u8>,
74+
pub(crate) blinded: Vec<Vec<u8>>,
8075
pub(crate) revealed: Vec<
8176
RevealedDidMerkleProofLeaf<
8277
ProviderDidKeyId,
@@ -92,7 +87,7 @@ impl<ProviderDidKeyId, ProviderAccountId, ProviderBlockNumber, ProviderWeb3Name,
9287
DidMerkleProof<ProviderDidKeyId, ProviderAccountId, ProviderBlockNumber, ProviderWeb3Name, ProviderLinkableAccountId>
9388
{
9489
pub fn new(
95-
blinded: BoundedBlindedValue<u8>,
90+
blinded: Vec<Vec<u8>>,
9691
revealed: Vec<
9792
RevealedDidMerkleProofLeaf<
9893
ProviderDidKeyId,
@@ -105,35 +100,17 @@ impl<ProviderDidKeyId, ProviderAccountId, ProviderBlockNumber, ProviderWeb3Name,
105100
) -> Self {
106101
Self { blinded, revealed }
107102
}
108-
}
109103

110-
#[cfg(feature = "runtime-benchmarks")]
111-
impl<
104+
pub fn revealed(
105+
&self,
106+
) -> &[RevealedDidMerkleProofLeaf<
112107
ProviderDidKeyId,
113108
ProviderAccountId,
114109
ProviderBlockNumber,
115110
ProviderWeb3Name,
116111
ProviderLinkableAccountId,
117-
Context,
118-
> kilt_support::traits::GetWorstCase<Context>
119-
for DidMerkleProof<
120-
ProviderDidKeyId,
121-
ProviderAccountId,
122-
ProviderBlockNumber,
123-
ProviderWeb3Name,
124-
ProviderLinkableAccountId,
125-
> where
126-
ProviderDidKeyId: Default + Clone,
127-
ProviderAccountId: Clone,
128-
ProviderBlockNumber: Default + Clone,
129-
ProviderWeb3Name: Clone,
130-
ProviderLinkableAccountId: Clone,
131-
{
132-
fn worst_case(context: Context) -> Self {
133-
Self {
134-
blinded: BoundedBlindedValue::worst_case(context),
135-
revealed: sp_std::vec![RevealedDidMerkleProofLeaf::default(); 64],
136-
}
112+
>] {
113+
self.revealed.as_ref()
137114
}
138115
}
139116

@@ -188,17 +165,3 @@ where
188165
}
189166
}
190167
}
191-
192-
#[cfg(feature = "runtime-benchmarks")]
193-
impl<BlockNumber, Context> kilt_support::traits::GetWorstCase<Context> for TimeBoundDidSignature<BlockNumber>
194-
where
195-
DidSignature: kilt_support::traits::GetWorstCase<Context>,
196-
BlockNumber: Default,
197-
{
198-
fn worst_case(context: Context) -> Self {
199-
Self {
200-
signature: DidSignature::worst_case(context),
201-
valid_until: BlockNumber::default(),
202-
}
203-
}
204-
}

0 commit comments

Comments
 (0)