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 @@ -668,6 +668,7 @@ codec = { version = "3.6.12", default-features = false, package = "parity-scale-
collectives-westend-emulated-chain = { path = "cumulus/parachains/integration-tests/emulated/chains/parachains/collectives/collectives-westend" }
collectives-westend-runtime = { path = "cumulus/parachains/runtimes/collectives/collectives-westend" }
color-eyre = { version = "0.6.1", default-features = false }
const-hex = { version = "1.10.0", default-features = false }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a new dependency because some of our dependency already depend on it.

I picked up the version already used by some dependencies in the Cargo.lock

color-print = { version = "0.3.4" }
colored = { version = "2.0.4" }
comfy-table = { version = "7.1.0", default-features = false }
Expand Down
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 @@ -36,4 +37,5 @@ std = [
"log/std",
"scale-info/std",
"sp-runtime/std",
"const-hex/std",
]
17 changes: 14 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,20 @@ 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(_) => None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find a way to print some error in const fn, but this behavior is unchanged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can panic in const? That should be good enough here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, done in 8ffd8a9

Note that I can't put the exact error in the panic because it only accepts a string literal.

}
} 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 @@ -152,6 +160,9 @@ impl<T: Config + Send + Sync> TransactionExtensionBase for CheckMetadataHash<T>

Ok(signed)
}
fn weight() -> Weight {
Weight::default()
}
}
impl<T: Config + Send + Sync> TransactionExtension<T::RuntimeCall> for CheckMetadataHash<T> {
type Val = ();
Expand Down
Loading