Skip to content

Commit

Permalink
♻️ Fix DelegateCheckerLib (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored Jul 1, 2024
1 parent da3f382 commit 4f273e6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
12 changes: 6 additions & 6 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ DateTimeLibTest:testNthWeekdayInMonthOfYearTimestamp() (gas: 12053)
DateTimeLibTest:testNthWeekdayInMonthOfYearTimestamp(uint256,uint256,uint256,uint256) (runs: 299, μ: 3568, ~: 3603)
DateTimeLibTest:testWeekday() (gas: 682)
DateTimeLibTest:test__codesize() (gas: 19095)
DelegateCheckerLibTest:testCheckDelegateForAll(address,address,bytes32) (runs: 299, μ: 345176, ~: 244162)
DelegateCheckerLibTest:testCheckDelegateForContract(address,address,address,bytes32) (runs: 299, μ: 416276, ~: 346951)
DelegateCheckerLibTest:testCheckDelegateForERC1155(address,address,address,uint256,bytes32) (runs: 299, μ: 417351, ~: 285585)
DelegateCheckerLibTest:testCheckDelegateForERC20(address,address,address,bytes32) (runs: 299, μ: 408210, ~: 286729)
DelegateCheckerLibTest:testCheckDelegateForERC721(address,address,address,uint256,bytes32) (runs: 299, μ: 566298, ~: 484012)
DelegateCheckerLibTest:test__codesize() (gas: 29602)
DelegateCheckerLibTest:testCheckDelegateForAll(address,address,bytes32) (runs: 299, μ: 313621, ~: 243866)
DelegateCheckerLibTest:testCheckDelegateForContract(address,address,address,bytes32) (runs: 299, μ: 400470, ~: 343029)
DelegateCheckerLibTest:testCheckDelegateForERC1155(address,address,address,uint256,bytes32) (runs: 299, μ: 383576, ~: 287255)
DelegateCheckerLibTest:testCheckDelegateForERC20(address,address,address,bytes32) (runs: 299, μ: 419055, ~: 356157)
DelegateCheckerLibTest:testCheckDelegateForERC721(address,address,address,uint256,bytes32) (runs: 299, μ: 525231, ~: 473777)
DelegateCheckerLibTest:test__codesize() (gas: 29728)
DeploylessPredeployQueryerTest:testPredeployQueryer(bytes32) (runs: 299, μ: 230933, ~: 227869)
DeploylessPredeployQueryerTest:test__codesize() (gas: 6732)
DynamicBufferLibTest:testClear(uint256) (runs: 299, μ: 34543, ~: 34592)
Expand Down
8 changes: 4 additions & 4 deletions src/utils/ext/delegatexyz/DelegateCheckerLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ library DelegateCheckerLib {
if not(amount) {
mstore(m, 0x90c9a2d0) // `checkDelegateForContract(address,address,address)`.
let t := staticcall(gas(), DELEGATE_REGISTRY_V1, add(m, 0x1c), 0x64, m, 0x20)
amount := sub(0, and(eq(mload(m), 1), t))
amount := or(sub(0, and(eq(mload(m), 1), t)), amount)
}
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ library DelegateCheckerLib {
if iszero(or(rights, iszero(not(amount)))) {
mstore(m, 0x90c9a2d0) // `checkDelegateForContract(address,address,address)`.
let t := staticcall(gas(), DELEGATE_REGISTRY_V1, add(m, 0x1c), 0x64, m, 0x20)
amount := sub(0, and(eq(mload(m), 1), t))
amount := or(sub(0, and(eq(mload(m), 1), t)), amount)
}
}
}
Expand Down Expand Up @@ -343,7 +343,7 @@ library DelegateCheckerLib {
if not(amount) {
mstore(m, 0x90c9a2d0) // `checkDelegateForContract(address,address,address)`.
let t := staticcall(gas(), DELEGATE_REGISTRY_V1, add(m, 0x1c), 0x64, m, 0x20)
amount := sub(0, and(eq(mload(m), 1), t))
amount := or(sub(0, and(eq(mload(m), 1), t)), amount)
}
}
}
Expand Down Expand Up @@ -384,7 +384,7 @@ library DelegateCheckerLib {
if iszero(or(rights, iszero(not(amount)))) {
mstore(m, 0x90c9a2d0) // `checkDelegateForContract(address,address,address)`.
let t := staticcall(gas(), DELEGATE_REGISTRY_V1, add(m, 0x1c), 0x64, m, 0x20)
amount := sub(0, and(eq(mload(m), 1), t))
amount := or(sub(0, and(eq(mload(m), 1), t)), amount)
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions test/DelegateCheckerLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ contract DelegateCheckerLibTest is SoladyTest {
}

function _maybeMutateRights(bytes32 rights) internal returns (bytes32) {
if (_random() % 2 == 0) return bytes32(0);
if (_random() % 2 == 0) return bytes32(_random());
if (_random() % 32 == 0) return bytes32(0);
if (_random() % 8 == 0) return bytes32(_random());
return rights;
}

Expand Down Expand Up @@ -200,6 +200,7 @@ contract DelegateCheckerLibTest is SoladyTest {
bytes32 rights
) public {
_maybeDelegateContract(to, from, contract_, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateAll(to, from, rights);
rights = _maybeMutateRights(rights);
_maybeBrutalizeMemory();
Expand Down Expand Up @@ -229,7 +230,9 @@ contract DelegateCheckerLibTest is SoladyTest {
bytes32 rights
) public {
_maybeDelegateERC721(to, from, contract_, id, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateContract(to, from, contract_, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateAll(to, from, rights);
rights = _maybeMutateRights(rights);
id = _maybeMutateId(id);
Expand All @@ -256,7 +259,9 @@ contract DelegateCheckerLibTest is SoladyTest {
public
{
_maybeDelegateERC20(to, from, contract_, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateContract(to, from, contract_, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateAll(to, from, rights);
rights = _maybeMutateRights(rights);
_maybeBrutalizeMemory();
Expand Down Expand Up @@ -290,7 +295,9 @@ contract DelegateCheckerLibTest is SoladyTest {
bytes32 rights
) public {
_maybeDelegateERC1155(to, from, contract_, id, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateContract(to, from, contract_, rights);
rights = _maybeMutateRights(rights);
if (_random() % 2 == 0) _maybeDelegateAll(to, from, rights);
rights = _maybeMutateRights(rights);
id = _maybeMutateId(id);
Expand Down

0 comments on commit 4f273e6

Please sign in to comment.