-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamWeightActor.t.sol
More file actions
323 lines (264 loc) · 12.6 KB
/
Copy pathStreamWeightActor.t.sol
File metadata and controls
323 lines (264 loc) · 12.6 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: Apache-2.0 OR MIT
pragma solidity ^0.8.36;
import {USR_FORBIDDEN, USR_ILLEGAL_ARGUMENT, USR_NOT_FOUND} from "fvm-solidity/FVMErrors.sol";
import {MockMultisig} from "./mocks/MockMultisig.sol";
import {MockRewardTest} from "./mocks/MockRewardTest.sol";
import {WAD} from "./mocks/FVMRewardActor.sol";
import {StreamWeightActor} from "../src/StreamWeightActor.sol";
import {IServiceRewardsActor} from "../src/interfaces/IServiceRewardsActor.sol";
import {PendingOp, Share, WeightRecord, WeightRecordUpdate} from "../src/lib/FVMRewardTypes.sol";
import {Epoch} from "../src/lib/Epoch.sol";
import {FVMRewards} from "../src/lib/FVMRewards.sol";
import {IsAMultisig} from "../src/lib/IsAMultisig.sol";
import {SWA_TIMELOCK} from "../src/lib/FVMRewardMethod.sol";
contract StreamWeightActorTest is MockRewardTest {
StreamWeightActor actor;
address owner1;
address owner2;
uint64 constant STREAM_ID = 1;
address constant WRITER = address(0xBEEF);
Epoch constant TEST_QUARTER = Epoch.wrap(262980); // epochs per 365.25/4 days
Epoch constant TEST_HOLD = Epoch.wrap(2 * 60 * 24 * 7); // epochs per 7 days
function setUp() public override {
super.setUp();
owner1 = _makeMultisigOwner("owner1");
owner2 = _makeMultisigOwner("owner2");
address sra = makeAddr("sra");
vm.mockCall(
sra, abi.encodeWithSelector(IServiceRewardsActor.EPOCHS_PER_QUARTER.selector), abi.encode(TEST_QUARTER)
);
vm.mockCall(sra, abi.encodeWithSelector(IServiceRewardsActor.SRA_CANCEL_HOLD.selector), abi.encode(TEST_HOLD));
actor = new StreamWeightActor(owner1, owner2, IServiceRewardsActor(sra));
rewardActor().mockSwa(address(actor));
}
function _makeMultisigOwner(string memory label) internal returns (address multisig) {
multisig = address(new MockMultisig(2));
vm.label(multisig, label);
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function _record(int256 w) internal pure returns (WeightRecord memory) {
return WeightRecord({vStart: w, slope: 0, tStart: Epoch.wrap(0), floor: 0, cap: WAD});
}
function _activation() internal view returns (uint64) {
return uint64(block.number) + SWA_TIMELOCK;
}
function _shares() internal pure returns (Share[] memory shares) {
shares = new Share[](1);
shares[0] = Share({wallet: WRITER, share: uint256(WAD)});
}
function _singleWeightRecord(uint64 id, WeightRecord memory record)
internal
pure
returns (WeightRecordUpdate[] memory updates)
{
updates = new WeightRecordUpdate[](1);
updates[0] = WeightRecordUpdate({id: id, record: record});
}
/// @dev Registers STREAM_ID (EXPLICIT, WRITER) through both owners, then rolls past the
/// timelock so the next dispatched call settles it into existence.
function _registerAndActivate(uint64 id) internal {
vm.prank(owner1);
actor.registerStream(id, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner2);
actor.registerStream(id, _record(0.1e18), WRITER, _shares(), _activation());
vm.roll(block.number + SWA_TIMELOCK);
}
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
function test_Constructor_AddsBothOwners() public {
// Indirect proof of ownership: cancelPendingWeight's owner check only lets a real owner
// through, and the mock accepts the empty-slot no-op once it does.
vm.prank(owner1);
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
vm.prank(owner2);
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
}
// -------------------------------------------------------------------------
// registerStream
// -------------------------------------------------------------------------
function test_RegisterStream_Success() public {
vm.prank(owner1);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner2);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
}
function test_RegisterStream_NotSwa_RevertsForbidden() public {
rewardActor().mockSwa(address(0xDEAD));
vm.prank(owner1);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.RegisterStreamFailed.selector, int256(uint256(USR_FORBIDDEN)))
);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
}
// Also proves the first registration itself succeeded: a nonexistent stream can't collide.
function test_RegisterStream_DuplicatePendingId_RevertsIllegalArgument() public {
vm.prank(owner1);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner2);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner1);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.RegisterStreamFailed.selector, int256(uint256(USR_ILLEGAL_ARGUMENT)))
);
actor.registerStream(STREAM_ID, _record(0.1e18), WRITER, _shares(), _activation());
}
// -------------------------------------------------------------------------
// removeStream
// -------------------------------------------------------------------------
function test_RemoveStream_Success() public {
_registerAndActivate(STREAM_ID);
vm.prank(owner1);
actor.removeStream(STREAM_ID);
vm.prank(owner2);
actor.removeStream(STREAM_ID);
}
function test_RemoveStream_NotSwa_RevertsForbidden() public {
rewardActor().mockSwa(address(0xDEAD));
vm.prank(owner1);
actor.removeStream(STREAM_ID);
vm.prank(owner2);
vm.expectRevert(abi.encodeWithSelector(FVMRewards.RemoveStreamFailed.selector, int256(uint256(USR_FORBIDDEN))));
actor.removeStream(STREAM_ID);
}
function test_RemoveStream_Nonexistent_RevertsNotFound() public {
vm.prank(owner1);
actor.removeStream(STREAM_ID);
vm.prank(owner2);
vm.expectRevert(abi.encodeWithSelector(FVMRewards.RemoveStreamFailed.selector, int256(uint256(USR_NOT_FOUND))));
actor.removeStream(STREAM_ID);
}
// -------------------------------------------------------------------------
// setWeightRecords
// -------------------------------------------------------------------------
function test_SetWeightRecords_Success() public {
_registerAndActivate(STREAM_ID);
WeightRecordUpdate[] memory updates = _singleWeightRecord(STREAM_ID, _record(0.2e18));
vm.prank(owner1);
actor.setWeightRecords(updates);
vm.prank(owner2);
actor.setWeightRecords(updates);
}
function test_SetWeightRecords_NotSwa_RevertsForbidden() public {
rewardActor().mockSwa(address(0xDEAD));
WeightRecordUpdate[] memory updates = _singleWeightRecord(STREAM_ID, _record(0.2e18));
vm.prank(owner1);
actor.setWeightRecords(updates);
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.SetWeightRecordsFailed.selector, int256(uint256(USR_FORBIDDEN)))
);
actor.setWeightRecords(updates);
}
function test_SetWeightRecords_Nonexistent_RevertsNotFound() public {
WeightRecordUpdate[] memory updates = _singleWeightRecord(STREAM_ID, _record(0.2e18));
vm.prank(owner1);
actor.setWeightRecords(updates);
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.SetWeightRecordsFailed.selector, int256(uint256(USR_NOT_FOUND)))
);
actor.setWeightRecords(updates);
}
// -------------------------------------------------------------------------
// setDistribution
// -------------------------------------------------------------------------
function test_SetDistribution_Success() public {
_registerAndActivate(STREAM_ID);
vm.prank(owner1);
actor.setDistribution(STREAM_ID, WRITER);
vm.prank(owner2);
actor.setDistribution(STREAM_ID, WRITER);
}
function test_SetDistribution_NotSwa_RevertsForbidden() public {
rewardActor().mockSwa(address(0xDEAD));
vm.prank(owner1);
actor.setDistribution(STREAM_ID, WRITER);
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.SetDistributionFailed.selector, int256(uint256(USR_FORBIDDEN)))
);
actor.setDistribution(STREAM_ID, WRITER);
}
function test_SetDistribution_Nonexistent_RevertsNotFound() public {
vm.prank(owner1);
actor.setDistribution(STREAM_ID, WRITER);
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.SetDistributionFailed.selector, int256(uint256(USR_NOT_FOUND)))
);
actor.setDistribution(STREAM_ID, WRITER);
}
function test_SetDistribution_ZeroWriter_RevertsIllegalArgument() public {
_registerAndActivate(STREAM_ID);
vm.prank(owner1);
actor.setDistribution(STREAM_ID, address(0));
vm.prank(owner2);
vm.expectRevert(
abi.encodeWithSelector(FVMRewards.SetDistributionFailed.selector, int256(uint256(USR_ILLEGAL_ARGUMENT)))
);
actor.setDistribution(STREAM_ID, address(0));
}
// -------------------------------------------------------------------------
// cancelPending -- ungoverned: any single owner may call immediately.
// -------------------------------------------------------------------------
function test_CancelPending_NotOwner_Reverts() public {
vm.prank(makeAddr("stranger"));
vm.expectRevert();
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
}
function test_CancelPending_Owner_EmptySlot_Succeeds() public {
vm.prank(owner1);
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
}
function test_CancelPending_NotSwa_RevertsForbidden() public {
rewardActor().mockSwa(address(0xDEAD));
vm.prank(owner1);
vm.expectRevert(abi.encodeWithSelector(FVMRewards.CancelPendingFailed.selector, int256(uint256(USR_FORBIDDEN))));
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
}
// -------------------------------------------------------------------------
// replaceOwner
// -------------------------------------------------------------------------
function test_ReplaceOwner_Success_SwapsApprovalRights() public {
address newOwner = _makeMultisigOwner("newOwner");
vm.prank(owner1);
actor.replaceOwner(owner2, newOwner);
vm.prank(owner2);
actor.replaceOwner(owner2, newOwner);
// owner2 was removed: no longer recognized by cancelPending's owner check.
vm.prank(owner2);
vm.expectRevert();
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
// newOwner was added: recognized immediately.
vm.prank(newOwner);
actor.cancelPendingWeight(PendingOp.SET_WEIGHT);
}
// -------------------------------------------------------------------------
// Owner must be a multisig
// -------------------------------------------------------------------------
function test_Constructor_EoaOwner_RevertsNotAMultisig() public {
address eoa = makeAddr("eoa");
vm.expectRevert(abi.encodeWithSelector(IsAMultisig.NotAMultisig.selector, eoa));
new StreamWeightActor(owner1, eoa, IServiceRewardsActor(makeAddr("sra")));
}
function test_Constructor_SingleSignerOwner_RevertsTooFewSigners() public {
address solo = address(new MockMultisig(1));
vm.expectRevert(abi.encodeWithSelector(IsAMultisig.TooFewSigners.selector, solo, uint256(1)));
new StreamWeightActor(owner1, solo, IServiceRewardsActor(makeAddr("sra")));
}
function test_ReplaceOwner_EoaOwner_RevertsNotAMultisig() public {
address eoa = makeAddr("eoa");
vm.prank(owner1);
actor.replaceOwner(owner2, eoa);
vm.prank(owner2);
vm.expectRevert(abi.encodeWithSelector(IsAMultisig.NotAMultisig.selector, eoa));
actor.replaceOwner(owner2, eoa);
}
}