-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(
linking
): correctly handle duplicated libraries, make library p…
…aths unique and fix path issues (#5364) * fix: use correct nonce to resolve lib address * fix: adjust test * chore: rm stray code * test: add more runner tests for linking * fix: correct nonce diagnostic * fix: adjust test * chore: forge fmt * chore: forge fmt * fix: resolve libraries correctly on dupe name * test: add more tests * refactor: simplify * chore: clippy * chore: properly strip prefix from all paths on scripting * chore: fmt --------- Co-authored-by: Oliver Nordbjerg <hi@notbjerg.me>
- Loading branch information
Showing
10 changed files
with
619 additions
and
199 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
|
||
// Linking scenario: contract has many dependencies, some of which appear to the linker | ||
// more than once. | ||
// | ||
// Each library should only have its address computed once | ||
|
||
library A { | ||
function increment(uint256 number, uint256 number2) external pure returns (uint256) { | ||
return number + number2; | ||
} | ||
} | ||
|
||
library B { | ||
function subtract(uint256 number) external pure returns (uint256) { | ||
return number - 1; | ||
} | ||
} | ||
|
||
library C { | ||
function double(uint256 number) external pure returns (uint256) { | ||
return A.increment(number, 0) + A.increment(number, 0); | ||
} | ||
} | ||
|
||
library D { | ||
function half(uint256 number) external pure returns (uint256) { | ||
return number / 2; | ||
} | ||
|
||
function sub2(uint256 number) external pure returns (uint256) { | ||
return B.subtract(number); | ||
} | ||
} | ||
|
||
library E { | ||
function pow(uint256 number, uint256 exponent) external pure returns (uint256) { | ||
return number ** exponent; | ||
} | ||
|
||
function quadruple(uint256 number) external pure returns (uint256) { | ||
return C.double(number) + C.double(number); | ||
} | ||
} | ||
|
||
contract LibraryConsumer { | ||
uint256 public number; | ||
|
||
function setNumber(uint256 newNumber) public { | ||
number = newNumber; | ||
} | ||
|
||
function increment() public { | ||
number++; | ||
} | ||
|
||
function add(uint256 num) external returns (uint256) { | ||
number = num; | ||
return A.increment(num, 1); | ||
} | ||
|
||
function sub(uint256 num) external returns (uint256) { | ||
number = num; | ||
return B.subtract(num); | ||
} | ||
|
||
function mul(uint256 num) external returns (uint256) { | ||
number = num; | ||
return C.double(num); | ||
} | ||
|
||
function div(uint256 num) external returns (uint256) { | ||
number = num; | ||
return D.half(num); | ||
} | ||
|
||
function pow(uint256 num, uint256 exponent) external returns (uint256) { | ||
number = num; | ||
return E.pow(num, exponent); | ||
} | ||
|
||
function sub2(uint256 num) external returns (uint256) { | ||
number = num; | ||
return D.sub2(num); | ||
} | ||
|
||
function quadruple(uint256 num) external returns (uint256) { | ||
number = num; | ||
return E.quadruple(num); | ||
} | ||
} | ||
|
||
contract DuplicateLibraryLinkingTest is DSTest { | ||
LibraryConsumer consumer; | ||
|
||
function setUp() public { | ||
consumer = new LibraryConsumer(); | ||
} | ||
|
||
function testA() public { | ||
assertEq(consumer.add(1), 2, "library call failed"); | ||
} | ||
|
||
function testB() public { | ||
consumer.setNumber(1); | ||
assertEq(consumer.sub(1), 0, "library call failed"); | ||
} | ||
|
||
function testC() public { | ||
consumer.setNumber(2); | ||
assertEq(consumer.mul(2), 4, "library call failed"); | ||
} | ||
|
||
function testD() public { | ||
consumer.setNumber(2); | ||
assertEq(consumer.div(2), 1, "library call failed"); | ||
} | ||
|
||
function testE() public { | ||
consumer.setNumber(2); | ||
assertEq(consumer.quadruple(2), 8, "library call failed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
|
||
// Linking scenario: contract with one library | ||
|
||
library Lib { | ||
function plus100(uint256 a) public pure returns (uint256) { | ||
return a + 100; | ||
} | ||
} | ||
|
||
contract LibraryConsumer { | ||
function consume(uint256 a) public pure returns (uint256) { | ||
return Lib.plus100(a); | ||
} | ||
} | ||
|
||
contract SimpleLibraryLinkingTest is DSTest { | ||
LibraryConsumer consumer; | ||
|
||
function setUp() public { | ||
consumer = new LibraryConsumer(); | ||
} | ||
|
||
function testCall() public { | ||
assertEq(consumer.consume(1), 101, "library call failed"); | ||
} | ||
} |
Oops, something went wrong.