Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace line endings on Windows to enforce deterministic metadata #108

Merged
merged 4 commits into from
Apr 20, 2024
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
16 changes: 13 additions & 3 deletions src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,11 @@ impl Source {
let file = file.as_ref();
trace!(file=%file.display());
let content = fs::read_to_string(file).map_err(|err| SolcIoError::new(err, file))?;

// Normalize line endings to ensure deterministic metadata.
#[cfg(windows)]
let content = content.replace("\r\n", "\n");

Ok(Self::new(content))
}

Expand Down Expand Up @@ -1534,9 +1539,14 @@ impl Source {
/// async version of `Self::read`
pub async fn async_read(file: impl AsRef<Path>) -> Result<Self, SolcIoError> {
let file = file.as_ref();
Ok(Self::new(
tokio::fs::read_to_string(file).await.map_err(|err| SolcIoError::new(err, file))?,
))
let content =
tokio::fs::read_to_string(file).await.map_err(|err| SolcIoError::new(err, file))?;

// Normalize line endings to ensure deterministic metadata.
#[cfg(windows)]
let content = content.replace("\r\n", "\n");

Ok(Self::new(content))
}

/// Finds all source files under the given dir path and reads them all
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

#[macro_use]
extern crate tracing;
Expand Down
1 change: 1 addition & 0 deletions test-data/dapp-test-bytecode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405260016000806101000a81548160ff02191690831515021790555034801561002a57600080fd5b506103118061003a6000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80630a9254e41461005c5780636dae014c1461006657806372c1591c14610070578063ba414fa61461007a578063fa7626d414610098575b600080fd5b6100646100b6565b005b61006e610121565b005b61007861012d565b005b610082610139565b60405161008f91906101e7565b60405180910390f35b6100a061014c565b6040516100ad91906101e7565b60405180910390f35b6040516100c2906101c0565b604051809103906000f0801580156100de573d6000803e3d6000fd5b50600060026101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b61012b600161015d565b565b610137600061015d565b565b600060019054906101000a900460ff1681565b60008054906101000a900460ff1681565b806101a0577f41304facd9323d75b11bcdd609cb38effffdb05710f7caf0e9b16c6d9d709f5060405161018f9061025f565b60405180910390a161019f6101a3565b5b50565b6001600060016101000a81548160ff021916908315150217905550565b605c8061028083390190565b60008115159050919050565b6101e1816101cc565b82525050565b60006020820190506101fc60008301846101d8565b92915050565b600082825260208201905092915050565b7f4572726f723a20417373657274696f6e204661696c6564000000000000000000600082015250565b6000610249601783610202565b915061025482610213565b602082019050919050565b600060208201905081810360008301526102788161023c565b905091905056fe6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea264697066735822122024c9edaf1ab40e0c9811ed82a763989adf05e776e99d615875518e2a1b717f4564736f6c63430008120033a2646970667358221220fb97e3945b4df641b270bbe48c219350e75f459ebec1cf796d6c5434a1fe5f0464736f6c63430008120033
25 changes: 24 additions & 1 deletion tests/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! project tests

use alloy_primitives::Address;
use alloy_primitives::{Address, Bytes};
use foundry_compilers::{
artifacts::{
BytecodeHash, DevDoc, ErrorDoc, EventDoc, Libraries, MethodDoc, ModelCheckerEngine::CHC,
Expand Down Expand Up @@ -3719,3 +3719,26 @@ contract D {
// Check that all contracts were recompiled
assert_eq!(output.compiled_artifacts().len(), 4);
}

#[test]
fn test_deterministic_metadata() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-sample");
let paths = ProjectPathsConfig::builder().sources(root.join("src")).lib(root.join("lib"));
let mut project = TempProject::<ConfigurableArtifacts>::new(paths).unwrap();

project.set_solc("0.8.18");

let compiled = project.compile().unwrap();
compiled.assert_success();
let artifact = compiled.find_first("DappTest").unwrap();

let bytecode = artifact.bytecode.as_ref().unwrap().bytes().unwrap().clone();
let expected_bytecode = Bytes::from_str(
&std::fs::read_to_string(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data/dapp-test-bytecode.txt"),
)
.unwrap(),
)
.unwrap();
assert_eq!(bytecode, expected_bytecode);
}