Skip to content

Commit

Permalink
C4 audit fixes and mainnet deployment (#63)
Browse files Browse the repository at this point in the history
C4 audit fixes and mainnet deployment to 0x00000000000000447e69651d841bD8D104Bed493
  • Loading branch information
0xfoobar authored Sep 21, 2023
1 parent 6d1254d commit a7135f6
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 171 deletions.
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
src = 'src'
out = 'out'
libs = ['lib']

auto_detect_remappings = false
# remappings = []

solc_version = "0.8.21"
# EVM version must be Paris not Shanghai to prevent PUSH0 incompatibility with other EVM chains
# Extra 137 deployment size, extra 0.1% runtime gas costs from using older version
Expand All @@ -18,4 +22,4 @@ via_ir = false
line_length = 180
wrap_comments = true # Increases readability of comments

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
24 changes: 17 additions & 7 deletions gasbenchmark10mil
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
No files changed, compilation skipped

Running 1 test for test/GasBenchmark.t.sol:GasBenchmark
[PASS] testGas(address,bytes32) (runs: 256, μ: 13573356, ~: 13573452)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 247.64ms
| src/DelegateRegistry.sol:DelegateRegistry contract | | | | | |
|----------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 1995913 | 10001 | | | | |
| 2011327 | 10078 | | | | |
| Function Name | min | avg | median | max | # calls |
| checkDelegateForAll | 2910 | 3106 | 3106 | 3303 | 2 |
| checkDelegateForContract | 5399 | 5807 | 5807 | 6216 | 2 |
| checkDelegateForERC1155 | 7840 | 8458 | 8458 | 9077 | 2 |
| checkDelegateForERC20 | 7790 | 8402 | 8402 | 9014 | 2 |
| checkDelegateForERC721 | 7883 | 8515 | 8515 | 9148 | 2 |
| checkDelegateForAll | 3002 | 3198 | 3198 | 3395 | 2 |
| checkDelegateForContract | 5491 | 5899 | 5899 | 6308 | 2 |
| checkDelegateForERC1155 | 7932 | 8550 | 8550 | 9169 | 2 |
| checkDelegateForERC20 | 7882 | 8494 | 8494 | 9106 | 2 |
| checkDelegateForERC721 | 7975 | 8607 | 8607 | 9240 | 2 |
| delegateAll | 135825 | 135825 | 135825 | 135825 | 2 |
| delegateContract | 114433 | 125383 | 125383 | 136333 | 2 |
| delegateERC1155 | 5710 | 93282 | 93282 | 180854 | 2 |
| delegateERC20 | 5357 | 81865 | 81865 | 158374 | 2 |
| delegateERC721 | 136921 | 147871 | 147871 | 158821 | 2 |
| multicall | 404294 | 404294 | 404294 | 404294 | 1 |
| multicall | 404294 | 404294 | 404294 | 404294 | 1 |




Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
12 changes: 11 additions & 1 deletion hashbenchmark10mil
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
No files changed, compilation skipped

Running 1 test for test/HashBenchmark.t.sol:HashBenchmark
[PASS] testHashGas(address,bytes32,address,uint256,address,bytes32) (runs: 256, μ: 19906, ~: 19906)
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 32.07ms
| test/HashBenchmark.t.sol:HashHarness contract | | | | | |
|-----------------------------------------------|-----------------|-----|--------|-----|---------|
| Deployment Cost | Deployment Size | | | | |
Expand All @@ -14,4 +19,9 @@
| erc20Location | 793 | 793 | 793 | 793 | 1 |
| erc721Hash | 830 | 830 | 830 | 830 | 1 |
| erc721Location | 867 | 867 | 867 | 867 | 1 |
| location | 384 | 384 | 384 | 384 | 1 |
| location | 384 | 384 | 384 | 384 | 1 |




Ran 1 test suites: 1 tests passed, 0 failed, 0 skipped (1 total tests)
4 changes: 1 addition & 3 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ds-test/=lib/forge-std/lib/ds-test/src/
erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/
forge-std/=lib/forge-std/src/
murky/=lib/murky/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/
openzeppelin/=lib/openzeppelin-contracts/contracts/
openzeppelin/=lib/openzeppelin-contracts/contracts/
15 changes: 14 additions & 1 deletion script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.21;
import {Script} from "forge-std/Script.sol";
import {console2} from "forge-std/console2.sol";
import {DelegateRegistry} from "../src/DelegateRegistry.sol";
import {Singlesig} from "../src/singlesig/Singlesig.sol";

interface ImmutableCreate2Factory {
function safeCreate2(bytes32 salt, bytes calldata initCode) external payable returns (address deploymentAddress);
Expand All @@ -14,15 +15,27 @@ interface ImmutableCreate2Factory {
contract Deploy is Script {
ImmutableCreate2Factory immutable factory = ImmutableCreate2Factory(0x0000000000FFe8B47B3e2130213B802212439497);
bytes initCode = type(DelegateRegistry).creationCode;
bytes32 salt = 0x00000000000000000000000000000000000000008b99e5a778edb02572010000;
// bytes32 salt = 0x0000000000000000000000000000000000000000fbe49ecfc3decb1164228b89;
bytes32 salt = 0x00000000000000000000000000000000000000002bbc593dd77cb93fbb932d5f;

// bytes initCode = abi.encodePacked(type(Singlesig).creationCode, abi.encode(address(0x6Ed7D526b020780f694f3c10Dfb25E1b134D3215)));
// bytes32 salt = 0x000000000000000000000000000000000000000016c7768a8c7a2824b846321d;

function run() external {
vm.startBroadcast();

// address singlesigAddress = factory.safeCreate2(salt, initCode);
// Singlesig singlesig = Singlesig(payable(singlesigAddress));
// console2.log(address(singlesig));

address registryAddress = factory.safeCreate2(salt, initCode);
DelegateRegistry registry = DelegateRegistry(registryAddress);
console2.log(address(registry));

// address registryAddress = factory.safeCreate2(salt, initCode);
// DelegateRegistry registry = DelegateRegistry(registryAddress);
// console2.log(address(registry));

vm.stopBroadcast();
}
}
10 changes: 4 additions & 6 deletions src/DelegateRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ contract DelegateRegistry is IDelegateRegistry {
}
} else if (loadedFrom == msg.sender) {
_updateFrom(location, Storage.DELEGATION_REVOKED);
_writeDelegation(location, Storage.POSITIONS_AMOUNT, amount);
_writeDelegation(location, Storage.POSITIONS_AMOUNT, uint256(0));
}
emit DelegateERC20(msg.sender, to, contract_, rights, amount);
}
Expand Down Expand Up @@ -149,11 +149,9 @@ contract DelegateRegistry is IDelegateRegistry {

/// @dev Transfer native token out
function sweep() external {
// TODO: Replace this with correct address at deployment time
// This hardcoded address is a CREATE2 factory counterfactual smart contract wallet that will always accept native token transfers
uint256 sc = uint256(uint160(0x0000000000000000000000000000000000000000));
assembly ("memory-safe") {
let result := call(gas(), sc, selfbalance(), 0, 0, 0, 0)
// This hardcoded address is a CREATE2 factory counterfactual smart contract wallet that will always accept native token transfers
let result := call(gas(), 0x000000dE1E80ea5a234FB5488fee2584251BC7e8, selfbalance(), 0, 0, 0, 0)
}
}

Expand Down Expand Up @@ -365,7 +363,7 @@ contract DelegateRegistry is IDelegateRegistry {
}
}

/// @dev Helper function that writes from whilst preserving the rest of the storage slot
/// @dev Helper function that writes `from` while preserving the rest of the storage slot
function _updateFrom(bytes32 location, address from) internal {
uint256 firstPacked = Storage.POSITIONS_FIRST_PACKED;
uint256 cleanAddress = Storage.CLEAN_ADDRESS;
Expand Down
4 changes: 2 additions & 2 deletions src/IDelegateRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,12 @@ interface IDelegateRegistry {
*/

/**
* @notice allows external contract to read arbitrary storage slot
* @notice Allows external contracts to read arbitrary storage slots
*/
function readSlot(bytes32 location) external view returns (bytes32);

/**
* @notice allows external contracts to read an arbitrary array of storage slots
* @notice Allows external contracts to read an arbitrary array of storage slots
*/
function readSlots(bytes32[] calldata locations) external view returns (bytes32[] memory);
}
2 changes: 1 addition & 1 deletion src/examples/IPLicenseCheck.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.21;

import {IERC721} from "openzeppelin-contracts/contracts/token/ERC721/IERC721.sol";
import {IERC721} from "openzeppelin/token/ERC721/IERC721.sol";
import {IDelegateRegistry} from "src/IDelegateRegistry.sol";

/**
Expand Down
Loading

0 comments on commit a7135f6

Please sign in to comment.