Skip to content

Commit

Permalink
fix(coverage): relax deployed bytecode accepted score (foundry-rs#8657)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Aug 13, 2024
1 parent e36bc81 commit 1c71ab1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
8 changes: 5 additions & 3 deletions crates/common/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArtifactWithContractRef<'_>> {
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<ArtifactWithContractRef<'_>> {
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<ArtifactWithContractRef<'_>> {
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
}
Expand Down
69 changes: 69 additions & 0 deletions crates/forge/tests/cli/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
"#]]);
});

0 comments on commit 1c71ab1

Please sign in to comment.