Denial of Service (DoS) Attacks
Introduction to Denial of Service (DoS) Attacks
How DoS Attacks Occur
Example Scenario: Crowdfunding Contract
solidityCopy codepragma solidity ^0.8.0;
contract Crowdfunding {
address public owner;
uint public endBlock;
uint public goalAmount;
mapping(address => uint) public contributions;
bool public fundingSuccessful;
bool public refundsIssued;
constructor(uint _duration, uint _goalAmount) {
owner = msg.sender;
endBlock = block.number + _duration;
goalAmount = _goalAmount;
}
function contribute() public payable {
require(block.number < endBlock, "The fundraising period has ended.");
require(msg.value > 0, "Contribution must be greater than 0.");
contributions[msg.sender] += msg.value;
}
function finalize() public {
require(msg.sender == owner, "Only the owner can finalize.");
require(block.number >= endBlock, "The fundraising period has not ended.");
if (address(this).balance >= goalAmount) {
fundingSuccessful = true;
payable(owner).transfer(address(this).balance);
} else {
refundsIssued = true;
}
}
function refund() public {
require(refundsIssued, "Refunds not available.");
uint amount = contributions[msg.sender];
require(amount > 0, "No contributions found.");
payable(msg.sender).transfer(amount);
contributions[msg.sender] = 0;
}
}Prevention Strategies for DoS Attacks
Limiting Gas Consumption
Validating External Calls
Using Pull Payments for Refunds
Comprehensive Testing and Audits
Conclusion
Last updated