Skip to content

Commit b7e4b93

Browse files
committed
Fix: reentrancy-benign issue
1 parent c386218 commit b7e4b93

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

src/EvolvingGameCharacter.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ contract EvolvingGameCharacter is ERC721, Ownable {
3131
function mint(address to) public {
3232
require(_nextTokenId <= MAX_SUPPLY, "Max supply reached");
3333
uint256 tokenId = _nextTokenId++;
34-
_safeMint(to, tokenId);
3534

3635
tokenAttributes[tokenId] = CharacterAttributes({
3736
level: 1, strength: _pseudoRandom(10, 20, tokenId), health: _pseudoRandom(80, 120, tokenId), experience: 0
3837
});
38+
39+
_safeMint(to, tokenId);
3940
}
4041

4142
function train(uint256 tokenId) public {
@@ -148,10 +149,7 @@ contract EvolvingGameCharacter is ERC721, Ownable {
148149
// slither-disable-start weak-prng
149150
function _pseudoRandom(uint256 min, uint256 max, uint256 seed) private view returns (uint256) {
150151
// Using block.prevrandao and disabling the warning for this learning project
151-
return min + (
152-
uint256(keccak256(abi.encodePacked(block.prevrandao, msg.sender, seed)))
153-
% (max - min + 1)
154-
);
152+
return min + (uint256(keccak256(abi.encodePacked(block.prevrandao, msg.sender, seed))) % (max - min + 1));
155153
}
156154
// slither-disable-end weak-prng
157155
}

src/GameCharacter.sol

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ contract GameCharacter is ERC721, Ownable {
5959
uint256 tokenId = _nextTokenId;
6060
_nextTokenId++;
6161

62-
_safeMint(to, tokenId);
63-
6462
tokenAttributes[tokenId] = CharacterAttributes({
6563
level: 1, strength: _pseudoRandom(10, 20, tokenId), health: _pseudoRandom(50, 100, tokenId), experience: 0
6664
});
6765

66+
_safeMint(to, tokenId);
67+
6868
emit CharacterMinted(
6969
to,
7070
tokenId,
@@ -263,10 +263,8 @@ contract GameCharacter is ERC721, Ownable {
263263
function _pseudoRandom(uint256 min, uint256 max, uint256 seed) private view returns (uint256) {
264264
// We use block.prevrandao (The Beacon Chain randomness)
265265
// We removed block.timestamp to reduce miner influence vectors
266-
uint256 randomHash = uint256(
267-
keccak256(abi.encodePacked(block.prevrandao, msg.sender, seed))
268-
);
269-
266+
uint256 randomHash = uint256(keccak256(abi.encodePacked(block.prevrandao, msg.sender, seed)));
267+
270268
return min + (randomHash % (max - min + 1));
271269
}
272270
// slither-disable-end weak-prng

0 commit comments

Comments
 (0)