Skip to content

Commit

Permalink
refactor: [v0.8-develop] account test base (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-alchemy authored and adamegyed committed Jul 26, 2024
1 parent 5f92f96 commit 02271b9
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 251 deletions.
46 changes: 13 additions & 33 deletions test/account/AccountExecHooks.t.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {EntryPoint} from "@eth-infinitism/account-abstraction/core/EntryPoint.sol";

import {
IPlugin,
ManifestAssociatedFunctionType,
Expand All @@ -12,24 +9,13 @@ import {
ManifestFunction,
PluginManifest
} from "../../src/interfaces/IPlugin.sol";
import {SingleOwnerPlugin} from "../../src/plugins/owner/SingleOwnerPlugin.sol";
import {PluginManagerInternals} from "../../src/account/PluginManagerInternals.sol";
import {UpgradeableModularAccount} from "../../src/account/UpgradeableModularAccount.sol";
import {FunctionReference, FunctionReferenceLib} from "../../src/helpers/FunctionReferenceLib.sol";

import {MockPlugin} from "../mocks/MockPlugin.sol";
import {MSCAFactoryFixture} from "../mocks/MSCAFactoryFixture.sol";
import {OptimizedTest} from "../utils/OptimizedTest.sol";

contract AccountExecHooksTest is OptimizedTest {
using ECDSA for bytes32;

EntryPoint public entryPoint;
SingleOwnerPlugin public singleOwnerPlugin;
MSCAFactoryFixture public factory;

UpgradeableModularAccount public account;
import {AccountTestBase} from "../utils/AccountTestBase.sol";

contract AccountExecHooksTest is AccountTestBase {
MockPlugin public mockPlugin1;
MockPlugin public mockPlugin2;
bytes32 public manifestHash1;
Expand All @@ -50,13 +36,7 @@ contract AccountExecHooksTest is OptimizedTest {
event ReceivedCall(bytes msgData, uint256 msgValue);

function setUp() public {
entryPoint = new EntryPoint();
singleOwnerPlugin = _deploySingleOwnerPlugin();
factory = new MSCAFactoryFixture(entryPoint, singleOwnerPlugin);

// Create an account with "this" as the owner, so we can execute along the runtime path with regular
// solidity semantics
account = factory.createAccount(address(this), 0);
_transferOwnershipToTest();

m1.executionFunctions.push(_EXEC_SELECTOR);

Expand Down Expand Up @@ -101,7 +81,7 @@ contract AccountExecHooksTest is OptimizedTest {
0 // msg value in call to plugin
);

(bool success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(bool success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertTrue(success);
}

Expand Down Expand Up @@ -154,7 +134,7 @@ contract AccountExecHooksTest is OptimizedTest {
0 // msg value in call to plugin
);

(bool success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(bool success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertTrue(success);
}

Expand Down Expand Up @@ -187,7 +167,7 @@ contract AccountExecHooksTest is OptimizedTest {
0 // msg value in call to plugin
);

(bool success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(bool success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertTrue(success);
}

Expand Down Expand Up @@ -225,7 +205,7 @@ contract AccountExecHooksTest is OptimizedTest {
}

function test_overlappingPreExecHooks_run() public {
(bool success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(bool success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertFalse(success);
}

Expand All @@ -236,14 +216,14 @@ contract AccountExecHooksTest is OptimizedTest {
_uninstallPlugin(mockPlugin2);

// Expect the pre hook to still exist.
(bool success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(bool success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertFalse(success);

// Uninstall the first plugin.
_uninstallPlugin(mockPlugin1);

// Execution selector should no longer exist.
(success,) = address(account).call(abi.encodeWithSelector(_EXEC_SELECTOR));
(success,) = address(account1).call(abi.encodeWithSelector(_EXEC_SELECTOR));
assertFalse(success);
}

Expand Down Expand Up @@ -302,7 +282,7 @@ contract AccountExecHooksTest is OptimizedTest {
vm.expectEmit(true, true, true, true);
emit PluginInstalled(address(mockPlugin1), manifestHash1, new FunctionReference[](0));

account.installPlugin({
account1.installPlugin({
plugin: address(mockPlugin1),
manifestHash: manifestHash1,
pluginInstallData: bytes(""),
Expand Down Expand Up @@ -333,7 +313,7 @@ contract AccountExecHooksTest is OptimizedTest {
vm.expectEmit(true, true, true, true);
emit PluginInstalled(address(mockPlugin2), manifestHash2, dependencies);

account.installPlugin({
account1.installPlugin({
plugin: address(mockPlugin2),
manifestHash: manifestHash2,
pluginInstallData: bytes(""),
Expand Down Expand Up @@ -361,7 +341,7 @@ contract AccountExecHooksTest is OptimizedTest {
manifestHash2 = keccak256(abi.encode(mockPlugin2.pluginManifest()));

vm.expectRevert(revertData);
account.installPlugin({
account1.installPlugin({
plugin: address(mockPlugin2),
manifestHash: manifestHash2,
pluginInstallData: bytes(""),
Expand All @@ -375,6 +355,6 @@ contract AccountExecHooksTest is OptimizedTest {
vm.expectEmit(true, true, true, true);
emit PluginUninstalled(address(plugin), true);

account.uninstallPlugin(address(plugin), bytes(""), bytes(""));
account1.uninstallPlugin(address(plugin), bytes(""), bytes(""));
}
}
19 changes: 3 additions & 16 deletions test/account/AccountLoupe.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
pragma solidity ^0.8.19;

import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import {EntryPoint} from "@eth-infinitism/account-abstraction/core/EntryPoint.sol";

import {UpgradeableModularAccount} from "../../src/account/UpgradeableModularAccount.sol";
import {FunctionReference, FunctionReferenceLib} from "../../src/helpers/FunctionReferenceLib.sol";
import {
ManifestAssociatedFunctionType,
Expand All @@ -16,34 +14,23 @@ import {IAccountLoupe} from "../../src/interfaces/IAccountLoupe.sol";
import {IPluginManager} from "../../src/interfaces/IPluginManager.sol";
import {IStandardExecutor} from "../../src/interfaces/IStandardExecutor.sol";
import {ISingleOwnerPlugin} from "../../src/plugins/owner/ISingleOwnerPlugin.sol";
import {SingleOwnerPlugin} from "../../src/plugins/owner/SingleOwnerPlugin.sol";

import {MSCAFactoryFixture} from "../mocks/MSCAFactoryFixture.sol";
import {ComprehensivePlugin} from "../mocks/plugins/ComprehensivePlugin.sol";
import {MockPlugin} from "../mocks/MockPlugin.sol";
import {OptimizedTest} from "../utils/OptimizedTest.sol";
import {AccountTestBase} from "../utils/AccountTestBase.sol";

contract AccountLoupeTest is OptimizedTest {
EntryPoint public entryPoint;
SingleOwnerPlugin public singleOwnerPlugin;
MSCAFactoryFixture public factory;
contract AccountLoupeTest is AccountTestBase {
ComprehensivePlugin public comprehensivePlugin;

UpgradeableModularAccount public account1;

FunctionReference public ownerValidation;

event ReceivedCall(bytes msgData, uint256 msgValue);

function setUp() public {
entryPoint = new EntryPoint();
_transferOwnershipToTest();

singleOwnerPlugin = _deploySingleOwnerPlugin();
factory = new MSCAFactoryFixture(entryPoint, singleOwnerPlugin);
comprehensivePlugin = new ComprehensivePlugin();

account1 = factory.createAccount(address(this), 0);

bytes32 manifestHash = keccak256(abi.encode(comprehensivePlugin.pluginManifest()));
account1.installPlugin(address(comprehensivePlugin), manifestHash, "", new FunctionReference[](0));

Expand Down
37 changes: 10 additions & 27 deletions test/account/AccountReturnData.t.sol
Original file line number Diff line number Diff line change
@@ -1,57 +1,40 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import {EntryPoint} from "@eth-infinitism/account-abstraction/core/EntryPoint.sol";

import {UpgradeableModularAccount} from "../../src/account/UpgradeableModularAccount.sol";
import {FunctionReference} from "../../src/helpers/FunctionReferenceLib.sol";
import {Call} from "../../src/interfaces/IStandardExecutor.sol";
import {SingleOwnerPlugin} from "../../src/plugins/owner/SingleOwnerPlugin.sol";

import {
RegularResultContract,
ResultCreatorPlugin,
ResultConsumerPlugin
} from "../mocks/plugins/ReturnDataPluginMocks.sol";
import {MSCAFactoryFixture} from "../mocks/MSCAFactoryFixture.sol";
import {OptimizedTest} from "../utils/OptimizedTest.sol";
import {AccountTestBase} from "../utils/AccountTestBase.sol";

// Tests all the different ways that return data can be read from plugins through an account
contract AccountReturnDataTest is OptimizedTest {
EntryPoint public entryPoint; // Just to be able to construct the factory
SingleOwnerPlugin public singleOwnerPlugin;
MSCAFactoryFixture public factory;

contract AccountReturnDataTest is AccountTestBase {
RegularResultContract public regularResultContract;
ResultCreatorPlugin public resultCreatorPlugin;
ResultConsumerPlugin public resultConsumerPlugin;

UpgradeableModularAccount public account;

function setUp() public {
entryPoint = new EntryPoint();
singleOwnerPlugin = _deploySingleOwnerPlugin();
factory = new MSCAFactoryFixture(entryPoint, singleOwnerPlugin);
_transferOwnershipToTest();

regularResultContract = new RegularResultContract();
resultCreatorPlugin = new ResultCreatorPlugin();
resultConsumerPlugin = new ResultConsumerPlugin(resultCreatorPlugin, regularResultContract);

// Create an account with "this" as the owner, so we can execute along the runtime path with regular
// solidity semantics
account = factory.createAccount(address(this), 0);

// Add the result creator plugin to the account
bytes32 resultCreatorManifestHash = keccak256(abi.encode(resultCreatorPlugin.pluginManifest()));
account.installPlugin({
account1.installPlugin({
plugin: address(resultCreatorPlugin),
manifestHash: resultCreatorManifestHash,
pluginInstallData: "",
dependencies: new FunctionReference[](0)
});
// Add the result consumer plugin to the account
bytes32 resultConsumerManifestHash = keccak256(abi.encode(resultConsumerPlugin.pluginManifest()));
account.installPlugin({
account1.installPlugin({
plugin: address(resultConsumerPlugin),
manifestHash: resultConsumerManifestHash,
pluginInstallData: "",
Expand All @@ -61,15 +44,15 @@ contract AccountReturnDataTest is OptimizedTest {

// Tests the ability to read the result of plugin execution functions via the account's fallback
function test_returnData_fallback() public {
bytes32 result = ResultCreatorPlugin(address(account)).foo();
bytes32 result = ResultCreatorPlugin(address(account1)).foo();

assertEq(result, keccak256("bar"));
}

// Tests the ability to read the results of contracts called via IStandardExecutor.execute
function test_returnData_singular_execute() public {
bytes memory returnData =
account.execute(address(regularResultContract), 0, abi.encodeCall(RegularResultContract.foo, ()));
account1.execute(address(regularResultContract), 0, abi.encodeCall(RegularResultContract.foo, ()));

bytes32 result = abi.decode(returnData, (bytes32));

Expand All @@ -90,7 +73,7 @@ contract AccountReturnDataTest is OptimizedTest {
data: abi.encodeCall(RegularResultContract.bar, ())
});

bytes[] memory returnDatas = account.executeBatch(calls);
bytes[] memory returnDatas = account1.executeBatch(calls);

bytes32 result1 = abi.decode(returnDatas[0], (bytes32));
bytes32 result2 = abi.decode(returnDatas[1], (bytes32));
Expand All @@ -101,14 +84,14 @@ contract AccountReturnDataTest is OptimizedTest {

// Tests the ability to read data via executeFromPlugin routing to fallback functions
function test_returnData_execFromPlugin_fallback() public {
bool result = ResultConsumerPlugin(address(account)).checkResultEFPFallback(keccak256("bar"));
bool result = ResultConsumerPlugin(address(account1)).checkResultEFPFallback(keccak256("bar"));

assertTrue(result);
}

// Tests the ability to read data via executeFromPluginExternal
function test_returnData_execFromPlugin_execute() public {
bool result = ResultConsumerPlugin(address(account)).checkResultEFPExternal(
bool result = ResultConsumerPlugin(address(account1)).checkResultEFPExternal(
address(regularResultContract), keccak256("bar")
);

Expand Down
Loading

0 comments on commit 02271b9

Please sign in to comment.