-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBaseRouter.t.sol
108 lines (82 loc) · 3.73 KB
/
TestBaseRouter.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
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.8.18;
import {Test} from "@forge-std/Test.sol";
import {Vm} from "@forge-std/Vm.sol";
import {BaseRouter} from "@src/base/BaseRouter.sol";
import {MulticallerEtcher} from "@vec-multicaller/MulticallerEtcher.sol";
import {MulticallerWithSender} from "@vec-multicaller/MulticallerWithSender.sol";
import "@test/base/TestBaseProtocol.sol";
contract Router is BaseRouter {
event CurrentCaller(address caller);
constructor(IProtocolAddressRegistry addressRegistry) BaseRouter(addressRegistry) {}
function fun4() external setCaller returns (uint256) {
emit CurrentCaller(caller);
return 4;
}
function fun5() external setCaller returns (uint256) {
emit CurrentCaller(caller);
return 5;
}
function shouldRevert1() external pure {
revert("should revert 1");
}
}
contract TestBaseRouter is TestBaseProtocol {
Router router;
function setUp() public override {
super.setUp();
router = new Router(protocolAddressRegistry);
vm.label(address(router), "Router");
}
modifier invariants() {
/// @notice the caller address is a transient variable that should always be 0 before each transaction
bytes32 callerAddress = vm.load(address(router), bytes32(uint256(0)));
assertEq(bytes32(0), callerAddress);
_;
/// @notice the caller address is a transient variable that should be reset after each transaction
callerAddress = vm.load(address(router), bytes32(uint256(0)));
assertEq(bytes32(0), callerAddress);
}
function test_caller_is_set() public invariants {
vm.recordLogs();
router.fun4();
router.fun5();
Vm.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries.length, 2);
assertEq(entries[0].emitter, address(router));
assertEq(entries[0].topics.length, 1);
assertEq(entries[0].topics[0], bytes32(uint256(keccak256("CurrentCaller(address)"))));
assertEq(entries[0].data.length, 32);
assertEq(abi.decode(entries[0].data, (address)), address(this));
assertEq(entries[1].emitter, address(router));
assertEq(entries[1].topics.length, 1);
assertEq(entries[1].topics[0], bytes32(uint256(keccak256("CurrentCaller(address)"))));
assertEq(entries[1].data.length, 32);
assertEq(abi.decode(entries[1].data, (address)), address(this));
}
function test_caller_is_forwarded_from_multicall() public invariants {
vm.recordLogs();
address[] memory targets = new address[](2);
targets[0] = address(router);
targets[1] = address(router);
bytes[] memory data = new bytes[](2);
data[0] = abi.encodeWithSelector(router.fun4.selector);
data[1] = abi.encodeWithSelector(router.fun5.selector);
uint256[] memory values = new uint256[](2);
values[0] = 0;
values[1] = 0;
multicallerWithSender.aggregateWithSender(targets, data, values);
Vm.Log[] memory entries = vm.getRecordedLogs();
assertEq(entries.length, 2);
assertEq(entries[0].emitter, address(router));
assertEq(entries[0].topics.length, 1);
assertEq(entries[0].topics[0], bytes32(uint256(keccak256("CurrentCaller(address)"))));
assertEq(entries[0].data.length, 32);
assertEq(abi.decode(entries[0].data, (address)), address(this));
assertEq(entries[1].emitter, address(router));
assertEq(entries[1].topics.length, 1);
assertEq(entries[1].topics[0], bytes32(uint256(keccak256("CurrentCaller(address)"))));
assertEq(entries[1].data.length, 32);
assertEq(abi.decode(entries[1].data, (address)), address(this));
}
}