Skip to content

Commit 9b305a0

Browse files
authored
Optimize delegatecall check (#295)
1 parent b34157d commit 9b305a0

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

contracts/accessors/SimulateTxAccessor.sol

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import "../base/Executor.sol";
66
/// @title Simulate Transaction Accessor - can be used with StorageAccessible to simulate Safe transactions
77
/// @author Richard Meissner - <richard@gnosis.pm>
88
contract SimulateTxAccessor is Executor {
9-
bytes32 private constant GUARD_VALUE = keccak256("simulate_tx_accessor.guard.bytes32");
10-
bytes32 private guard;
9+
address private immutable accessorSingleton;
1110

1211
constructor() {
13-
guard = GUARD_VALUE;
12+
accessorSingleton = address(this);
1413
}
1514

1615
modifier onlyDelegateCall() {
17-
require(guard != GUARD_VALUE, "SimulateTxAccessor should only be called via delegatecall");
16+
require(address(this) != accessorSingleton, "SimulateTxAccessor should only be called via delegatecall");
1817
_;
1918
}
2019

contracts/libraries/Migrate_1_3_0_to_1_2_0.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ pragma solidity >=0.7.0 <0.9.0;
44
/// @title Migration - migrates a Safe contract from 1.3.0 to 1.2.0
55
/// @author Richard Meissner - <richard@gnosis.io>
66
contract Migration {
7-
bytes32 private constant GUARD_VALUE = keccak256("migration_130_to_120.guard.bytes32");
87
bytes32 private constant DOMAIN_SEPARATOR_TYPEHASH = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;
98

9+
address public immutable migrationSingleton;
1010
address public immutable safe120Singleton;
1111

1212
constructor(address targetSingleton) {
1313
require(targetSingleton != address(0), "Invalid singleton address provided");
1414
safe120Singleton = targetSingleton;
15-
guard = GUARD_VALUE;
15+
migrationSingleton = address(this);
1616
}
1717

1818
event ChangedMasterCopy(address singleton);
@@ -33,7 +33,7 @@ contract Migration {
3333

3434
/// @dev Allows to migrate the contract. This can only be called via a delegatecall.
3535
function migrate() public {
36-
require(guard != GUARD_VALUE, "Migration should only be called via delegatecall");
36+
require(address(this) != migrationSingleton, "Migration should only be called via delegatecall");
3737
// Master copy address cannot be null.
3838
singleton = safe120Singleton;
3939
domainSeparator = keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, this));

contracts/libraries/MultiSend.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ pragma solidity >=0.7.0 <0.9.0;
77
/// @author Stefan George - <stefan@gnosis.io>
88
/// @author Richard Meissner - <richard@gnosis.io>
99
contract MultiSend {
10-
bytes32 private constant GUARD_VALUE = keccak256("multisend.guard.bytes32");
11-
12-
bytes32 private guard;
10+
address private immutable multisendSingleton;
1311

1412
constructor() {
15-
guard = GUARD_VALUE;
13+
multisendSingleton = address(this);
1614
}
1715

1816
/// @dev Sends multiple transactions and reverts all if one fails.
@@ -26,7 +24,7 @@ contract MultiSend {
2624
/// @notice This method is payable as delegatecalls keep the msg.value from the previous call
2725
/// If the calling method (e.g. execTransaction) received ETH this would revert otherwise
2826
function multiSend(bytes memory transactions) public payable {
29-
require(guard != GUARD_VALUE, "MultiSend should only be called via delegatecall");
27+
require(address(this) != multisendSingleton, "MultiSend should only be called via delegatecall");
3028
// solhint-disable-next-line no-inline-assembly
3129
assembly {
3230
let length := mload(transactions)

test/migration/subTests.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BigNumber } from "@ethersproject/bignumber";
1+
import { BigNumber } from "ethers";
22
import { Contract } from "@ethersproject/contracts"
33
import { parseEther } from "@ethersproject/units"
44
import { expect } from "chai";

0 commit comments

Comments
 (0)