diff --git a/crates/common/src/contracts.rs b/crates/common/src/contracts.rs index 7e07f9db6471..38f73a7133f5 100644 --- a/crates/common/src/contracts.rs +++ b/crates/common/src/contracts.rs @@ -115,24 +115,26 @@ impl ContractsByArtifact { /// Finds a contract which has a similar bytecode as `code`. pub fn find_by_creation_code(&self, code: &[u8]) -> Option> { - self.find_by_code(code, ContractData::bytecode) + self.find_by_code(code, 0.1, ContractData::bytecode) } /// Finds a contract which has a similar deployed bytecode as `code`. pub fn find_by_deployed_code(&self, code: &[u8]) -> Option> { - self.find_by_code(code, ContractData::deployed_bytecode) + self.find_by_code(code, 0.15, ContractData::deployed_bytecode) } + /// Finds a contract based on provided bytecode and accepted match score. fn find_by_code( &self, code: &[u8], + accepted_score: f64, get: impl Fn(&ContractData) -> Option<&Bytes>, ) -> Option> { self.iter() .filter_map(|(id, contract)| { if let Some(deployed_bytecode) = get(contract) { let score = bytecode_diff_score(deployed_bytecode.as_ref(), code); - (score <= 0.1).then_some((score, (id, contract))) + (score <= accepted_score).then_some((score, (id, contract))) } else { None } diff --git a/crates/forge/tests/cli/coverage.rs b/crates/forge/tests/cli/coverage.rs index 078e06d9512a..1b668afacd53 100644 --- a/crates/forge/tests/cli/coverage.rs +++ b/crates/forge/tests/cli/coverage.rs @@ -1202,3 +1202,72 @@ contract AContractTest is DSTest { "#]], ); }); + +forgetest!(test_identical_bytecodes, |prj, cmd| { + prj.insert_ds_test(); + prj.add_source( + "AContract.sol", + r#" +contract AContract { + uint256 public number; + address public immutable usdc1; + address public immutable usdc2; + address public immutable usdc3; + address public immutable usdc4; + address public immutable usdc5; + address public immutable usdc6; + + constructor() { + address a = 0x176211869cA2b568f2A7D4EE941E073a821EE1ff; + usdc1 = a; + usdc2 = a; + usdc3 = a; + usdc4 = a; + usdc5 = a; + usdc6 = a; + } + + function setNumber(uint256 newNumber) public { + number = newNumber; + } + + function increment() public { + number++; + } +} + "#, + ) + .unwrap(); + + prj.add_source( + "AContractTest.sol", + r#" +import "./test.sol"; +import {AContract} from "./AContract.sol"; + +contract AContractTest is DSTest { + AContract public counter; + + function setUp() public { + counter = new AContract(); + counter.setNumber(0); + } + + function test_Increment() public { + counter.increment(); + assertEq(counter.number(), 1); + } +} + "#, + ) + .unwrap(); + + cmd.arg("coverage").args(["--summary".to_string()]).assert_success().stdout_eq(str![[r#" +... +| File | % Lines | % Statements | % Branches | % Funcs | +|-------------------|---------------|---------------|---------------|---------------| +| src/AContract.sol | 100.00% (9/9) | 100.00% (9/9) | 100.00% (0/0) | 100.00% (3/3) | +| Total | 100.00% (9/9) | 100.00% (9/9) | 100.00% (0/0) | 100.00% (3/3) | + +"#]]); +});