Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Valid

Defender has always more chances to win than expected

Description

RapBattle:_battle does the fight and calculates the victory. This is calculated with the sum of both rappers' points, and a random number is chosen below this maximum. If the score is lower than the defender's points, the defender wins; otherwise, the challenger wins. The problem is that the condition checks if the random number is lower OR EQUAL to the defender points.

Concrete example:

  • Defender and attacker have both 50 points.

  • totalBattleSkill = 100.

  • Defender win if random is between 0 and 50 : 51/100 chances.

  • Challenger win if random is between 51 and 99 : 49/100 chances.

function _battle(uint256 _tokenId, uint256 _credBet) internal {
...
uint256 totalBattleSkill = defenderRapperSkill + challengerRapperSkill;
uint256 totalPrize = defenderBet + _credBet;
uint256 random = uint256(
keccak256(
abi.encodePacked(block.timestamp, block.prevrandao, msg.sender)
)
) % totalBattleSkill;
...
// If random <= defenderRapperSkill -> defenderRapperSkill wins, otherwise they lose
@> if (random <= defenderRapperSkill) {
...
}
...
}

Risk

Likelyhood: High

  • Every battle.

Impact: High

  • Always more chance to win than expected for defenders.

Recommended Mitigation

Correct the check to be strictly lower.

function _battle(uint256 _tokenId, uint256 _credBet) internal {
...
uint256 totalBattleSkill = defenderRapperSkill + challengerRapperSkill;
uint256 totalPrize = defenderBet + _credBet;
uint256 random = uint256(
keccak256(
abi.encodePacked(block.timestamp, block.prevrandao, msg.sender)
)
) % totalBattleSkill;
...
// If random <= defenderRapperSkill -> defenderRapperSkill wins, otherwise they lose
- if (random <= defenderRapperSkill) {
+ if (random < defenderRapperSkill) {
...
}
...
}
Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Design choice
Assigned finding tags:

Defender's advantage

n0kto Submitter
over 1 year ago
inallhonesty Lead Judge
over 1 year ago
inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

Defender's advantage

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.