[BUG]: with a proposal and no registered voters distributeFunds always reverts and deposits get locked forever

Open Beginner friendly
#92 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
solidity
Domain
blockchain

Research direction

Start in app/blockchain/src/FundingVault.sol at calculateFundingToBeReceived(), around lines 249-252, where it reverts when no voting power tokens exist, then check its callers in distributeFunds() (around lines 294-295) and withdrawRemaining() (around lines 345-348). Run the reproduction with cd app/blockchain && forge test --match-contract FundingVaultNoVotersTest -vvv in test/unit/FundingVaultNoVoters.t.sol. Done means the lock case no longer reverts, depositors can withdraw, and the existing test suite still passes.

Written by the indexing model from the issue text.

Description

bug
Bug Description

if nobody calls register() before the tally date and the vault has at least one proposal then distributeFunds() reverts every time and no depositor can ever withdraw

calculateFundingToBeReceived() reverts when no voting power tokens exist

https://github.com/StabilityNexus/FairFund/blob/4f8d67655362367fbfb0b444a7ab28bc6cdb4e44/app/blockchain/src/FundingVault.sol#L249-L252

distributeFunds() calls it for every proposal so one proposal is enough

https://github.com/StabilityNexus/FairFund/blob/4f8d67655362367fbfb0b444a7ab28bc6cdb4e44/app/blockchain/src/FundingVault.sol#L294-L295

and withdrawRemaining() only works after distribution

https://github.com/StabilityNexus/FairFund/blob/4f8d67655362367fbfb0b444a7ab28bc6cdb4e44/app/blockchain/src/FundingVault.sol#L345-L348

voting power tokens only come from register() because VotingPowerToken.mint is onlyOwner and the owner is the vault (FairFund.sol L112). the vault only mints in register() (L183). register() stops working once block.timestamp > tallyDate

https://github.com/StabilityNexus/FairFund/blob/4f8d67655362367fbfb0b444a7ab28bc6cdb4e44/app/blockchain/src/FundingVault.sol#L118-L123

so after the tally date nothing can fix it and all deposits stay in the vault forever. the vault has no owner and no rescue function

anyone can do this since submitProposal() needs no deposit or voting tokens only gas (L196). it can also happen by accident in a quiet vault where people submit proposals but nobody registers

with no proposals the loop never runs and depositors get everything back. one registration before the tally date also avoids the lock. the test below shows all 3 cases

possible fix: make calculateFundingToBeReceived() return 0 instead of reverting when no voting power tokens exist (L250-L252). no proposal can win without votes so distributeFunds() pays nobody and marks the funds as distributed and depositors can withdraw everything. its a one line change and it also stops the frontend tally route from failing for these vaults. i tried it locally and all existing tests still pass

Steps to Reproduce
  1. create a vault and have two users deposit
  2. anyone submits one proposal
  3. nobody calls register() before the tally date
  4. after the tally date call distributeFunds() and it reverts with FundingVault__NoVotingPowerTokenMinted
  5. depositors call withdrawRemaining() and it reverts with FundingVault__FundsNotDistributedYet
  6. register() now reverts with FundingVault__TallyDatePassed and 10 years later it all still reverts

test file is below (app/blockchain/test/unit/FundingVaultNoVoters.t.sol)

cd app/blockchain
forge test --match-contract FundingVaultNoVotersTest -vvv
Logs and Screenshots
Ran 3 tests for test/unit/FundingVaultNoVoters.t.sol:FundingVaultNoVotersTest
[PASS] test_NoProposalsNoVoters_DepositorsWithdrawAll() (gas: 127256)
[PASS] test_OneProposalNoVoters_LocksDepositsForever() (gas: 375278)
[PASS] test_OneRegistrationBeforeTally_AvoidsLock() (gas: 416212)
Suite result: ok. 3 passed; 0 failed; 0 skipped; finished in 4.79ms (1.41ms CPU time)

Ran 1 test suite in 108.33ms (4.79ms CPU time): 3 tests passed, 0 failed, 0 skipped (3 total tests)

trace for test_OneProposalNoVoters_LocksDepositsForever

    ├─ [37040] FundingVault::distributeFunds()
    │   ├─ [2482] FairFund::getPlatformFee() [staticcall]
    │   │   └─ ← [Return] 5
    │   ├─ [2505] VotingPowerToken::totalSupply() [staticcall]
    │   │   └─ ← [Return] 0
    │   └─ ← [Revert] FundingVault__NoVotingPowerTokenMinted()
    ├─ [0] VM::prank(alice: [0x328809Bc894f92807417D2dAD6b7C998c1aFdac6])
    │   └─ ← [Return]
    ├─ [0] VM::expectRevert(custom error 0xc31eb0e0: 82f3861b00000000000000000000000000000000000000000000000000000000)
    │   └─ ← [Return]
    ├─ [5528] FundingVault::withdrawRemaining()
    │   └─ ← [Revert] FundingVault__FundsNotDistributedYet()
    ├─ [0] VM::prank(bob: [0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e])
    │   └─ ← [Return]
    ├─ [0] VM::expectRevert(custom error 0xc31eb0e0: 82f3861b00000000000000000000000000000000000000000000000000000000)
    │   └─ ← [Return]
    ├─ [5528] FundingVault::withdrawRemaining()
    │   └─ ← [Revert] FundingVault__FundsNotDistributedYet()
FundingVaultNoVoters.t.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {FundingVault} from "../../src/FundingVault.sol";
import {FairFund} from "../../src/FairFund.sol";
import {VotingPowerToken} from "../../src/VotingPowerToken.sol";
import {MockERC20} from "../../src/mocks/MockERC20.sol";

contract FundingVaultNoVotersTest is Test {
    FairFund fairFund;
    FundingVault vault;
    MockERC20 fundingToken;
    MockERC20 votingToken;
    VotingPowerToken votingPowerToken;

    address alice = makeAddr("alice"); // depositor
    address bob = makeAddr("bob"); // depositor
    address attacker = makeAddr("attacker"); // holds no tokens at all
    address voter = makeAddr("voter");

    uint256 constant ALICE_DEPOSIT = 60 ether;
    uint256 constant BOB_DEPOSIT = 40 ether;

    function setUp() external {
        fairFund = new FairFund(5);
        fundingToken = new MockERC20("FundingToken", "FTK");
        votingToken = new MockERC20("VotingToken", "VTK");
        votingPowerToken = new VotingPowerToken("VotingPowerToken", "VOTE", votingToken.decimals());
        vault = new FundingVault(
            address(fundingToken),
            address(votingToken),
            address(votingPowerToken),
            1,
            50 ether,
            block.timestamp + 1 days,
            address(fairFund)
        );
        votingPowerToken.transferOwnership(address(vault));

        _deposit(alice, ALICE_DEPOSIT);
        _deposit(bob, BOB_DEPOSIT);
    }

    function _deposit(address user, uint256 amount) internal {
        fundingToken.mint(user, amount);
        vm.startPrank(user);
        fundingToken.approve(address(vault), amount);
        vault.deposit(amount);
        vm.stopPrank();
    }

    // Baseline: with no proposals and no voters, depositors get everything back.
    function test_NoProposalsNoVoters_DepositorsWithdrawAll() public {
        vm.warp(block.timestamp + 2 days);
        vault.distributeFunds();

        vm.prank(alice);
        vault.withdrawRemaining();
        vm.prank(bob);
        vault.withdrawRemaining();
        assertEq(fundingToken.balanceOf(alice), ALICE_DEPOSIT);
        assertEq(fundingToken.balanceOf(bob), BOB_DEPOSIT);
        assertEq(fundingToken.balanceOf(address(vault)), 0);
    }

    // One proposal from anyone, and nobody registers voting tokens before the tally date.
    function test_OneProposalNoVoters_LocksDepositsForever() public {
        vm.prank(attacker);
        vault.submitProposal("any proposal", 1, 1 ether, attacker);
        vm.warp(block.timestamp + 2 days);

        vm.expectRevert(FundingVault.FundingVault__NoVotingPowerTokenMinted.selector);
        vault.distributeFunds();

        vm.prank(alice);
        vm.expectRevert(FundingVault.FundingVault__FundsNotDistributedYet.selector);
        vault.withdrawRemaining();
        vm.prank(bob);
        vm.expectRevert(FundingVault.FundingVault__FundsNotDistributedYet.selector);
        vault.withdrawRemaining();

        // Nobody can fix it after the tally date: register is closed.
        votingToken.mint(alice, 1 ether);
        vm.startPrank(alice);
        votingToken.approve(address(vault), 1 ether);
        vm.expectRevert(FundingVault.FundingVault__TallyDatePassed.selector);
        vault.register(1 ether);
        vm.stopPrank();

        // Still locked 10 years later.
        vm.warp(block.timestamp + 3650 days);
        vm.expectRevert(FundingVault.FundingVault__NoVotingPowerTokenMinted.selector);
        vault.distributeFunds();
        vm.prank(alice);
        vm.expectRevert(FundingVault.FundingVault__FundsNotDistributedYet.selector);
        vault.withdrawRemaining();
        assertEq(fundingToken.balanceOf(address(vault)), ALICE_DEPOSIT + BOB_DEPOSIT);
    }

    // The lock only happens when nobody registers. One registration before the tally date avoids it.
    function test_OneRegistrationBeforeTally_AvoidsLock() public {
        vm.prank(attacker);
        vault.submitProposal("any proposal", 1, 1 ether, attacker);
        votingToken.mint(voter, 1);
        vm.startPrank(voter);
        votingToken.approve(address(vault), 1);
        vault.register(1);
        vm.stopPrank();
        vm.warp(block.timestamp + 2 days);

        vault.distributeFunds();
        vm.prank(alice);
        vault.withdrawRemaining();
        assertEq(fundingToken.balanceOf(alice), ALICE_DEPOSIT);
    }
}
Environment Details
  • commit 4f8d676 on main
  • forge 1.5.1 with solc 0.8.33 and evm Paris
  • macOS
Impact

high. one proposal and zero registrations lock every deposit in the vault forever and anyone can submit that proposal

no live vault is affected right now since FairFund on Polygon mainnet has 0 vaults

related to #90 (same symptom but a different cause). PR #91 does not touch this

Code of Conduct
  • I have joined the Discord server and will post updates there
  • I have searched existing issues to avoid duplicates
Dominant language
Solidity
Stars
17
Forks
29
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from StabilityNexus/FairFund

All issues in StabilityNexus/FairFund

Similar issues

More Blockchain issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.