Skip to content

Commit 632d653

Browse files
committed
feat: impl pixel bid cooldown to prevent DoS risk
1 parent b53b3d1 commit 632d653

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/EthPixelWar.sol

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ contract EthPixelWar is Ownable {
1818
bool public pixelWarIsActive;
1919
mapping(uint16 => Pixel) public grid;
2020
mapping(address => uint256) public pendingWithdrawals;
21+
mapping(uint16 => uint256) public lastBidTime;
22+
uint256 public constant BID_COOLDOWN = 1 minutes;
2123

2224
event PixelBid(uint16 pixelId, address bidder, uint256 bidAmount);
2325
event ColorUpdated(uint16 pixelId, uint8 r, uint8 g, uint8 b);
@@ -51,13 +53,26 @@ contract EthPixelWar is Ownable {
5153
_;
5254
}
5355

54-
function bid(uint16 pixelId) public payable validPixelId(pixelId) validBid(pixelId) onlyActivePixelWar {
56+
modifier bidCooldown(uint16 pixelId) {
57+
require(block.timestamp >= lastBidTime[pixelId] + BID_COOLDOWN, "Bid cooldown in effect");
58+
_;
59+
}
60+
61+
function bid(uint16 pixelId)
62+
public
63+
payable
64+
validPixelId(pixelId)
65+
validBid(pixelId)
66+
onlyActivePixelWar
67+
bidCooldown(pixelId)
68+
{
5569
Pixel storage pixel = grid[pixelId];
5670
if (pixel.owner != address(0)) {
5771
pendingWithdrawals[pixel.owner] += pixel.highestBid;
5872
}
5973
pixel.owner = msg.sender;
6074
pixel.highestBid = msg.value;
75+
lastBidTime[pixelId] = block.timestamp;
6176

6277
emit PixelBid(pixelId, msg.sender, msg.value);
6378
}
@@ -66,7 +81,7 @@ contract EthPixelWar is Ownable {
6681
uint256 amount = pendingWithdrawals[msg.sender];
6782
require(amount > 0, "No funds to withdraw");
6883
pendingWithdrawals[msg.sender] = 0;
69-
(bool success, ) = payable(msg.sender).call{value: amount}("");
84+
(bool success,) = payable(msg.sender).call{value: amount}("");
7085
require(success, "Withdrawal failed");
7186
}
7287

0 commit comments

Comments
 (0)