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

Optimize Address.functionCall removing redundant isContract check #3469

Merged
merged 6 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/mocks/crosschain/bridges.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract contract BaseRelayMock {
_currentSender = sender;

(bool success, bytes memory returndata) = target.call(data);
Address.verifyCallResult(success, returndata, "low-level call reverted");
Address.verifyCallResult(target, success, returndata, "low-level call reverted");

_currentSender = previousSender;
}
Expand Down
54 changes: 35 additions & 19 deletions contracts/utils/Address.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ library Address {
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");

(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
return verifyCallResult(target, success, returndata, errorMessage);
}

/**
Expand All @@ -159,10 +157,8 @@ library Address {
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");

(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
return verifyCallResult(target, success, returndata, errorMessage);
}

/**
Expand All @@ -186,10 +182,8 @@ library Address {
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");

(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
return verifyCallResult(target, success, returndata, errorMessage);
}

/**
Expand All @@ -198,6 +192,24 @@ library Address {
*
* _Available since v4.3._
*/
function verifyCallResult(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}

function verifyCallResult(
bool success,
bytes memory returndata,
Expand All @@ -206,17 +218,21 @@ library Address {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
_revert(returndata, errorMessage);
}
}

function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
6 changes: 3 additions & 3 deletions test/utils/Address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ contract('Address', function (accounts) {
}, []);

await expectRevert(
this.mock.functionCall(this.contractRecipient.address, abiEncodedCall, { gas: '100000' }),
this.mock.functionCall(this.contractRecipient.address, abiEncodedCall, { gas: '120000' }),
'Address: low-level call failed',
);
});
Expand Down Expand Up @@ -329,7 +329,7 @@ contract('Address', function (accounts) {
}, []);
await expectRevert(
this.mock.functionStaticCall(recipient, abiEncodedCall),
'Address: static call to non-contract',
'Address: call to non-contract',
);
});
});
Expand Down Expand Up @@ -375,7 +375,7 @@ contract('Address', function (accounts) {
}, []);
await expectRevert(
this.mock.functionDelegateCall(recipient, abiEncodedCall),
'Address: delegate call to non-contract',
'Address: call to non-contract',
);
});
});
Expand Down