From 98e8d7be46be578156b9223587a91f8d896e93ba Mon Sep 17 00:00:00 2001 From: kopy-kat Date: Fri, 23 Feb 2024 12:54:36 +0000 Subject: [PATCH] chore: cleanup mocks --- accounts/safe7579/test/Base.t.sol | 3 +- accounts/safe7579/test/SafeERC7579.t.sol | 13 ++--- accounts/safe7579/test/mocks/MockExecutor.sol | 16 +----- accounts/safe7579/test/mocks/MockFallback.sol | 19 +------ accounts/safe7579/test/mocks/MockRegistry.sol | 54 ------------------- accounts/safe7579/test/mocks/MockTarget.sol | 11 ---- .../safe7579/test/mocks/MockValidator.sol | 35 ++---------- .../modulekit/src/mocks/MockFallback.sol | 17 +++--- packages/modulekit/src/mocks/MockHook.sol | 3 +- .../modulekit/src/mocks/MockValidator.sol | 2 + 10 files changed, 26 insertions(+), 147 deletions(-) delete mode 100644 accounts/safe7579/test/mocks/MockRegistry.sol delete mode 100644 accounts/safe7579/test/mocks/MockTarget.sol rename accounts/safe7579/test/mocks/MockHook.sol => packages/modulekit/src/mocks/MockFallback.sol (52%) diff --git a/accounts/safe7579/test/Base.t.sol b/accounts/safe7579/test/Base.t.sol index ad97493f..70978819 100644 --- a/accounts/safe7579/test/Base.t.sol +++ b/accounts/safe7579/test/Base.t.sol @@ -6,7 +6,8 @@ import { SafeERC7579 } from "src/SafeERC7579.sol"; import { ModuleManager } from "src/core/ModuleManager.sol"; import { MockValidator } from "./mocks/MockValidator.sol"; import { MockExecutor } from "./mocks/MockExecutor.sol"; -import { MockTarget } from "./mocks/MockTarget.sol"; +import { MockFallback } from "./mocks/MockFallback.sol"; +import { MockTarget } from "@rhinestone/modulekit/src/mocks/MockTarget.sol"; import { Safe } from "@safe-global/safe-contracts/contracts/Safe.sol"; import { LibClone } from "solady/src/utils/LibClone.sol"; diff --git a/accounts/safe7579/test/SafeERC7579.t.sol b/accounts/safe7579/test/SafeERC7579.t.sol index f9a1b900..fe8c6baa 100644 --- a/accounts/safe7579/test/SafeERC7579.t.sol +++ b/accounts/safe7579/test/SafeERC7579.t.sol @@ -4,9 +4,7 @@ pragma solidity ^0.8.23; import "erc7579/interfaces/IERC7579Account.sol"; import "erc7579/lib/ModeLib.sol"; import "erc7579/lib/ExecutionLib.sol"; -import { MockTarget } from "./mocks/MockTarget.sol"; -import { MockFallback } from "./mocks/MockFallback.sol"; -import "./Base.t.sol"; +import { TestBaseUtil, MockTarget, MockFallback } from "./Base.t.sol"; contract MSATest is TestBaseUtil { function setUp() public override { @@ -15,7 +13,7 @@ contract MSATest is TestBaseUtil { function test_execSingle() public { // Create calldata for the account to execute - bytes memory setValueOnTarget = abi.encodeCall(MockTarget.setValue, 1337); + bytes memory setValueOnTarget = abi.encodeCall(MockTarget.set, 1337); // Encode the call into the calldata for the userOp bytes memory userOpCalldata = abi.encodeCall( @@ -41,7 +39,6 @@ contract MSATest is TestBaseUtil { userOps[0] = userOp; // Send the userOp to the entrypoint - console2.log("userOps"); entrypoint.handleOps(userOps, payable(address(0x69))); // Assert that the value was set ie that execution was successful @@ -50,7 +47,7 @@ contract MSATest is TestBaseUtil { function test_execBatch() public { // Create calldata for the account to execute - bytes memory setValueOnTarget = abi.encodeCall(MockTarget.setValue, 1337); + bytes memory setValueOnTarget = abi.encodeCall(MockTarget.set, 1337); address target2 = address(0x420); uint256 target2Amount = 1 wei; @@ -91,12 +88,12 @@ contract MSATest is TestBaseUtil { IERC7579Account(address(safe)), address(target), 0, - abi.encodeWithSelector(MockTarget.setValue.selector, 1337) + abi.encodeWithSelector(MockTarget.set.selector, 1337) ); } function test_execBatchFromExecutor() public { - bytes memory setValueOnTarget = abi.encodeCall(MockTarget.setValue, 1338); + bytes memory setValueOnTarget = abi.encodeCall(MockTarget.set, 1338); Execution[] memory executions = new Execution[](2); executions[0] = Execution({ target: address(target), value: 0, callData: setValueOnTarget }); executions[1] = Execution({ target: address(target), value: 0, callData: setValueOnTarget }); diff --git a/accounts/safe7579/test/mocks/MockExecutor.sol b/accounts/safe7579/test/mocks/MockExecutor.sol index 99f13092..04cec703 100644 --- a/accounts/safe7579/test/mocks/MockExecutor.sol +++ b/accounts/safe7579/test/mocks/MockExecutor.sol @@ -1,16 +1,12 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.23; -import { IExecutor } from "erc7579/interfaces/IERC7579Module.sol"; import { IERC7579Account, Execution } from "erc7579/interfaces/IERC7579Account.sol"; import { ExecutionLib } from "erc7579/lib/ExecutionLib.sol"; import { ModeLib } from "erc7579/lib/ModeLib.sol"; +import { MockExecutor as MockExecutorBase } from "@rhinestone/modulekit/src/mocks/MockExecutor.sol"; -contract MockExecutor is IExecutor { - function onInstall(bytes calldata data) external override { } - - function onUninstall(bytes calldata data) external override { } - +contract MockExecutor is MockExecutorBase { function executeViaAccount( IERC7579Account account, address target, @@ -36,12 +32,4 @@ contract MockExecutor is IExecutor { ModeLib.encodeSimpleBatch(), ExecutionLib.encodeBatch(execs) ); } - - function isModuleType(uint256 typeID) external view returns (bool) { - return typeID == 2; - } - - function isInitialized(address smartAccount) external view returns (bool) { - return false; - } } diff --git a/accounts/safe7579/test/mocks/MockFallback.sol b/accounts/safe7579/test/mocks/MockFallback.sol index b5ec2fd2..acb84d76 100644 --- a/accounts/safe7579/test/mocks/MockFallback.sol +++ b/accounts/safe7579/test/mocks/MockFallback.sol @@ -1,17 +1,10 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.23; -import { IFallback } from "erc7579/interfaces/IERC7579Module.sol"; -import { IERC7579Account, Execution } from "erc7579/interfaces/IERC7579Account.sol"; -import { ExecutionLib } from "erc7579/lib/ExecutionLib.sol"; -import { ModeLib } from "erc7579/lib/ModeLib.sol"; import { HandlerContext } from "@safe-global/safe-contracts/contracts/handler/HandlerContext.sol"; +import { MockFallback as MockFallbackBase } from "@rhinestone/modulekit/src/mocks/MockFallback.sol"; -contract MockFallback is IFallback, HandlerContext { - function onInstall(bytes calldata data) external override { } - - function onUninstall(bytes calldata data) external override { } - +contract MockFallback is MockFallbackBase, HandlerContext { function target(uint256 value) external returns (uint256 _value, address erc2771Sender, address msgSender) @@ -20,12 +13,4 @@ contract MockFallback is IFallback, HandlerContext { erc2771Sender = _msgSender(); msgSender = msg.sender; } - - function isModuleType(uint256 typeID) external view returns (bool) { - return typeID == 3; - } - - function isInitialized(address smartAccount) external view returns (bool) { - return false; - } } diff --git a/accounts/safe7579/test/mocks/MockRegistry.sol b/accounts/safe7579/test/mocks/MockRegistry.sol deleted file mode 100644 index 4e5c674a..00000000 --- a/accounts/safe7579/test/mocks/MockRegistry.sol +++ /dev/null @@ -1,54 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.19; - -/** - * @title MockRegistry - * @author zeroknots - * @notice Mock registry for testing purposes - */ -contract MockRegistry { - function check( - address plugin, - address trustedEntity - ) - external - view - returns (uint256 listedAt) - { - return uint256(1234); - } - - function checkN( - address module, - address[] memory attesters, - uint256 threshold - ) - external - view - returns (uint256[] memory) - { - uint256 attestersLength = attesters.length; - uint256[] memory attestedAtArray = new uint256[](attestersLength); - for (uint256 i; i < attestersLength; ++i) { - attestedAtArray[i] = uint256(1234); - } - return attestedAtArray; - } - - function checkNUnsafe( - address module, - address[] memory attesters, - uint256 threshold - ) - external - view - returns (uint256[] memory) - { - uint256 attestersLength = attesters.length; - uint256[] memory attestedAtArray = new uint256[](attestersLength); - for (uint256 i; i < attestersLength; ++i) { - attestedAtArray[i] = uint256(1234); - } - return attestedAtArray; - } -} diff --git a/accounts/safe7579/test/mocks/MockTarget.sol b/accounts/safe7579/test/mocks/MockTarget.sol deleted file mode 100644 index 6e8f4b77..00000000 --- a/accounts/safe7579/test/mocks/MockTarget.sol +++ /dev/null @@ -1,11 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity ^0.8.23; - -contract MockTarget { - uint256 public value; - - function setValue(uint256 _value) public returns (uint256) { - value = _value; - return _value; - } -} diff --git a/accounts/safe7579/test/mocks/MockValidator.sol b/accounts/safe7579/test/mocks/MockValidator.sol index d9be70ac..d11cbd01 100644 --- a/accounts/safe7579/test/mocks/MockValidator.sol +++ b/accounts/safe7579/test/mocks/MockValidator.sol @@ -1,46 +1,21 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.23; -import { - IValidator, - PackedUserOperation, - VALIDATION_SUCCESS -} from "erc7579/interfaces/IERC7579Module.sol"; - -contract MockValidator is IValidator { - function onInstall(bytes calldata data) external override { } - - function onUninstall(bytes calldata data) external override { } +import { PackedUserOperation } from "erc7579/interfaces/IERC7579Module.sol"; +import { MockValidator as MockValidatorBase } from + "@rhinestone/modulekit/src/mocks/MockValidator.sol"; +contract MockValidator is MockValidatorBase { function validateUserOp( PackedUserOperation calldata userOp, bytes32 userOpHash ) external override - returns (uint256) + returns (ValidationData) { bytes4 execSelector = bytes4(userOp.callData[:4]); return VALIDATION_SUCCESS; } - - function isValidSignatureWithSender( - address sender, - bytes32 hash, - bytes calldata data - ) - external - view - override - returns (bytes4) - { } - - function isModuleType(uint256 typeID) external view returns (bool) { - return typeID == 1; - } - - function isInitialized(address smartAccount) external view returns (bool) { - return false; - } } diff --git a/accounts/safe7579/test/mocks/MockHook.sol b/packages/modulekit/src/mocks/MockFallback.sol similarity index 52% rename from accounts/safe7579/test/mocks/MockHook.sol rename to packages/modulekit/src/mocks/MockFallback.sol index 87c02555..60024b3d 100644 --- a/accounts/safe7579/test/mocks/MockHook.sol +++ b/packages/modulekit/src/mocks/MockFallback.sol @@ -1,24 +1,19 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.23; -import { IHook } from "erc7579/interfaces/IERC7579Module.sol"; +import { ERC7579FallbackBase } from "../Modules.sol"; -contract MockHook is IHook { +contract MockFallback is ERC7579FallbackBase { function onInstall(bytes calldata data) external override { } function onUninstall(bytes calldata data) external override { } - function preCheck( - address msgSender, - bytes calldata msgData - ) - external - returns (bytes memory hookData) - { } - function postCheck(bytes calldata hookData) external returns (bool success) { } + function targetFunction() external returns (bool) { + return true; + } function isModuleType(uint256 typeID) external view returns (bool) { - return typeID == 4; + return typeID == TYPE_FALLBACK; } function isInitialized(address smartAccount) external view returns (bool) { diff --git a/packages/modulekit/src/mocks/MockHook.sol b/packages/modulekit/src/mocks/MockHook.sol index a8c97f22..e7ef64c9 100644 --- a/packages/modulekit/src/mocks/MockHook.sol +++ b/packages/modulekit/src/mocks/MockHook.sol @@ -13,11 +13,12 @@ contract MockHook is ERC7579HookBase { bytes calldata msgData ) external + virtual override returns (bytes memory hookData) { } - function postCheck(bytes calldata) external override returns (bool success) { + function postCheck(bytes calldata) external virtual override returns (bool success) { return true; } diff --git a/packages/modulekit/src/mocks/MockValidator.sol b/packages/modulekit/src/mocks/MockValidator.sol index 417c25ee..2f5f6c52 100644 --- a/packages/modulekit/src/mocks/MockValidator.sol +++ b/packages/modulekit/src/mocks/MockValidator.sol @@ -15,6 +15,7 @@ contract MockValidator is ERC7579ValidatorBase { bytes32 userOpHash ) external + virtual override returns (ValidationData) { @@ -29,6 +30,7 @@ contract MockValidator is ERC7579ValidatorBase { ) external view + virtual override returns (bytes4) {