AI Arena - ziyou-'s results

In AI Arena you train an AI character to battle in a platform fighting game. Imagine a cross between Pokémon and Super Smash Bros, but the characters are AIs, and you can train them to learn almost any skill in preparation for battle.

General Information

Platform: Code4rena

Start Date: 09/02/2024

Pot Size: $60,500 USDC

Total HM: 17

Participants: 283

Period: 12 days

Judge:

Id: 328

League: ETH

AI Arena

Findings Distribution

Researcher Performance

Rank: 151/283

Findings: 1

Award: $13.63

🌟 Selected for report: 0

🚀 Solo Findings: 0

Awards

13.6293 USDC - $13.63

Labels

bug
G (Gas Optimization)
grade-b
sufficient quality report
edited-by-warden
G-30

External Links

IssueInstancesGas Savings
[G-01]The burnFrom function evaluates the value of allowance(account, msg.sender) multiple times1237
[G-02]The claimNRN function uses the value of numRoundsClaimed[msg.sender] multiple times1344
[G-03]The updateBattleRecord function uses 100 several times instead of setting a constant1

[G-01] The burnFrom function evaluates the value of allowance(account, msg.sender) multiple times

The burnFrom function evaluates the value of allowance(account, msg.sender) multiple times. It can be replaced by uint256 allow to avoid double calculations

File: src/Neuron.sol


    function burnFrom(address account, uint256 amount) public virtual {
+       uint256 allow = allowance(account, msg.sender);
+       require(allow >= amount,"ERC20: burn amount exceeds allowance");

-       require(
-           allowance(account, msg.sender) >= amount, 
-           "ERC20: burn amount exceeds allowance"
-       );
-       uint256 decreasedAllowance = allowance(account, msg.sender) - amount;

+       uint256 decreasedAllowance = allow - amount;
        _burn(account, amount);
        _approve(account, msg.sender, decreasedAllowance);
    }

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L196-L204

Before making changes [PASS] testBurnFrom() (gas: 103644)

After making changes [PASS] testBurnFrom() (gas: 103407)

[G-02] The claimNRN function uses the value of numRoundsClaimed[msg.sender] multiple times

The claimNRN function uses the value of numRoundsClaimed[msg.sender] multiple times.It can be replaced by uint32 _numRoundsClaimed to avoid double calculations.

File: src/RankedBattle.sol


    function claimNRN() external {
-       require(numRoundsClaimed[msg.sender] < roundId, "Already claimed NRNs for this period");
+       uint32 _numRoundsClaimed = numRoundsClaimed[msg.sender];
+       require(_numRoundsClaimed < roundId, "Already claimed NRNs for this period");
        uint256 claimableNRN = 0;
        uint256 nrnDistribution;
+       uint32 lowerBound = _numRoundsClaimed;
-       uint32 lowerBound = numRoundsClaimed[msg.sender];
        for (uint32 currentRound = lowerBound; currentRound < roundId; currentRound++) {
            nrnDistribution = getNrnDistribution(currentRound);
            claimableNRN += (
                accumulatedPointsPerAddress[msg.sender][currentRound] * nrnDistribution   
            ) / totalAccumulatedPoints[currentRound];
            numRoundsClaimed[msg.sender] += 1;
        }
        if (claimableNRN > 0) {
            amountClaimed[msg.sender] += claimableNRN;
            _neuronInstance.mint(msg.sender, claimableNRN);
            emit Claimed(msg.sender, claimableNRN);
        }
    }

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L294-L311

Before making changes [PASS] testClaimNRN() (gas: 1781228)

After making changes [PASS] testClaimNRN() (gas: 1780884)

[G-03] The updateBattleRecord function uses 100 several times instead of setting a constant

File: src/RankedBattle.sol
+       uint256 public constant x = 100;
+       require(mergingPortion <= x);
-       require(mergingPortion <= 100);
https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L332
File: src/RankedBattle.sol
+       uint256 mergingPoints = (points * mergingPortion) / x;
-       uint256 mergingPoints = (points * mergingPortion) / 100;
https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L449

Before making changes [PASS] testSetNewRoundFromAdmin() (gas: 957090) [PASS] testClaimNRN() (gas: 1781228) [PASS] testGetBattleRecord() (gas: 874511) [PASS] testGetCurrentStakingData() (gas: 876469) [PASS] testgetUnclaimedNRN() (gas: 963071) [PASS] testUpdateBattleRecordPlayerLossBattle() (gas: 854453) [PASS] testUpdateBattleRecordPlayerTiedBattle() (gas: 754196) [PASS] testUpdateBattleRecordPlayerWonBattle() (gas: 958706)

After making changes [PASS] testSetNewRoundFromAdmin() (gas: 957067) [PASS] testClaimNRN() (gas: 1781226) [PASS] testGetBattleRecord() (gas: 874488) [PASS] testGetCurrentStakingData() (gas: 876424) [PASS] testgetUnclaimedNRN() (gas: 963070) [PASS] testUpdateBattleRecordPlayerLossBattle() (gas: 854430) [PASS] testUpdateBattleRecordPlayerTiedBattle() (gas: 754173) [PASS] testUpdateBattleRecordPlayerWonBattle() (gas: 958705).

#0 - raymondfam

2024-02-25T22:26:42Z

3G

#1 - c4-pre-sort

2024-02-25T22:26:46Z

raymondfam marked the issue as sufficient quality report

#2 - c4-judge

2024-03-19T07:45:36Z

HickupHH3 marked the issue as grade-b

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter