Skip to content

Commit

Permalink
Merge pull request #484 from lidofinance/feature/withdrawal_tests
Browse files Browse the repository at this point in the history
- restore basic Withdrawal queue tests
- fix a bug sneaked from WQv0 on request creation
- fix a bug with shares precision calculation on finalization
- unify assertReverts for custom error solutions
  • Loading branch information
folkyatina authored Jan 13, 2023
2 parents 86d68d2 + da08679 commit 5680024
Show file tree
Hide file tree
Showing 29 changed files with 295 additions and 476 deletions.
2 changes: 1 addition & 1 deletion contracts/0.4.24/interfaces/IWithdrawalQueue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface IWithdrawalQueue {
function calculateFinalizationParams(
uint256 _lastIdToFinalize,
uint256 _shareRate
) external view returns (uint256 sharesToBurn, uint256 etherToLock);
) external view returns (uint128 sharesToBurn, uint128 etherToLock);

function finalize(
uint256 _lastIdToFinalize,
Expand Down
2 changes: 1 addition & 1 deletion contracts/0.4.24/test_helpers/StETHMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "../StETH.sol";
contract StETHMock is StETH {
uint256 private totalPooledEther;

constructor() public {
constructor() public payable{
_resume();
}

Expand Down
16 changes: 10 additions & 6 deletions contracts/0.8.9/WithdrawalQueue.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Lido <[email protected]>
// SPDX-FileCopyrightText: 2023 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0

/* See contracts/COMPILERS.md */
Expand Down Expand Up @@ -122,6 +122,8 @@ contract WithdrawalQueue {
*/
uint256 public constant MAX_STETH_WITHDRAWAL_AMOUNT = 1000 ether;

uint256 public constant SHARE_RATE_PRECISION = 1e27;

///! STRUCTURED STORAGE OF THE CONTRACT
///! SLOT 0: uint128 lockedEtherAmount
///! SLOT 1: uint256 finalizedRequestsCounter
Expand All @@ -139,7 +141,7 @@ contract WithdrawalQueue {
uint256 public finalizedRequestsCounter = 0;

/// @notice queue for withdrawal requests
WithdrawalRequest[] public queue;
WithdrawalRequest[] internal queue;

/// @notice withdrawal requests mapped to the recipients
mapping(address => uint256[]) public requestsByRecipient;
Expand Down Expand Up @@ -311,7 +313,9 @@ contract WithdrawalQueue {
if (_lastIdToFinalize < finalizedRequestsCounter || _lastIdToFinalize >= queue.length) {
revert InvalidFinalizationId();
}
if (lockedEtherAmount + msg.value > address(this).balance) revert NotEnoughEther();
(uint128 ethToWithdraw, ) = _calculateDiscountedBatch(finalizedRequestsCounter, _lastIdToFinalize, _shareRate);

if (msg.value < ethToWithdraw) revert NotEnoughEther();

_updateRateHistory(_shareRate, _lastIdToFinalize);

Expand All @@ -331,7 +335,7 @@ contract WithdrawalQueue {
function calculateFinalizationParams(
uint256 _lastIdToFinalize,
uint256 _shareRate
) external view returns (uint256 etherToLock, uint256 sharesToBurn) {
) external view returns (uint128 etherToLock, uint128 sharesToBurn) {
return _calculateDiscountedBatch(finalizedRequestsCounter, _lastIdToFinalize, _shareRate);
}

Expand Down Expand Up @@ -413,7 +417,7 @@ contract WithdrawalQueue {
shares -= queue[_firstId - 1].cumulativeShares;
}

eth = _min(eth, _toUint128((shares * _shareRate) / 1e9));
eth = _min(eth, _toUint128(shares * _shareRate / SHARE_RATE_PRECISION));
}

/// @dev checks if provided request included in the rate hint boundaries
Expand Down Expand Up @@ -498,7 +502,7 @@ contract WithdrawalQueue {
WithdrawalRequest memory prevRequest = queue[requestId - 1];

cumulativeShares += prevRequest.cumulativeShares;
cumulativeShares += prevRequest.cumulativeEther;
cumulativeEther += prevRequest.cumulativeEther;
}

queue.push(
Expand Down
11 changes: 0 additions & 11 deletions contracts/0.8.9/test_helpers/Owner.sol

This file was deleted.

25 changes: 0 additions & 25 deletions contracts/0.8.9/test_helpers/StETHMockForWithdrawalQueue.sol

This file was deleted.

2 changes: 1 addition & 1 deletion lib/abi/WithdrawalQueue.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"gasprofile"
],
"scripts": {
"postinstall": "patch-package && husky install",
"postinstall": "husky install",
"build:apps": "yarn compile && hardhat run --no-compile scripts/build-apps-frontend.js",
"lint": "yarn lint:sol && yarn lint:js",
"lint:sol": "solhint \"contracts/**/*.sol\" --ignore-path .solhintignore",
Expand Down Expand Up @@ -127,7 +127,6 @@
"ethers": "^5.1.4",
"node-gyp": "^8.4.1",
"openzeppelin-solidity": "2.0.0",
"patch-package": "^6.4.7",
"solhint-plugin-lido": "^0.0.4",
"yargs": "^16.0.3"
}
Expand Down
130 changes: 0 additions & 130 deletions patches/@aragon+contract-helpers-test+0.1.0.patch

This file was deleted.

2 changes: 1 addition & 1 deletion test/0.4.24/lido.rewards-distribution.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { assert } = require('chai')
const { newDao, newApp } = require('./helpers/dao')
const { assertBn, assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn } = require('@aragon/contract-helpers-test/src/asserts')
const { ZERO_ADDRESS, bn } = require('@aragon/contract-helpers-test')

const NodeOperatorsRegistry = artifacts.require('NodeOperatorsRegistryMock')
Expand Down
12 changes: 7 additions & 5 deletions test/0.4.24/lido.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const { assert } = require('chai')
const { artifacts } = require('hardhat')

const { getInstalledApp } = require('@aragon/contract-helpers-test/src/aragon-os')
const { assertBn, assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')


const { newDao, newApp } = require('./helpers/dao')
const { ZERO_ADDRESS, bn, getEventAt } = require('@aragon/contract-helpers-test')
Expand Down Expand Up @@ -457,17 +459,17 @@ contract('Lido', ([appManager, voting, user1, user2, user3, nobody, depositor, t

await assertRevert(
stakingRouter.updateStakingModule(module1.id, 10001, 300, 700, { from: voting }),
`ed with custom error 'ErrorValueOver100Percent("_targetShare")`
`ErrorValueOver100Percent("_targetShare")`
)

await assertRevert(
stakingRouter.updateStakingModule(module1.id, 10000, 10001, 700, { from: voting }),
`ed with custom error 'ErrorValueOver100Percent("_moduleFee + _treasuryFee")`
`ErrorValueOver100Percent("_moduleFee + _treasuryFee")`
)

await assertRevert(
stakingRouter.updateStakingModule(module1.id, 10000, 300, 10001, { from: voting }),
`ed with custom error 'ErrorValueOver100Percent("_moduleFee + _treasuryFee")`
`ErrorValueOver100Percent("_moduleFee + _treasuryFee")`
)

// distribution fee calculates on active keys in modules
Expand Down Expand Up @@ -608,7 +610,7 @@ contract('Lido', ([appManager, voting, user1, user2, user3, nobody, depositor, t
// can not deposit with unset withdrawalCredentials
await assertRevert(
app.methods['deposit(uint256,uint24,bytes)'](MAX_DEPOSITS, CURATED_MODULE_ID, CALLDATA, { from: depositor }),
`ed with custom error 'ErrorEmptyWithdrawalsCredentials()`
'ErrorEmptyWithdrawalsCredentials()'
)
// set withdrawalCredentials with keys, because they were trimmed
await stakingRouter.setWithdrawalCredentials(pad('0x0202', 32), { from: voting })
Expand Down
3 changes: 2 additions & 1 deletion test/0.4.24/lidoHandleOracleReport.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { assert } = require('chai')
const { newDao, newApp } = require('./helpers/dao')
const { assertBn, assertRevert } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')

const LidoPushableMock = artifacts.require('LidoPushableMock.sol')
const OracleMock = artifacts.require('OracleMock.sol')
Expand Down
4 changes: 3 additions & 1 deletion test/0.4.24/lidooracle.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { assert } = require('chai')
const { newDao, newApp } = require('./helpers/dao')
const { assertBn, assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')

const { toBN } = require('../helpers/utils')
const { ZERO_ADDRESS } = require('@aragon/contract-helpers-test')
const keccak256 = require('js-sha3').keccak_256
Expand Down
4 changes: 3 additions & 1 deletion test/0.4.24/node-operators-registry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const { hexSplit, toBN } = require('../helpers/utils')
const { newDao, newApp } = require('./helpers/dao')
const { EvmSnapshot } = require('../helpers/blockchain')
const { ZERO_ADDRESS, getEventAt } = require('@aragon/contract-helpers-test')
const { assertBn, assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')

const keccak256 = require('js-sha3').keccak_256
const nodeOperators = require('../helpers/node-operators')

Expand Down
3 changes: 2 additions & 1 deletion test/0.4.24/staking-limit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai')
const { assertBn, assertRevert } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')
const { bn, MAX_UINT256 } = require('@aragon/contract-helpers-test')
const { toBN } = require('../helpers/utils')
const { waitBlocks } = require('../helpers/blockchain')
Expand Down
3 changes: 2 additions & 1 deletion test/0.4.24/steth.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { assert } = require('chai')
const { assertBn, assertRevert, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')
const { ZERO_ADDRESS, bn } = require('@aragon/contract-helpers-test')

const StETH = artifacts.require('StETHMock')
Expand Down
3 changes: 2 additions & 1 deletion test/0.8.9/composite-post-rebase-beacon-receiver.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint no-unmodified-loop-condition: "warn" */

const { assertBn, assertRevert, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent, assertAmountOfEvents } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')
const { ZERO_ADDRESS, bn } = require('@aragon/contract-helpers-test')

const CompositePostRebaseBeaconReceiver = artifacts.require('CompositePostRebaseBeaconReceiver.sol')
Expand Down
3 changes: 2 additions & 1 deletion test/0.8.9/deposit-security-module.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const hre = require('hardhat')
const { assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')
const { assert } = require('chai')
const { DSMAttestMessage, DSMPauseMessage } = require('./helpers/signatures')
const { ZERO_ADDRESS } = require('@aragon/contract-helpers-test')
Expand Down
3 changes: 2 additions & 1 deletion test/0.8.9/lido-exec-layer-rewards-vault.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { assert } = require('chai')

const { assertBn, assertRevert, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertBn, assertEvent } = require('@aragon/contract-helpers-test/src/asserts')
const { assertRevert } = require('../helpers/assertThrow')
const { ZERO_ADDRESS, bn } = require('@aragon/contract-helpers-test')
const { newDao, newApp } = require('../0.4.24/helpers/dao')
const { StETH, ETH } = require('../helpers/utils')
Expand Down
Loading

0 comments on commit 5680024

Please sign in to comment.