/
SystemSwitch.t.sol
231 lines (186 loc) · 8.76 KB
/
SystemSwitch.t.sol
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
import { World } from "@latticexyz/world/src/World.sol";
import { createCoreModule } from "@latticexyz/world/test/createCoreModule.sol";
import { ResourceId, WorldResourceIdLib, WorldResourceIdInstance } from "@latticexyz/world/src/WorldResourceId.sol";
import { RESOURCE_SYSTEM } from "@latticexyz/world/src/worldResourceTypes.sol";
import { ROOT_NAMESPACE } from "@latticexyz/world/src/constants.sol";
import { SystemSwitch } from "../src/utils/SystemSwitch.sol";
contract EchoSystem is System {
function msgSender() public view returns (address) {
return _msgSender();
}
function world() public view returns (address) {
return _world();
}
function echo(string memory message) public view returns (string memory) {
return message;
}
function call(ResourceId systemId, bytes memory callData) public returns (bytes memory) {
return SystemSwitch.call(systemId, callData);
}
function callViaSelector(bytes memory callData) public returns (bytes memory) {
return SystemSwitch.call(callData);
}
}
address constant caller = address(4232);
contract SystemSwitchTest is Test, GasReporter {
using WorldResourceIdInstance for ResourceId;
IBaseWorld world;
EchoSystem systemA;
EchoSystem systemB;
EchoSystem rootSystemA;
EchoSystem rootSystemB;
ResourceId systemAId;
ResourceId systemBId;
ResourceId rootSystemAId;
ResourceId rootSystemBId;
function setUp() public {
// Deploy world
World _world = new World();
_world.initialize(createCoreModule());
world = IBaseWorld(address(_world));
// Deploy systems
systemA = new EchoSystem();
systemB = new EchoSystem();
rootSystemA = new EchoSystem();
rootSystemB = new EchoSystem();
// Encode system IDs
systemAId = WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: "namespaceA", name: "systemA" });
systemBId = WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: "namespaceB", name: "systemB" });
rootSystemAId = WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: ROOT_NAMESPACE, name: "systemA" });
rootSystemBId = WorldResourceIdLib.encode({ typeId: RESOURCE_SYSTEM, namespace: ROOT_NAMESPACE, name: "systemB" });
// Register namespaces
world.registerNamespace(systemAId.getNamespaceId());
world.registerNamespace(systemBId.getNamespaceId());
// Register systems
world.registerSystem(systemAId, systemA, true);
world.registerSystem(systemBId, systemB, true);
world.registerSystem(rootSystemAId, rootSystemA, true);
world.registerSystem(rootSystemBId, rootSystemB, true);
}
function _executeFromRootSystemA(ResourceId systemId, bytes memory callData) public returns (bytes memory) {
return abi.decode(world.call(rootSystemAId, abi.encodeCall(EchoSystem.call, (systemId, callData))), (bytes));
}
function _executeFromSystemA(ResourceId systemId, bytes memory callData) public returns (bytes memory) {
return abi.decode(world.call(systemAId, abi.encodeCall(EchoSystem.call, (systemId, callData))), (bytes));
}
function _executeFromRootSystemA(bytes memory callData) public returns (bytes memory) {
return abi.decode(world.call(rootSystemAId, abi.encodeCall(EchoSystem.callViaSelector, (callData))), (bytes));
}
function _executeFromSystemA(bytes memory callData) public returns (bytes memory) {
return abi.decode(world.call(systemAId, abi.encodeCall(EchoSystem.callViaSelector, (callData))), (bytes));
}
// - ROOT FROM ROOT ---------------------------------------------------------------------------- //
function testCallRootFromRootMsgSender() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(rootSystemBId, abi.encodeCall(EchoSystem.msgSender, ()));
assertEq(abi.decode(returnData, (address)), caller);
}
function testCallRootFromRootWorld() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(rootSystemBId, abi.encodeCall(EchoSystem.world, ()));
assertEq(abi.decode(returnData, (address)), address(world));
}
function testCallRootFromRootEcho() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(rootSystemBId, abi.encodeCall(EchoSystem.echo, ("hello")));
assertEq(abi.decode(returnData, (string)), "hello");
}
function testCallRootFromRootWorldSelector() public {
bytes4 worldFunctionSelector = world.registerRootFunctionSelector(
rootSystemBId,
"echo(string)",
EchoSystem.echo.selector
);
bytes memory callData = abi.encodeWithSelector(worldFunctionSelector, "hello");
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(callData);
assertEq(abi.decode(returnData, (string)), "hello");
}
// - ROOT FROM NON ROOT ---------------------------------------------------------------------------- //
function testCallRootFromNonRootMsgSender() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(rootSystemBId, abi.encodeCall(EchoSystem.msgSender, ()));
assertEq(abi.decode(returnData, (address)), address(systemA));
}
function testCallRootFromNonRootWorld() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(rootSystemBId, abi.encodeCall(EchoSystem.world, ()));
assertEq(abi.decode(returnData, (address)), address(world));
}
function testCallRootFromNonRootEcho() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(rootSystemBId, abi.encodeCall(EchoSystem.echo, ("hello")));
assertEq(abi.decode(returnData, (string)), "hello");
}
function testCallRootFromNonRootWorldSelector() public {
bytes4 worldFunctionSelector = world.registerRootFunctionSelector(
rootSystemBId,
"echo(string)",
EchoSystem.echo.selector
);
bytes memory callData = abi.encodeWithSelector(worldFunctionSelector, "hello");
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(callData);
assertEq(abi.decode(returnData, (string)), "hello");
}
// - NON ROOT FROM ROOT ---------------------------------------------------------------------------- //
function testCallNonRootFromRootMsgSender() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(systemBId, abi.encodeCall(EchoSystem.msgSender, ()));
assertEq(abi.decode(returnData, (address)), caller);
}
function testNonCallRootFromRootWorld() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(systemBId, abi.encodeCall(EchoSystem.world, ()));
assertEq(abi.decode(returnData, (address)), address(world));
}
function testNonCallRootFromRootEcho() public {
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(systemBId, abi.encodeCall(EchoSystem.echo, ("hello")));
assertEq(abi.decode(returnData, (string)), "hello");
}
function testNonCallRootFromRootWorldSelector() public {
bytes4 worldFunctionSelector = world.registerRootFunctionSelector(
systemBId,
"echo(string)",
EchoSystem.echo.selector
);
bytes memory callData = abi.encodeWithSelector(worldFunctionSelector, "hello");
vm.prank(caller);
bytes memory returnData = _executeFromRootSystemA(callData);
assertEq(abi.decode(returnData, (string)), "hello");
}
// - NON ROOT FROM NON ROOT ---------------------------------------------------------------------------- //
function testCallNonRootFromNonRootMsgSender() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(systemBId, abi.encodeCall(EchoSystem.msgSender, ()));
assertEq(abi.decode(returnData, (address)), address(systemA));
}
function testNonCallRootFromNonRootWorld() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(systemBId, abi.encodeCall(EchoSystem.world, ()));
assertEq(abi.decode(returnData, (address)), address(world));
}
function testNonCallRootFromNonRootEcho() public {
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(systemBId, abi.encodeCall(EchoSystem.echo, ("hello")));
assertEq(abi.decode(returnData, (string)), "hello");
}
function testNonCallRootFromNonRootWorldSelector() public {
bytes4 worldFunctionSelector = world.registerRootFunctionSelector(
systemBId,
"echo(string)",
EchoSystem.echo.selector
);
bytes memory callData = abi.encodeWithSelector(worldFunctionSelector, "hello");
vm.prank(caller);
bytes memory returnData = _executeFromSystemA(callData);
assertEq(abi.decode(returnData, (string)), "hello");
}
}