Skip to content

Commit f4d6d50

Browse files
committed
fix: prevent legacy slash from breaking provision accounting (OZ H-01)
Signed-off-by: Tomás Migone <[email protected]>
1 parent 6db9beb commit f4d6d50

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

packages/subgraph-service/test/disputeManager/DisputeManager.t.sol

+46-16
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ contract DisputeManagerTest is SubgraphServiceSharedTest {
195195
return _disputeID;
196196
}
197197

198+
struct Balances {
199+
uint256 indexer;
200+
uint256 fisherman;
201+
uint256 arbitrator;
202+
uint256 disputeManager;
203+
uint256 staking;
204+
}
205+
198206
function _createLegacyDispute(
199207
address _allocationId,
200208
address _fisherman,
@@ -204,40 +212,62 @@ contract DisputeManagerTest is SubgraphServiceSharedTest {
204212
(, address arbitrator, ) = vm.readCallers();
205213
address indexer = staking.getAllocation(_allocationId).indexer;
206214

207-
bytes32 expectedDisputeId = keccak256(abi.encodePacked(_allocationId, "legacy"));
208-
uint256 disputeDeposit = 0;
209-
210-
uint256 beforeIndexerBalance = token.balanceOf(indexer);
211-
uint256 beforeFishermanBalance = token.balanceOf(_fisherman);
212-
uint256 beforeArbitratorBalance = token.balanceOf(arbitrator);
215+
Balances memory beforeBalances = Balances({
216+
indexer: token.balanceOf(indexer),
217+
fisherman: token.balanceOf(_fisherman),
218+
arbitrator: token.balanceOf(arbitrator),
219+
disputeManager: token.balanceOf(address(disputeManager)),
220+
staking: token.balanceOf(address(staking))
221+
});
213222

214223
vm.expectEmit(address(disputeManager));
215224
emit IDisputeManager.LegacyDisputeCreated(
216-
expectedDisputeId,
225+
keccak256(abi.encodePacked(_allocationId, "legacy")),
217226
indexer,
218227
_fisherman,
219228
_allocationId,
220229
_tokensSlash,
221230
_tokensRewards
222231
);
223232
vm.expectEmit(address(disputeManager));
224-
emit IDisputeManager.DisputeAccepted(expectedDisputeId, indexer, _fisherman, _tokensRewards);
233+
emit IDisputeManager.DisputeAccepted(
234+
keccak256(abi.encodePacked(_allocationId, "legacy")),
235+
indexer,
236+
_fisherman,
237+
_tokensRewards
238+
);
225239
bytes32 _disputeId = disputeManager.createLegacyDispute(
226240
_allocationId,
227241
_fisherman,
228242
_tokensSlash,
229243
_tokensRewards
230244
);
231245

232-
uint256 afterIndexerBalance = token.balanceOf(indexer);
233-
uint256 afterFishermanBalance = token.balanceOf(_fisherman);
234-
uint256 afterArbitratorBalance = token.balanceOf(arbitrator);
246+
Balances memory afterBalances = Balances({
247+
indexer: token.balanceOf(indexer),
248+
fisherman: token.balanceOf(_fisherman),
249+
arbitrator: token.balanceOf(arbitrator),
250+
disputeManager: token.balanceOf(address(disputeManager)),
251+
staking: token.balanceOf(address(staking))
252+
});
253+
254+
assertEq(afterBalances.indexer, beforeBalances.indexer);
255+
assertEq(afterBalances.fisherman, beforeBalances.fisherman + _tokensRewards);
256+
assertEq(afterBalances.arbitrator, beforeBalances.arbitrator);
257+
assertEq(afterBalances.disputeManager, beforeBalances.disputeManager);
258+
assertEq(afterBalances.staking, beforeBalances.staking - _tokensSlash);
235259

236-
assertEq(afterIndexerBalance, beforeIndexerBalance - _tokensSlash);
237-
assertEq(afterFishermanBalance, beforeFishermanBalance + _tokensRewards);
238-
assertEq(afterArbitratorBalance, beforeArbitratorBalance);
239-
assertTrue(disputeManager.isDisputeCreated(_disputeId));
240-
assertEq(expectedDisputeId, _disputeId);
260+
IDisputeManager.Dispute memory dispute = _getDispute(_disputeId);
261+
assertEq(dispute.indexer, indexer);
262+
assertEq(dispute.fisherman, _fisherman);
263+
assertEq(dispute.deposit, 0);
264+
assertEq(dispute.relatedDisputeId, bytes32(0));
265+
assertEq(uint8(dispute.disputeType), uint8(IDisputeManager.DisputeType.LegacyDispute));
266+
assertEq(uint8(dispute.status), uint8(IDisputeManager.DisputeStatus.Accepted));
267+
assertEq(dispute.createdAt, block.timestamp);
268+
assertEq(dispute.stakeSnapshot, 0);
269+
270+
return _disputeId;
241271
}
242272

243273
struct BeforeValues_CreateQueryDisputeConflict {

packages/subgraph-service/test/disputeManager/disputes/legacy.t.sol

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contract DisputeManagerLegacyDisputeTest is DisputeManagerTest {
1919
* TESTS
2020
*/
2121

22-
function test_LegacyDisputeONly(
22+
function test_LegacyDispute(
2323
uint256 tokensStaked,
2424
uint256 tokensProvisioned,
2525
uint256 tokensSlash,
@@ -29,7 +29,7 @@ contract DisputeManagerLegacyDisputeTest is DisputeManagerTest {
2929
vm.assume(tokensStaked >= minimumProvisionTokens);
3030
tokensProvisioned = bound(tokensProvisioned, minimumProvisionTokens, tokensStaked);
3131
tokensSlash = bound(tokensSlash, 2, tokensProvisioned);
32-
tokensRewards = bound(tokensRewards, 1, uint256(maxSlashingPercentage).mulPPM(tokensSlash));
32+
tokensRewards = bound(tokensRewards, 1, tokensSlash.mulPPM(maxSlashingPercentage));
3333

3434
// setup indexer state
3535
resetPrank(users.indexer);
@@ -45,4 +45,10 @@ contract DisputeManagerLegacyDisputeTest is DisputeManagerTest {
4545
vm.expectRevert(abi.encodeWithSelector(IDisputeManager.DisputeManagerNotArbitrator.selector));
4646
disputeManager.createLegacyDispute(allocationID, users.fisherman, 0, 0);
4747
}
48+
49+
function test_LegacyDispute_RevertIf_AllocationNotFound() public useIndexer {
50+
resetPrank(users.arbitrator);
51+
vm.expectRevert(abi.encodeWithSelector(IDisputeManager.DisputeManagerIndexerNotFound.selector, address(0)));
52+
disputeManager.createLegacyDispute(address(0), users.fisherman, 0, 0);
53+
}
4854
}

0 commit comments

Comments
 (0)