From e2b6389dce83684ad5d7aa07a427f95be6310bc7 Mon Sep 17 00:00:00 2001 From: eNddy Date: Wed, 24 Jul 2024 11:38:40 -0600 Subject: [PATCH] feat(runtime): add metadata signed-extension Add a new signed extension that enables the metadata hash verification feature approved under [RFC 0078](https://polkadot-fellows.github.io/RFCs/approved/0078-merkleized-metadata.html#rfc-0078-merkleized-metadata). This enhancement will support the new generic ledger hardware wallet app and future hardware wallets within the Polkadot ecosystem. --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ node/cli/Cargo.toml | 1 + node/cli/src/benchmarking.rs | 2 ++ runtime/frequency/Cargo.toml | 16 +++++++++-- runtime/frequency/build.rs | 20 +++++++------ runtime/frequency/src/lib.rs | 1 + 7 files changed, 86 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ffda1995b..9051863582 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3583,6 +3583,7 @@ dependencies = [ "derive_more", "frame-benchmarking", "frame-benchmarking-cli", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frequency-runtime", @@ -3633,6 +3634,7 @@ dependencies = [ "cumulus-primitives-timestamp", "frame-benchmarking", "frame-executive", + "frame-metadata-hash-extension", "frame-support", "frame-system", "frame-system-benchmarking", @@ -5713,6 +5715,20 @@ dependencies = [ "hash-db", ] +[[package]] +name = "merkleized-metadata" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f313fcff1d2a4bcaa2deeaa00bf7530d77d5f7bd0467a117dde2e29a75a7a17a" +dependencies = [ + "array-bytes 6.2.3", + "blake3", + "frame-metadata", + "parity-scale-codec", + "scale-decode", + "scale-info", +] + [[package]] name = "merlin" version = "3.0.0" @@ -11731,6 +11747,29 @@ dependencies = [ "sp-arithmetic", ] +[[package]] +name = "scale-bits" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57b1e7f6b65ed1f04e79a85a57d755ad56d76fdf1e9bddcc9ae14f71fcdcf54" +dependencies = [ + "parity-scale-codec", + "scale-type-resolver", +] + +[[package]] +name = "scale-decode" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e98f3262c250d90e700bb802eb704e1f841e03331c2eb815e46516c4edbf5b27" +dependencies = [ + "derive_more", + "parity-scale-codec", + "scale-bits", + "scale-type-resolver", + "smallvec", +] + [[package]] name = "scale-info" version = "2.11.3" @@ -11757,6 +11796,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "scale-type-resolver" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0cded6518aa0bd6c1be2b88ac81bf7044992f0f154bfbabd5ad34f43512abcb" + [[package]] name = "schannel" version = "0.1.23" @@ -13523,13 +13568,22 @@ name = "substrate-wasm-builder" version = "17.0.0" source = "git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.8.0#a51b5dd3dd7d00b0c0ae5bdd28650eb13f0ae70c" dependencies = [ + "array-bytes 6.2.3", "build-helper", "cargo_metadata", "console", "filetime", + "frame-metadata", + "merkleized-metadata", + "parity-scale-codec", "parity-wasm", "polkavm-linker", + "sc-executor", + "sp-core", + "sp-io", "sp-maybe-compressed-blob", + "sp-tracing 16.0.0 (git+https://github.com/paritytech/polkadot-sdk?branch=release-polkadot-v1.8.0)", + "sp-version", "strum 0.24.1", "tempfile", "toml 0.8.14", diff --git a/Cargo.toml b/Cargo.toml index 63bf0c5708..4436415973 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ frame-system = { git = "https://github.com/paritytech/polkadot-sdk", branch = "r frame-system-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } frame-try-runtime = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } +frame-metadata-hash-extension = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } sp-api = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } sp-core = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } sp-io = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } @@ -44,6 +45,7 @@ scale-info = { version = "2.10.0", default-features = false, features = [ sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } sp-keyring = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } sp-version = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } +substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0", default-features = false } chrono = { version = "0.4.24" } pretty_assertions = { version = "1.3.0" } smallvec = "1.11.0" diff --git a/node/cli/Cargo.toml b/node/cli/Cargo.toml index 88db5ca4c2..a558457ce4 100644 --- a/node/cli/Cargo.toml +++ b/node/cli/Cargo.toml @@ -31,6 +31,7 @@ frame-benchmarking-cli = { workspace = true, optional = true } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } +frame-metadata-hash-extension = { workspace = true } pallet-balances = { workspace = true } pallet-transaction-payment = { workspace = true } sc-cli = { workspace = true, optional = true } diff --git a/node/cli/src/benchmarking.rs b/node/cli/src/benchmarking.rs index 7da5492912..2259784391 100644 --- a/node/cli/src/benchmarking.rs +++ b/node/cli/src/benchmarking.rs @@ -130,6 +130,7 @@ pub fn create_benchmark_extrinsic( pallet_frequency_tx_payment::ChargeFrqTransactionPayment::::from(0), pallet_msa::CheckFreeExtrinsicUse::::new(), pallet_handles::handles_signed_extension::HandlesSignedExtension::::new(), + frame_metadata_hash_extension::CheckMetadataHash::::new(false), ); let raw_payload = sp_runtime::generic::SignedPayload::from_raw( @@ -146,6 +147,7 @@ pub fn create_benchmark_extrinsic( (), (), (), + None, ), ); let signature = raw_payload.using_encoded(|e| sender.sign(e)); diff --git a/runtime/frequency/Cargo.toml b/runtime/frequency/Cargo.toml index 4ba4054007..d7f07754c3 100644 --- a/runtime/frequency/Cargo.toml +++ b/runtime/frequency/Cargo.toml @@ -12,7 +12,7 @@ version = "0.0.0" targets = ["x86_64-unknown-linux-gnu"] [build-dependencies] -substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.8.0"} +substrate-wasm-builder = { optional = true, workspace = true } [dependencies] parity-scale-codec = { workspace = true, features = ["derive"] } @@ -30,6 +30,7 @@ frame-system = { workspace = true } frame-system-benchmarking = { workspace = true, optional = true } frame-system-rpc-runtime-api = { workspace = true } frame-try-runtime = { workspace = true, optional = true } +frame-metadata-hash-extension = { workspace = true } pallet-aura = { workspace = true } pallet-authorship = { workspace = true } @@ -162,6 +163,8 @@ std = [ "common-runtime/std", "sp-version/std", "system-runtime-api/std", + "substrate-wasm-builder", + "frame-metadata-hash-extension/std", ] runtime-benchmarks = [ "cumulus-pallet-parachain-system/runtime-benchmarks", @@ -238,4 +241,13 @@ frequency-local = ["common-runtime/frequency-local"] frequency-no-relay = ["common-runtime/frequency-no-relay"] # Following features are used in generating lean wasms no-metadata-docs = ["frame-support/no-metadata-docs"] -on-chain-release-build = ["sp-api/disable-logging"] +on-chain-release-build = ["metadata-hash", "sp-api/disable-logging"] + +# Enable the metadata hash generation. +# +# This is hidden behind a feature because it increases the compile time. +# The wasm binary needs to be compiled twice, once to fetch the metadata, +# generate the metadata hash and then a second time with the +# `RUNTIME_METADATA_HASH` environment variable set for the `CheckMetadataHash` +# extension. +metadata-hash = ["substrate-wasm-builder/metadata-hash"] diff --git a/runtime/frequency/build.rs b/runtime/frequency/build.rs index 7e33800782..7f0c5a1035 100644 --- a/runtime/frequency/build.rs +++ b/runtime/frequency/build.rs @@ -1,12 +1,16 @@ -use substrate_wasm_builder::WasmBuilder; +#[cfg(all(feature = "std", not(feature = "metadata-hash")))] +fn main() { + substrate_wasm_builder::WasmBuilder::build_using_defaults() +} +#[cfg(all(feature = "std", feature = "metadata-hash"))] fn main() { - // VSCode Users: Uncomment the following line to disable the ANSI color codes. - // The OUTPUT pane does not understand ANSI color codes and will show garbage without this. - // std::env::set_var("WASM_BUILD_NO_COLOR", "1"); - WasmBuilder::new() - .with_current_project() - .export_heap_base() - .import_memory() + substrate_wasm_builder::WasmBuilder::init_with_defaults() + .enable_metadata_hash("FRQCY", 8) .build() } + +/// The wasm builder is deactivated when compiling +/// this crate for wasm to speed up the compilation. +#[cfg(not(feature = "std"))] +fn main() {} diff --git a/runtime/frequency/src/lib.rs b/runtime/frequency/src/lib.rs index 6eef5a176e..c45023245f 100644 --- a/runtime/frequency/src/lib.rs +++ b/runtime/frequency/src/lib.rs @@ -298,6 +298,7 @@ pub type SignedExtra = ( pallet_frequency_tx_payment::ChargeFrqTransactionPayment, pallet_msa::CheckFreeExtrinsicUse, pallet_handles::handles_signed_extension::HandlesSignedExtension, + frame_metadata_hash_extension::CheckMetadataHash, ); /// A Block signed with a Justification pub type SignedBlock = generic::SignedBlock;