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

Improve CheckMetadataHash: make it constant time and compile error on wrong env variable #6141

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_6141.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Improve `CheckMetadataHash` transaction extension weight and logic

doc:
- audience: Runtime Dev
description: |
The compilation now panics if the optional compile-time environment variable `RUNTIME_METADATA_HASH` contains an invalid value.
The weight for the `CheckMetadataHash` transaction extension is more accurate as it is almost compile-time.

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
26 changes: 22 additions & 4 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,24 @@ 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 +167,11 @@ impl<T: Config + Send + Sync> TransactionExtension<T::RuntimeCall> for CheckMeta
type Val = ();
type Pre = ();

impl_tx_ext_default!(T::RuntimeCall; weight validate prepare);
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