-
Notifications
You must be signed in to change notification settings - Fork 137
Post Sherlock L2 Upgrade via forced inclusion #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
473f18f
Modifying for forced inclusion
anikaraghu 7a96dc0
Temp fix, hardcode Portal addr
anikaraghu 7581c75
Use multicall with delegate address
anikaraghu af9f838
Call multicall on L1
anikaraghu 6c75788
Pass in chain ID
anikaraghu 86aabb9
Comments
anikaraghu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,203 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
import { console } from "forge-std/console.sol"; | ||
import { IMulticall3 } from "forge-std/interfaces/IMulticall3.sol"; | ||
|
||
import { LibSort } from "@eth-optimism-bedrock/scripts/libraries/LibSort.sol"; | ||
import { IGnosisSafe, Enum } from "@eth-optimism-bedrock/scripts/interfaces/IGnosisSafe.sol"; | ||
import { EnhancedScript } from "@eth-optimism-bedrock/scripts/universal/EnhancedScript.sol"; | ||
import { GlobalConstants } from "@eth-optimism-bedrock/scripts/universal/GlobalConstants.sol"; | ||
|
||
/** | ||
* @title SafeBuilder | ||
* @notice Builds SafeTransactions | ||
* Assumes that a gnosis safe is used as the privileged account and the same | ||
* gnosis safe is the owner the proxy admin. | ||
* This could be optimized by checking for the number of approvals up front | ||
* and not submitting the final approval as `execTransaction` can be called when | ||
* there are `threshold - 1` approvals. | ||
* Uses the "approved hashes" method of interacting with the gnosis safe. Allows | ||
* for the most simple user experience when using automation and no indexer. | ||
* Run the command without the `--broadcast` flag and it will print a tenderly URL. | ||
*/ | ||
abstract contract SafeBuilder is EnhancedScript, GlobalConstants { | ||
|
||
/** | ||
* @notice Interface for multicall3. | ||
*/ | ||
IMulticall3 internal constant multicall = IMulticall3(MULTICALL3_ADDRESS); | ||
|
||
/** | ||
* @notice An array of approvals, used to generate the execution transaction. | ||
*/ | ||
address[] internal approvals; | ||
|
||
/** | ||
* ----------------------------------------------------------- | ||
* Virtual Functions | ||
* ----------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* @notice Follow up assertions to ensure that the script ran to completion. | ||
*/ | ||
function _postCheck() internal virtual view; | ||
|
||
/** | ||
* @notice Creates the calldata | ||
*/ | ||
function buildCalldata() internal virtual view returns (bytes memory); | ||
|
||
|
||
/** | ||
* ----------------------------------------------------------- | ||
* Implemented Functions | ||
* ----------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* @notice The entrypoint to this script. | ||
*/ | ||
function run(address _safe) public returns (bool) { | ||
vm.startBroadcast(); | ||
bool success = _run(_safe); | ||
if (success) _postCheck(); | ||
return success; | ||
} | ||
|
||
/** | ||
* @notice Computes the safe transaction hash for the provided safe and proxy admin. | ||
*/ | ||
function _getTransactionHash(address _safe) internal view returns (bytes32) { | ||
// Ensure that the required contracts exist | ||
require(address(multicall).code.length > 0, "multicall3 not deployed"); | ||
require(_safe.code.length > 0, "no code at safe address"); | ||
|
||
IGnosisSafe safe = IGnosisSafe(payable(_safe)); | ||
uint256 nonce = safe.nonce(); | ||
|
||
bytes memory data = buildCalldata(); | ||
|
||
// Compute the safe transaction hash | ||
bytes32 hash = safe.getTransactionHash({ | ||
to: address(multicall), | ||
value: 0, | ||
data: data, | ||
operation: Enum.Operation.DelegateCall, | ||
safeTxGas: 0, | ||
baseGas: 0, | ||
gasPrice: 0, | ||
gasToken: address(0), | ||
refundReceiver: address(0), | ||
_nonce: nonce | ||
}); | ||
|
||
return hash; | ||
} | ||
|
||
/** | ||
* @notice The implementation of the upgrade. Split into its own function | ||
* to allow for testability. This is subject to a race condition if | ||
* the nonce changes by a different transaction finalizing while not | ||
* all of the signers have used this script. | ||
*/ | ||
function _run(address _safe) public returns (bool) { | ||
IGnosisSafe safe = IGnosisSafe(payable(_safe)); | ||
bytes memory data = buildCalldata(); | ||
|
||
// Compute the safe transaction hash | ||
bytes32 hash = _getTransactionHash(_safe); | ||
|
||
// Send a transaction to approve the hash | ||
safe.approveHash(hash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we make it so we only approve if the given owner hasn't already? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed this would be good to save gas 👍 I'll address this as a follow up |
||
|
||
logSimulationLink({ | ||
_to: address(safe), | ||
_from: msg.sender, | ||
_data: abi.encodeCall(safe.approveHash, (hash)) | ||
}); | ||
|
||
uint256 threshold = safe.getThreshold(); | ||
address[] memory owners = safe.getOwners(); | ||
|
||
for (uint256 i; i < owners.length; i++) { | ||
address owner = owners[i]; | ||
uint256 approved = safe.approvedHashes(owner, hash); | ||
if (approved == 1) { | ||
approvals.push(owner); | ||
} | ||
} | ||
|
||
if (approvals.length >= threshold) { | ||
bytes memory signatures = buildSignatures(); | ||
|
||
bool success = safe.execTransaction({ | ||
to: address(multicall), | ||
value: 0, | ||
data: data, | ||
operation: Enum.Operation.DelegateCall, | ||
safeTxGas: 0, | ||
baseGas: 0, | ||
gasPrice: 0, | ||
gasToken: address(0), | ||
refundReceiver: payable(address(0)), | ||
signatures: signatures | ||
}); | ||
|
||
logSimulationLink({ | ||
_to: address(safe), | ||
_from: msg.sender, | ||
_data: abi.encodeCall( | ||
safe.execTransaction, | ||
( | ||
address(multicall), | ||
0, | ||
data, | ||
Enum.Operation.DelegateCall, | ||
0, | ||
0, | ||
0, | ||
address(0), | ||
payable(address(0)), | ||
signatures | ||
) | ||
) | ||
}); | ||
|
||
require(success, "call not successful"); | ||
return true; | ||
} else { | ||
console.log("not enough approvals"); | ||
} | ||
|
||
// Reset the approvals because they are only used transiently. | ||
assembly { | ||
sstore(approvals.slot, 0) | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @notice Builds the signatures by tightly packing them together. | ||
* Ensures that they are sorted. | ||
*/ | ||
function buildSignatures() internal view returns (bytes memory) { | ||
address[] memory addrs = new address[](approvals.length); | ||
for (uint256 i; i < approvals.length; i++) { | ||
addrs[i] = approvals[i]; | ||
} | ||
|
||
LibSort.sort(addrs); | ||
|
||
bytes memory signatures; | ||
uint8 v = 1; | ||
bytes32 s = bytes32(0); | ||
for (uint256 i; i < addrs.length; i++) { | ||
bytes32 r = bytes32(uint256(uint160(addrs[i]))); | ||
signatures = bytes.concat(signatures, abi.encodePacked(r, s, v)); | ||
} | ||
return signatures; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff with Optimism's code: