referrerpolicy=no-referrer-when-downgrade

pallet_revive_fixtures/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19
20extern crate alloc;
21
22// generated file that tells us where to find the fixtures
23include!(concat!(env!("OUT_DIR"), "/fixture_location.rs"));
24
25/// Enum for different fixture types
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum FixtureType {
28	/// Polkavm (compiled Rust contracts)
29	Rust,
30	/// Resolc (compiled Solidity contracts to Polkavm)
31	Resolc,
32	/// Solc (compiled Solidity contracts to EVM bytecode)
33	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/// Load a fixture module with the specified type and return binary contents along with its hash.
48#[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/// Load a given polkavm module and returns a polkavm binary contents along with its hash.
61#[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
66/// Fixtures used in runtime benchmarks.
67///
68/// We explicitly include those fixtures into the binary to make them
69/// available in no-std environments (runtime benchmarks).
70pub 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}