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
Rank: 41/283
Findings: 1
Award: $166.17
๐ Selected for report: 0
๐ Solo Findings: 0
๐ Selected for report: 0xSmartContract
Also found by: 0xAsen, 0xDetermination, 0xStriker, 0xblack_bird, 0xbrett8571, 0xepley, 0xweb3boy, 14si2o_Flint, Bauchibred, DarkTower, JayShreeRAM, JcFichtner, K42, McToady, Myd, PoeAudits, Rolezn, SAQ, ZanyBonzy, aariiif, albahaca, ansa-zanjbeel, cheatc0d3, clara, cudo, dimulski, fouzantanveer, foxb868, hassanshakeel13, hunter_w3b, kaveyjoe, klau5, peanuts, pipidu83, popeye, rspadi, scokaf, shaflow2, tala7985, wahedtalash77, yongskiws
166.1676 USDC - $166.17
I reviewed the core smart contracts comprising the AI Arena platform, including FighterFarm, RankedBattle, Neuron token, and supporting contracts like MergingPool and GameItems.
My analysis looked at:
Architecture
AI Arena follows a modular architecture with well-scoped contracts for NFTs, staking, battles, etc. This provides separation of concerns.
There is still strong centralization around owner addresses that poses a risk. Suggest defining more granular roles using AccessControl as opposed to reliance on _ownerAddress
.
Overall the architecture sets a good foundation, but removing single points of failure via decentralized governance will strengthen integrity over time.
Code Quality
I found extensive natspec documentation in modules, following Solidity coding standards, and clear logical flow in code.
Adding unit test coverage would significantly improve robustness against edge cases. Formal verification would also mathematically prove properties.
But logical commenting allows the modules to be understood at a glance.
Security Analysis
The contracts utilize access controls in functions via checks on msg.sender
, but privilege separation could be stronger. Owner accounts have far reaching power that poses centralization risks if keys are compromised.
No safeguards against threats like frontrunning or integer overflows are apparent. Additional validation on untrusted inputs would improve safety.
There are also no revocation or pause mechanisms if vulnerabilities emerge. So while the code avoids major issues like reentrancy, risk surfaces remain.
The FighterFarm
, RankedBattle
, and other core contracts designate an _ownerAddress
that has sweeping control over crucial functions like minting NFTs, updating match results, distributing rewards, etc.
For example, in FighterFarm.sol
the owner can: https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/FighterFarm.sol#L129-L134
function incrementGeneration(uint8 fighterType) external returns (uint8) { require(msg.sender == _ownerAddress); // increment generation return generation[fighterType]; } function addMinter(address newMinterAddress) external { require(msg.sender == _ownerAddress); // add new minter }
If the key controlling the owner address were to be compromised, the attacker could exploit these privileged functions to:
They can essentially extract full value out of the contracts since the owner role overrides all other privilege restrictions.
This is quite likely to happen if measures are not taken to restrict access to the owner keys. Private keys can be lost, stolen, socially engineered, etc if not properly secured. Keys held on internet-connected devices have higher risks.
An attacker would first need to gain access to the private key controlling the owner address, whether via hacking, physical access to devices, or social engineering tactics. Once they have the key, they can easily trigger the privileged functions through contract calls.
Preventative Techniques
The broad owner privileges in these contracts violate the principle of least authority by allowing a single compromised key to exploit critical functions. Implementing controls around the owner address is crucial.
In the RankedBattle.sol
contract, the _gameServerAddress
is authorized to call sensitive functions like https://github.com/code-423n4/2024-02-ai-arena/blob/cd1a0e6d1b40168657d1aaee8223dc050e15f8cc/src/RankedBattle.sol#L322-L349
function updateBattleRecord( uint256 tokenId, uint8 battleResult, uint256 eloFactor, bool initiatorBool ) external { require(msg.sender == _gameServerAddress); // update match records // calculate rewards // handle stake }
This allows the game server to fully control match results, ELO changes, and reward distributions without checks.
Since match outcomes determine the earning potential of NFTs, inconsistent or tampered results from a centralized authority presents risks:
This hurts the sustainability of the play-to-earn model.
Such abuse is likely given the lack of checks and balances on the centralized control. There are no visibility measures that would deter malicious admin behaviors.
The game server admins would simply need to pass biased outputs for match results, ranking changes, and rewards distributions into the updateBattleRecord
function to unfairly benefit addresses they select.
Preventative Techniques
Centralized control without mitigating controls introduces unacceptable manipulation risks destabilizing the in-game economy. Adding layers of transparency and accountability for such privileged roles would align incentives through community oversight.
Square root curves on staking reduce runaway leader dominance
Points decay over time to require ongoing participation
Relative rankings determine distributions rather than absolute thresholds
Voltage costs rate limit pace of matches to avoid exploitation
Cyclical rounds reset stakes to rebalance accessibility
Risks
No mechanisms to handle rampant pooling/collusion across factions
No protections against whale-dominated saturation staking
Chance for complex smart contract interactions leading to edge cases
Subjective administrative overrides of rankings/eligibility
Potential Enhancements
Implement token bonding curves to help stabilize valuations
Introduce randomness and raffles as alternative reward channels
Cap max percentage share of distributions per address
Formal verification of contract logic flows
The dual token model provides levers to balance scalability, while still encouraging participation with Achievement-based distribution qualifiers.
Adding decentralized governance for parameter changes would help sustain integrity over time. But the core design seems economically sound, with a few tweaks needed around preventing systemic gaming.
I also performed an analysis of the AI Arena's smart contracts to assess protections against common vulnerabilities, and adherence to best practices:
Reentrancy
โ Mitigated
Integer Overflows
โ๏ธ Improvements needed
Frontrunning
โ๏ธ Risks possible
Additional Analysis
โ Follows many best practices
Suggestions
There are moderate protections against major risks like reentrancy, but gaps remain around integer bounds, frontrunning protections, and validating inputs that open up vectors for potential exploitation.
Adding security-focused code auditing, static analysis scanning, and formal verification would significantly improve the contracts' reliability against threats.
Based on my analysis, the AI Arena contracts do make efforts to apply access control protections, but there are still gaps that provide opportunities for more privilege risks:
Positives
Gaps
Potential Enhancements
There are good starting points in applying access restrictions like checks on msg.sender and use of admin mappings. However, over-reliance on owner addresses and game servers with no takeback mechanisms still pose too much risk.
A revamped access controls architecture could help minimize attack surfaces by more granularly restricting the capabilities of any single account compromise using whitelists, multi-sig, revocations, and enhanced ratelimiting.
The dual token model aligns incentives towards participation while allowing leveraging of monetary policy variables to moderate volatility. Cyclical rounds with decaying rewards promote ongoing engagement.
Risks around insider manipulation or whale collusions should be monitored, but game theory checks seem reasonably balanced. Randomness could be improved via Chainlink VRF.
So at a high level, the system seems structured to align incentives and drive recurring participation.
22 hours
#0 - c4-pre-sort
2024-02-25T20:23:15Z
raymondfam marked the issue as sufficient quality report
#1 - c4-judge
2024-03-19T08:24:07Z
HickupHH3 marked the issue as grade-a