From ecc21065e64ac19c4ba6a0374d916c9dfec7bc5e Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Fri, 9 Aug 2024 19:27:20 +0400 Subject: [PATCH 1/8] Add tests for EscrowState library --- test/unit/libraries/EscrowState.t.sol | 322 ++++++++++++++++++++++++++ 1 file changed, 322 insertions(+) create mode 100644 test/unit/libraries/EscrowState.t.sol diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol new file mode 100644 index 00000000..d5740784 --- /dev/null +++ b/test/unit/libraries/EscrowState.t.sol @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.26; + +import {Vm} from "forge-std/Vm.sol"; + +import {Duration, Durations, MAX_VALUE as DURATION_MAX_VALUE} from "contracts/types/Duration.sol"; +import {Timestamp, Timestamps, MAX_TIMESTAMP_VALUE, TimestampOverflow} from "contracts/types/Timestamp.sol"; +import {EscrowState, State} from "contracts/libraries/EscrowState.sol"; + +import {UnitTest} from "test/utils/unit-test.sol"; + +Duration constant D0 = Durations.ZERO; +Timestamp constant T0 = Timestamps.ZERO; + +contract EscrowStateUnitTests is UnitTest { + EscrowState.Context private _context; + + // --- + // initialize() + // --- + + function testFuzz_initialize_happyPath(uint32 minAssetsLockSeconds) external { + Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); + _context.state = State.NotInitialized; + + vm.expectEmit(); + emit EscrowState.EscrowStateChanged(State.NotInitialized, State.SignallingEscrow); + emit EscrowState.MinAssetsLockDurationSet(minAssetsLockDuration); + + EscrowState.initialize(_context, minAssetsLockDuration); + + checkContext(State.SignallingEscrow, minAssetsLockDuration, D0, D0, T0); + } + + function testFuzz_initialize_RevertOn_InvalidState(uint32 minAssetsLockSeconds) external { + _context.state = State.SignallingEscrow; + + // TODO: not very informative, maybe need to change to `revert UnexpectedState(self.state);` + vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.NotInitialized)); + + EscrowState.initialize(_context, Durations.from(minAssetsLockSeconds)); + } + + // --- + // startRageQuit() + // --- + + function testFuzz_startRageQuit_happyPath( + uint32 rageQuitExtensionDelaySeconds, + uint32 rageQuitWithdrawalsTimelockSeconds + ) external { + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + + _context.state = State.SignallingEscrow; + + vm.expectEmit(); + emit EscrowState.EscrowStateChanged(State.SignallingEscrow, State.RageQuitEscrow); + emit EscrowState.RageQuitStarted(rageQuitExtensionDelay, rageQuitWithdrawalsTimelock); + + EscrowState.startRageQuit(_context, rageQuitExtensionDelay, rageQuitWithdrawalsTimelock); + + checkContext(State.RageQuitEscrow, D0, rageQuitExtensionDelay, rageQuitWithdrawalsTimelock, T0); + } + + function testFuzz_startRageQuit_RevertOn_InvalidState( + uint32 rageQuitExtensionDelaySeconds, + uint32 rageQuitWithdrawalsTimelockSeconds + ) external { + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + + _context.state = State.NotInitialized; + + vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.SignallingEscrow)); + + EscrowState.startRageQuit(_context, rageQuitExtensionDelay, rageQuitWithdrawalsTimelock); + } + + // --- + // startRageQuitExtensionDelay() + // --- + + function test_startRageQuitExtensionDelay_happyPath() external { + vm.expectEmit(); + emit EscrowState.RageQuitTimelockStarted(); + + EscrowState.startRageQuitExtensionDelay(_context); + + checkContext(State.NotInitialized, D0, D0, D0, Timestamps.now()); + } + + // --- + // setMinAssetsLockDuration() + // --- + + function test_setMinAssetsLockDuration_happyPath(uint32 minAssetsLockSeconds) external { + vm.assume(minAssetsLockSeconds != 0); + Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); + + vm.expectEmit(); + emit EscrowState.MinAssetsLockDurationSet(minAssetsLockDuration); + + EscrowState.setMinAssetsLockDuration(_context, minAssetsLockDuration); + + checkContext(State.NotInitialized, minAssetsLockDuration, D0, D0, T0); + } + + function test_setMinAssetsLockDuration_WhenDurationNotChanged(uint32 minAssetsLockSeconds) external { + Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); + + _context.minAssetsLockDuration = minAssetsLockDuration; + + Vm.Log[] memory entries = vm.getRecordedLogs(); + + EscrowState.setMinAssetsLockDuration(_context, minAssetsLockDuration); + + checkContext(State.NotInitialized, minAssetsLockDuration, D0, D0, T0); + + assertEq(entries.length, 0); + } + + // --- + // checkSignallingEscrow() + // --- + + function test_checkSignallingEscrow_happyPath() external { + _context.state = State.SignallingEscrow; + EscrowState.checkSignallingEscrow(_context); + } + + function test_checkSignallingEscrow_RevertOn_InvalidState() external { + vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.SignallingEscrow)); + + EscrowState.checkSignallingEscrow(_context); + } + + // --- + // checkRageQuitEscrow() + // --- + + function test_checkRageQuitEscrow_happyPath() external { + _context.state = State.RageQuitEscrow; + EscrowState.checkRageQuitEscrow(_context); + } + + function test_checkRageQuitEscrow_RevertOn_InvalidState() external { + vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.RageQuitEscrow)); + + EscrowState.checkRageQuitEscrow(_context); + } + + // --- + // checkBatchesClaimingInProgress() + // --- + + function test_checkBatchesClaimingInProgress_happyPath() external view { + EscrowState.checkBatchesClaimingInProgress(_context); + } + + function testFuzz_checkBatchesClaimingInProgress_RevertOn_InvalidState( + uint40 rageQuitExtensionDelayStartedAtSeconds + ) external { + vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + vm.expectRevert(EscrowState.ClaimingIsFinished.selector); + + EscrowState.checkBatchesClaimingInProgress(_context); + } + + // --- + // checkWithdrawalsTimelockPassed() + // --- + + function testFuzz_checkWithdrawalsTimelockPassed_happyPath( + uint16 rageQuitExtensionDelayStartedAtSeconds, + uint16 rageQuitExtensionDelaySeconds, + uint16 rageQuitWithdrawalsTimelockSeconds + ) external { + vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelay = rageQuitExtensionDelay; + _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; + + vm.warp( + uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) + + uint256(rageQuitWithdrawalsTimelockSeconds) + 1 + ); + EscrowState.checkWithdrawalsTimelockPassed(_context); + } + + function test_checkWithdrawalsTimelockPassed_RevertWhen_RageQuitExtraTimelockNotStarted() external { + vm.expectRevert(EscrowState.RageQuitExtraTimelockNotStarted.selector); + + EscrowState.checkWithdrawalsTimelockPassed(_context); + } + + function testFuzz_checkWithdrawalsTimelockPassed_RevertWhen_WithdrawalsTimelockNotPassed( + uint16 rageQuitExtensionDelayStartedAtSeconds, + uint16 rageQuitExtensionDelaySeconds, + uint16 rageQuitWithdrawalsTimelockSeconds + ) external { + vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelay = rageQuitExtensionDelay; + _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; + + vm.warp( + uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) + + uint256(rageQuitWithdrawalsTimelockSeconds) + ); + + vm.expectRevert(EscrowState.WithdrawalsTimelockNotPassed.selector); + + EscrowState.checkWithdrawalsTimelockPassed(_context); + } + + function test_checkWithdrawalsTimelockPassed_RevertWhen_WithdrawalsTimelockOverflow() external { + Duration rageQuitExtensionDelay = Durations.from(DURATION_MAX_VALUE / 2); + Duration rageQuitWithdrawalsTimelock = Durations.from(DURATION_MAX_VALUE / 2 + 1); + + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(MAX_TIMESTAMP_VALUE - 1); + _context.rageQuitExtensionDelay = rageQuitExtensionDelay; + _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; + + vm.expectRevert(TimestampOverflow.selector); + + EscrowState.checkWithdrawalsTimelockPassed(_context); + } + + // --- + // isRageQuitExtensionDelayStarted() + // --- + + function testFuzz_isRageQuitExtensionDelayStarted_happyPath(uint40 rageQuitExtensionDelayStartedAtSeconds) + external + { + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + bool res = EscrowState.isRageQuitExtensionDelayStarted(_context); + assert(res == _context.rageQuitExtensionDelayStartedAt.isNotZero()); + } + + // --- + // isRageQuitExtensionDelayPassed() + // --- + + function testFuzz_isRageQuitExtensionDelayPassed_ReturnsTrue( + uint16 rageQuitExtensionDelayStartedAtSeconds, + uint16 rageQuitExtensionDelaySeconds + ) external { + vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelay = rageQuitExtensionDelay; + + vm.warp(uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) + 1); + bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); + assert(res == true); + } + + function testFuzz_isRageQuitExtensionDelayPassed_ReturnsFalse( + uint16 rageQuitExtensionDelayStartedAtSeconds, + uint16 rageQuitExtensionDelaySeconds + ) external { + vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + + Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + + _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelay = rageQuitExtensionDelay; + + vm.warp(uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds)); + bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); + assert(res == false); + } + + function test_isRageQuitExtensionDelayPassed_ReturnsFalseWhenRageQuitExtraTimelockNotStarted() external { + vm.warp(1234); + bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); + assert(res == false); + } + + // --- + // isRageQuitEscrow() + // --- + + function testFuzz_isRageQuitEscrow(bool expectedResult) external { + if (expectedResult) { + _context.state = State.RageQuitEscrow; + } + bool actualResult = EscrowState.isRageQuitEscrow(_context); + assert(actualResult == expectedResult); + } + + // --- + // helpers() + // --- + + function checkContext( + State state, + Duration minAssetsLockDuration, + Duration rageQuitExtensionDelay, + Duration rageQuitWithdrawalsTimelock, + Timestamp rageQuitExtensionDelayStartedAt + ) internal view { + assert(_context.state == state); + assert(_context.minAssetsLockDuration == minAssetsLockDuration); + assert(_context.rageQuitExtensionDelay == rageQuitExtensionDelay); + assert(_context.rageQuitWithdrawalsTimelock == rageQuitWithdrawalsTimelock); + assert(_context.rageQuitExtensionDelayStartedAt == rageQuitExtensionDelayStartedAt); + } +} From a400f978785f6bed06425483a57586e81301dfac Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 12 Aug 2024 17:52:02 +0400 Subject: [PATCH 2/8] Fix AssetsAccounting tests after renaming --- test/unit/libraries/AssetsAccounting.t.sol | 582 +++++++++++---------- 1 file changed, 293 insertions(+), 289 deletions(-) diff --git a/test/unit/libraries/AssetsAccounting.t.sol b/test/unit/libraries/AssetsAccounting.t.sol index f41865cc..408d4674 100644 --- a/test/unit/libraries/AssetsAccounting.t.sol +++ b/test/unit/libraries/AssetsAccounting.t.sol @@ -17,7 +17,7 @@ import { import {UnitTest, Duration} from "test/utils/unit-test.sol"; contract AssetsAccountingUnitTests is UnitTest { - AssetsAccounting.Context private _accountingState; + AssetsAccounting.Context private _accountingContext; // --- // accountStETHSharesLock() @@ -34,19 +34,21 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue shares = SharesValues.from(sharesAmount); - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectEmit(); emit AssetsAccounting.StETHSharesLocked(holder, shares); - AssetsAccounting.accountStETHSharesLock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesLock(_accountingContext, holder, shares); - checkAccountingStateTotalCounters(totalLockedShares + shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); - assert(_accountingState.assets[holder].stETHLockedShares == holderLockedShares + shares); - assert(_accountingState.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + checkAccountingContextTotalCounters( + totalLockedShares + shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO + ); + assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares + shares); + assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountStETHSharesLock_RevertWhen_ZeroSharesProvided(address holder) external { @@ -54,7 +56,7 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); - AssetsAccounting.accountStETHSharesLock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesLock(_accountingContext, holder, shares); } function testFuzz_accountStETHSharesLock_WhenNoSharesWereLockedBefore( @@ -68,20 +70,20 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue shares = SharesValues.from(sharesAmount); - _accountingState.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; vm.expectEmit(); emit AssetsAccounting.StETHSharesLocked(stranger, shares); - AssetsAccounting.accountStETHSharesLock(_accountingState, stranger, shares); + AssetsAccounting.accountStETHSharesLock(_accountingContext, stranger, shares); - assert(_accountingState.stETHTotals.lockedShares == totalLockedShares + shares); - assert(_accountingState.assets[stranger].stETHLockedShares == shares); - assert(_accountingState.assets[stranger].lastAssetsLockTimestamp <= Timestamps.now()); + assert(_accountingContext.stETHTotals.lockedShares == totalLockedShares + shares); + assert(_accountingContext.assets[stranger].stETHLockedShares == shares); + assert(_accountingContext.assets[stranger].lastAssetsLockTimestamp <= Timestamps.now()); } // --- - // accountStETHSharesUnlock(State storage self, address holder, SharesValue shares) + // accountStETHSharesUnlock(Context storage self, address holder, SharesValue shares) // --- function testFuzz_accountStETHSharesUnlock_happyPath( @@ -98,19 +100,21 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectEmit(); emit AssetsAccounting.StETHSharesUnlocked(holder, shares); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder, shares); - checkAccountingStateTotalCounters(totalLockedShares - shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); - assert(_accountingState.assets[holder].stETHLockedShares == holderLockedShares - shares); - assert(_accountingState.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + checkAccountingContextTotalCounters( + totalLockedShares - shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO + ); + assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares - shares); + assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountStETHSharesUnlock_RevertOn_ZeroSharesProvided(address holder) external { @@ -118,7 +122,7 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder, shares); } function testFuzz_accountStETHSharesUnlock_RevertWhen_HolderHaveLessSharesThanProvided( @@ -135,12 +139,12 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder, shares); } function testFuzz_accountStETHSharesUnlock_RevertOn_AccountingError_TotalLockedSharesCounterIsLessThanProvidedSharesAmount( @@ -155,12 +159,12 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderLockedShares = SharesValues.from(sharesAmount); SharesValue totalLockedShares = SharesValues.from(totalSharesAmount); - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, holder, shares); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder, shares); } function testFuzz_accountStETHSharesUnlock_RevertWhen_NoSharesWereLockedBefore( @@ -173,11 +177,11 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, stranger, shares); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, stranger, shares); } // --- - // accountStETHSharesUnlock(State storage self, address holder) + // accountStETHSharesUnlock(Context storage self, address holder) // --- function testFuzz_accountStETHSharesUnlock_simple_happyPath(address holder, uint128 holderSharesAmount) external { @@ -188,28 +192,28 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectEmit(); emit AssetsAccounting.StETHSharesUnlocked(holder, holderLockedShares); - SharesValue unlockedShares = AssetsAccounting.accountStETHSharesUnlock(_accountingState, holder); + SharesValue unlockedShares = AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder); assert(unlockedShares == holderLockedShares); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( totalLockedShares - holderLockedShares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO ); - assert(_accountingState.assets[holder].stETHLockedShares == holderLockedShares - holderLockedShares); - assert(_accountingState.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares - holderLockedShares); + assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountStETHSharesUnlock_simple_RevertWhen_NoSharesWereLockedBefore(address stranger) external { vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, SharesValues.ZERO)); - AssetsAccounting.accountStETHSharesUnlock(_accountingState, stranger); + AssetsAccounting.accountStETHSharesUnlock(_accountingContext, stranger); } // --- @@ -230,9 +234,9 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.stETHTotals.claimedETH = totalClaimedETH; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.stETHTotals.claimedETH = totalClaimedETH; ETHValue expectedETHWithdrawn = ETHValues.from((uint256(totalClaimedETHAmount) * holderLockedSharesAmount) / totalLockedSharesAmount); @@ -240,20 +244,20 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.ETHWithdrawn(holder, holderLockedShares, expectedETHWithdrawn); - ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingState, holder); + ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); assert(ethWithdrawn == expectedETHWithdrawn); - checkAccountingStateTotalCounters(totalLockedShares, totalClaimedETH, SharesValues.ZERO, ETHValues.ZERO); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + checkAccountingContextTotalCounters(totalLockedShares, totalClaimedETH, SharesValues.ZERO, ETHValues.ZERO); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountStETHSharesWithdraw_RevertWhen_HolderHaveZeroShares(address stranger) external { vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, SharesValues.ZERO)); - AssetsAccounting.accountStETHSharesWithdraw(_accountingState, stranger); + AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, stranger); } function testFuzz_accountStETHSharesWithdraw_RevertOn_AccountingError_TotalLockedSharesCounterIsZero( @@ -266,13 +270,13 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderLockedShares = SharesValues.from(holderLockedSharesAmount); ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; - _accountingState.stETHTotals.lockedShares = SharesValues.ZERO; - _accountingState.stETHTotals.claimedETH = totalClaimedETH; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = SharesValues.ZERO; + _accountingContext.stETHTotals.claimedETH = totalClaimedETH; vm.expectRevert(stdError.divisionError); - AssetsAccounting.accountStETHSharesWithdraw(_accountingState, holder); + AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); } function testFuzz_accountStETHSharesWithdraw_AccountingError_WithdrawAmountMoreThanTotalClaimedETH( @@ -289,9 +293,9 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.stETHTotals.claimedETH = totalClaimedETH; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.stETHTotals.claimedETH = totalClaimedETH; ETHValue expectedETHWithdrawn = ETHValues.from((uint256(totalClaimedETHAmount) * holderLockedSharesAmount) / totalLockedSharesAmount); @@ -299,11 +303,11 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.ETHWithdrawn(holder, holderLockedShares, expectedETHWithdrawn); - ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingState, holder); + ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); assert(ethWithdrawn == expectedETHWithdrawn); assert(ethWithdrawn.toUint256() >= totalClaimedETHAmount); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); } function testFuzz_accountStETHSharesWithdraw_RevertOn_AccountingError_WithdrawAmountOverflow(address holder) @@ -313,13 +317,13 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue totalLockedShares = SharesValues.from(1); ETHValue totalClaimedETH = ETHValues.from(type(uint96).max); - _accountingState.assets[holder].stETHLockedShares = holderLockedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; - _accountingState.stETHTotals.claimedETH = totalClaimedETH; + _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.stETHTotals.claimedETH = totalClaimedETH; vm.expectRevert(ETHValueOverflow.selector); - AssetsAccounting.accountStETHSharesWithdraw(_accountingState, holder); + AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); } // --- @@ -333,14 +337,14 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue amount = ETHValues.from(ethAmount); ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); - _accountingState.stETHTotals.claimedETH = totalClaimedETH; + _accountingContext.stETHTotals.claimedETH = totalClaimedETH; vm.expectEmit(); emit AssetsAccounting.ETHClaimed(amount); - AssetsAccounting.accountClaimedStETH(_accountingState, amount); + AssetsAccounting.accountClaimedStETH(_accountingContext, amount); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, totalClaimedETH + amount, SharesValues.ZERO, ETHValues.ZERO ); } @@ -363,9 +367,9 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalUnstETHLockedAmount = 0; - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.assets[holder].unstETHIds.push(genRandomUnstEthId(1024)); - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.assets[holder].unstETHIds.push(genRandomUnstEthId(1024)); + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](amountsOfShares.length); @@ -376,33 +380,33 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[i].amountOfShares = amountsOfShares[i]; withdrawalRequestStatuses[i].isFinalized = false; withdrawalRequestStatuses[i].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; expectedTotalUnstETHLockedAmount += amountsOfShares[i]; } vm.expectEmit(); emit AssetsAccounting.UnstETHLocked(holder, unstETHIds, SharesValues.from(expectedTotalUnstETHLockedAmount)); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares + SharesValues.from(expectedTotalUnstETHLockedAmount), ETHValues.ZERO ); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); assert( - _accountingState.assets[holder].unstETHLockedShares + _accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares + SharesValues.from(expectedTotalUnstETHLockedAmount) ); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingState.assets[holder].unstETHIds.length == amountsOfShares.length + 1); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); + assert(_accountingContext.assets[holder].unstETHIds.length == amountsOfShares.length + 1); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingState.unstETHRecords[unstETHIds[i]].lockedBy == holder); - assert(_accountingState.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Locked); - assert(_accountingState.unstETHRecords[unstETHIds[i]].index.toZeroBasedValue() == i + 1); - assert(_accountingState.unstETHRecords[unstETHIds[i]].shares == SharesValues.from(amountsOfShares[i])); - assert(_accountingState.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.ZERO); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy == holder); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Locked); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].index.toZeroBasedValue() == i + 1); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.from(amountsOfShares[i])); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.ZERO); } } @@ -414,7 +418,7 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectRevert(); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_RevertOn_WithdrawalRequestStatusIsFinalized( @@ -429,8 +433,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](amountsOfShares.length); @@ -441,7 +445,7 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[i].amountOfShares = amountsOfShares[i]; withdrawalRequestStatuses[i].isFinalized = false; withdrawalRequestStatuses[i].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; } withdrawalRequestStatuses[withdrawalRequestStatuses.length - 1].isFinalized = true; @@ -454,7 +458,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_RevertOn_WithdrawalRequestStatusIsClaimed( @@ -469,8 +473,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](amountsOfShares.length); @@ -481,14 +485,14 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[i].amountOfShares = amountsOfShares[i]; withdrawalRequestStatuses[i].isFinalized = false; withdrawalRequestStatuses[i].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; } withdrawalRequestStatuses[withdrawalRequestStatuses.length - 1].isClaimed = true; vm.expectRevert(); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_RevertOn_UnstETHRecordStatusIsNot_NotLocked( @@ -503,8 +507,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](amountsOfShares.length); @@ -515,10 +519,10 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[i].amountOfShares = amountsOfShares[i]; withdrawalRequestStatuses[i].isFinalized = false; withdrawalRequestStatuses[i].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; } - _accountingState.unstETHRecords[unstETHIds[unstETHIds.length - 1]].status = UnstETHRecordStatus.Withdrawn; + _accountingContext.unstETHRecords[unstETHIds[unstETHIds.length - 1]].status = UnstETHRecordStatus.Withdrawn; vm.expectRevert( abi.encodeWithSelector( @@ -528,7 +532,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_RevertWhen_DuplicatingUnstETHIdsProvided( @@ -543,8 +547,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](amountsOfShares.length); @@ -555,7 +559,7 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[i].amountOfShares = amountsOfShares[i]; withdrawalRequestStatuses[i].isFinalized = false; withdrawalRequestStatuses[i].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.NotLocked; } unstETHIds[unstETHIds.length - 1] = unstETHIds[unstETHIds.length - 2]; @@ -568,7 +572,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } // TODO: is it expected behavior? @@ -580,23 +584,23 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](0); uint256[] memory unstETHIds = new uint256[](0); vm.expectEmit(); emit AssetsAccounting.UnstETHLocked(holder, unstETHIds, SharesValues.ZERO); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, ETHValues.ZERO ); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountUnstETHLock_AccountingError_WithdrawalRequestStatusAmountOfSharesOverflow( @@ -607,8 +611,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](1); uint256[] memory unstETHIds = new uint256[](1); @@ -617,11 +621,11 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[0].amountOfShares = uint256(type(uint128).max) + 1; withdrawalRequestStatuses[0].isFinalized = false; withdrawalRequestStatuses[0].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; vm.expectRevert(SharesValueOverflow.selector); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_AccountingError_HolderUnstETHLockedSharesOverflow( @@ -631,8 +635,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(type(uint128).max / 2 + 1); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](1); uint256[] memory unstETHIds = new uint256[](1); @@ -641,11 +645,11 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[0].amountOfShares = uint128(type(uint128).max / 2) + 1; withdrawalRequestStatuses[0].isFinalized = false; withdrawalRequestStatuses[0].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } function testFuzz_accountUnstETHLock_AccountingError_TotalUnfinalizedSharesOverflow( @@ -655,8 +659,8 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(type(uint128).max / 2 + 1); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; WithdrawalRequestStatus[] memory withdrawalRequestStatuses = new WithdrawalRequestStatus[](1); uint256[] memory unstETHIds = new uint256[](1); @@ -665,11 +669,11 @@ contract AssetsAccountingUnitTests is UnitTest { withdrawalRequestStatuses[0].amountOfShares = uint128(type(uint128).max / 2) + 1; withdrawalRequestStatuses[0].isFinalized = false; withdrawalRequestStatuses[0].isClaimed = false; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHLock(_accountingState, holder, unstETHIds, withdrawalRequestStatuses); + AssetsAccounting.accountUnstETHLock(_accountingContext, holder, unstETHIds, withdrawalRequestStatuses); } // --- @@ -694,19 +698,19 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalSharesUnlockedAmount = 0; - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](amountsOfShares.length); for (uint256 i = 0; i < amountsOfShares.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(amountsOfShares[i]); - _accountingState.unstETHRecords[unstETHIds[i]].index = IndicesOneBased.fromOneBasedValue(i + 1); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(amountsOfShares[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].index = IndicesOneBased.fromOneBasedValue(i + 1); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[i]); expectedTotalSharesUnlockedAmount += amountsOfShares[i]; } @@ -715,23 +719,23 @@ contract AssetsAccountingUnitTests is UnitTest { holder, unstETHIds, SharesValues.from(expectedTotalSharesUnlockedAmount), ETHValues.ZERO ); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares - SharesValues.from(expectedTotalSharesUnlockedAmount), initialTotalFinalizedETH ); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); assert( - _accountingState.assets[holder].unstETHLockedShares + _accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingState.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); } } @@ -753,20 +757,20 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalSharesUnlockedAmount = 0; - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](amountsOfShares.length); for (uint256 i = 0; i < amountsOfShares.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; - _accountingState.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(amountsOfShares[i]); - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(amountsOfShares[i]); - _accountingState.unstETHRecords[unstETHIds[i]].index = IndicesOneBased.fromOneBasedValue(i + 1); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; + _accountingContext.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(amountsOfShares[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(amountsOfShares[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].index = IndicesOneBased.fromOneBasedValue(i + 1); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[i]); expectedTotalSharesUnlockedAmount += amountsOfShares[i]; } @@ -778,23 +782,23 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValues.from(expectedTotalSharesUnlockedAmount) ); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH - ETHValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); assert( - _accountingState.assets[holder].unstETHLockedShares + _accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingState.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); } } @@ -808,7 +812,7 @@ contract AssetsAccountingUnitTests is UnitTest { abi.encodeWithSelector(AssetsAccounting.InvalidUnstETHHolder.selector, unstETHIds[0], holder, address(0x0)) ); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } function testFuzz_accountUnstETHUnlock_RevertWhen_UnstETHRecordDoesNotBelongToCurrent( @@ -819,20 +823,20 @@ contract AssetsAccountingUnitTests is UnitTest { uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; vm.expectRevert( abi.encodeWithSelector(AssetsAccounting.InvalidUnstETHHolder.selector, unstETHIds[0], current, holder) ); - AssetsAccounting.accountUnstETHUnlock(_accountingState, current, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, current, unstETHIds); } function testFuzz_accountUnstETHUnlock_RevertWhen_UnstETHRecordStatusInvalid(address holder) external { uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.NotLocked; vm.expectRevert( abi.encodeWithSelector( @@ -840,21 +844,21 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } function testFuzz_accountUnstETHUnlock_RevertWhen_UnstETHRecordIndexInvalid_OOB(address holder) external { uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(1234); - _accountingState.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(10); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[0]); + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(1234); + _accountingContext.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(10); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[0]); vm.expectRevert(stdError.indexOOBError); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } // TODO: is it expected behavior? @@ -871,81 +875,81 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](0); vm.expectEmit(); emit AssetsAccounting.UnstETHUnlocked(holder, unstETHIds, SharesValues.ZERO, ETHValues.ZERO); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH ); - assert(_accountingState.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingState.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); - assert(_accountingState.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingState.assets[holder].unstETHIds.length == 0); + assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); + assert(_accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); + assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); + assert(_accountingContext.assets[holder].unstETHIds.length == 0); } function testFuzz_accountUnstETHUnlock_RevertOn_AccountingError_HolderUnstETHLockedSharesUnderflow(address holder) external { - _accountingState.assets[holder].unstETHLockedShares = SharesValues.from(5); + _accountingContext.assets[holder].unstETHLockedShares = SharesValues.from(5); uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(10); - _accountingState.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[0]); + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(10); + _accountingContext.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[0]); vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } function testFuzz_accountUnstETHUnlock_RevertOn_AccountingError_TotalFinalizedETHUnderflow(address holder) external { - _accountingState.assets[holder].unstETHLockedShares = SharesValues.from(10); - _accountingState.unstETHTotals.finalizedETH = ETHValues.from(5); + _accountingContext.assets[holder].unstETHLockedShares = SharesValues.from(10); + _accountingContext.unstETHTotals.finalizedETH = ETHValues.from(5); uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Finalized; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(5); - _accountingState.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); - _accountingState.unstETHRecords[unstETHIds[0]].claimableAmount = ETHValues.from(10); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[0]); + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Finalized; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(5); + _accountingContext.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); + _accountingContext.unstETHRecords[unstETHIds[0]].claimableAmount = ETHValues.from(10); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[0]); vm.expectRevert(ETHValueUnderflow.selector); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } function testFuzz_accountUnstETHUnlock_RevertOn_AccountingError_TotalUnfinalizedSharesUnderflow(address holder) external { - _accountingState.assets[holder].unstETHLockedShares = SharesValues.from(10); - _accountingState.unstETHTotals.unfinalizedShares = SharesValues.from(5); + _accountingContext.assets[holder].unstETHLockedShares = SharesValues.from(10); + _accountingContext.unstETHTotals.unfinalizedShares = SharesValues.from(5); uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1234); - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(10); - _accountingState.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); - _accountingState.assets[holder].unstETHIds.push(unstETHIds[0]); + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(10); + _accountingContext.unstETHRecords[unstETHIds[0]].index = IndicesOneBased.fromOneBasedValue(1); + _accountingContext.assets[holder].unstETHIds.push(unstETHIds[0]); vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHUnlock(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHUnlock(_accountingContext, holder, unstETHIds); } // --- @@ -967,17 +971,17 @@ contract AssetsAccountingUnitTests is UnitTest { uint256 expectedTotalSharesFinalized = 0; uint256 expectedTotalAmountFinalized = 0; - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](claimableAmounts.length); uint256[] memory claimableAmountsPrepared = new uint256[](claimableAmounts.length); for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; uint256 sharesAmount = 5 * uint256(claimableAmounts[i]); - _accountingState.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(sharesAmount); + _accountingContext.unstETHRecords[unstETHIds[i]].shares = SharesValues.from(sharesAmount); expectedTotalSharesFinalized += sharesAmount; expectedTotalAmountFinalized += claimableAmounts[i]; claimableAmountsPrepared[i] = claimableAmounts[i]; @@ -988,9 +992,9 @@ contract AssetsAccountingUnitTests is UnitTest { unstETHIds, SharesValues.from(expectedTotalSharesFinalized), ETHValues.from(expectedTotalAmountFinalized) ); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares - SharesValues.from(expectedTotalSharesFinalized), @@ -1009,7 +1013,7 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectRevert(stdError.assertionError); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); } function testFuzz_accountUnstETHFinalized_When_NoClaimableAmountsProvided( @@ -1021,8 +1025,8 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](0); uint256[] memory claimableAmountsPrepared = new uint256[](0); @@ -1030,9 +1034,9 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHFinalized(unstETHIds, SharesValues.from(0), ETHValues.from(0)); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH ); } @@ -1047,8 +1051,8 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](1); uint256[] memory claimableAmountsPrepared = new uint256[](1); @@ -1059,9 +1063,9 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHFinalized(unstETHIds, SharesValues.from(0), ETHValues.from(0)); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH ); } @@ -1075,23 +1079,23 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](1); uint256[] memory claimableAmountsPrepared = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(9876); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); claimableAmountsPrepared[0] = 0; vm.expectEmit(); emit AssetsAccounting.UnstETHFinalized(unstETHIds, SharesValues.from(0), ETHValues.from(0)); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters( + checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH ); } @@ -1105,20 +1109,20 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](1); uint256[] memory claimableAmountsPrepared = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(9876); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); claimableAmountsPrepared[0] = uint256(type(uint128).max) + 1; vm.expectRevert(ETHValueOverflow.selector); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); } function testFuzz_accountUnstETHFinalized_RevertOn_TotalFinalizedETHOverflow( @@ -1130,20 +1134,20 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](1); uint256[] memory claimableAmountsPrepared = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(9876); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(123); claimableAmountsPrepared[0] = uint256(type(uint128).max - 2); vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); } function testFuzz_accountUnstETHFinalized_RevertOn_TotalUnfinalizedSharesUnderflow( @@ -1155,20 +1159,20 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = initialTotalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; + _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; uint256[] memory unstETHIds = new uint256[](1); uint256[] memory claimableAmountsPrepared = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(9876); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; - _accountingState.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(type(uint64).max); + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].shares = SharesValues.from(type(uint64).max); claimableAmountsPrepared[0] = 1; vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHFinalized(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHFinalized(_accountingContext, unstETHIds, claimableAmountsPrepared); } // --- @@ -1187,7 +1191,7 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; expectedTotalAmountClaimed += claimableAmounts[i]; claimableAmountsPrepared[i] = claimableAmounts[i]; } @@ -1195,14 +1199,14 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHClaimed(unstETHIds, ETHValues.from(expectedTotalAmountClaimed)); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); + checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { assert( - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) ); - assert(_accountingState.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); } } @@ -1218,13 +1222,13 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Locked; claimableAmountsPrepared[i] = claimableAmounts[i]; } vm.expectRevert(stdError.indexOOBError); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); } function test_accountUnstETHClaimed_WhenNoUnstETHIdsProvided() external { @@ -1236,9 +1240,9 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHClaimed(unstETHIds, ETHValues.from(expectedTotalAmountClaimed)); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); + checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); } function testFuzz_accountUnstETHClaimed_RevertWhen_UnstETHRecordNotFoundOrHasWrongStatus( @@ -1261,7 +1265,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); } function testFuzz_accountUnstETHClaimed_RevertWhen_UnstETHRecordIsFinalizedAndClaimableAmountIsIncorrect( @@ -1275,8 +1279,8 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount = + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(uint256(claimableAmounts[i]) + 1); claimableAmountsPrepared[i] = claimableAmounts[i]; } @@ -1290,7 +1294,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); } function testFuzz_accountUnstETHClaimed_When_UnstETHRecordIsFinalizedAndClaimableAmountIsCorrect( @@ -1306,8 +1310,8 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(claimableAmounts[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Finalized; + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(claimableAmounts[i]); claimableAmountsPrepared[i] = claimableAmounts[i]; expectedTotalAmountClaimed += claimableAmounts[i]; } @@ -1315,14 +1319,14 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHClaimed(unstETHIds, ETHValues.from(expectedTotalAmountClaimed)); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); - checkAccountingStateTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); + checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { assert( - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) ); - assert(_accountingState.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); } } @@ -1331,12 +1335,12 @@ contract AssetsAccountingUnitTests is UnitTest { uint256[] memory claimableAmountsPrepared = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(1); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Locked; claimableAmountsPrepared[0] = uint256(type(uint128).max) + 1; vm.expectRevert(ETHValueOverflow.selector); - AssetsAccounting.accountUnstETHClaimed(_accountingState, unstETHIds, claimableAmountsPrepared); + AssetsAccounting.accountUnstETHClaimed(_accountingContext, unstETHIds, claimableAmountsPrepared); } // --- @@ -1354,26 +1358,26 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < claimableAmounts.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Claimed; - _accountingState.unstETHRecords[unstETHIds[i]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(claimableAmounts[i]); + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Claimed; + _accountingContext.unstETHRecords[unstETHIds[i]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(claimableAmounts[i]); expectedAmountWithdrawn += claimableAmounts[i]; } vm.expectEmit(); emit AssetsAccounting.UnstETHWithdrawn(unstETHIds, ETHValues.from(expectedAmountWithdrawn)); - ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingState, holder, unstETHIds); + ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); assert(amountWithdrawn == ETHValues.from(expectedAmountWithdrawn)); - checkAccountingStateTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); + checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { assert( - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) ); - assert(_accountingState.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Withdrawn); - assert(_accountingState.unstETHRecords[unstETHIds[i]].lockedBy == holder); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Withdrawn); + assert(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy == holder); } } @@ -1383,7 +1387,7 @@ contract AssetsAccountingUnitTests is UnitTest { vm.expectEmit(); emit AssetsAccounting.UnstETHWithdrawn(unstETHIds, ETHValues.ZERO); - ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingState, holder, unstETHIds); + ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); assert(amountWithdrawn == ETHValues.ZERO); } @@ -1407,7 +1411,7 @@ contract AssetsAccountingUnitTests is UnitTest { ) ); - AssetsAccounting.accountUnstETHWithdraw(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); } function testFuzz_accountUnstETHWithdraw_RevertWhen_UnstETHRecordDoesNotBelongToCurrent( @@ -1419,15 +1423,15 @@ contract AssetsAccountingUnitTests is UnitTest { uint256[] memory unstETHIds = new uint256[](1); unstETHIds[0] = genRandomUnstEthId(567); - _accountingState.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Claimed; - _accountingState.unstETHRecords[unstETHIds[0]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[0]].claimableAmount = ETHValues.from(123); + _accountingContext.unstETHRecords[unstETHIds[0]].status = UnstETHRecordStatus.Claimed; + _accountingContext.unstETHRecords[unstETHIds[0]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[0]].claimableAmount = ETHValues.from(123); vm.expectRevert( abi.encodeWithSelector(AssetsAccounting.InvalidUnstETHHolder.selector, unstETHIds[0], current, holder) ); - AssetsAccounting.accountUnstETHWithdraw(_accountingState, current, unstETHIds); + AssetsAccounting.accountUnstETHWithdraw(_accountingContext, current, unstETHIds); } function testFuzz_accountUnstETHWithdraw_RevertOn_WithdrawnAmountOverflow(address holder) external { @@ -1435,15 +1439,15 @@ contract AssetsAccountingUnitTests is UnitTest { for (uint256 i = 0; i < unstETHIds.length; ++i) { unstETHIds[i] = genRandomUnstEthId(i); - _accountingState.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Claimed; - _accountingState.unstETHRecords[unstETHIds[i]].lockedBy = holder; - _accountingState.unstETHRecords[unstETHIds[i]].claimableAmount = + _accountingContext.unstETHRecords[unstETHIds[i]].status = UnstETHRecordStatus.Claimed; + _accountingContext.unstETHRecords[unstETHIds[i]].lockedBy = holder; + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount = ETHValues.from(uint256(type(uint128).max) / 2 + 1); } vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.accountUnstETHWithdraw(_accountingState, holder, unstETHIds); + AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); } // --- @@ -1459,12 +1463,12 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue totalUnfinalizedShares = SharesValues.from(totalUnfinalizedSharesAmount); SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); - _accountingState.unstETHTotals.finalizedETH = totalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = totalUnfinalizedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.unstETHTotals.finalizedETH = totalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = totalUnfinalizedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; (SharesValue unfinalizedShares, ETHValue finalizedETH) = - AssetsAccounting.getLockedAssetsTotals(_accountingState); + AssetsAccounting.getLockedAssetsTotals(_accountingContext); assert(unfinalizedShares == totalLockedShares + totalUnfinalizedShares); assert(finalizedETH == totalFinalizedETH); @@ -1475,12 +1479,12 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue totalUnfinalizedShares = SharesValues.from(type(uint128).max - 1); SharesValue totalLockedShares = SharesValues.from(type(uint128).max - 1); - _accountingState.unstETHTotals.finalizedETH = totalFinalizedETH; - _accountingState.unstETHTotals.unfinalizedShares = totalUnfinalizedShares; - _accountingState.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.unstETHTotals.finalizedETH = totalFinalizedETH; + _accountingContext.unstETHTotals.unfinalizedShares = totalUnfinalizedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedShares; vm.expectRevert(stdError.arithmeticError); - AssetsAccounting.getLockedAssetsTotals(_accountingState); + AssetsAccounting.getLockedAssetsTotals(_accountingContext); } // --- @@ -1489,22 +1493,22 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_checkMinAssetsLockDurationPassed_happyPath(address holder) external { Duration minAssetsLockDuration = Durations.from(0); - _accountingState.assets[holder].lastAssetsLockTimestamp = Timestamps.from(Timestamps.now().toSeconds() - 1); + _accountingContext.assets[holder].lastAssetsLockTimestamp = Timestamps.from(Timestamps.now().toSeconds() - 1); - AssetsAccounting.checkMinAssetsLockDurationPassed(_accountingState, holder, minAssetsLockDuration); + AssetsAccounting.checkMinAssetsLockDurationPassed(_accountingContext, holder, minAssetsLockDuration); } function testFuzz_checkMinAssetsLockDurationPassed_RevertOn_MinAssetsLockDurationNotPassed(address holder) external { Duration minAssetsLockDuration = Durations.from(1); - _accountingState.assets[holder].lastAssetsLockTimestamp = Timestamps.from(Timestamps.now().toSeconds() - 1); + _accountingContext.assets[holder].lastAssetsLockTimestamp = Timestamps.from(Timestamps.now().toSeconds() - 1); vm.expectRevert( abi.encodeWithSelector(AssetsAccounting.MinAssetsLockDurationNotPassed.selector, Timestamps.now()) ); - AssetsAccounting.checkMinAssetsLockDurationPassed(_accountingState, holder, minAssetsLockDuration); + AssetsAccounting.checkMinAssetsLockDurationPassed(_accountingContext, holder, minAssetsLockDuration); } // --- @@ -1515,15 +1519,15 @@ contract AssetsAccountingUnitTests is UnitTest { return uint256(keccak256(abi.encodePacked(block.timestamp, salt))); // random id } - function checkAccountingStateTotalCounters( + function checkAccountingContextTotalCounters( SharesValue lockedShares, ETHValue claimedETH, SharesValue unfinalizedShares, ETHValue finalizedETH ) internal view { - assert(_accountingState.stETHTotals.lockedShares == lockedShares); - assert(_accountingState.stETHTotals.claimedETH == claimedETH); - assert(_accountingState.unstETHTotals.unfinalizedShares == unfinalizedShares); - assert(_accountingState.unstETHTotals.finalizedETH == finalizedETH); + assert(_accountingContext.stETHTotals.lockedShares == lockedShares); + assert(_accountingContext.stETHTotals.claimedETH == claimedETH); + assert(_accountingContext.unstETHTotals.unfinalizedShares == unfinalizedShares); + assert(_accountingContext.unstETHTotals.finalizedETH == finalizedETH); } } From d5e6b02ebad0d1d82f6d886f7fbe1fc8773ce617 Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:39:57 +0400 Subject: [PATCH 3/8] Use Duration/Timestamp instead of primitive type as a fuzzing parameter --- test/unit/libraries/EscrowState.t.sol | 116 ++++++++++++-------------- 1 file changed, 52 insertions(+), 64 deletions(-) diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol index d5740784..dbd4e12c 100644 --- a/test/unit/libraries/EscrowState.t.sol +++ b/test/unit/libraries/EscrowState.t.sol @@ -19,8 +19,7 @@ contract EscrowStateUnitTests is UnitTest { // initialize() // --- - function testFuzz_initialize_happyPath(uint32 minAssetsLockSeconds) external { - Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); + function testFuzz_initialize_happyPath(Duration minAssetsLockDuration) external { _context.state = State.NotInitialized; vm.expectEmit(); @@ -32,13 +31,13 @@ contract EscrowStateUnitTests is UnitTest { checkContext(State.SignallingEscrow, minAssetsLockDuration, D0, D0, T0); } - function testFuzz_initialize_RevertOn_InvalidState(uint32 minAssetsLockSeconds) external { + function testFuzz_initialize_RevertOn_InvalidState(Duration minAssetsLockDuration) external { _context.state = State.SignallingEscrow; - // TODO: not very informative, maybe need to change to `revert UnexpectedState(self.state);` + // TODO: not very informative, maybe need to change to `revert UnexpectedState(self.state);`: UnexpectedState(NotInitialized)[current implementation] => UnexpectedState(SignallingEscrow)[proposed] vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.NotInitialized)); - EscrowState.initialize(_context, Durations.from(minAssetsLockSeconds)); + EscrowState.initialize(_context, minAssetsLockDuration); } // --- @@ -46,12 +45,9 @@ contract EscrowStateUnitTests is UnitTest { // --- function testFuzz_startRageQuit_happyPath( - uint32 rageQuitExtensionDelaySeconds, - uint32 rageQuitWithdrawalsTimelockSeconds + Duration rageQuitExtensionDelay, + Duration rageQuitWithdrawalsTimelock ) external { - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); - Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); - _context.state = State.SignallingEscrow; vm.expectEmit(); @@ -64,12 +60,9 @@ contract EscrowStateUnitTests is UnitTest { } function testFuzz_startRageQuit_RevertOn_InvalidState( - uint32 rageQuitExtensionDelaySeconds, - uint32 rageQuitWithdrawalsTimelockSeconds + Duration rageQuitExtensionDelay, + Duration rageQuitWithdrawalsTimelock ) external { - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); - Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); - _context.state = State.NotInitialized; vm.expectRevert(abi.encodeWithSelector(EscrowState.UnexpectedState.selector, State.SignallingEscrow)); @@ -94,9 +87,8 @@ contract EscrowStateUnitTests is UnitTest { // setMinAssetsLockDuration() // --- - function test_setMinAssetsLockDuration_happyPath(uint32 minAssetsLockSeconds) external { - vm.assume(minAssetsLockSeconds != 0); - Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); + function test_setMinAssetsLockDuration_happyPath(Duration minAssetsLockDuration) external { + vm.assume(minAssetsLockDuration != Durations.ZERO); vm.expectEmit(); emit EscrowState.MinAssetsLockDurationSet(minAssetsLockDuration); @@ -106,9 +98,7 @@ contract EscrowStateUnitTests is UnitTest { checkContext(State.NotInitialized, minAssetsLockDuration, D0, D0, T0); } - function test_setMinAssetsLockDuration_WhenDurationNotChanged(uint32 minAssetsLockSeconds) external { - Duration minAssetsLockDuration = Durations.from(minAssetsLockSeconds); - + function test_setMinAssetsLockDuration_WhenDurationNotChanged(Duration minAssetsLockDuration) external { _context.minAssetsLockDuration = minAssetsLockDuration; Vm.Log[] memory entries = vm.getRecordedLogs(); @@ -158,11 +148,11 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.checkBatchesClaimingInProgress(_context); } - function testFuzz_checkBatchesClaimingInProgress_RevertOn_InvalidState( - uint40 rageQuitExtensionDelayStartedAtSeconds - ) external { - vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + function testFuzz_checkBatchesClaimingInProgress_RevertOn_InvalidState(Timestamp rageQuitExtensionDelayStartedAt) + external + { + vm.assume(rageQuitExtensionDelayStartedAt > Timestamps.ZERO); + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; vm.expectRevert(EscrowState.ClaimingIsFinished.selector); EscrowState.checkBatchesClaimingInProgress(_context); @@ -173,22 +163,22 @@ contract EscrowStateUnitTests is UnitTest { // --- function testFuzz_checkWithdrawalsTimelockPassed_happyPath( - uint16 rageQuitExtensionDelayStartedAtSeconds, - uint16 rageQuitExtensionDelaySeconds, - uint16 rageQuitWithdrawalsTimelockSeconds + Timestamp rageQuitExtensionDelayStartedAt, + Duration rageQuitExtensionDelay, + Duration rageQuitWithdrawalsTimelock ) external { - vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); - - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); - Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + vm.assume(rageQuitExtensionDelayStartedAt > Timestamps.ZERO); + vm.assume(rageQuitExtensionDelayStartedAt < Timestamps.from(type(uint16).max)); + vm.assume(rageQuitExtensionDelay < Durations.from(type(uint16).max)); + vm.assume(rageQuitWithdrawalsTimelock < Durations.from(type(uint16).max)); - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; vm.warp( - uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) - + uint256(rageQuitWithdrawalsTimelockSeconds) + 1 + rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() + + rageQuitWithdrawalsTimelock.toSeconds() + 1 ); EscrowState.checkWithdrawalsTimelockPassed(_context); } @@ -200,22 +190,22 @@ contract EscrowStateUnitTests is UnitTest { } function testFuzz_checkWithdrawalsTimelockPassed_RevertWhen_WithdrawalsTimelockNotPassed( - uint16 rageQuitExtensionDelayStartedAtSeconds, - uint16 rageQuitExtensionDelaySeconds, - uint16 rageQuitWithdrawalsTimelockSeconds + Timestamp rageQuitExtensionDelayStartedAt, + Duration rageQuitExtensionDelay, + Duration rageQuitWithdrawalsTimelock ) external { - vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); - - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); - Duration rageQuitWithdrawalsTimelock = Durations.from(rageQuitWithdrawalsTimelockSeconds); + vm.assume(rageQuitExtensionDelayStartedAt > Timestamps.ZERO); + vm.assume(rageQuitExtensionDelayStartedAt < Timestamps.from(type(uint16).max)); + vm.assume(rageQuitExtensionDelay < Durations.from(type(uint16).max)); + vm.assume(rageQuitWithdrawalsTimelock < Durations.from(type(uint16).max)); - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; vm.warp( - uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) - + uint256(rageQuitWithdrawalsTimelockSeconds) + rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() + + rageQuitWithdrawalsTimelock.toSeconds() ); vm.expectRevert(EscrowState.WithdrawalsTimelockNotPassed.selector); @@ -240,10 +230,8 @@ contract EscrowStateUnitTests is UnitTest { // isRageQuitExtensionDelayStarted() // --- - function testFuzz_isRageQuitExtensionDelayStarted_happyPath(uint40 rageQuitExtensionDelayStartedAtSeconds) - external - { - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + function testFuzz_isRageQuitExtensionDelayStarted_happyPath(Timestamp rageQuitExtensionDelayStartedAt) external { + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; bool res = EscrowState.isRageQuitExtensionDelayStarted(_context); assert(res == _context.rageQuitExtensionDelayStartedAt.isNotZero()); } @@ -253,33 +241,33 @@ contract EscrowStateUnitTests is UnitTest { // --- function testFuzz_isRageQuitExtensionDelayPassed_ReturnsTrue( - uint16 rageQuitExtensionDelayStartedAtSeconds, - uint16 rageQuitExtensionDelaySeconds + Timestamp rageQuitExtensionDelayStartedAt, + Duration rageQuitExtensionDelay ) external { - vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); + vm.assume(rageQuitExtensionDelayStartedAt > Timestamps.ZERO); + vm.assume(rageQuitExtensionDelayStartedAt < Timestamps.from(type(uint16).max)); + vm.assume(rageQuitExtensionDelay < Durations.from(type(uint16).max)); - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); - - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; - vm.warp(uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds) + 1); + vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() + 1); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); assert(res == true); } function testFuzz_isRageQuitExtensionDelayPassed_ReturnsFalse( - uint16 rageQuitExtensionDelayStartedAtSeconds, - uint16 rageQuitExtensionDelaySeconds + Timestamp rageQuitExtensionDelayStartedAt, + Duration rageQuitExtensionDelay ) external { - vm.assume(rageQuitExtensionDelayStartedAtSeconds > 0); - - Duration rageQuitExtensionDelay = Durations.from(rageQuitExtensionDelaySeconds); + vm.assume(rageQuitExtensionDelayStartedAt > Timestamps.ZERO); + vm.assume(rageQuitExtensionDelayStartedAt < Timestamps.from(type(uint16).max)); + vm.assume(rageQuitExtensionDelay < Durations.from(type(uint16).max)); - _context.rageQuitExtensionDelayStartedAt = Timestamps.from(rageQuitExtensionDelayStartedAtSeconds); + _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; - vm.warp(uint256(rageQuitExtensionDelayStartedAtSeconds) + uint256(rageQuitExtensionDelaySeconds)); + vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds()); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); assert(res == false); } From a06b67aa37f28daf7d00985d79b979d453aedf38 Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:50:32 +0400 Subject: [PATCH 4/8] Pass named parameters to checkContext() --- test/unit/libraries/EscrowState.t.sol | 40 +++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol index dbd4e12c..f54b9c54 100644 --- a/test/unit/libraries/EscrowState.t.sol +++ b/test/unit/libraries/EscrowState.t.sol @@ -28,7 +28,13 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.initialize(_context, minAssetsLockDuration); - checkContext(State.SignallingEscrow, minAssetsLockDuration, D0, D0, T0); + checkContext({ + state: State.SignallingEscrow, + minAssetsLockDuration: minAssetsLockDuration, + rageQuitExtensionDelay: D0, + rageQuitWithdrawalsTimelock: D0, + rageQuitExtensionDelayStartedAt: T0 + }); } function testFuzz_initialize_RevertOn_InvalidState(Duration minAssetsLockDuration) external { @@ -56,7 +62,13 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.startRageQuit(_context, rageQuitExtensionDelay, rageQuitWithdrawalsTimelock); - checkContext(State.RageQuitEscrow, D0, rageQuitExtensionDelay, rageQuitWithdrawalsTimelock, T0); + checkContext({ + state: State.RageQuitEscrow, + minAssetsLockDuration: D0, + rageQuitExtensionDelay: rageQuitExtensionDelay, + rageQuitWithdrawalsTimelock: rageQuitWithdrawalsTimelock, + rageQuitExtensionDelayStartedAt: T0 + }); } function testFuzz_startRageQuit_RevertOn_InvalidState( @@ -80,7 +92,13 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.startRageQuitExtensionDelay(_context); - checkContext(State.NotInitialized, D0, D0, D0, Timestamps.now()); + checkContext({ + state: State.NotInitialized, + minAssetsLockDuration: D0, + rageQuitExtensionDelay: D0, + rageQuitWithdrawalsTimelock: D0, + rageQuitExtensionDelayStartedAt: Timestamps.now() + }); } // --- @@ -95,7 +113,13 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.setMinAssetsLockDuration(_context, minAssetsLockDuration); - checkContext(State.NotInitialized, minAssetsLockDuration, D0, D0, T0); + checkContext({ + state: State.NotInitialized, + minAssetsLockDuration: minAssetsLockDuration, + rageQuitExtensionDelay: D0, + rageQuitWithdrawalsTimelock: D0, + rageQuitExtensionDelayStartedAt: T0 + }); } function test_setMinAssetsLockDuration_WhenDurationNotChanged(Duration minAssetsLockDuration) external { @@ -105,7 +129,13 @@ contract EscrowStateUnitTests is UnitTest { EscrowState.setMinAssetsLockDuration(_context, minAssetsLockDuration); - checkContext(State.NotInitialized, minAssetsLockDuration, D0, D0, T0); + checkContext({ + state: State.NotInitialized, + minAssetsLockDuration: minAssetsLockDuration, + rageQuitExtensionDelay: D0, + rageQuitWithdrawalsTimelock: D0, + rageQuitExtensionDelayStartedAt: T0 + }); assertEq(entries.length, 0); } From b1103f101406a0c9f7f0c742c1b78ac7d50ce0ee Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:08:28 +0400 Subject: [PATCH 5/8] Use assertEq() instead of regular assert() --- test/unit/libraries/EscrowState.t.sol | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol index f54b9c54..74b452cc 100644 --- a/test/unit/libraries/EscrowState.t.sol +++ b/test/unit/libraries/EscrowState.t.sol @@ -263,7 +263,7 @@ contract EscrowStateUnitTests is UnitTest { function testFuzz_isRageQuitExtensionDelayStarted_happyPath(Timestamp rageQuitExtensionDelayStartedAt) external { _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; bool res = EscrowState.isRageQuitExtensionDelayStarted(_context); - assert(res == _context.rageQuitExtensionDelayStartedAt.isNotZero()); + assertEq(res, _context.rageQuitExtensionDelayStartedAt.isNotZero()); } // --- @@ -283,7 +283,7 @@ contract EscrowStateUnitTests is UnitTest { vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() + 1); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); - assert(res == true); + assertTrue(res); } function testFuzz_isRageQuitExtensionDelayPassed_ReturnsFalse( @@ -299,13 +299,13 @@ contract EscrowStateUnitTests is UnitTest { vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds()); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); - assert(res == false); + assertFalse(res); } function test_isRageQuitExtensionDelayPassed_ReturnsFalseWhenRageQuitExtraTimelockNotStarted() external { vm.warp(1234); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); - assert(res == false); + assertFalse(res); } // --- @@ -317,7 +317,7 @@ contract EscrowStateUnitTests is UnitTest { _context.state = State.RageQuitEscrow; } bool actualResult = EscrowState.isRageQuitEscrow(_context); - assert(actualResult == expectedResult); + assertEq(actualResult, expectedResult); } // --- @@ -330,11 +330,15 @@ contract EscrowStateUnitTests is UnitTest { Duration rageQuitExtensionDelay, Duration rageQuitWithdrawalsTimelock, Timestamp rageQuitExtensionDelayStartedAt - ) internal view { - assert(_context.state == state); - assert(_context.minAssetsLockDuration == minAssetsLockDuration); - assert(_context.rageQuitExtensionDelay == rageQuitExtensionDelay); - assert(_context.rageQuitWithdrawalsTimelock == rageQuitWithdrawalsTimelock); - assert(_context.rageQuitExtensionDelayStartedAt == rageQuitExtensionDelayStartedAt); + ) internal { + assertEq(_context.state, state); + assertEq(_context.minAssetsLockDuration, minAssetsLockDuration); + assertEq(_context.rageQuitExtensionDelay, rageQuitExtensionDelay); + assertEq(_context.rageQuitWithdrawalsTimelock, rageQuitWithdrawalsTimelock); + assertEq(_context.rageQuitExtensionDelayStartedAt, rageQuitExtensionDelayStartedAt); + } + + function assertEq(State a, State b) internal { + assertEq(uint256(a), uint256(b)); } } From 4e4982e42ae1ff0d3e919589ec37e1bf9e49c44b Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 19 Aug 2024 19:18:02 +0400 Subject: [PATCH 6/8] Use _wait() instead of vm.warp() --- test/unit/libraries/EscrowState.t.sol | 28 ++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol index 74b452cc..6602520c 100644 --- a/test/unit/libraries/EscrowState.t.sol +++ b/test/unit/libraries/EscrowState.t.sol @@ -206,9 +206,13 @@ contract EscrowStateUnitTests is UnitTest { _context.rageQuitExtensionDelay = rageQuitExtensionDelay; _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; - vm.warp( - rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() - + rageQuitWithdrawalsTimelock.toSeconds() + 1 + _wait( + Durations.between( + (rageQuitExtensionDelay + rageQuitWithdrawalsTimelock).plusSeconds(1).addTo( + rageQuitExtensionDelayStartedAt + ), + Timestamps.now() + ) ); EscrowState.checkWithdrawalsTimelockPassed(_context); } @@ -233,9 +237,11 @@ contract EscrowStateUnitTests is UnitTest { _context.rageQuitExtensionDelay = rageQuitExtensionDelay; _context.rageQuitWithdrawalsTimelock = rageQuitWithdrawalsTimelock; - vm.warp( - rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() - + rageQuitWithdrawalsTimelock.toSeconds() + _wait( + Durations.between( + (rageQuitExtensionDelay + rageQuitWithdrawalsTimelock).addTo(rageQuitExtensionDelayStartedAt), + Timestamps.now() + ) ); vm.expectRevert(EscrowState.WithdrawalsTimelockNotPassed.selector); @@ -281,7 +287,11 @@ contract EscrowStateUnitTests is UnitTest { _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; - vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds() + 1); + _wait( + Durations.between( + rageQuitExtensionDelay.plusSeconds(1).addTo(rageQuitExtensionDelayStartedAt), Timestamps.now() + ) + ); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); assertTrue(res); } @@ -297,13 +307,13 @@ contract EscrowStateUnitTests is UnitTest { _context.rageQuitExtensionDelayStartedAt = rageQuitExtensionDelayStartedAt; _context.rageQuitExtensionDelay = rageQuitExtensionDelay; - vm.warp(rageQuitExtensionDelayStartedAt.toSeconds() + rageQuitExtensionDelay.toSeconds()); + _wait(Durations.between(rageQuitExtensionDelay.addTo(rageQuitExtensionDelayStartedAt), Timestamps.now())); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); assertFalse(res); } function test_isRageQuitExtensionDelayPassed_ReturnsFalseWhenRageQuitExtraTimelockNotStarted() external { - vm.warp(1234); + _wait(Durations.from(1234)); bool res = EscrowState.isRageQuitExtensionDelayPassed(_context); assertFalse(res); } From ba395fa76536190a62c05668413adf39f16d9b7f Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:56:51 +0400 Subject: [PATCH 7/8] Remove excessive type casts in AssetsAccounting tests --- test/unit/libraries/AssetsAccounting.t.sol | 500 ++++++++++----------- 1 file changed, 238 insertions(+), 262 deletions(-) diff --git a/test/unit/libraries/AssetsAccounting.t.sol b/test/unit/libraries/AssetsAccounting.t.sol index 408d4674..b624ed33 100644 --- a/test/unit/libraries/AssetsAccounting.t.sol +++ b/test/unit/libraries/AssetsAccounting.t.sol @@ -23,17 +23,16 @@ contract AssetsAccountingUnitTests is UnitTest { // accountStETHSharesLock() // --- - function testFuzz_accountStETHSharesLock_happyPath(address holder, uint128 sharesAmount) external { + function testFuzz_accountStETHSharesLock_happyPath(address holder, SharesValue shares) external { SharesValue totalLockedShares = SharesValues.from(3); SharesValue holderLockedShares = SharesValues.from(1); - vm.assume(sharesAmount > 0); + vm.assume(shares.toUint256() > 0); vm.assume( - sharesAmount < type(uint128).max - Math.max(totalLockedShares.toUint256(), holderLockedShares.toUint256()) + shares.toUint256() + < type(uint128).max - Math.max(totalLockedShares.toUint256(), holderLockedShares.toUint256()) ); - SharesValue shares = SharesValues.from(sharesAmount); - _accountingContext.stETHTotals.lockedShares = totalLockedShares; _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; @@ -45,10 +44,10 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters( totalLockedShares + shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO ); - assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares + shares); - assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, holderLockedShares + shares); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, SharesValues.ZERO); + assertLe(_accountingContext.assets[holder].lastAssetsLockTimestamp.toSeconds(), Timestamps.now().toSeconds()); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountStETHSharesLock_RevertWhen_ZeroSharesProvided(address holder) external { @@ -61,14 +60,12 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesLock_WhenNoSharesWereLockedBefore( address stranger, - uint128 sharesAmount + SharesValue shares ) external { SharesValue totalLockedShares = SharesValues.from(3); - vm.assume(sharesAmount > 0); - vm.assume(sharesAmount < type(uint128).max - totalLockedShares.toUint256()); - - SharesValue shares = SharesValues.from(sharesAmount); + vm.assume(shares.toUint256() > 0); + vm.assume(shares.toUint256() < type(uint128).max - totalLockedShares.toUint256()); _accountingContext.stETHTotals.lockedShares = totalLockedShares; @@ -77,9 +74,9 @@ contract AssetsAccountingUnitTests is UnitTest { AssetsAccounting.accountStETHSharesLock(_accountingContext, stranger, shares); - assert(_accountingContext.stETHTotals.lockedShares == totalLockedShares + shares); - assert(_accountingContext.assets[stranger].stETHLockedShares == shares); - assert(_accountingContext.assets[stranger].lastAssetsLockTimestamp <= Timestamps.now()); + assertEq(_accountingContext.stETHTotals.lockedShares, totalLockedShares + shares); + assertEq(_accountingContext.assets[stranger].stETHLockedShares, shares); + assertLe(_accountingContext.assets[stranger].lastAssetsLockTimestamp.toSeconds(), Timestamps.now().toSeconds()); } // --- @@ -88,16 +85,14 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesUnlock_happyPath( address holder, - uint128 sharesAmount, - uint128 holderSharesAmount + SharesValue shares, + SharesValue holderLockedShares ) external { SharesValue totalLockedSharesWithoutHolder = SharesValues.from(3); - vm.assume(sharesAmount > 0); - vm.assume(holderSharesAmount < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); - vm.assume(sharesAmount <= holderSharesAmount); + vm.assume(shares.toUint256() > 0); + vm.assume(holderLockedShares.toUint256() < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); + vm.assume(shares.toUint256() <= holderLockedShares.toUint256()); - SharesValue shares = SharesValues.from(sharesAmount); - SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; _accountingContext.stETHTotals.lockedShares = totalLockedShares; @@ -111,10 +106,10 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters( totalLockedShares - shares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO ); - assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares - shares); - assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, holderLockedShares - shares); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountStETHSharesUnlock_RevertOn_ZeroSharesProvided(address holder) external { @@ -127,19 +122,15 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesUnlock_RevertWhen_HolderHaveLessSharesThanProvided( address holder, - uint128 sharesAmount, - uint128 holderSharesAmount + SharesValue shares, + SharesValue holderLockedShares ) external { SharesValue totalLockedSharesWithoutHolder = SharesValues.from(3); - vm.assume(sharesAmount > 0); - vm.assume(holderSharesAmount < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); - vm.assume(sharesAmount > holderSharesAmount); + vm.assume(shares.toUint256() > 0); + vm.assume(holderLockedShares.toUint256() < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); + vm.assume(shares.toUint256() > holderLockedShares.toUint256()); - SharesValue shares = SharesValues.from(sharesAmount); - SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); - SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; - - _accountingContext.stETHTotals.lockedShares = totalLockedShares; + _accountingContext.stETHTotals.lockedShares = totalLockedSharesWithoutHolder + holderLockedShares; _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); @@ -149,18 +140,14 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesUnlock_RevertOn_AccountingError_TotalLockedSharesCounterIsLessThanProvidedSharesAmount( address holder, - uint128 sharesAmount, - uint128 totalSharesAmount + SharesValue shares, + SharesValue totalLockedShares ) external { - vm.assume(sharesAmount > 0); - vm.assume(totalSharesAmount < sharesAmount); - - SharesValue shares = SharesValues.from(sharesAmount); - SharesValue holderLockedShares = SharesValues.from(sharesAmount); - SharesValue totalLockedShares = SharesValues.from(totalSharesAmount); + vm.assume(shares.toUint256() > 0); + vm.assume(totalLockedShares.toUint256() < shares.toUint256()); _accountingContext.stETHTotals.lockedShares = totalLockedShares; - _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; + _accountingContext.assets[holder].stETHLockedShares = shares; vm.expectRevert(stdError.arithmeticError); @@ -169,11 +156,9 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesUnlock_RevertWhen_NoSharesWereLockedBefore( address stranger, - uint128 sharesAmount + SharesValue shares ) external { - vm.assume(sharesAmount > 0); - - SharesValue shares = SharesValues.from(sharesAmount); + vm.assume(shares.toUint256() > 0); vm.expectRevert(abi.encodeWithSelector(AssetsAccounting.InvalidSharesValue.selector, shares)); @@ -184,12 +169,14 @@ contract AssetsAccountingUnitTests is UnitTest { // accountStETHSharesUnlock(Context storage self, address holder) // --- - function testFuzz_accountStETHSharesUnlock_simple_happyPath(address holder, uint128 holderSharesAmount) external { + function testFuzz_accountStETHSharesUnlock_simple_happyPath( + address holder, + SharesValue holderLockedShares + ) external { SharesValue totalLockedSharesWithoutHolder = SharesValues.from(3); - vm.assume(holderSharesAmount > 0); - vm.assume(holderSharesAmount < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); + vm.assume(holderLockedShares.toUint256() > 0); + vm.assume(holderLockedShares.toUint256() < type(uint128).max - totalLockedSharesWithoutHolder.toUint256()); - SharesValue holderLockedShares = SharesValues.from(holderSharesAmount); SharesValue totalLockedShares = totalLockedSharesWithoutHolder + holderLockedShares; _accountingContext.stETHTotals.lockedShares = totalLockedShares; @@ -200,14 +187,14 @@ contract AssetsAccountingUnitTests is UnitTest { SharesValue unlockedShares = AssetsAccounting.accountStETHSharesUnlock(_accountingContext, holder); - assert(unlockedShares == holderLockedShares); + assertEq(unlockedShares, holderLockedShares); checkAccountingContextTotalCounters( totalLockedShares - holderLockedShares, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO ); - assert(_accountingContext.assets[holder].stETHLockedShares == holderLockedShares - holderLockedShares); - assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, holderLockedShares - holderLockedShares); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountStETHSharesUnlock_simple_RevertWhen_NoSharesWereLockedBefore(address stranger) external { @@ -222,36 +209,33 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesWithdraw_happyPath( address holder, - uint128 holderLockedSharesAmount, - uint128 totalLockedSharesAmount, - uint128 totalClaimedETHAmount + SharesValue holderLockedShares, + SharesValue totalLockedShares, + ETHValue totalClaimedETH ) external { - vm.assume(totalLockedSharesAmount > 0); - vm.assume(holderLockedSharesAmount > 0); - vm.assume(holderLockedSharesAmount <= totalLockedSharesAmount); - - SharesValue holderLockedShares = SharesValues.from(holderLockedSharesAmount); - SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); - ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); + vm.assume(totalLockedShares.toUint256() > 0); + vm.assume(holderLockedShares.toUint256() > 0); + vm.assume(holderLockedShares.toUint256() <= totalLockedShares.toUint256()); _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; _accountingContext.stETHTotals.lockedShares = totalLockedShares; _accountingContext.stETHTotals.claimedETH = totalClaimedETH; - ETHValue expectedETHWithdrawn = - ETHValues.from((uint256(totalClaimedETHAmount) * holderLockedSharesAmount) / totalLockedSharesAmount); + ETHValue expectedETHWithdrawn = ETHValues.from( + (totalClaimedETH.toUint256() * holderLockedShares.toUint256()) / totalLockedShares.toUint256() + ); vm.expectEmit(); emit AssetsAccounting.ETHWithdrawn(holder, holderLockedShares, expectedETHWithdrawn); ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); - assert(ethWithdrawn == expectedETHWithdrawn); + assertEq(ethWithdrawn, expectedETHWithdrawn); checkAccountingContextTotalCounters(totalLockedShares, totalClaimedETH, SharesValues.ZERO, ETHValues.ZERO); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].unstETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountStETHSharesWithdraw_RevertWhen_HolderHaveZeroShares(address stranger) external { @@ -262,13 +246,10 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesWithdraw_RevertOn_AccountingError_TotalLockedSharesCounterIsZero( address holder, - uint128 holderLockedSharesAmount, - uint128 totalClaimedETHAmount + SharesValue holderLockedShares, + ETHValue totalClaimedETH ) external { - vm.assume(holderLockedSharesAmount > 0); - - SharesValue holderLockedShares = SharesValues.from(holderLockedSharesAmount); - ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); + vm.assume(holderLockedShares.toUint256() > 0); _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; _accountingContext.stETHTotals.lockedShares = SharesValues.ZERO; @@ -281,33 +262,31 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountStETHSharesWithdraw_AccountingError_WithdrawAmountMoreThanTotalClaimedETH( address holder, - uint128 holderLockedSharesAmount, - uint128 totalClaimedETHAmount + SharesValue holderLockedShares, + ETHValue totalClaimedETH ) external { uint128 totalLockedSharesAmount = 10; - vm.assume(holderLockedSharesAmount > totalLockedSharesAmount); - vm.assume(holderLockedSharesAmount < type(uint64).max); - vm.assume(totalClaimedETHAmount < type(uint64).max); + vm.assume(holderLockedShares.toUint256() > totalLockedSharesAmount); + vm.assume(holderLockedShares.toUint256() < type(uint64).max); + vm.assume(totalClaimedETH.toUint256() < type(uint64).max); - SharesValue holderLockedShares = SharesValues.from(holderLockedSharesAmount); SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); - ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); _accountingContext.assets[holder].stETHLockedShares = holderLockedShares; _accountingContext.stETHTotals.lockedShares = totalLockedShares; _accountingContext.stETHTotals.claimedETH = totalClaimedETH; ETHValue expectedETHWithdrawn = - ETHValues.from((uint256(totalClaimedETHAmount) * holderLockedSharesAmount) / totalLockedSharesAmount); + ETHValues.from((totalClaimedETH.toUint256() * holderLockedShares.toUint256()) / totalLockedSharesAmount); vm.expectEmit(); emit AssetsAccounting.ETHWithdrawn(holder, holderLockedShares, expectedETHWithdrawn); ETHValue ethWithdrawn = AssetsAccounting.accountStETHSharesWithdraw(_accountingContext, holder); - assert(ethWithdrawn == expectedETHWithdrawn); - assert(ethWithdrawn.toUint256() >= totalClaimedETHAmount); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); + assertEq(ethWithdrawn, expectedETHWithdrawn); + assertGe(ethWithdrawn.toUint256(), totalClaimedETH.toUint256()); + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); } function testFuzz_accountStETHSharesWithdraw_RevertOn_AccountingError_WithdrawAmountOverflow(address holder) @@ -330,12 +309,9 @@ contract AssetsAccountingUnitTests is UnitTest { // accountClaimedStETH // --- - function testFuzz_accountClaimedStETH_happyPath(uint128 ethAmount, uint128 totalClaimedETHAmount) external { - vm.assume(ethAmount < type(uint128).max / 2); - vm.assume(totalClaimedETHAmount < type(uint128).max / 2); - - ETHValue amount = ETHValues.from(ethAmount); - ETHValue totalClaimedETH = ETHValues.from(totalClaimedETHAmount); + function testFuzz_accountClaimedStETH_happyPath(ETHValue amount, ETHValue totalClaimedETH) external { + vm.assume(amount.toUint256() < type(uint128).max / 2); + vm.assume(totalClaimedETH.toUint256() < type(uint128).max / 2); _accountingContext.stETHTotals.claimedETH = totalClaimedETH; @@ -357,14 +333,14 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_happyPath( address holder, uint96[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 1); vm.assume(amountsOfShares.length <= 500); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalUnstETHLockedAmount = 0; _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; @@ -394,19 +370,19 @@ contract AssetsAccountingUnitTests is UnitTest { initialTotalUnfinalizedShares + SharesValues.from(expectedTotalUnstETHLockedAmount), ETHValues.ZERO ); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert( - _accountingContext.assets[holder].unstETHLockedShares - == holderUnstETHLockedShares + SharesValues.from(expectedTotalUnstETHLockedAmount) + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq( + _accountingContext.assets[holder].unstETHLockedShares, + holderUnstETHLockedShares + SharesValues.from(expectedTotalUnstETHLockedAmount) ); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingContext.assets[holder].unstETHIds.length == amountsOfShares.length + 1); + assertLe(_accountingContext.assets[holder].lastAssetsLockTimestamp.toSeconds(), Timestamps.now().toSeconds()); + assertEq(_accountingContext.assets[holder].unstETHIds.length, amountsOfShares.length + 1); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy == holder); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Locked); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].index.toZeroBasedValue() == i + 1); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.from(amountsOfShares[i])); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.ZERO); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy, holder); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].status, UnstETHRecordStatus.Locked); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].index.toZeroBasedValue(), i + 1); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].shares, SharesValues.from(amountsOfShares[i])); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount, ETHValues.ZERO); } } @@ -424,14 +400,13 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_RevertOn_WithdrawalRequestStatusIsFinalized( address holder, uint96[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 0); vm.assume(amountsOfShares.length <= 500); - - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -464,14 +439,13 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_RevertOn_WithdrawalRequestStatusIsClaimed( address holder, uint96[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 0); vm.assume(amountsOfShares.length <= 500); - - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -498,14 +472,13 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_RevertOn_UnstETHRecordStatusIsNot_NotLocked( address holder, uint96[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 0); vm.assume(amountsOfShares.length <= 500); - - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -538,14 +511,13 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_RevertWhen_DuplicatingUnstETHIdsProvided( address holder, uint96[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 1); vm.assume(amountsOfShares.length <= 500); - - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -578,11 +550,11 @@ contract AssetsAccountingUnitTests is UnitTest { // TODO: is it expected behavior? function testFuzz_accountUnstETHLock_WhenNoUnstETHIdsProvided( address holder, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -597,19 +569,19 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, ETHValues.ZERO ); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp <= Timestamps.now()); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, holderUnstETHLockedShares); + assertLe(_accountingContext.assets[holder].lastAssetsLockTimestamp.toSeconds(), Timestamps.now().toSeconds()); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountUnstETHLock_AccountingError_WithdrawalRequestStatusAmountOfSharesOverflow( address holder, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + SharesValue initialTotalUnfinalizedShares ) external { - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -630,10 +602,11 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_AccountingError_HolderUnstETHLockedSharesOverflow( address holder, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue initialTotalUnfinalizedShares ) external { + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); + SharesValue holderUnstETHLockedShares = SharesValues.from(type(uint128).max / 2 + 1); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -654,9 +627,9 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHLock_AccountingError_TotalUnfinalizedSharesOverflow( address holder, - uint96 holderUnstETHLockedSharesAmount + SharesValue holderUnstETHLockedShares ) external { - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); SharesValue initialTotalUnfinalizedShares = SharesValues.from(type(uint128).max / 2 + 1); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; @@ -684,18 +657,18 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHUnlock_happyPath( address holder, uint64[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 0); vm.assume(amountsOfShares.length <= 500); - vm.assume(holderUnstETHLockedSharesAmount > 500 * uint128(type(uint64).max)); - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); + vm.assume(holderUnstETHLockedShares.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalUnfinalizedShares.toUint256() > 500 * uint128(type(uint64).max)); - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalSharesUnlockedAmount = 0; _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; @@ -727,34 +700,34 @@ contract AssetsAccountingUnitTests is UnitTest { initialTotalUnfinalizedShares - SharesValues.from(expectedTotalSharesUnlockedAmount), initialTotalFinalizedETH ); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert( - _accountingContext.assets[holder].unstETHLockedShares - == holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq( + _accountingContext.assets[holder].unstETHLockedShares, + holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].shares, SharesValues.ZERO); } } function testFuzz_accountUnstETHUnlock_WhenFinalizedUnstETHUnlocked( address holder, uint64[] memory amountsOfShares, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(amountsOfShares.length > 0); vm.assume(amountsOfShares.length <= 500); - vm.assume(holderUnstETHLockedSharesAmount > 500 * uint128(type(uint64).max)); - vm.assume(initialTotalFinalizedETHAmount > 500 * uint128(type(uint64).max)); - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); + vm.assume(holderUnstETHLockedShares.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalSharesUnlockedAmount = 0; _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; @@ -790,15 +763,15 @@ contract AssetsAccountingUnitTests is UnitTest { initialTotalUnfinalizedShares, initialTotalFinalizedETH - ETHValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert( - _accountingContext.assets[holder].unstETHLockedShares - == holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq( + _accountingContext.assets[holder].unstETHLockedShares, + holderUnstETHLockedShares - SharesValues.from(expectedTotalSharesUnlockedAmount) ); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert(_accountingContext.unstETHRecords[unstETHIds[i]].shares == SharesValues.ZERO); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].shares, SharesValues.ZERO); } } @@ -864,16 +837,15 @@ contract AssetsAccountingUnitTests is UnitTest { // TODO: is it expected behavior? function testFuzz_accountUnstETHUnlock_WhenNoUnstETHIdsProvided( address holder, - uint96 holderUnstETHLockedSharesAmount, - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + SharesValue holderUnstETHLockedShares, + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(holderUnstETHLockedSharesAmount > 500 * uint128(type(uint64).max)); - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); - - SharesValue holderUnstETHLockedShares = SharesValues.from(holderUnstETHLockedSharesAmount); - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(holderUnstETHLockedShares.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(holderUnstETHLockedShares.toUint256() < type(uint96).max); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.assets[holder].unstETHLockedShares = holderUnstETHLockedShares; _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; @@ -889,10 +861,10 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters( SharesValues.ZERO, ETHValues.ZERO, initialTotalUnfinalizedShares, initialTotalFinalizedETH ); - assert(_accountingContext.assets[holder].stETHLockedShares == SharesValues.ZERO); - assert(_accountingContext.assets[holder].unstETHLockedShares == holderUnstETHLockedShares); - assert(_accountingContext.assets[holder].lastAssetsLockTimestamp == Timestamps.ZERO); - assert(_accountingContext.assets[holder].unstETHIds.length == 0); + assertEq(_accountingContext.assets[holder].stETHLockedShares, SharesValues.ZERO); + assertEq(_accountingContext.assets[holder].unstETHLockedShares, holderUnstETHLockedShares); + assertEq(_accountingContext.assets[holder].lastAssetsLockTimestamp, Timestamps.ZERO); + assertEq(_accountingContext.assets[holder].unstETHIds.length, 0); } function testFuzz_accountUnstETHUnlock_RevertOn_AccountingError_HolderUnstETHLockedSharesUnderflow(address holder) @@ -959,15 +931,15 @@ contract AssetsAccountingUnitTests is UnitTest { // TODO: make a research on gas consumption when a lot of unstNFTs provided. function testFuzz_accountUnstETHFinalized_happyPath( uint64[] memory claimableAmounts, - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { vm.assume(claimableAmounts.length > 0); vm.assume(claimableAmounts.length <= 500); - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalUnfinalizedShares.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); uint256 expectedTotalSharesFinalized = 0; uint256 expectedTotalAmountFinalized = 0; @@ -1017,13 +989,12 @@ contract AssetsAccountingUnitTests is UnitTest { } function testFuzz_accountUnstETHFinalized_When_NoClaimableAmountsProvided( - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1043,13 +1014,12 @@ contract AssetsAccountingUnitTests is UnitTest { function testFuzz_accountUnstETHFinalized_When_UnstETHRecordNotFound( uint64 claimableAmount, - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1071,13 +1041,12 @@ contract AssetsAccountingUnitTests is UnitTest { } function testFuzz_accountUnstETHFinalized_When_ClaimableAmountIsZero( - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1101,13 +1070,12 @@ contract AssetsAccountingUnitTests is UnitTest { } function testFuzz_accountUnstETHFinalized_RevertOn_ClaimableAmountOverflow( - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalUnfinalizedSharesAmount > 500 * uint128(type(uint64).max)); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalFinalizedETH.toUint256() > 500 * uint128(type(uint64).max)); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1126,13 +1094,11 @@ contract AssetsAccountingUnitTests is UnitTest { } function testFuzz_accountUnstETHFinalized_RevertOn_TotalFinalizedETHOverflow( - uint128 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalFinalizedETHAmount > type(uint96).max); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalFinalizedETH.toUint256() > type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1151,13 +1117,12 @@ contract AssetsAccountingUnitTests is UnitTest { } function testFuzz_accountUnstETHFinalized_RevertOn_TotalUnfinalizedSharesUnderflow( - uint96 initialTotalFinalizedETHAmount, - uint96 initialTotalUnfinalizedSharesAmount + ETHValue initialTotalFinalizedETH, + SharesValue initialTotalUnfinalizedShares ) external { - vm.assume(initialTotalUnfinalizedSharesAmount < type(uint64).max); - - ETHValue initialTotalFinalizedETH = ETHValues.from(initialTotalFinalizedETHAmount); - SharesValue initialTotalUnfinalizedShares = SharesValues.from(initialTotalUnfinalizedSharesAmount); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint64).max); + vm.assume(initialTotalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(initialTotalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = initialTotalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = initialTotalUnfinalizedShares; @@ -1203,10 +1168,10 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert( - _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + assertEq( + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount, ETHValues.from(claimableAmounts[i]) ); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].status, UnstETHRecordStatus.Claimed); } } @@ -1323,10 +1288,10 @@ contract AssetsAccountingUnitTests is UnitTest { checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert( - _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + assertEq( + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount, ETHValues.from(claimableAmounts[i]) ); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Claimed); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].status, UnstETHRecordStatus.Claimed); } } @@ -1369,15 +1334,15 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); - assert(amountWithdrawn == ETHValues.from(expectedAmountWithdrawn)); + assertEq(amountWithdrawn, ETHValues.from(expectedAmountWithdrawn)); checkAccountingContextTotalCounters(SharesValues.ZERO, ETHValues.ZERO, SharesValues.ZERO, ETHValues.ZERO); for (uint256 i = 0; i < unstETHIds.length; ++i) { - assert( - _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount == ETHValues.from(claimableAmounts[i]) + assertEq( + _accountingContext.unstETHRecords[unstETHIds[i]].claimableAmount, ETHValues.from(claimableAmounts[i]) ); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].status == UnstETHRecordStatus.Withdrawn); - assert(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy == holder); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].status, UnstETHRecordStatus.Withdrawn); + assertEq(_accountingContext.unstETHRecords[unstETHIds[i]].lockedBy, holder); } } @@ -1389,7 +1354,7 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue amountWithdrawn = AssetsAccounting.accountUnstETHWithdraw(_accountingContext, holder, unstETHIds); - assert(amountWithdrawn == ETHValues.ZERO); + assertEq(amountWithdrawn, ETHValues.ZERO); } function testFuzz_accountUnstETHWithdraw_RevertWhen_UnstETHRecordNotFound( @@ -1455,14 +1420,13 @@ contract AssetsAccountingUnitTests is UnitTest { // --- function testFuzz_getLockedAssetsTotals_happyPath( - uint96 totalFinalizedETHAmount, - uint96 totalLockedSharesAmount, - uint96 totalUnfinalizedSharesAmount + ETHValue totalFinalizedETH, + SharesValue totalLockedShares, + SharesValue totalUnfinalizedShares ) external { - ETHValue totalFinalizedETH = ETHValues.from(totalFinalizedETHAmount); - SharesValue totalUnfinalizedShares = SharesValues.from(totalUnfinalizedSharesAmount); - SharesValue totalLockedShares = SharesValues.from(totalLockedSharesAmount); - + vm.assume(totalFinalizedETH.toUint256() < type(uint96).max); + vm.assume(totalLockedShares.toUint256() < type(uint96).max); + vm.assume(totalUnfinalizedShares.toUint256() < type(uint96).max); _accountingContext.unstETHTotals.finalizedETH = totalFinalizedETH; _accountingContext.unstETHTotals.unfinalizedShares = totalUnfinalizedShares; _accountingContext.stETHTotals.lockedShares = totalLockedShares; @@ -1470,8 +1434,8 @@ contract AssetsAccountingUnitTests is UnitTest { (SharesValue unfinalizedShares, ETHValue finalizedETH) = AssetsAccounting.getLockedAssetsTotals(_accountingContext); - assert(unfinalizedShares == totalLockedShares + totalUnfinalizedShares); - assert(finalizedETH == totalFinalizedETH); + assertEq(unfinalizedShares, totalLockedShares + totalUnfinalizedShares); + assertEq(finalizedETH, totalFinalizedETH); } function test_getLockedAssetsTotals_RevertOn_UnfinalizedSharesOverflow() external { @@ -1524,10 +1488,22 @@ contract AssetsAccountingUnitTests is UnitTest { ETHValue claimedETH, SharesValue unfinalizedShares, ETHValue finalizedETH - ) internal view { - assert(_accountingContext.stETHTotals.lockedShares == lockedShares); - assert(_accountingContext.stETHTotals.claimedETH == claimedETH); - assert(_accountingContext.unstETHTotals.unfinalizedShares == unfinalizedShares); - assert(_accountingContext.unstETHTotals.finalizedETH == finalizedETH); + ) internal { + assertEq(_accountingContext.stETHTotals.lockedShares, lockedShares); + assertEq(_accountingContext.stETHTotals.claimedETH, claimedETH); + assertEq(_accountingContext.unstETHTotals.unfinalizedShares, unfinalizedShares); + assertEq(_accountingContext.unstETHTotals.finalizedETH, finalizedETH); + } + + function assertEq(SharesValue a, SharesValue b) internal { + assertEq(a.toUint256(), b.toUint256()); + } + + function assertEq(ETHValue a, ETHValue b) internal { + assertEq(a.toUint256(), b.toUint256()); + } + + function assertEq(UnstETHRecordStatus a, UnstETHRecordStatus b) internal { + assertEq(uint256(a), uint256(b)); } } From 3b9eb9dcda0b496d5a736a636a13b2d690634f1e Mon Sep 17 00:00:00 2001 From: Artem G <175325367+sandstone-ag@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:01:43 +0400 Subject: [PATCH 8/8] Fix EscrowState tests --- test/unit/libraries/EscrowState.t.sol | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/test/unit/libraries/EscrowState.t.sol b/test/unit/libraries/EscrowState.t.sol index 6602520c..918fbebb 100644 --- a/test/unit/libraries/EscrowState.t.sol +++ b/test/unit/libraries/EscrowState.t.sol @@ -1,8 +1,6 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.26; -import {Vm} from "forge-std/Vm.sol"; - import {Duration, Durations, MAX_VALUE as DURATION_MAX_VALUE} from "contracts/types/Duration.sol"; import {Timestamp, Timestamps, MAX_TIMESTAMP_VALUE, TimestampOverflow} from "contracts/types/Timestamp.sol"; import {EscrowState, State} from "contracts/libraries/EscrowState.sol"; @@ -88,7 +86,7 @@ contract EscrowStateUnitTests is UnitTest { function test_startRageQuitExtensionDelay_happyPath() external { vm.expectEmit(); - emit EscrowState.RageQuitTimelockStarted(); + emit EscrowState.RageQuitTimelockStarted(Timestamps.now()); EscrowState.startRageQuitExtensionDelay(_context); @@ -122,22 +120,13 @@ contract EscrowStateUnitTests is UnitTest { }); } - function test_setMinAssetsLockDuration_WhenDurationNotChanged(Duration minAssetsLockDuration) external { + function test_setMinAssetsLockDuration_RevertWhen_DurationNotChanged(Duration minAssetsLockDuration) external { _context.minAssetsLockDuration = minAssetsLockDuration; - Vm.Log[] memory entries = vm.getRecordedLogs(); - + vm.expectRevert( + abi.encodeWithSelector(EscrowState.InvalidMinAssetsLockDuration.selector, minAssetsLockDuration) + ); EscrowState.setMinAssetsLockDuration(_context, minAssetsLockDuration); - - checkContext({ - state: State.NotInitialized, - minAssetsLockDuration: minAssetsLockDuration, - rageQuitExtensionDelay: D0, - rageQuitWithdrawalsTimelock: D0, - rageQuitExtensionDelayStartedAt: T0 - }); - - assertEq(entries.length, 0); } // ---