AI Arena - 0xStriker'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: 116/283

Findings: 3

Award: $30.00

🌟 Selected for report: 0

πŸš€ Solo Findings: 0

Lines of code

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L470 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L500 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L380

Vulnerability details

Impact

in the FighterFarm contract the _createFighterBase function while calculating the (element) variable, the numElements mapping will only map (0) which is the generation of the fighter type to 3 elements, means the fighter at the zero generation will have 3 elements and will revert on any other generation since there is no value set for the numElements to other generations throughout the contract, this means that except for the zero generation, which will have valid value, the mapping (numElements) will return 0 for other fighter generations, when the dna is mod(%) by the 0 it will revert. Hence not allowing fighter to be created or rerolled when called from the respective functions.

Proof of Concept

function _createFighterBase(
        uint256 dna, 
        uint8 fighterType
    ) 
        private 
        view 
        returns (uint256, uint256, uint256) 
    {
        uint256 element = dna % numElements[generation[fighterType]];
        uint256 weight = dna % 31 + 65;
        uint256 newDna = fighterType == 0 ? dna : uint256(fighterType);
        return (element, weight, newDna);
    }

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L470 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L500 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L380 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L110

Tools Used

Manual Review

Ensure that numElements is initialized with a valid non-zero value for each new generation before any fighters of that generation are created. This could be done within the function that increments the generation. the incrementGeneration function could be updated to account for numElements too like this:

        function incrementGeneration(uint8 fighterType, uint8 newNumElements) external {
        require(msg.sender == _ownerAddress, "Only the owner can increment generation");
        require(newNumElements > 0, "Number of elements must be greater than zero");
        generation[fighterType] += 1;
        maxRerollsAllowed[fighterType] += 1;
        numElements[generation[fighterType]] = newNumElements;
    }

Assessed type

Other

#0 - c4-pre-sort

2024-02-22T19:05:58Z

raymondfam marked the issue as sufficient quality report

#1 - c4-pre-sort

2024-02-22T19:06:09Z

raymondfam marked the issue as duplicate of #45

#2 - c4-judge

2024-03-08T03:18:46Z

HickupHH3 marked the issue as satisfactory

Summary

Low Risk Issues

noIssueInstances
[L-01]add more validation checks for the input data in the createGameItem() function1
[L-02]the function call will fail if the maxId is more than one1
[L-03]zero nrn distribution1
[L-04]the function adjusts the unstake amount without reverting1
[L-05]it is a best practice to use the safeErc20 library12
[L-06]no zero address checks15

Non-critical

noIssueInstances
[N-01]The values of the attributeProbabilities mapping are redundantly set1
[N-02]the function name could be changed to represent it much clearer1
[N-03]comment suggestion and function implementation difference1
[N-04]redundant code logic1
[N-05]the resetting should be skiped if the value is already set to the same value3

[L-01] add more validation checks for the input data in the createGameItem() function

since the function createGameItem() creates gemeItems that will be in the systme permanently it is a good practice to add some more validation checks for the input data, such as check if the finiteSupply is true, if yes,than the itemsRemaining should be more than zero, and the item price should be more than zero.

        if(itemPrice <= 0 ){ 
            revert();
        }
        if (finiteSupply) {
            if(itemsRemaining <= 0 ) {
                revert();
            }
        }

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/GameItems.sol#L208-L235

[l-02] the function call will fail if the maxId is more than one

In the getFighterPoints() function, the array points is initialized with a length of 1, which will cause the function call to fail if maxId is greater than 1. When the loop tries to access points[i] for i > 1, it will result in an out-of-bounds array access, which will cause a runtime error and revert the transaction.

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/MergingPool.sol#L205-L211

[L-03] zero nrn distribution

in the setRankedNrnDistribution() function If the rankedNrnDistribution for a round is set to zero (either intentionally or by mistake), players would receive no rewards for that round, which will be unexpected if they have accumulated points. Its best to add a check for zero before adding the distribution value for the round to the rankedNrnDistribution mapping.

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

[L-04] the function adjusts the unstake amount without reverting

in the function unstakeNRN there is a check where if the amount to be unstaked is more than the amountStaked it will set the amount to be unstaked to the amountStaked and then continue on unstaking the balance, the problem is it could potentially lead to confusion for users. The function allows a user to attempt to unstake more NRN tokens than they actually have staked, and instead of reverting with an error, it silently adjusts the unstake amount to the maximum available, without informing the user that their input was higher than the their stakedAmount. A user might not realize that they unstaked less than they intended.

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

[L-05] it is a best practice to use the safeErc20 library

while the protocol only interacts only with the $NRN token which implements the standard erc20, it is still best practice to use the safERC20 library while interacting with the erc20 functions respectivley (transfer, trnaferFrom, approve) functions.

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L376 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/GameItems.sol#L164 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L132 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L143 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L176 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L189 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L203 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L251 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L283 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L493 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/StakeAtRisk.sol#L100 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/StakeAtRisk.sol#L143

[L-06] no zero address checks

the functions below does not check for zero address while setting important protocol addresses

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L149 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L157 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L165 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/GameItems.sol#L119 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/GameItems.sol#L141 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/GameItems.sol#L187 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/MergingPool.sol#L100 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L95 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L103 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L111 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/Neuron.sol#L120 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L178 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L203 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L211 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/StakeAtRisk.sol#L67

[N-01] The values of the attributeProbabilities mapping are redundantly set

in the constructor of the AiArenaHelper contract the values for the attributeProbabilities are set twice which is redundant, and should be mitigated to set this value once.

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/AiArenaHelper.sol#L45 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/AiArenaHelper.sol#L49

[N-02] the function name could be changed to represent it much clearer

the function danToIndex() in the AiArenaHelper contract name could be changed to represent the function much clearer, and since the function does not take dna as input attribute, it could be misleading. The function name could be changed to calculateAttributeIndex() which represents the function's purpose much clearer.

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/AiArenaHelper.sol#L169

[N-03] comment suggestion and function implementation difference

the function comment suggests that all the arrays should be of the same length but the require statement doesn't check for the iconsTypes array's length. either the iconsTypes should be added to the checks or the comment should be updated.

https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L225 https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L243

[N-04] redundant code logic

in the setStakeAtRiskAddress() function an address is provided to the function to be set as the _stakeAtRiskInstance but the function first assigns the address to the _stakeAtRiskAddress and then assigns the _stakeAtRiskAddress to the _stakeAtRiskInstance. This is redundant. To fix this redundancy, you can remove the _stakeAtRiskAddress state variable and directly assign the passed address to the function to the _stakeAtRiskInstance while wrapping it in the StakeAtRisk.

the function that includes redundancy https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L192-L196 the redundant state variable https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L69 the place the redundant variable is used which could be replaced with the _stakeAtRiskInstance https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L493

[N-05] the resetting should be skiped if the value is already set

after calculating the stakingFactor the _calculatedStakingFactor mapping is set to true for the tokenId and roundId, this mapping should be checked if it already contains the intended value, then the resetting of the mapping to the same value should be skiped. This mapping is used in the stakeNrn and in the unstakeNrn it is possible that one of these functions for the same values are called many times, and since the _calculatedStakingFactor only stores boolean values it is ok to skip the resetting if the values is already the same as the one the contract wants to reset to. It is the same case with the hasUnstaked mapping in the unstakeNrn function which saves a boolean value. The first time the function is called it will set the value for the tokenId and the roundId to true and if called again this will be reset to the same value which is unnecessary and could be skiped by adding an if statement check.

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

#0 - raymondfam

2024-02-26T04:32:53Z

Adequate amount of L and NC entailed.

#1 - c4-pre-sort

2024-02-26T04:32:58Z

raymondfam marked the issue as sufficient quality report

#2 - HickupHH3

2024-03-05T10:49:33Z

#1604: R #1593: R #1616: L

#3 - c4-judge

2024-03-11T10:49:45Z

HickupHH3 marked the issue as grade-b

Analysis AIArena Contest

AIArena Protocol

Description Overview of the AiArena

AI Arena is a gaming platform that incorporates human with AI. Users go on to the AI Arena platform, acquire NFTs representing their AI models. The users then train the AI models with a process called imitation learning, which basically means that the AI model observes the user's movements and techniques and learns from it, the more the AI model is trained the better it becomes. These AI models are then submitted to ranked matches and compete in matches with other AI models trained by other users. The winner of the match is payed in $NRN which is the AI Arena native token, and this is the ultimate goal, train the best AI model, compete in ranked matches and win rewards.

In summary AIArena is:

  • A platform where users acquire NFTs representing their AI model.
  • The users train the AI models and submit them in battles and win rewards if the AI model wins.

The key contracts of the AI Arena for this audit are

  • FighterFarm.sol: This smart contract administers the creation, possession, and exchange of AI Arena Fighter NFTs. It facilitates the minting of new NFTs either from a merging pool or via the redemption of mint passes, in addition to managing ownership rights. Auditing this smart contract ensures that the creation and possession of the NFTs are implemented as intended.
  • RankedBattle.sol: This contract enables users to stake/unstake NRN tokens on/from fighters, monitor battle records, compute and dispense rewards according to battle results and staked quantities, and permits the claiming of accrued rewards. A thorough audit of this contract is needed to ensure the rewards distribution, staking and unstaking of the tokens is as it supposed to be.
  • Neuron.sol: The Neuron contract is an ERC20 token representing Neuron (NRN) tokens, used within the AI Arena's in-game economy. It includes roles for minting, spending, and staking tokens, and features an initial minting for the treasury and contributors. The contract allows for token claims from the treasury, and includes admin functionality for managing roles and setting up airdrops. Auditing this smart contract is critical to validate the ERC20 functionalities the contract implement.

Auditing the key contracts of the AI Arena Protocol is essential to ensure the security of the protocol. These contracts form the backbone of the AI Arena protocol. Focusing on these contracts first will provide a solid foundation for understanding the protocol's operation and how it manages creation of different fighters, calculating battle results, increasing and decreasing a user's rewards.

In auditing these contracts, I examined for possible security risks like reentrancy, access control vulnerabilities, and logic inconsistencies. Moreover, I rigorously tested the functions and roles outlined in these contracts to ensure they operate as intended.

System Overview

Scope

  • FighterFarm.sol: The FighterFarm contract manages the creation and ownership of AI Arena Fighter NFTs, allowing users to mint, claim, and reroll fighters with unique attributes. It includes mechanisms for staking fighters, transferring ownership, and interacting with the native ERC20 token (Neuron). The contract enforces a maximum number of fighters per address and rerolls per fighter, and integrates with external contracts for additional functionality. It adheres to the ERC721 standard for NFTs, with custom logic for fighter attributes and generation management.
  • RankedBattle.sol: The RankedBattle contract facilitates a staking-based reward system for the game where players can stake NRN tokens on fighters, participate in battles, and earn points based on battle outcomes. It tracks battle records, calculates staking factors, and distributes NRN rewards proportionally to the points earned. The contract includes mechanisms for staking, unstaking, claiming rewards, and managing rounds, with administrative functions to adjust game parameters and advance rounds. It interacts with other contracts like FighterFarm, VoltageManager, MergingPool, and StakeAtRisk to manage fighters, energy costs, point merging, and risked stakes, respectively.
  • Neuron.sol: The Neuron contract serves as an ERC20 token that embodies Neuron (NRN) tokens within the AI Arena's in-game ecosystem. It encompasses roles for minting, spending, and staking tokens, and initiates an initial minting process for the treasury and contributors. The contract enables token claims from the treasury and incorporates administrative features for role management and airdrop setup. Additionally, it facilitates mechanisms for minting new tokens up to a predefined maximum supply, burning tokens to decrease supply, and managing allowances for token transfers.
  • AiArenaHelper.sol: The AiArenaHelper contract is designed to manage the physical attributes of AI fighters in a the arena, with functionality to generate attributes based on fighter DNA and generation. It allows the owner to set and update probabilities and divisors for attribute generation, and includes ownership transfer capabilities. Special cases for certain icon types are handled, and dendroid fighters receive a fixed set of attributes. The contract also provides functions to add, delete, and retrieve attribute probabilities for different generations.
  • FighterOps.sol: The FighterOps library is designed for providing tools to manage Fighter NFTs with unique attributes. It includes functions to emit events upon fighter creation and to extract or view a fighter's detailed attributes. The library operates on Fighter structs, which encapsulate both physical and identifying characteristics of the NFTs.
  • GameItems.sol: The GameItems contract is an ERC1155-based smart contract for managing a collection of tradable and non-tradable game items with finite or infinite supply. It allows users to mint items by spending Neuron tokens (NRN), with daily minting allowances and the ability for admins to create new items and set their attributes. The contract includes functionality for transferring ownership, adjusting admin access, and burning items. It also enforces rules around item transferability and integrates with the Neuron contract for payment processing.
  • MergingPool.sol: The MergingPool contract allows users to earn new fighter NFTs by participating in a system where points are accumulated for fighters. Admins select winners each round based on external criteria, and winners can claim their NFT rewards. The contract tracks points, winners, and claims, and includes functions for ownership transfer and admin management. It integrates with the FighterFarm contract to mint NFTs for winners.
  • StakeAtRisk.sol: The StakeAtRisk contract manages the stakes (NRNs) that are at risk of being lost by fighters during ranked battles in a competition round. It allows the associated RankedBattle contract to update stakes at risk, reclaim stakes for winners, and set new rounds, during which it sweeps lost stakes to a treasury. Stake information is tracked per round and per fighter, with events emitted for key actions. Access to critical functions is restricted to the RankedBattle contract.
  • VoltageManager.sol: The VoltageManager contract manages a voltage system for a the game, allowing players to replenish their voltage using in-game items and spend voltage for certain actions. It includes ownership and admin controls to manage permissions. Voltage can be automatically replenished daily, and the contract interacts with the GameItems contract to burn items used for replenishment. Events are emitted for voltage changes to track player balances.

Privileged Roles

There are several important roles in the protocol.

  • Owner: This role is essential in every contract. It grants administrative permissions and control over critical functions. The holder of this role can manage other roles, configure contract parameters, and perform various administrative tasks.
  • Admin: Admins have elevated privileges that allow them to adjust game settings. Admins are designated by the owner.
  • GameServer: GameServer is an important role in the RankedBattle contract that updates the battle records for a player.
  • MINTER_ROLE - SPENDER_ROLE - STAKER_ROLE: These are important roles that are set by the contract owner, that permits the minting,spending and staking of tokens. These roles are mainly present in the Neuron contract.

Approach Taken-in Evaluating The AI Arena Protocol

Consequently, I conducted an analysis and audit of the subject protocol through the following steps:

  1. Core Protocol Contract Overview:

    My primary focus was to comprehensively comprehend the code base and offer recommendations for enhancing its functionality. The central objective was to examine the critical contracts and their interplay within the AI Arena Protocol.

    I started with the following contracts, which play crucial roles in the AI Arena protocol:

    Main Contracts I Looked At this phase

    FighterFarm.sol RankedBattle.sol Neuron.sol MergingPool.sol StakeAtRisk.sol

I started my analysis by examining the FighterFarm.sol contract which plays crucial role in the AI Arena protocol and manages the creation and ownership of the AI Arena fighter NFTs. After that I delved into the RankedBattle.sol contract which has the logic for the staking, unstaking and calculating the battle results. the Neuron.sol, MergingPool.sol and the stakeAtRisk.sol, were also thoroughly examined to ensure the secure flow of logic within the contracts.

  1. Documentation Review: Afterward, I proceeded to Review these Docs for a more comprehensive and technical understanding of the AI Arena protocol.

  2. Compiling code and running provided tests: I compiled the code and ran the provided tests.

  3. Manual Code Review: In this phase, I conducted a line-by-line analysis.

    • Line by Line Analysis: Carefully observed the contract's intended functionality in contrast with its actual behavior, analyzing it line by line.

Codebase Quality

Overall, I consider the quality of the AI Arena protocol codebase to be Good. The code appears to be mature and well-developed. The implementation of the code adheres to various standards properly. Details are explained below:

Codebase Quality CategoriesComments
Code Maintainability and ReliabilityThe codebase demonstrates moderate maintainability with well-structured functions and comments, promoting readability. It exhibits reliability through defensive programming practices, parameter validation, and handling external calls safely. The use of internal functions for related operations enhances code modularity, reducing duplication. Libraries improve reliability by minimizing arithmetic errors. Adherence to standard conventions and practices contributes to overall code quality. However, full reliability depends on external contract implementations like openzeppelin.
Code CommentsThe contracts include comments that describes the purpose and operations of various sections, aiding developers and auditors in comprehending and managing the code. These comments offer insights into methods, variables, and the overarching architecture of the contracts. For instance, the "RankedBattle" contract's code comments furnish crucial details. They declare the title and the objective of the contract, explain all the variables and the functions within the contract.
DocumentationThe documentation of the AI Arena protocol is quite comprehensive and detailed, providing a solid overview of how AI Arena is structured and how its various aspects function. However, what the documentation does not provide is a sub part in the docs that explain each AI Arena smart contract in details, explaining the objectives of the contracts, explaining its methods, specific diagrams representing the internal interactions between the contracts, and how users interact with these contracts. Explaining the contracts with diagrams is a way to gain a deeper understanding of how different contracts interact and the functions they implement. With considerable enthusiasm. I am confident that these diagrams will bring significant value to the protocol as they can be seamlessly integrated into the existing documentation, enriching it and providing a more comprehensive and detailed understanding for users, developers and auditors.
TestingThe overall line coverage percentage provided by tests is 90% which should be aimed at 100%.
Code Structure and FormattingThe codebase contracts are well-structured and formatted. It inherits from OpenZeppelin governance, and token standards in some contracts. The constructors initializes the contract with parameters and specified roles, contracts override functions, each component is provided with accompanying comments explaining their purpose. The code is organized, making it readable and maintainable.
ErrorsCustom errors should be used instead of require statements with string errors. Custom errors are easier to re-use and maintain.

Systemic & Centralization Risks

Systemic Risks:

  1. In the FighterFarm contract, if rerollCost is too low, it may cause inflation by devaluing the Neuron token due to excessive rerolling. Conversely, if MAX_FIGHTERS_ALLOWED is too high, it could lead to player hoarding and market saturation, giving an unfair edge to certain players and potentially causing deflation by reducing token circulation. Both scenarios can disrupt the game's economy and player experience.
  2. The FighterFarm contract allows for the incrementing of fighter generations and the number of rerolls allowed. If this functionality is mismanaged or exploited, it could lead to a loss of rarity and uniqueness of fighters, affecting their value and the overall balance of the game.
  3. The FighterFarm contract relies on external contracts such as the AiArenaHelper, AAMintPass, and Neuron token. If any of these contracts have vulnerabilities, are manipulated, or fail, it could impact the FighterFarm contract's operations, potentially leading to loss of funds or NFTs.
  4. In the RankedBattle contract the rankedNrnDistribution mapping determines the NRN token distribution for each round. If admins can arbitrarily change the distribution amount without checks or community consensus, it could lead to unfair reward allocation or inflationary pressure on the NRN token.
  5. The contract RankedBattle relies on a designated _gameServerAddress to report battle outcomes. If this address is compromised or behaves maliciously, it could falsify battle records, leading to incorrect point and reward distribution.
  6. The RankedBattle contract's economic parameters (e.g., bpsLostPerLoss, VOLTAGE_COST) need to be carefully balanced. If they are not set correctly, they could lead to an imbalanced game economy, where either too much risk or too little risk is associated with battles, affecting player engagement and the value of the NRN token.
  7. In the Neuron contract while there is a MAX_SUPPLY limit, the contract allows for additional minting of tokens up to this cap. If the minter role is abused or if too many tokens are minted too quickly, it could lead to inflationary pressure on the NRN token.
  8. In the MergingPool contract the pickWinner function allows admins to select winners for each round. If this power is abused or if the admin selection process lacks transparency, it could lead to unfairness and loss of trust among participants.

Centralization Risks:

  1. The contracts has an owner address (_ownerAddress) with exclusive rights to perform critical actions such as transferring ownership, incrementing fighter generations, adding staker addresses, setting contract instances for helper contracts, and updating the merging pool address, and many other actions in different contracts. If this address is an EOA it will be a centralized entity controlling the system.
  2. In the RankedBattle contract admins can change critical game settings, such as the NRN distribution for each round and the basis points lost per loss. They can also advance the game to a new round. If admins act maliciously or carelessly, they could disrupt the game's economy and fairness, and is a centralization risk.
  3. The contract RankedBattle relies on the _gameServerAddress which is a centralized entity to report battle outcomes. If compromised, it could lead to incorrect updates to battle records and points.
  4. In the MergingPool contract there are no mechanisms within the contract for decentralized decision-making or community consensus. Changes to critical parameters and the selection of winners are at the discretion of the owner and admins.

Properly managing these risks and implementing best practices in security and decentralization will contribute to the sustainability and long-term success of the AI Arena protocol.

Conclusion

The AI Arena protocol showcases a robust and innovative gaming platform that effectively integrates AI with NFTs, offering users a unique experience in training and battling AI models. The key contracts, including FighterFarm.sol, RankedBattle.sol, and Neuron.sol, are well-structured and adhere to industry standards, ensuring a secure and functional ecosystem. However, the protocol faces systemic risks such as potential token inflation, alongside centralization risks stemming from the significant control wielded by owner and admin roles. To enhance the protocol's resilience and trustworthiness, it is recommended to implement decentralized governance mechanisms, introduce additional security practices, and consider further audits and bug bounty programs. These measures will help mitigate risks, optimize the gaming economy, and uphold the protocol's integrity.

Time spent:

65 hours

Time spent:

65 hours

#0 - c4-pre-sort

2024-02-25T20:32:23Z

raymondfam marked the issue as sufficient quality report

#1 - c4-judge

2024-03-19T08:14:31Z

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