Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: make DG.activateNextState() first call in the Escrow's lock/unlock methods #95

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions contracts/Escrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ contract Escrow is IEscrow {
// ---

function lockStETH(uint256 amount) external returns (uint256 lockedStETHShares) {
_escrowState.checkSignallingEscrow();
DUAL_GOVERNANCE.activateNextState();
_escrowState.checkSignallingEscrow();

lockedStETHShares = ST_ETH.getSharesByPooledEth(amount);
_accounting.accountStETHSharesLock(msg.sender, SharesValues.from(lockedStETHShares));
Expand All @@ -142,10 +142,10 @@ contract Escrow is IEscrow {
}

function unlockStETH() external returns (uint256 unlockedStETHShares) {
_escrowState.checkSignallingEscrow();

DUAL_GOVERNANCE.activateNextState();
_escrowState.checkSignallingEscrow();
_accounting.checkMinAssetsLockDurationPassed(msg.sender, _escrowState.minAssetsLockDuration);

unlockedStETHShares = _accounting.accountStETHSharesUnlock(msg.sender).toUint256();
ST_ETH.transferShares(msg.sender, unlockedStETHShares);

Expand All @@ -157,8 +157,8 @@ contract Escrow is IEscrow {
// ---

function lockWstETH(uint256 amount) external returns (uint256 lockedStETHShares) {
_escrowState.checkSignallingEscrow();
DUAL_GOVERNANCE.activateNextState();
_escrowState.checkSignallingEscrow();

WST_ETH.transferFrom(msg.sender, address(this), amount);
lockedStETHShares = ST_ETH.getSharesByPooledEth(WST_ETH.unwrap(amount));
Expand All @@ -168,10 +168,10 @@ contract Escrow is IEscrow {
}

function unlockWstETH() external returns (uint256 unlockedStETHShares) {
_escrowState.checkSignallingEscrow();
DUAL_GOVERNANCE.activateNextState();

_escrowState.checkSignallingEscrow();
_accounting.checkMinAssetsLockDurationPassed(msg.sender, _escrowState.minAssetsLockDuration);

SharesValue wstETHUnlocked = _accounting.accountStETHSharesUnlock(msg.sender);
unlockedStETHShares = WST_ETH.wrap(ST_ETH.getPooledEthByShares(wstETHUnlocked.toUint256()));
WST_ETH.transfer(msg.sender, unlockedStETHShares);
Expand All @@ -183,8 +183,8 @@ contract Escrow is IEscrow {
// Lock & unlock unstETH
// ---
function lockUnstETH(uint256[] memory unstETHIds) external {
_escrowState.checkSignallingEscrow();
DUAL_GOVERNANCE.activateNextState();
_escrowState.checkSignallingEscrow();

WithdrawalRequestStatus[] memory statuses = WITHDRAWAL_QUEUE.getWithdrawalStatus(unstETHIds);
_accounting.accountUnstETHLock(msg.sender, unstETHIds, statuses);
Expand All @@ -197,10 +197,10 @@ contract Escrow is IEscrow {
}

function unlockUnstETH(uint256[] memory unstETHIds) external {
_escrowState.checkSignallingEscrow();
DUAL_GOVERNANCE.activateNextState();

_escrowState.checkSignallingEscrow();
_accounting.checkMinAssetsLockDurationPassed(msg.sender, _escrowState.minAssetsLockDuration);

_accounting.accountUnstETHUnlock(msg.sender, unstETHIds);
uint256 unstETHIdsCount = unstETHIds.length;
for (uint256 i = 0; i < unstETHIdsCount; ++i) {
Expand Down
44 changes: 44 additions & 0 deletions test/scenario/escrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,50 @@ contract EscrowHappyPath is ScenarioTestBlueprint {
this.externalUnlockUnstETH(_VETOER_1, lockedWithdrawalNfts);
}

// TODO: rewrite this test to use all stETH/wstETH/unstETH tokens
function testFork_EdgeCase_FrontRunningRageQuitWithTokensUnlockForbidden() external {
// lock enough funds to initiate RageQuit
_lockStETH(_VETOER_1, PercentsD16.fromBasisPoints(7_99));
_lockStETH(_VETOER_2, PercentsD16.fromBasisPoints(7_99));
_assertVetoSignalingState();

// wait till the last second of the dynamic timelock duration
_wait(_dualGovernanceConfigProvider.DYNAMIC_TIMELOCK_MAX_DURATION());
_activateNextState();
_assertVetoSignalingState();

( /* bool isActive */ , uint256 duration, uint256 activatedAt, /* uint256 enteredAt */ ) =
_getVetoSignallingState();
assertEq(duration + activatedAt, block.timestamp);

// validate that while the VetoSignalling has not passed, vetoer can unlock funds from Escrow
uint256 snapshotId = vm.snapshot();
_unlockStETH(_VETOER_1);
_assertVetoSignalingDeactivationState();

// Rollback the state of the node before vetoer unlocked his funds
vm.revertTo(snapshotId);

// validate that the DualGovernance still in the VetoSignalling state
_activateNextState();
_assertVetoSignalingState();

// wait 1 block duration. Full VetoSignalling duration has passed and RageQuit may be started now
_wait(Durations.from(12 seconds));

// validate that RageQuit will start when the activateNextState() is called
snapshotId = vm.snapshot();
_activateNextState();
_assertRageQuitState();

// Rollback the state of the node as it was before RageQuit activation
vm.revertTo(snapshotId);

// The attempt to unlock funds from Escrow will fail
vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.SignallingEscrow));
this.externalUnlockStETH(_VETOER_1);
}

// ---
// Helper external methods to test reverts
// ---
Expand Down