pallet_revive_fixtures/
lib.rs1#![cfg_attr(not(feature = "std"), no_std)]
19
20extern crate alloc;
21
22include!(concat!(env!("OUT_DIR"), "/fixture_location.rs"));
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum FixtureType {
28 Rust,
30 Resolc,
32 Solc,
34}
35
36#[cfg(feature = "std")]
37impl FixtureType {
38 fn file_extension(&self) -> &'static str {
39 match self {
40 Self::Rust => ".polkavm",
41 Self::Resolc => ".resolc.polkavm",
42 Self::Solc => ".sol.bin",
43 }
44 }
45}
46
47#[cfg(feature = "std")]
49pub fn compile_module_with_type(
50 fixture_name: &str,
51 fixture_type: FixtureType,
52) -> anyhow::Result<(Vec<u8>, sp_core::H256)> {
53 let out_dir: std::path::PathBuf = FIXTURE_DIR.into();
54 let fixture_path = out_dir.join(format!("{fixture_name}{}", fixture_type.file_extension()));
55 let binary = std::fs::read(fixture_path)?;
56 let code_hash = sp_io::hashing::keccak_256(&binary);
57 Ok((binary, sp_core::H256(code_hash)))
58}
59
60#[cfg(feature = "std")]
62pub fn compile_module(fixture_name: &str) -> anyhow::Result<(Vec<u8>, sp_core::H256)> {
63 compile_module_with_type(fixture_name, FixtureType::Rust)
64}
65
66pub mod bench {
71 use alloc::vec::Vec;
72 pub const DUMMY: &[u8] = fixture!("dummy");
73 pub const NOOP: &[u8] = fixture!("noop");
74
75 pub fn dummy_unique(replace_with: u32) -> Vec<u8> {
76 let mut dummy = DUMMY.to_vec();
77 let idx = dummy
78 .windows(4)
79 .position(|w| w == &[0xDE, 0xAD, 0xBE, 0xEF])
80 .expect("Benchmark fixture contains this pattern; qed");
81 dummy[idx..idx + 4].copy_from_slice(&replace_with.to_le_bytes());
82 dummy
83 }
84}
85
86#[cfg(test)]
87mod test {
88 #[test]
89 fn out_dir_should_have_compiled_mocks() {
90 let out_dir: std::path::PathBuf = crate::FIXTURE_DIR.into();
91 assert!(out_dir.join("dummy.polkavm").exists());
92 }
93}