Skip to content

Commit

Permalink
feat: generalizing adder example (#186)
Browse files Browse the repository at this point in the history
* feat: generalizing adder example

* fix: renaming to adhere to the concepts

* Use forge-deploy-lib

---------

Co-authored-by: Guilherme Dantas <[email protected]>
  • Loading branch information
claudioantonio and guidanoli authored Nov 6, 2024
1 parent e3525db commit e0ffaa6
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 104 deletions.
2 changes: 1 addition & 1 deletion tests/bounties/src/adder/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
broadcast
cache
deployments.json
deployment.json
exploited
lib
out
6 changes: 3 additions & 3 deletions tests/bounties/src/adder/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[profile.deploy]
fs_permissions = [
{ access = "read-write", path = "./deployments.json" },
{ access = "read-write", path = "./deployment.json" },
]

[profile.exploit]
fs_permissions = [
{ access = "read", path = "./deployments.json" },
{ access = "read", path = "./deployment.json" },
]

[profile.test]
fs_permissions = [
{ access = "read", path = "./deployments.json" },
{ access = "read", path = "./deployment.json" },
{ access = "read-write", path = "./exploited" },
]
16 changes: 16 additions & 0 deletions tests/bounties/src/adder/script/Assertion.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.27;

import {DeploymentReaderScript} from "forge-deploy-lib/DeploymentScript.sol";

import {IAdder} from "src/IAdder.sol";

contract AssertionScript is DeploymentReaderScript {
function run() external {
loadDeployment();
IAdder adder = IAdder(getContract("Adder"));
if (adder.number() >= 1) return;
vm.writeFile("./exploited", "");
}
}
29 changes: 0 additions & 29 deletions tests/bounties/src/adder/script/LibDeployments.sol

This file was deleted.

17 changes: 0 additions & 17 deletions tests/bounties/src/adder/script/Test.s.sol

This file was deleted.

22 changes: 0 additions & 22 deletions tests/bounties/src/adder/script/safe/Deploy.s.sol

This file was deleted.

17 changes: 17 additions & 0 deletions tests/bounties/src/adder/script/safe/Setup.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.27;

import {DeploymentWriterScript} from "forge-deploy-lib/DeploymentScript.sol";

import {SafeAdder} from "src/safe/Adder.sol";

contract SetupScript is DeploymentWriterScript {
function run() external {
vm.startBroadcast();
addContract("Adder", address(new SafeAdder()));
vm.stopBroadcast();

storeDeployment();
}
}
22 changes: 0 additions & 22 deletions tests/bounties/src/adder/script/unsafe/Deploy.s.sol

This file was deleted.

13 changes: 5 additions & 8 deletions tests/bounties/src/adder/script/unsafe/Exploit.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

pragma solidity ^0.8.27;

import {Script} from "forge-std/Script.sol";
import {Vm} from "forge-std/Vm.sol";
import {IAdder} from "src/IAdder.sol";
import {Deployments, LibDeployments} from "script/LibDeployments.sol";
import {DeploymentReaderScript} from "forge-deploy-lib/DeploymentScript.sol";

contract ExploitScript is Script {
using LibDeployments for Vm;
import {IAdder} from "src/IAdder.sol";

contract ExploitScript is DeploymentReaderScript {
function run() external {
Deployments memory deployments = vm.loadDeployments();
IAdder adder = deployments.adder;
loadDeployment();
IAdder adder = IAdder(getContract("Adder"));
uint256 number = adder.number();
uint256 increment;
unchecked {
Expand Down
17 changes: 17 additions & 0 deletions tests/bounties/src/adder/script/unsafe/Setup.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.27;

import {DeploymentWriterScript} from "forge-deploy-lib/DeploymentScript.sol";

import {UnsafeAdder} from "src/unsafe/Adder.sol";

contract SetupScript is DeploymentWriterScript {
function run() external {
vm.startBroadcast();
addContract("Adder", address(new UnsafeAdder()));
vm.stopBroadcast();

storeDeployment();
}
}
4 changes: 2 additions & 2 deletions tests/bounties/src/adder/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ run_forge_script_broadcast() {
}

>&2 echo "Deploying contracts..."
FOUNDRY_PROFILE=deploy run_forge_script_broadcast script/Deploy.s.sol:DeployScript
FOUNDRY_PROFILE=deploy run_forge_script_broadcast script/Setup.s.sol:SetupScript

>&2 echo "Running exploit..."
FOUNDRY_PROFILE=exploit run_forge_script_broadcast script/Exploit.s.sol:ExploitScript

>&2 echo "Testing contracts..."
FOUNDRY_PROFILE=test run_forge_script script/Test.s.sol:TestScript
FOUNDRY_PROFILE=test run_forge_script script/Assertion.s.sol:AssertionScript

if [ -f exploited ]
then
Expand Down

0 comments on commit e0ffaa6

Please sign in to comment.