Skip to content

Commit c6b0df2

Browse files
committed
fix: add copilot suggestions
1 parent f38a71f commit c6b0df2

3 files changed

Lines changed: 101 additions & 3 deletions

File tree

src/CollateralManagement.sol

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ contract CollateralManagementContract is
5050
/// @param oldFlyoverDiscovery The previous FlyoverDiscovery address
5151
/// @param newFlyoverDiscovery The new FlyoverDiscovery address
5252
event FlyoverDiscoverySet(address indexed oldFlyoverDiscovery, address indexed newFlyoverDiscovery);
53+
/// @notice Emitted when an error occurs while removing a provider from the FlyoverDiscovery contract
54+
/// @param reason The error message
55+
event FlyoverDiscoveryError(bytes reason);
5356
/// @notice Emitted when the minimum collateral is set
5457
/// @param oldMinCollateral The old minimum collateral
5558
/// @param newMinCollateral The new minimum collateral
@@ -146,6 +149,7 @@ contract CollateralManagementContract is
146149
address flyoverDiscovery
147150
) external reinitializer(2) onlyRole(DEFAULT_ADMIN_ROLE) {
148151
if (address(flyoverDiscovery).code.length == 0) revert Flyover.NoContract(flyoverDiscovery);
152+
emit FlyoverDiscoverySet(address(_flyoverDiscovery), flyoverDiscovery);
149153
_flyoverDiscovery = IFlyoverDiscovery(flyoverDiscovery);
150154
}
151155

@@ -390,7 +394,12 @@ contract CollateralManagementContract is
390394
/// @param providerAddress The address of the provider
391395
function _notifyCollateralWithdrawal(address providerAddress) private {
392396
if (address(_flyoverDiscovery) != address(0)) {
393-
_flyoverDiscovery.removeProvider(providerAddress);
397+
// try / catch is to avoid blocking the collateral withdrawal if the discovery is misconfigured
398+
// solhint-disable-next-line no-empty-blocks
399+
try _flyoverDiscovery.removeProvider(providerAddress) {}
400+
catch (bytes memory reason) {
401+
emit FlyoverDiscoveryError(reason);
402+
}
394403
}
395404
}
396405

src/FlyoverDiscovery.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ contract FlyoverDiscovery is
8080
// solhint-disable-next-line comprehensive-interface,func-name-mixedcase
8181
function initializeV2_1_0() external reinitializer(2) onlyRole(DEFAULT_ADMIN_ROLE) {
8282
Flyover.LiquidityProvider storage lp;
83+
bool providerExists;
8384
for (uint i = 1; i < lastProviderId + 1; ++i) {
8485
lp = _liquidityProviders[i];
85-
if (_collateralManagement.isRegistered(lp.providerType, lp.providerAddress)) {
86-
_addActiveProvider(i);
86+
providerExists = lp.providerAddress != address(0);
87+
if (providerExists) {
8788
_registrationStates[lp.providerAddress] = RegistrationState.Approved;
8889
}
90+
if (providerExists && _collateralManagement.isRegistered(lp.providerType, lp.providerAddress)) {
91+
_addActiveProvider(i);
92+
}
8993
}
9094
}
9195

test/discovery/ActiveListing.t.sol

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,41 @@ contract ActiveListingTest is DiscoveryTestBase {
273273
_assertProvidersExclude(lp);
274274
}
275275

276+
function test_WithdrawCollateral_SucceedsWhenRemoveProviderReverts()
277+
public
278+
{
279+
deployDiscovery();
280+
address lp = makeAddr("revertCallbackLp");
281+
_registerAndApprove(
282+
lp,
283+
"LP",
284+
Flyover.ProviderType.PegIn,
285+
MIN_COLLATERAL
286+
);
287+
288+
RemoveProviderRevertMock brokenDiscovery = new RemoveProviderRevertMock();
289+
vm.prank(owner);
290+
collateralManagement.setFlyoverDiscovery(address(brokenDiscovery));
291+
292+
uint256 balanceBefore = lp.balance;
293+
294+
vm.prank(lp);
295+
collateralManagement.resign();
296+
vm.roll(block.number + TEST_RESIGN_DELAY_BLOCKS);
297+
298+
vm.expectEmit(true, true, false, true);
299+
emit CollateralManagementContract.FlyoverDiscoveryError(
300+
abi.encodeWithSelector(
301+
RemoveProviderRevertMock.RemoveProviderFailed.selector
302+
)
303+
);
304+
vm.prank(lp);
305+
collateralManagement.withdrawCollateral();
306+
307+
assertEq(lp.balance, balanceBefore + MIN_COLLATERAL);
308+
assertEq(collateralManagement.getPegInCollateral(lp), 0);
309+
}
310+
276311
// ============ Initialization — FlyoverDiscovery ============
277312

278313
function test_InitializeV2_1_0_EmptyStateSucceeds() public {
@@ -411,6 +446,37 @@ contract ActiveListingTest is DiscoveryTestBase {
411446

412447
assertEq(discovery.getProviders().length, 1);
413448
_assertProvidersContain(keptLp);
449+
450+
assertEq(
451+
uint256(discovery.getRegistrationState(pendingLp)),
452+
uint256(IFlyoverDiscovery.RegistrationState.Pending),
453+
"pending-only slots must not be backfilled to Approved"
454+
);
455+
456+
assertEq(
457+
uint256(discovery.getRegistrationState(resignedLp)),
458+
uint256(IFlyoverDiscovery.RegistrationState.Approved),
459+
"resigned LPs with discovery records should be marked Approved"
460+
);
461+
assertEq(discovery.getProvider(resignedLp).providerAddress, resignedLp);
462+
assertFalse(
463+
discovery.isOperational(Flyover.ProviderType.PegIn, resignedLp)
464+
);
465+
_assertProvidersExclude(resignedLp);
466+
467+
assertEq(
468+
uint256(discovery.getRegistrationState(withdrawnLp)),
469+
uint256(IFlyoverDiscovery.RegistrationState.Approved),
470+
"withdrawn LPs with discovery records should be marked Approved"
471+
);
472+
assertEq(
473+
discovery.getProvider(withdrawnLp).providerAddress,
474+
withdrawnLp
475+
);
476+
assertFalse(
477+
discovery.isOperational(Flyover.ProviderType.PegIn, withdrawnLp)
478+
);
479+
_assertProvidersExclude(withdrawnLp);
414480
}
415481

416482
function test_InitializeV2_1_0_BackfillIgnoresInflatedLastProviderId()
@@ -453,6 +519,11 @@ contract ActiveListingTest is DiscoveryTestBase {
453519
MIN_COLLATERAL
454520
);
455521

522+
vm.expectEmit(true, true, false, true);
523+
emit CollateralManagementContract.FlyoverDiscoverySet(
524+
address(0),
525+
address(discovery)
526+
);
456527
vm.prank(owner);
457528
collateralManagement.initializeV2_1_0(address(discovery));
458529

@@ -537,6 +608,11 @@ contract ActiveListingTest is DiscoveryTestBase {
537608
cm.initializeV2_1_0(address(discoveryA));
538609
discoveryA.initializeV2_1_0();
539610
cm.grantRole(cm.COLLATERAL_ADDER(), address(discoveryB));
611+
vm.expectEmit(true, true, false, true);
612+
emit CollateralManagementContract.FlyoverDiscoverySet(
613+
address(discoveryA),
614+
address(discoveryB)
615+
);
540616
cm.setFlyoverDiscovery(address(discoveryB));
541617
discoveryB.initializeV2_1_0();
542618
vm.stopPrank();
@@ -677,3 +753,12 @@ contract ActiveListingTest is DiscoveryTestBase {
677753
assertEq(discovery.getProviders().length, 0);
678754
}
679755
}
756+
757+
/// @dev Discovery stand-in that always reverts on removeProvider
758+
contract RemoveProviderRevertMock {
759+
error RemoveProviderFailed();
760+
761+
function removeProvider(address) external pure {
762+
revert RemoveProviderFailed();
763+
}
764+
}

0 commit comments

Comments
 (0)