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
28 changes: 28 additions & 0 deletions solx-dev/hardhat-overlays/hardhat-v3.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import hardhatSlangSolx from '@nomicfoundation/hardhat-slang-solx';

import baseConfig from './hardhat.config.base.ts';

const { path: _path, ...defaultProfile } = baseConfig.solidity.profiles.default as {
version: string;
path?: string;
};

export default {
...baseConfig,
plugins: [...baseConfig.plugins, hardhatSlangSolx],
solidity: {
profiles: {
default: defaultProfile,
...(process.env.USE_SOLX === 'true'
? {
'slang-solx': {
type: 'slang-solx',
version: defaultProfile.version,
path: process.env.SOLX,
settings: { viaIR: process.env.VIA_IR === 'true' },
},
}
: {}),
},
},
};
36 changes: 36 additions & 0 deletions solx-dev/hardhat-overlays/openzeppelin.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// hardhat-exposed generates its wrappers from the AST of the default profile, so solc stays there.
import hardhatSlangSolx from '@nomicfoundation/hardhat-slang-solx';

import baseConfig from './hardhat.config.base.ts';

const solidity = baseConfig.solidity as { version: string; settings: { evmVersion: string } };

export default {
...baseConfig,
plugins: [...baseConfig.plugins, hardhatSlangSolx],
solidity: {
profiles: {
default: solidity,
...(process.env.USE_SOLX === 'true'
? {
'slang-solx': {
type: 'slang-solx',
version: solidity.version,
path: process.env.SOLX,
settings: {
evmVersion: solidity.settings.evmVersion,
viaIR: process.env.VIA_IR === 'true',
},
},
}
: {}),
},
},
test: {
...baseConfig.test,
mocha: {
reporter: 'json',
reporterOptions: { output: process.env.JUNIT_REPORT },
},
},
};
17 changes: 11 additions & 6 deletions solx-dev/hardhat-tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
# description = "..." # Project description
# build_system = "npm" # npm, yarn, pnpm, or bun (default: npm)
# dependencies = ["pkg@version"] # Additional npm dependencies to install
# build_profile = "..." # Build profile solx toolchains compile and test with
# config_overlay = "./path.ts" # Installed as hardhat.config.ts; imports the project's config as hardhat.config.base.<ext>
# disabled = true/false # Whether to skip this project
# [projects.<name>.env]
# VAR = "value" # Environment variables for build/test
Expand Down Expand Up @@ -113,16 +115,19 @@ dependencies = [ "hardhat@2.29.1" ]
url = "https://github.com/antonbaliasnikov/solx-hh3"
commit = "bfe0366f7f5936ec9e5e76fa37fe21d284a53c88"
description = "An example Hardhat v3 project with solx support."
dependencies = [ "ts-node" ]
dependencies = [ "ts-node", "hardhat@3.18.0", "@nomicfoundation/hardhat-mocha@3.0.2", "@nomicfoundation/hardhat-slang-solx@3.0.2" ]
build_profile = "slang-solx"
config_overlay = "./solx-dev/hardhat-overlays/hardhat-v3.config.ts"

[projects.openzeppelin]
url = "https://github.com/antonbaliasnikov/openzeppelin-contracts"
commit = "bd3c4ba5d950860ecd957a84acc65083c3aec20e"
url = "https://github.com/OpenZeppelin/openzeppelin-contracts"
commit = "6c2ab375a9c7db0fa8c21fba1ed3b2057f75dd75"
description = "A library for secure smart contract development."
build_system = "pnpm"
dependencies = [ "hardhat@2.29.1" ]
dependencies = [ "@nomicfoundation/hardhat-slang-solx@3.0.2" ]
build_profile = "slang-solx"
config_overlay = "./solx-dev/hardhat-overlays/openzeppelin.config.ts"
[projects.openzeppelin.env]
EVM_DISABLE_MEMORY_SAFE_ASM_CHECK = "true"
COMPILER = "0.8.34"

[projects.zksafe]
url = "https://github.com/antonbaliasnikov/zksafe"
Expand Down
7 changes: 7 additions & 0 deletions solx-dev/src/test/hardhat/config/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
pub mod build_system;

use std::collections::HashMap;
use std::path::PathBuf;

use self::build_system::BuildSystem;

Expand All @@ -30,6 +31,12 @@ pub struct Project {
/// Environment variables required for every command.
#[serde(default)]
pub env: HashMap<String, String>,
/// Build profile solx toolchains compile and test with.
#[serde(default)]
pub build_profile: Option<String>,
/// Config installed as `hardhat.config.ts`, importing the project's own as `hardhat.config.base.<ext>`.
#[serde(default)]
pub config_overlay: Option<PathBuf>,
/// Whether the project is disabled.
#[serde(default)]
pub disabled: bool,
Expand Down
69 changes: 69 additions & 0 deletions solx-dev/src/test/hardhat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,30 @@ pub fn test(
],
)?;
}
if let Some(config_overlay) = project.config_overlay.as_deref() {
let config_file_name = config_file_name.ok_or_else(|| {
anyhow::anyhow!(
"Hardhat project {project_name} has a config overlay but no hardhat.config.ts or hardhat.config.js"
)
})?;
let base_config_file_name = config_file_name.replace(".config.", ".config.base.");
std::fs::rename(
project_directory.join(config_file_name),
project_directory.join(base_config_file_name.as_str()),
)
.map_err(|error| {
anyhow::anyhow!(
"Renaming {config_file_name} of Hardhat project {project_name} to {base_config_file_name}: {error}"
)
})?;
std::fs::copy(config_overlay, project_directory.join("hardhat.config.ts")).map_err(
|error| {
anyhow::anyhow!(
"Installing config overlay {config_overlay:?} into Hardhat project {project_name}: {error}"
)
},
)?;
}

let compiler_shim = compiler_shims
.get(identifier.as_str())
Expand All @@ -260,6 +284,9 @@ pub fn test(
if toolchain_name.contains("solx") {
npm_compile_command.env("USE_SOLX", "true");
npm_compile_command.env("SOLX", &*compiler_path_str);
if let Some(build_profile) = project.build_profile.as_deref() {
npm_compile_command.args(["--", "--build-profile", build_profile]);
}
}
npm_compile_command.env("VIA_IR", (codegen == "viaIR").to_string());
let build_timestamp_start = Instant::now();
Expand Down Expand Up @@ -297,6 +324,7 @@ pub fn test(
// toolchains can be identity-checked.
if toolchain_name.contains("solx") {
compiler_shim.verify(toolchain_name.as_str(), project_name.as_str())?;
stub_slang_build_info_asts(project_directory.as_path())?;
}

let mut npm_test_command = Command::new("npm");
Expand All @@ -312,6 +340,9 @@ pub fn test(
if toolchain_name.contains("solx") {
npm_test_command.env("USE_SOLX", "true");
npm_test_command.env("SOLX", &*compiler_path_str);
if let Some(build_profile) = project.build_profile.as_deref() {
npm_test_command.args(["--", "--build-profile", build_profile]);
}
}
npm_test_command.env("VIA_IR", (codegen == "viaIR").to_string());
let test_timestamp_start = Instant::now();
Expand Down Expand Up @@ -472,3 +503,41 @@ pub fn test(

Ok(())
}

///
/// Replaces every non-solc AST in the project's build infos with an empty solc source unit, since
/// EDR refuses to start on an AST it cannot read and slang emits its own format.
///
/// TODO: remove once Hardhat runs get slang stack traces (NomicFoundation/solx#806).
///
fn stub_slang_build_info_asts(project_directory: &std::path::Path) -> anyhow::Result<()> {
let pattern = project_directory.join("artifacts/build-info/*.json");
for path in glob::glob(pattern.to_string_lossy().as_ref())
.expect("Always valid")
.filter_map(Result::ok)
{
let content = std::fs::read_to_string(path.as_path())
.map_err(|error| anyhow::anyhow!("Reading build info {path:?}: {error}"))?;
let mut build_info: serde_json::Value = serde_json::from_str(content.as_str())
.map_err(|error| anyhow::anyhow!("Parsing build info {path:?}: {error}"))?;
let Some(sources) = build_info
.pointer_mut("/output/sources")
.and_then(serde_json::Value::as_object_mut)
else {
continue;
};
let mut is_stubbed = false;
for source in sources.values_mut() {
if source.pointer("/ast/nodeType").is_some() {
continue;
}
source["ast"] = serde_json::json!({ "nodeType": "SourceUnit", "nodes": [] });
is_stubbed = true;
}
if is_stubbed {
std::fs::write(path.as_path(), build_info.to_string())
.map_err(|error| anyhow::anyhow!("Writing build info {path:?}: {error}"))?;
}
}
Ok(())
}
Loading