Skip to content

Commit

Permalink
fix runtime-benchmarks for pallet-identity
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ committed Mar 5, 2024
1 parent 18b97f1 commit 19b9662
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 151 deletions.
9 changes: 5 additions & 4 deletions primitives/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ repository = { workspace = true }
version = "0.1.1"

[package.metadata.docs.rs]
targets = [ "x86_64-unknown-linux-gnu" ]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
blake2-rfc = { workspace = true, optional = true }
impl-serde = { workspace = true }
libsecp256k1 = { workspace = true, features = [ "hmac" ] }
libsecp256k1 = { workspace = true, features = ["hmac"] }
log = { workspace = true }
serde = { workspace = true, features = [ "derive" ] }
serde = { workspace = true, features = ["derive"] }
sha3 = { workspace = true }

# Substrate
Expand All @@ -31,7 +31,7 @@ sp-std = { workspace = true }
hex = { workspace = true }

[features]
default = [ "std" ]
default = ["std"]
std = [
"full_crypto",
"hex/std",
Expand All @@ -45,6 +45,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = []

full_crypto = [
"blake2-rfc",
Expand Down
42 changes: 42 additions & 0 deletions primitives/account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ impl From<[u8; 32]> for AccountId20 {
Self(buffer)
}
}
/// Required for pallet_identity benchmarks
#[cfg(feature = "runtime-benchmarks")]
impl From<sp_runtime::AccountId32> for AccountId20 {
fn from(value: sp_runtime::AccountId32) -> Self {
let bytes: [u8; 32] = value.into();

bytes.into()
}
}

impl From<H160> for AccountId20 {
fn from(h160: H160) -> Self {
Expand Down Expand Up @@ -110,12 +119,45 @@ impl std::str::FromStr for AccountId20 {
)]
pub struct EthereumSignature(ecdsa::Signature);

/// Required for pallet_identity benchmarks
#[cfg(feature = "runtime-benchmarks")]
impl From<sp_runtime::MultiSignature> for EthereumSignature {
fn from(sig: sp_runtime::MultiSignature) -> Self {
match sig {
sp_runtime::MultiSignature::Ecdsa(s) => EthereumSignature(s),
// Benchmarking mock
// !!! Should never be used in production !!!
sp_runtime::MultiSignature::Sr25519(_) => {
let buffer = [0u8; 65];

This comment has been minimized.

Copy link
@librelois

librelois Mar 5, 2024

Collaborator

We should panic if the variant is MultiSignature::Sr25519

let sig: ecdsa::Signature = ecdsa::Signature(buffer);

EthereumSignature(sig)
}
_ => panic!("Unexpected signature."),
}
}
}

impl From<ecdsa::Signature> for EthereumSignature {
fn from(x: ecdsa::Signature) -> Self {
EthereumSignature(x)
}
}

/// Required for pallet_identity benchmarks
#[cfg(feature = "runtime-benchmarks")]
impl sp_runtime::traits::Verify for EthereumSignature {
type Signer = EthereumSigner;
fn verify<L: sp_runtime::traits::Lazy<[u8]>>(
&self,
mut _msg: L,
_signer: &AccountId20,
) -> bool {
true
}
}

#[cfg(not(feature = "runtime-benchmarks"))]
impl sp_runtime::traits::Verify for EthereumSignature {
type Signer = EthereumSigner;
fn verify<L: sp_runtime::traits::Lazy<[u8]>>(&self, mut msg: L, signer: &AccountId20) -> bool {
Expand Down
1 change: 1 addition & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,6 @@ runtime-benchmarks = [
"pallet-xcm/runtime-benchmarks",
"pallet-moonbeam-lazy-migrations/runtime-benchmarks",
"moonbeam-xcm-benchmarks/runtime-benchmarks",
"account/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime", "pallet-migrations/try-runtime"]
Loading

0 comments on commit 19b9662

Please sign in to comment.