Skip to content

Commit

Permalink
Get pending withdrawal (#9369)
Browse files Browse the repository at this point in the history
### Description

LockedGold.sol currently provides a way to retrieve all pendingWithdrawals associated to a given account.

To be able to extract a pendingWithdrawal at specific index, one would need to first do getPendingWithdrawals and then loop through the result to find the record 0(n) and has a DOS vector as found by @tkporter 

### Tested

- [x] unit test

### Related issues
 https://app.zenhub.com/workspaces/yield-61b75a715d3ecb001007ab9d/issues/celo-org/yield/16

### Backwards compatibility

_Brief explanation of why these changes are/are not backwards compatible._
This change is backward compatible because it is completely transparent and does not modify existing function signatures etc
  • Loading branch information
montera82 authored and martinvol committed May 13, 2022
1 parent 84194c7 commit 0edc59d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
21 changes: 20 additions & 1 deletion packages/protocol/contracts/governance/LockedGold.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ contract LockedGold is
* @return The storage, major, minor, and patch version of the contract.
*/
function getVersionNumber() external pure returns (uint256, uint256, uint256, uint256) {
return (1, 1, 1, 2);
return (1, 1, 2, 0);
}

/**
Expand Down Expand Up @@ -284,6 +284,25 @@ contract LockedGold is
return (values, timestamps);
}

/**
* @notice Returns the pending withdrawal at a given index for a given account.
* @param account The address of the account.
* @param index The index of the pending withdrawal.
* @return The value of the pending withdrawal.
* @return The timestamp of the pending withdrawal.
*/
function getPendingWithdrawal(address account, uint256 index)
external
view
returns (uint256, uint256)
{
require(getAccounts().isAccount(account), "Unknown account");
require(index < balances[account].pendingWithdrawals.length, "Bad pending withdrawal index");
PendingWithdrawal memory pendingWithdrawal = (balances[account].pendingWithdrawals[index]);

return (pendingWithdrawal.value, pendingWithdrawal.timestamp);
}

/**
* @notice Returns the total amount to withdraw from unlocked gold for an account.
* @param account The address of the account.
Expand Down
9 changes: 4 additions & 5 deletions packages/protocol/test/governance/voting/lockedgold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,10 @@ contract('LockedGold', (accounts: string[]) => {
})

it('should add a pending withdrawal', async () => {
const [values, timestamps] = await lockedGold.getPendingWithdrawals(account)
assert.equal(values.length, 1)
assert.equal(timestamps.length, 1)
assertEqualBN(values[0], value)
assertEqualBN(timestamps[0], availabilityTime)
const [val, timestamp] = await lockedGold.getPendingWithdrawal(account, 0)
assertEqualBN(val, value)
assertEqualBN(timestamp, availabilityTime)
await assertRevert(lockedGold.getPendingWithdrawal(account, 1))
})

it("should decrease the account's nonvoting locked gold balance", async () => {
Expand Down

0 comments on commit 0edc59d

Please sign in to comment.