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

[After Transaction Extension PR] CheckMetadataHash transaction extension benchmark. #5277

Draft
wants to merge 13 commits into
base: george/restore-gav-tx-ext
Choose a base branch
from
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ color-print = { version = "0.3.4" }
colored = { version = "2.0.4" }
comfy-table = { version = "7.1.0", default-features = false }
console = { version = "0.15.8" }
const-hex = { version = "1.10.0", default-features = false }
contracts-rococo-runtime = { path = "cumulus/parachains/runtimes/contracts/contracts-rococo" }
coretime-rococo-emulated-chain = { path = "cumulus/parachains/integration-tests/emulated/chains/parachains/coretime/coretime-rococo" }
coretime-rococo-runtime = { path = "cumulus/parachains/runtimes/coretime/coretime-rococo" }
Expand Down
11 changes: 11 additions & 0 deletions prdoc/pr_5277.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Improve weight for `CheckMetadataHash` transaction extension

doc:
- audience: Runtime Dev
description: |
This PR improves the weight for the `CheckMetadataHash` transaction extension.
The compilation now panics if the optional compile-time environment variable `RUNTIME_METADATA_HASH` contains an invalid value.

crates:
- name: frame-metadata-hash-extension
bump: minor
2 changes: 2 additions & 0 deletions substrate/frame/metadata-hash-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
log = { workspace = true }
docify = { workspace = true }
const-hex = { workspace = true }

[dev-dependencies]
substrate-wasm-builder = { features = ["metadata-hash"], workspace = true, default-features = true }
Expand All @@ -31,6 +32,7 @@ sp-tracing = { workspace = true, default-features = true }
default = ["std"]
std = [
"codec/std",
"const-hex/std",
"frame-support/std",
"frame-system/std",
"log/std",
Expand Down
22 changes: 19 additions & 3 deletions substrate/frame/metadata-hash-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extern crate alloc;
extern crate self as frame_metadata_hash_extension;

use codec::{Decode, Encode};
use frame_support::DebugNoBound;
use frame_support::{pallet_prelude::Weight, DebugNoBound};
use frame_system::Config;
use scale_info::TypeInfo;
use sp_runtime::{
Expand Down Expand Up @@ -68,12 +68,22 @@ enum MetadataHash {
Custom([u8; 32]),
}

const RUNTIME_METADATA: Option<[u8; 32]> = if let Some(hex) = option_env!("RUNTIME_METADATA_HASH") {
match const_hex::const_decode_to_array(hex.as_bytes()) {
Ok(hex) => Some(hex),
Err(_) => panic!("Invalid RUNTIME_METADATA_HASH environment variable: it must be a 32 \
bytes value in hexadecimal: e.g. 0x123ABCabd...123ABCabc. Upper case or lower case, \
0x prefix is optional."),
}
} else {
None
};

impl MetadataHash {
/// Returns the metadata hash.
fn hash(&self) -> Option<[u8; 32]> {
match self {
Self::FetchFromEnv =>
option_env!("RUNTIME_METADATA_HASH").map(array_bytes::hex2array_unchecked),
Self::FetchFromEnv => RUNTIME_METADATA,
Self::Custom(hash) => Some(*hash),
}
}
Expand Down Expand Up @@ -155,5 +165,11 @@ impl<T: Config + Send + Sync> TransactionExtension<T::RuntimeCall> for CheckMeta
type Val = ();
type Pre = ();

fn weight(&self, _: &T::RuntimeCall) -> Weight {
// The weight is the weight of implicit, it consists of a few match operation, it is
// negligible.
Weight::zero()
}

impl_tx_ext_default!(T::RuntimeCall; validate prepare);
}
Loading