Skip to content

Commit f53beff

Browse files
[stable2509] Backport #9791 (#10531)
Backport #9791 into `stable2509` from bkchr. See the [documentation](https://github.com/paritytech/polkadot-sdk/blob/master/docs/BACKPORT.md) on how to use this bot. <!-- # To be used by other automation, do not modify: original-pr-number: #${pull_number} --> Co-authored-by: Bastian Köcher <[email protected]>
1 parent a54988c commit f53beff

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

substrate/frame/revive/fixtures/build.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ use std::{
2828
const OVERRIDE_RUSTUP_TOOLCHAIN_ENV_VAR: &str = "PALLET_REVIVE_FIXTURES_RUSTUP_TOOLCHAIN";
2929
const OVERRIDE_STRIP_ENV_VAR: &str = "PALLET_REVIVE_FIXTURES_STRIP";
3030
const OVERRIDE_OPTIMIZE_ENV_VAR: &str = "PALLET_REVIVE_FIXTURES_OPTIMIZE";
31+
/// Do not build the fixtures, they will resolve to `None`.
32+
///
33+
/// Depending on the usage, they will probably panic at runtime.
34+
const SKIP_PALLET_REVIVE_FIXTURES: &str = "SKIP_PALLET_REVIVE_FIXTURES";
3135

3236
/// A contract entry.
3337
#[derive(Clone)]
@@ -250,7 +254,10 @@ fn compile_with_standard_json(
250254
.stderr(std::process::Stdio::piped())
251255
.spawn()
252256
.with_context(|| {
253-
format!("Failed to execute {}. Make sure {} is installed.", compiler, compiler)
257+
format!(
258+
"Failed to execute {compiler}. Make sure {compiler} is installed or \
259+
set env variable `{SKIP_PALLET_REVIVE_FIXTURES}=1` to skip fixtures compilation."
260+
)
254261
})?;
255262

256263
let mut stdin = compiler_output.stdin.as_ref().unwrap();
@@ -436,6 +443,21 @@ fn generate_fixture_location(temp_dir: &Path, out_dir: &Path, entries: &[Entry])
436443
let mut file = fs::File::create(temp_dir.join("fixture_location.rs"))
437444
.context("Failed to create fixture_location.rs")?;
438445

446+
let (fixtures, fixtures_resolc) = if env::var(SKIP_PALLET_REVIVE_FIXTURES).is_err() {
447+
(
448+
format!(
449+
r#"Some(include_bytes!(concat!("{}", "/", $name, ".polkavm")))"#,
450+
out_dir.display()
451+
),
452+
format!(
453+
r#"Some(include_bytes!(concat!("{}", "/", $name, ".resolc.polkavm")))"#,
454+
out_dir.display()
455+
),
456+
)
457+
} else {
458+
("None".into(), "None".into())
459+
};
460+
439461
write!(
440462
file,
441463
r#"
@@ -445,14 +467,14 @@ fn generate_fixture_location(temp_dir: &Path, out_dir: &Path, entries: &[Entry])
445467
#[macro_export]
446468
macro_rules! fixture {{
447469
($name: literal) => {{
448-
include_bytes!(concat!("{0}", "/", $name, ".polkavm"))
470+
{fixtures}
449471
}};
450472
}}
451473
452474
#[macro_export]
453475
macro_rules! fixture_resolc {{
454476
($name: literal) => {{
455-
include_bytes!(concat!("{0}", "/", $name, ".resolc.polkavm"))
477+
{fixtures_resolc}
456478
}};
457479
}}
458480
"#,
@@ -494,19 +516,21 @@ pub fn main() -> Result<()> {
494516
return Ok(());
495517
}
496518

497-
// Compile Rust contracts
498-
let rust_entries: Vec<_> = entries
499-
.iter()
500-
.filter(|e| matches!(e.contract_type, ContractType::Rust))
501-
.collect();
502-
if !rust_entries.is_empty() {
503-
create_cargo_toml(&fixtures_dir, rust_entries.into_iter(), &build_dir)?;
504-
invoke_build(&build_dir)?;
505-
write_output(&build_dir, &out_dir, entries.clone())?;
506-
}
519+
if env::var(SKIP_PALLET_REVIVE_FIXTURES).is_err() {
520+
// Compile Rust contracts
521+
let rust_entries: Vec<_> = entries
522+
.iter()
523+
.filter(|e| matches!(e.contract_type, ContractType::Rust))
524+
.collect();
525+
if !rust_entries.is_empty() {
526+
create_cargo_toml(&fixtures_dir, rust_entries.into_iter(), &build_dir)?;
527+
invoke_build(&build_dir)?;
528+
write_output(&build_dir, &out_dir, entries.clone())?;
529+
}
507530

508-
// Compile Solidity contracts
509-
compile_solidity_contracts(&contracts_dir, &out_dir, &entries)?;
531+
// Compile Solidity contracts
532+
compile_solidity_contracts(&contracts_dir, &out_dir, &entries)?;
533+
}
510534

511535
let temp_dir: PathBuf =
512536
env::var("OUT_DIR").context("Failed to fetch `OUT_DIR` env variable")?.into();

substrate/frame/revive/fixtures/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,19 @@ pub fn compile_module(fixture_name: &str) -> anyhow::Result<(Vec<u8>, sp_core::H
6969
/// available in no-std environments (runtime benchmarks).
7070
pub mod bench {
7171
use alloc::vec::Vec;
72-
pub const DUMMY: &[u8] = fixture!("dummy");
73-
pub const NOOP: &[u8] = fixture!("noop");
72+
pub const DUMMY: Option<&[u8]> = fixture!("dummy");
73+
pub const NOOP: Option<&[u8]> = fixture!("noop");
74+
75+
pub fn dummy() -> &'static [u8] {
76+
DUMMY.expect("`DUMMY` fixture not available, remove `SKIP_PALLET_REVIVE_FIXTURES` env variable to compile them.")
77+
}
78+
79+
pub fn noop() -> &'static [u8] {
80+
NOOP.expect("`NOOP` fixture not available, remove `SKIP_PALLET_REVIVE_FIXTURES` env variable to compile them.")
81+
}
7482

7583
pub fn dummy_unique(replace_with: u32) -> Vec<u8> {
76-
let mut dummy = DUMMY.to_vec();
84+
let mut dummy = dummy().to_vec();
7785
let idx = dummy
7886
.windows(4)
7987
.position(|w| w == &[0xDE, 0xAD, 0xBE, 0xEF])

substrate/frame/revive/src/call_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ pub struct VmBinaryModule {
390390
impl VmBinaryModule {
391391
/// Return a contract code that does nothing.
392392
pub fn dummy() -> Self {
393-
Self::new(bench_fixtures::DUMMY.to_vec())
393+
Self::new(bench_fixtures::dummy().to_vec())
394394
}
395395

396396
fn new(code: Vec<u8>) -> Self {
@@ -458,7 +458,7 @@ impl VmBinaryModule {
458458

459459
/// A contract code that calls the "noop" host function in a loop depending in the input.
460460
pub fn noop() -> Self {
461-
Self::new(bench_fixtures::NOOP.to_vec())
461+
Self::new(bench_fixtures::noop().to_vec())
462462
}
463463

464464
/// A contract code that does unaligned memory accessed in a loop.

0 commit comments

Comments
 (0)