Skip to content

Commit

Permalink
Merge pull request #1 from alchemyplatform/dp/tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
fangting-alchemy authored Sep 8, 2023
2 parents ad8845b + ac1c5dc commit 250bbf4
Show file tree
Hide file tree
Showing 5 changed files with 497 additions and 35 deletions.
115 changes: 102 additions & 13 deletions .gasestimates.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ Generated via `bash utils/inspect.sh`.
---

`forge test --gas-report --no-match-path "test/script/**/*"`
| lib/account-abstraction/contracts/core/EntryPoint.sol:EntryPoint contract | | | | | |
|---------------------------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 4128022 | 20516 | | | | |
| Function Name | min | avg | median | max | # calls |
| balanceOf | 614 | 1014 | 614 | 2614 | 5 |
| depositTo | 22537 | 23870 | 24537 | 24537 | 3 |
| getUserOpHash | 2178 | 2181 | 2178 | 2187 | 3 |
| handleOps | 67556 | 114566 | 128637 | 147506 | 3 |
| innerHandleOp | 33718 | 43164 | 43164 | 52611 | 2 |
| receive | 22127 | 22127 | 22127 | 22127 | 3 |
| withdrawTo | 36996 | 36996 | 36996 | 36996 | 1 |


| lib/account-abstraction/contracts/samples/SimpleAccount.sol:SimpleAccount contract | | | | | |
|------------------------------------------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
Expand All @@ -19,33 +33,109 @@ Generated via `bash utils/inspect.sh`.
| Deployment Cost | Deployment Size | | | | |
| 120671 | 1228 | | | | |
| Function Name | min | avg | median | max | # calls |
| entryPoint | 723 | 723 | 723 | 723 | 1 |
| owner | 826 | 826 | 826 | 826 | 1 |
| transferOwnership | 7710 | 10026 | 10026 | 12342 | 2 |
| upgradeToAndCall | 8145 | 25133 | 25133 | 42122 | 2 |
| addDeposit | 30212 | 36212 | 39212 | 39212 | 3 |
| disableInitializers | 11105 | 11105 | 11105 | 11105 | 1 |
| entryPoint | 723 | 2984 | 2984 | 5245 | 2 |
| execute | 7455 | 16650 | 8658 | 32984 | 5 |
| executeBatch | 7794 | 20627 | 20627 | 33460 | 2 |
| getDeposit | 1825 | 4825 | 1825 | 10825 | 3 |
| getInitializedVersion | 677 | 677 | 677 | 677 | 1 |
| isValidSignature | 11229 | 12909 | 12909 | 14589 | 2 |
| owner | 826 | 826 | 826 | 826 | 2 |
| transferOwnership | 5863 | 8281 | 7744 | 12342 | 5 |
| upgradeToAndCall | 4786 | 17805 | 13316 | 42122 | 7 |
| validateUserOp | 40706 | 41828 | 40712 | 44066 | 3 |
| withdrawDepositTo | 2978 | 21626 | 21626 | 40275 | 2 |


| src/LightAccount.sol:LightAccount contract | | | | | |
|--------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 1634741 | 8506 | | | | |
| 1661569 | 8640 | | | | |
| Function Name | min | avg | median | max | # calls |
| initialize | 49609 | 49609 | 49609 | 49609 | 5 |
| owner | 510 | 510 | 510 | 510 | 1 |
| transferOwnership | 2878 | 5202 | 5202 | 7526 | 2 |
| addDeposit | 29899 | 32899 | 34399 | 34399 | 3 |
| entryPoint | 429 | 429 | 429 | 429 | 1 |
| execute | 2950 | 13606 | 7112 | 28147 | 5 |
| executeBatch | 2920 | 15762 | 15762 | 28605 | 2 |
| getDeposit | 1509 | 3009 | 1509 | 6009 | 3 |
| initialize | 49609 | 49609 | 49609 | 49609 | 25 |
| isValidSignature | 6383 | 8063 | 8063 | 9743 | 2 |
| owner | 510 | 510 | 510 | 510 | 2 |
| transferOwnership | 2878 | 4355 | 2912 | 7526 | 5 |
| upgradeToAndCall | 3295 | 20290 | 20290 | 37285 | 2 |
| validateUserOp | 35733 | 36853 | 35733 | 39093 | 3 |
| withdrawDepositTo | 2643 | 21299 | 21299 | 39956 | 2 |


| src/LightAccountFactory.sol:LightAccountFactory contract | | | | | |
|----------------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | |
| 2127724 | 11008 | | | | |
| 2154580 | 11142 | | | | |
| Function Name | min | avg | median | max | # calls |
| createAccount | 161027 | 161027 | 161027 | 161027 | 5 |
| createAccount | 4821 | 155115 | 161027 | 163527 | 26 |
| getAddress | 4672 | 4672 | 4672 | 4672 | 1 |


| test/CustomSlotInitializable.t.sol:DisablesInitializersWhileInitializing contract | | | | | |
|-----------------------------------------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 156238 | 852 | | | | |
| Function Name | min | avg | median | max | # calls |
| initialize | 22755 | 22755 | 22755 | 22755 | 1 |


| test/CustomSlotInitializable.t.sol:IsInitializingChecker contract | | | | | |
|-------------------------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 154653 | 851 | | | | |
| Function Name | min | avg | median | max | # calls |
| initialize | 46069 | 46069 | 46069 | 46069 | 1 |
| isInitializing | 303 | 303 | 303 | 303 | 1 |
| wasInitializing | 267 | 267 | 267 | 267 | 1 |


| test/CustomSlotInitializable.t.sol:V1 contract | | | | | |
|------------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 762520 | 3920 | | | | |
| Function Name | min | avg | median | max | # calls |
| disableInitializers | 6292 | 6292 | 6292 | 6292 | 1 |
| getInitializedVersion | 391 | 391 | 391 | 391 | 1 |
| initialize | 684 | 19455 | 23898 | 23898 | 10 |
| proxiableUUID | 273 | 273 | 273 | 273 | 2 |
| upgradeToAndCall | 8469 | 13681 | 14454 | 17349 | 4 |


| test/CustomSlotInitializable.t.sol:V2 contract | | | | | |
|------------------------------------------------|-----------------|------|--------|------|---------|
| Deployment Cost | Deployment Size | | | | |
| 703047 | 3616 | | | | |
| Function Name | min | avg | median | max | # calls |
| getInitializedVersion | 361 | 361 | 361 | 361 | 1 |
| initialize | 504 | 4410 | 6363 | 6363 | 3 |
| proxiableUUID | 243 | 243 | 243 | 243 | 3 |
| upgradeToAndCall | 3552 | 3552 | 3552 | 3552 | 1 |


| test/LightAccount.t.sol:LightSwitch contract | | | | | |
|----------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 52111 | 288 | | | | |
| Function Name | min | avg | median | max | # calls |
| on | 267 | 267 | 267 | 267 | 3 |
| turnOn | 22274 | 22274 | 22274 | 22274 | 3 |


| test/LightAccount.t.sol:Reverter contract | | | | | |
|-------------------------------------------|-----------------|-----|--------|-----|---------|
| Deployment Cost | Deployment Size | | | | |
| 46905 | 261 | | | | |
| Function Name | min | avg | median | max | # calls |
| doRevert | 201 | 201 | 201 | 201 | 1 |



Ran 1 test suites: 5 tests passed, 0 failed, 0 skipped (5 total tests)
Ran 3 test suites: 31 tests passed, 0 failed, 0 skipped (31 total tests)
`forge inspect src/CustomSlotInitializable.sol:CustomSlotInitializable gasestimates`
```json
null
Expand All @@ -55,7 +145,7 @@ null
```json
{
"creation": {
"codeDepositCost": "1609400",
"codeDepositCost": "1636200",
"executionCost": "infinite",
"totalCost": "infinite"
},
Expand Down Expand Up @@ -86,7 +176,6 @@ null
"_call(address,uint256,bytes memory)": "infinite",
"_getStorage()": "infinite",
"_initialize(address)": "infinite",
"_isValidSignature(bytes32,bytes memory)": "infinite",
"_onlyOwner()": "infinite",
"_requireFromEntryPointOrOwner()": "infinite",
"_transferOwnership(address)": "27881",
Expand Down
14 changes: 7 additions & 7 deletions src/CustomSlotInitializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ abstract contract CustomSlotInitializable {
_storagePosition = storagePosition;
}

function _getInitialiazableStorage() private view returns (CustomSlotInitializableStorage storage _storage) {
bytes32 position = _storagePosition;
assembly {
_storage.slot := position
}
}

/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts.
Expand Down Expand Up @@ -186,4 +179,11 @@ abstract contract CustomSlotInitializable {
function _isInitializing() internal view returns (bool) {
return _getInitialiazableStorage().initializing;
}

function _getInitialiazableStorage() private view returns (CustomSlotInitializableStorage storage _storage) {
bytes32 position = _storagePosition;
assembly {
_storage.slot := position
}
}
}
118 changes: 118 additions & 0 deletions test/CustomSlotInitializable.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.21;

import "forge-std/Test.sol";

import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";

import {CustomSlotInitializable} from "../src/CustomSlotInitializable.sol";

contract CustomSlotInitializableTest is Test {
using stdStorage for StdStorage;

event Initialized(uint8 version);

address v1Impl;
address v2Impl;
V1 v1Proxy;

function setUp() public {
v1Impl = address(new V1());
v2Impl = address(new V2());
v1Proxy = V1(address(new ERC1967Proxy(v1Impl, abi.encodeCall(V1.initialize, ()))));
}

function testSimpleInitialization() public {
V1 v1 = new V1();
vm.expectEmit(false, false, false, true);
emit Initialized(1);
v1.initialize();
assertEq(v1.getInitializedVersion(), 1);
}

function testUpgrade() public {
vm.expectEmit(false, false, false, true);
emit Initialized(2);
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ()));
V2 v2Proxy = V2(address(v1Proxy));
assertEq(v2Proxy.getInitializedVersion(), 2);
}

function testCannotReinitialize() public {
vm.expectRevert(bytes("Initializable: contract is already initialized"));
v1Proxy.upgradeToAndCall(v1Impl, abi.encodeCall(V1.initialize, ()));
}

function testCannotUpgradeBackwards() public {
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ()));
V2 v2Proxy = V2(address(v1Proxy));
vm.expectRevert(bytes("Initializable: contract is already initialized"));
v2Proxy.upgradeToAndCall(v1Impl, abi.encodeCall(V1.initialize, ()));
}

function testDisableInitializers() public {
v1Proxy.disableInitializers();
vm.expectRevert(bytes("Initializable: contract is already initialized"));
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ()));
}

function testCannotCallDisableInitializersInInitializer() public {
DisablesInitializersWhileInitializing account = new DisablesInitializersWhileInitializing();
vm.expectRevert("Initializable: contract is initializing");
account.initialize();
}

function testIsInitializing() public {
IsInitializingChecker checker = new IsInitializingChecker();
checker.initialize();
assertTrue(checker.wasInitializing());
assertFalse(checker.isInitializing());
}
}

contract V1 is CustomSlotInitializable(keccak256("storage")), UUPSUpgradeable {
function initialize() public initializer {}

function getInitializedVersion() public view returns (uint8) {
return _getInitializedVersion();
}

function disableInitializers() public {
_disableInitializers();
}

function _authorizeUpgrade(address newImplementation) internal pure override {
(newImplementation);
}
}

contract V2 is CustomSlotInitializable(keccak256("storage")), UUPSUpgradeable {
function initialize() public reinitializer(2) {}

function getInitializedVersion() public view returns (uint8) {
return _getInitializedVersion();
}

function _authorizeUpgrade(address newImplementation) internal pure override {
(newImplementation);
}
}

contract DisablesInitializersWhileInitializing is CustomSlotInitializable(keccak256("storage")) {
function initialize() public initializer {
_disableInitializers();
}
}

contract IsInitializingChecker is CustomSlotInitializable(keccak256("storage")) {
bool public wasInitializing;

function initialize() public initializer {
wasInitializing = _isInitializing();
}

function isInitializing() public view returns (bool) {
return _isInitializing();
}
}
Loading

0 comments on commit 250bbf4

Please sign in to comment.