Skip to content

Commit c052dc8

Browse files
authored
test(solx-dev): run OpenZeppelin on Hardhat 3 with the slang compiler (#804)
1 parent 222f925 commit c052dc8

5 files changed

Lines changed: 151 additions & 6 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import hardhatSlangSolx from '@nomicfoundation/hardhat-slang-solx';
2+
3+
import baseConfig from './hardhat.config.base.ts';
4+
5+
const { path: _path, ...defaultProfile } = baseConfig.solidity.profiles.default as {
6+
version: string;
7+
path?: string;
8+
};
9+
10+
export default {
11+
...baseConfig,
12+
plugins: [...baseConfig.plugins, hardhatSlangSolx],
13+
solidity: {
14+
profiles: {
15+
default: defaultProfile,
16+
...(process.env.USE_SOLX === 'true'
17+
? {
18+
'slang-solx': {
19+
type: 'slang-solx',
20+
version: defaultProfile.version,
21+
path: process.env.SOLX,
22+
settings: { viaIR: process.env.VIA_IR === 'true' },
23+
},
24+
}
25+
: {}),
26+
},
27+
},
28+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// hardhat-exposed generates its wrappers from the AST of the default profile, so solc stays there.
2+
import hardhatSlangSolx from '@nomicfoundation/hardhat-slang-solx';
3+
4+
import baseConfig from './hardhat.config.base.ts';
5+
6+
const solidity = baseConfig.solidity as { version: string; settings: { evmVersion: string } };
7+
8+
export default {
9+
...baseConfig,
10+
plugins: [...baseConfig.plugins, hardhatSlangSolx],
11+
solidity: {
12+
profiles: {
13+
default: solidity,
14+
...(process.env.USE_SOLX === 'true'
15+
? {
16+
'slang-solx': {
17+
type: 'slang-solx',
18+
version: solidity.version,
19+
path: process.env.SOLX,
20+
settings: {
21+
evmVersion: solidity.settings.evmVersion,
22+
viaIR: process.env.VIA_IR === 'true',
23+
},
24+
},
25+
}
26+
: {}),
27+
},
28+
},
29+
test: {
30+
...baseConfig.test,
31+
mocha: {
32+
reporter: 'json',
33+
reporterOptions: { output: process.env.JUNIT_REPORT },
34+
},
35+
},
36+
};

‎solx-dev/hardhat-tests.toml‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
# description = "..." # Project description
2828
# build_system = "npm" # npm, yarn, pnpm, or bun (default: npm)
2929
# dependencies = ["pkg@version"] # Additional npm dependencies to install
30+
# build_profile = "..." # Build profile solx toolchains compile and test with
31+
# config_overlay = "./path.ts" # Installed as hardhat.config.ts; imports the project's config as hardhat.config.base.<ext>
3032
# disabled = true/false # Whether to skip this project
3133
# [projects.<name>.env]
3234
# VAR = "value" # Environment variables for build/test
@@ -113,16 +115,19 @@ dependencies = [ "hardhat@2.29.1" ]
113115
url = "https://github.com/antonbaliasnikov/solx-hh3"
114116
commit = "bfe0366f7f5936ec9e5e76fa37fe21d284a53c88"
115117
description = "An example Hardhat v3 project with solx support."
116-
dependencies = [ "ts-node" ]
118+
dependencies = [ "ts-node", "hardhat@3.18.0", "@nomicfoundation/hardhat-mocha@3.0.2", "@nomicfoundation/hardhat-slang-solx@3.0.2" ]
119+
build_profile = "slang-solx"
120+
config_overlay = "./solx-dev/hardhat-overlays/hardhat-v3.config.ts"
117121

118122
[projects.openzeppelin]
119-
url = "https://github.com/antonbaliasnikov/openzeppelin-contracts"
120-
commit = "bd3c4ba5d950860ecd957a84acc65083c3aec20e"
123+
url = "https://github.com/OpenZeppelin/openzeppelin-contracts"
124+
commit = "6c2ab375a9c7db0fa8c21fba1ed3b2057f75dd75"
121125
description = "A library for secure smart contract development."
122-
build_system = "pnpm"
123-
dependencies = [ "hardhat@2.29.1" ]
126+
dependencies = [ "@nomicfoundation/hardhat-slang-solx@3.0.2" ]
127+
build_profile = "slang-solx"
128+
config_overlay = "./solx-dev/hardhat-overlays/openzeppelin.config.ts"
124129
[projects.openzeppelin.env]
125-
EVM_DISABLE_MEMORY_SAFE_ASM_CHECK = "true"
130+
COMPILER = "0.8.34"
126131

127132
[projects.zksafe]
128133
url = "https://github.com/antonbaliasnikov/zksafe"

‎solx-dev/src/test/hardhat/config/project/mod.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
pub mod build_system;
66

77
use std::collections::HashMap;
8+
use std::path::PathBuf;
89

910
use self::build_system::BuildSystem;
1011

@@ -30,6 +31,12 @@ pub struct Project {
3031
/// Environment variables required for every command.
3132
#[serde(default)]
3233
pub env: HashMap<String, String>,
34+
/// Build profile solx toolchains compile and test with.
35+
#[serde(default)]
36+
pub build_profile: Option<String>,
37+
/// Config installed as `hardhat.config.ts`, importing the project's own as `hardhat.config.base.<ext>`.
38+
#[serde(default)]
39+
pub config_overlay: Option<PathBuf>,
3340
/// Whether the project is disabled.
3441
#[serde(default)]
3542
pub disabled: bool,

‎solx-dev/src/test/hardhat/mod.rs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,30 @@ pub fn test(
242242
],
243243
)?;
244244
}
245+
if let Some(config_overlay) = project.config_overlay.as_deref() {
246+
let config_file_name = config_file_name.ok_or_else(|| {
247+
anyhow::anyhow!(
248+
"Hardhat project {project_name} has a config overlay but no hardhat.config.ts or hardhat.config.js"
249+
)
250+
})?;
251+
let base_config_file_name = config_file_name.replace(".config.", ".config.base.");
252+
std::fs::rename(
253+
project_directory.join(config_file_name),
254+
project_directory.join(base_config_file_name.as_str()),
255+
)
256+
.map_err(|error| {
257+
anyhow::anyhow!(
258+
"Renaming {config_file_name} of Hardhat project {project_name} to {base_config_file_name}: {error}"
259+
)
260+
})?;
261+
std::fs::copy(config_overlay, project_directory.join("hardhat.config.ts")).map_err(
262+
|error| {
263+
anyhow::anyhow!(
264+
"Installing config overlay {config_overlay:?} into Hardhat project {project_name}: {error}"
265+
)
266+
},
267+
)?;
268+
}
245269

246270
let compiler_shim = compiler_shims
247271
.get(identifier.as_str())
@@ -260,6 +284,9 @@ pub fn test(
260284
if toolchain_name.contains("solx") {
261285
npm_compile_command.env("USE_SOLX", "true");
262286
npm_compile_command.env("SOLX", &*compiler_path_str);
287+
if let Some(build_profile) = project.build_profile.as_deref() {
288+
npm_compile_command.args(["--", "--build-profile", build_profile]);
289+
}
263290
}
264291
npm_compile_command.env("VIA_IR", (codegen == "viaIR").to_string());
265292
let build_timestamp_start = Instant::now();
@@ -297,6 +324,7 @@ pub fn test(
297324
// toolchains can be identity-checked.
298325
if toolchain_name.contains("solx") {
299326
compiler_shim.verify(toolchain_name.as_str(), project_name.as_str())?;
327+
stub_slang_build_info_asts(project_directory.as_path())?;
300328
}
301329

302330
let mut npm_test_command = Command::new("npm");
@@ -312,6 +340,9 @@ pub fn test(
312340
if toolchain_name.contains("solx") {
313341
npm_test_command.env("USE_SOLX", "true");
314342
npm_test_command.env("SOLX", &*compiler_path_str);
343+
if let Some(build_profile) = project.build_profile.as_deref() {
344+
npm_test_command.args(["--", "--build-profile", build_profile]);
345+
}
315346
}
316347
npm_test_command.env("VIA_IR", (codegen == "viaIR").to_string());
317348
let test_timestamp_start = Instant::now();
@@ -472,3 +503,41 @@ pub fn test(
472503

473504
Ok(())
474505
}
506+
507+
///
508+
/// Replaces every non-solc AST in the project's build infos with an empty solc source unit, since
509+
/// EDR refuses to start on an AST it cannot read and slang emits its own format.
510+
///
511+
/// TODO: remove once Hardhat runs get slang stack traces (NomicFoundation/solx#806).
512+
///
513+
fn stub_slang_build_info_asts(project_directory: &std::path::Path) -> anyhow::Result<()> {
514+
let pattern = project_directory.join("artifacts/build-info/*.json");
515+
for path in glob::glob(pattern.to_string_lossy().as_ref())
516+
.expect("Always valid")
517+
.filter_map(Result::ok)
518+
{
519+
let content = std::fs::read_to_string(path.as_path())
520+
.map_err(|error| anyhow::anyhow!("Reading build info {path:?}: {error}"))?;
521+
let mut build_info: serde_json::Value = serde_json::from_str(content.as_str())
522+
.map_err(|error| anyhow::anyhow!("Parsing build info {path:?}: {error}"))?;
523+
let Some(sources) = build_info
524+
.pointer_mut("/output/sources")
525+
.and_then(serde_json::Value::as_object_mut)
526+
else {
527+
continue;
528+
};
529+
let mut is_stubbed = false;
530+
for source in sources.values_mut() {
531+
if source.pointer("/ast/nodeType").is_some() {
532+
continue;
533+
}
534+
source["ast"] = serde_json::json!({ "nodeType": "SourceUnit", "nodes": [] });
535+
is_stubbed = true;
536+
}
537+
if is_stubbed {
538+
std::fs::write(path.as_path(), build_info.to_string())
539+
.map_err(|error| anyhow::anyhow!("Writing build info {path:?}: {error}"))?;
540+
}
541+
}
542+
Ok(())
543+
}

0 commit comments

Comments
 (0)