Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 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 @@ -293,6 +293,7 @@ members = [
"substrate/frame/child-bounties",
"substrate/frame/collective",
"substrate/frame/contracts",
"substrate/frame/contracts/fixtures",
"substrate/frame/contracts/primitives",
"substrate/frame/contracts/proc-macro",
"substrate/frame/conviction-voting",
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ assert_matches = "1"
env_logger = "0.9"
pretty_assertions = "1"
wat = "1"
pallet-contracts-fixtures = { path = "./fixtures" }

# Substrate Dependencies
pallet-balances = { path = "../balances" }
Expand All @@ -73,6 +74,7 @@ std = [
"frame-system/std",
"log/std",
"pallet-balances?/std",
"pallet-contracts-fixtures/std",
"pallet-contracts-primitives/std",
"pallet-contracts-proc-macro/full",
"pallet-insecure-randomness-collective-flip/std",
Expand Down
18 changes: 18 additions & 0 deletions substrate/frame/contracts/fixtures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "pallet-contracts-fixtures"
publish = false
version = "1.0.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
description = "Fixtures for testing contracts pallet."

[dependencies]
wat = "1"
frame-system = { path = "../../system", default-features = false}
sp-runtime = { path = "../../../primitives/runtime", default-features = false}

[features]
default = [ "std" ]
std = [ "frame-system/std", "sp-runtime/std" ]

42 changes: 42 additions & 0 deletions substrate/frame/contracts/fixtures/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use sp_runtime::traits::Hash;
use std::{env::var, path::PathBuf};

fn fixtures_root_dir() -> PathBuf {
match (var("CARGO_MANIFEST_DIR"), var("CARGO_PKG_NAME")) {
// When `CARGO_MANIFEST_DIR` is not set, Rust resolves relative paths from the root folder
(Err(_), _) => "substrate/frame/contracts/fixtures/data".into(),
(Ok(path), Ok(s)) if s == "pallet-contracts" => PathBuf::from(path).join("fixtures/data"),
(Ok(_), pkg_name) => panic!("Failed to resolve fixture dir for tests from {pkg_name:?}."),
}
}

/// Load a given wasm module represented by a .wat file and returns a wasm binary contents along
/// with it's hash.
///
/// The fixture files are located under the `fixtures/` directory.
pub fn compile_module<T>(fixture_name: &str) -> wat::Result<(Vec<u8>, <T::Hashing as Hash>::Output)>
where
T: frame_system::Config,
{
let fixture_path = fixtures_root_dir().join(format!("{fixture_name}.wat"));
let wasm_binary = wat::parse_file(fixture_path)?;
let code_hash = T::Hashing::hash(&wasm_binary);
Ok((wasm_binary, code_hash))
}
24 changes: 1 addition & 23 deletions substrate/frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use frame_support::{
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
};
use frame_system::{EventRecord, Phase};
use pallet_contracts_fixtures::compile_module;
use pallet_contracts_primitives::CodeUploadReturnValue;
use pretty_assertions::{assert_eq, assert_ne};
use sp_core::ByteArray;
Expand Down Expand Up @@ -555,29 +556,6 @@ impl ExtBuilder {
}
}

/// Load a given wasm module represented by a .wat file and returns a wasm binary contents along
/// with it's hash.
///
/// The fixture files are located under the `fixtures/` directory.
fn compile_module<T>(fixture_name: &str) -> wat::Result<(Vec<u8>, <T::Hashing as Hash>::Output)>
where
T: frame_system::Config,
{
let fixture_path = [
// When `CARGO_MANIFEST_DIR` is not set, Rust resolves relative paths from the root folder
std::env::var("CARGO_MANIFEST_DIR")
.as_deref()
.unwrap_or("substrate/frame/contracts"),
"/fixtures/",
fixture_name,
".wat",
]
.concat();
let wasm_binary = wat::parse_file(fixture_path)?;
let code_hash = T::Hashing::hash(&wasm_binary);
Ok((wasm_binary, code_hash))
}

fn initialize_block(number: u64) {
System::reset_events();
System::initialize(&number, &[0u8; 32].into(), &Default::default());
Expand Down