|
| 1 | +import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; |
| 2 | +import chai, { expect } from "chai"; |
| 3 | +import chaiAsPromised from "chai-as-promised"; |
| 4 | +import { ethers } from "hardhat"; |
| 5 | + |
| 6 | +import { BlockGasLimitVulnerable, BlockGasLimitAttacker, RejectEtherAttacker } from "../typechain-types"; |
| 7 | + |
| 8 | +chai.use(chaiAsPromised); |
| 9 | + |
| 10 | +describe("BlockGasLimit", () => { |
| 11 | + let deployer: SignerWithAddress; |
| 12 | + let attacker: SignerWithAddress; |
| 13 | + let blockGasLimitVulnerable: BlockGasLimitVulnerable; |
| 14 | + let blockGasLimitAttacker: BlockGasLimitAttacker; |
| 15 | + let rejectEtherAttacker: RejectEtherAttacker; |
| 16 | + |
| 17 | + before(async () => { |
| 18 | + [deployer, attacker] = await ethers.getSigners(); |
| 19 | + }); |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + const BlockGasLimitVulnerable = await ethers.getContractFactory("BlockGasLimitVulnerable"); |
| 23 | + blockGasLimitVulnerable = await BlockGasLimitVulnerable.deploy(); |
| 24 | + await blockGasLimitVulnerable.deployed(); |
| 25 | + |
| 26 | + const BlockGasLimitAttacker = await ethers.getContractFactory("BlockGasLimitAttacker"); |
| 27 | + blockGasLimitAttacker = await BlockGasLimitAttacker.deploy(blockGasLimitVulnerable.address); |
| 28 | + await blockGasLimitAttacker.deployed(); |
| 29 | + |
| 30 | + const RejectEtherAttacker = await ethers.getContractFactory("RejectEtherAttacker"); |
| 31 | + rejectEtherAttacker = await RejectEtherAttacker.deploy(blockGasLimitVulnerable.address); |
| 32 | + await rejectEtherAttacker.deployed(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should not be vulnerable to RejectEtherAttacker DoS", async () => { |
| 36 | + await blockGasLimitVulnerable.bid({ value: ethers.utils.parseEther("1") }); |
| 37 | + console.log("Balance after first bid: ", ethers.utils.formatEther(await ethers.provider.getBalance(blockGasLimitVulnerable.address))); |
| 38 | + |
| 39 | + console.log("Attacker is attacking..."); |
| 40 | + await rejectEtherAttacker.connect(attacker).attack({ value: ethers.utils.parseEther("2") }); |
| 41 | + console.log("Balance after attack bid: ", ethers.utils.formatEther(await ethers.provider.getBalance(blockGasLimitVulnerable.address))); |
| 42 | + |
| 43 | + await blockGasLimitVulnerable.bid({ value: ethers.utils.parseEther("3") }); |
| 44 | + expect(await blockGasLimitVulnerable.highestBid()).to.equal(ethers.utils.parseEther("3")); |
| 45 | + expect(await blockGasLimitVulnerable.highestBidder()).to.equal(deployer.address); |
| 46 | + |
| 47 | + console.log("Balance after third bid: ", ethers.utils.formatEther(await ethers.provider.getBalance(blockGasLimitVulnerable.address))); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should be vulnerable to BlockGasLimitAttacker DoS", async () => { |
| 51 | + await blockGasLimitVulnerable.bid({ value: ethers.utils.parseEther("1") }); |
| 52 | + |
| 53 | + console.log("Attacker is attacking..."); |
| 54 | + await blockGasLimitAttacker.connect(attacker).attack({ value: ethers.utils.parseEther("2") }); |
| 55 | + |
| 56 | + try { |
| 57 | + await blockGasLimitVulnerable.bid({ value: ethers.utils.parseEther("3"), gasLimit: 100000 }); |
| 58 | + } catch (error: any) { |
| 59 | + console.log("..."); |
| 60 | + console.log("Bid transaction reverted with reason: ", error.message); |
| 61 | + } |
| 62 | + }); |
| 63 | +}); |
0 commit comments