-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDirectCallsFromModule.t.sol
More file actions
183 lines (139 loc) · 6.71 KB
/
DirectCallsFromModule.t.sol
File metadata and controls
183 lines (139 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;
import {ReferenceModularAccount} from "../../src/account/ReferenceModularAccount.sol";
import {HookConfigLib} from "../../src/helpers/HookConfigLib.sol";
import {ModuleEntity, ModuleEntityLib} from "../../src/helpers/ModuleEntityLib.sol";
import {ValidationConfig, ValidationConfigLib} from "../../src/helpers/ValidationConfigLib.sol";
import {Call, IModularAccount} from "../../src/interfaces/IModularAccount.sol";
import {DirectCallModule} from "../mocks/modules/DirectCallModule.sol";
import {AccountTestBase} from "../utils/AccountTestBase.sol";
import {DIRECT_CALL_VALIDATION_ENTITYID} from "../../src/helpers/Constants.sol";
contract DirectCallsFromModuleTest is AccountTestBase {
using ValidationConfigLib for ValidationConfig;
DirectCallModule internal _module;
ModuleEntity internal _moduleEntity;
event ValidationUninstalled(address indexed module, uint32 indexed entityId, bool onUninstallSucceeded);
modifier randomizedValidationType(bool selectorValidation) {
if (selectorValidation) {
_installValidationSelector();
} else {
_installValidationGlobal();
}
_;
}
function setUp() public {
_module = new DirectCallModule();
assertFalse(_module.preHookRan());
assertFalse(_module.postHookRan());
_moduleEntity = ModuleEntityLib.pack(address(_module), DIRECT_CALL_VALIDATION_ENTITYID);
}
/* -------------------------------------------------------------------------- */
/* Negatives */
/* -------------------------------------------------------------------------- */
function test_Fail_DirectCallModuleNotInstalled() external {
vm.prank(address(_module));
vm.expectRevert(_buildDirectCallDisallowedError(IModularAccount.execute.selector));
account1.execute(address(0), 0, "");
}
function testFuzz_Fail_DirectCallModuleUninstalled(bool validationType)
external
randomizedValidationType(validationType)
{
_uninstallValidation();
vm.prank(address(_module));
vm.expectRevert(_buildDirectCallDisallowedError(IModularAccount.execute.selector));
account1.execute(address(0), 0, "");
}
function test_Fail_DirectCallModuleCallOtherSelector() external {
_installValidationSelector();
Call[] memory calls = new Call[](0);
vm.prank(address(_module));
vm.expectRevert(_buildDirectCallDisallowedError(IModularAccount.executeBatch.selector));
account1.executeBatch(calls);
}
/* -------------------------------------------------------------------------- */
/* Positives */
/* -------------------------------------------------------------------------- */
function testFuzz_Pass_DirectCallFromModulePrank(bool validationType)
external
randomizedValidationType(validationType)
{
vm.prank(address(_module));
account1.execute(address(0), 0, "");
assertTrue(_module.preHookRan());
assertTrue(_module.postHookRan());
}
function testFuzz_Pass_DirectCallFromModuleCallback(bool validationType)
external
randomizedValidationType(validationType)
{
bytes memory encodedCall = abi.encodeCall(DirectCallModule.directCall, ());
vm.prank(address(entryPoint));
bytes memory result = account1.execute(address(_module), 0, encodedCall);
assertTrue(_module.preHookRan());
assertTrue(_module.postHookRan());
// the directCall() function in the _module calls back into `execute()` with an encoded call back into the
// _module's getData() function.
assertEq(abi.decode(result, (bytes)), abi.encode(_module.getData()));
}
function testFuzz_Flow_DirectCallFromModuleSequence(bool validationType)
external
randomizedValidationType(validationType)
{
// Install => Succeesfully call => uninstall => fail to call
vm.prank(address(_module));
account1.execute(address(0), 0, "");
assertTrue(_module.preHookRan());
assertTrue(_module.postHookRan());
_uninstallValidation();
vm.prank(address(_module));
vm.expectRevert(_buildDirectCallDisallowedError(IModularAccount.execute.selector));
account1.execute(address(0), 0, "");
}
function test_directCallsFromEOA() external {
address extraOwner = makeAddr("extraOwner");
bytes4[] memory selectors = new bytes4[](1);
selectors[0] = IModularAccount.execute.selector;
vm.prank(address(entryPoint));
account1.installValidation(
ValidationConfigLib.pack(extraOwner, DIRECT_CALL_VALIDATION_ENTITYID, false, false, false),
selectors,
"",
new bytes[](0)
);
vm.prank(extraOwner);
account1.execute(makeAddr("dead"), 0, "");
}
/* -------------------------------------------------------------------------- */
/* Internals */
/* -------------------------------------------------------------------------- */
function _installValidationSelector() internal {
bytes4[] memory selectors = new bytes4[](1);
selectors[0] = IModularAccount.execute.selector;
bytes[] memory hooks = new bytes[](1);
hooks[0] = abi.encodePacked(
HookConfigLib.packExecHook({_hookFunction: _moduleEntity, _hasPre: true, _hasPost: true}),
hex"00" // onInstall data
);
vm.prank(address(entryPoint));
ValidationConfig validationConfig = ValidationConfigLib.pack(_moduleEntity, false, false, false);
account1.installValidation(validationConfig, selectors, "", hooks);
}
function _installValidationGlobal() internal {
bytes[] memory hooks = new bytes[](1);
hooks[0] = abi.encodePacked(
HookConfigLib.packExecHook({_hookFunction: _moduleEntity, _hasPre: true, _hasPost: true}),
hex"00" // onInstall data
);
vm.prank(address(entryPoint));
ValidationConfig validationConfig = ValidationConfigLib.pack(_moduleEntity, true, false, false);
account1.installValidation(validationConfig, new bytes4[](0), "", hooks);
}
function _uninstallValidation() internal {
(address module, uint32 entityId) = ModuleEntityLib.unpack(_moduleEntity);
vm.prank(address(entryPoint));
vm.expectEmit(true, true, true, true);
emit ValidationUninstalled(module, entityId, true);
account1.uninstallValidation(_moduleEntity, "", new bytes[](1));
}
}