Skip to content

Commit 314eded

Browse files
authored
fix(forge): do not set balance as apparent value in delegate prank (#10304)
1 parent f0e24fb commit 314eded

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

crates/cheatcodes/src/inspector.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use proptest::test_runner::{RngAlgorithm, TestRng, TestRunner};
4646
use rand::Rng;
4747
use revm::{
4848
interpreter::{
49-
opcode as op, CallInputs, CallOutcome, CallScheme, CallValue, CreateInputs, CreateOutcome,
49+
opcode as op, CallInputs, CallOutcome, CallScheme, CreateInputs, CreateOutcome,
5050
EOFCreateInputs, EOFCreateKind, Gas, InstructionResult, Interpreter, InterpreterAction,
5151
InterpreterResult,
5252
},
@@ -1047,8 +1047,6 @@ where {
10471047
if let CallScheme::DelegateCall | CallScheme::ExtDelegateCall = call.scheme {
10481048
call.target_address = prank.new_caller;
10491049
call.caller = prank.new_caller;
1050-
let acc = ecx.journaled_state.account(prank.new_caller);
1051-
call.value = CallValue::Apparent(acc.info.balance);
10521050
if let Some(new_origin) = prank.new_origin {
10531051
ecx.env.tx.caller = new_origin;
10541052
}

crates/forge/tests/it/repros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,6 @@ test_repro!(9643);
398398

399399
// https://github.com/foundry-rs/foundry/issues/7238
400400
test_repro!(7238);
401+
402+
// https://github.com/foundry-rs/foundry/issues/10302
403+
test_repro!(10302);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pragma solidity ^0.8.18;
3+
4+
import "ds-test/test.sol";
5+
import "cheats/Vm.sol";
6+
7+
contract A {
8+
function foo() public pure returns (bool) {
9+
return true;
10+
}
11+
}
12+
13+
contract Issue10302Test is DSTest {
14+
Vm constant vm = Vm(HEVM_ADDRESS);
15+
16+
function testDelegateFails() external {
17+
vm.createSelectFork("sepolia");
18+
A a = new A();
19+
vm.startPrank(0x0fe884546476dDd290eC46318785046ef68a0BA9, true);
20+
(bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
21+
vm.stopPrank();
22+
require(success, "Delegate call should succeed");
23+
}
24+
25+
function testDelegatePassesWhenBalanceSetToZero() external {
26+
vm.createSelectFork("sepolia");
27+
A a = new A();
28+
vm.startPrank(0x0fe884546476dDd290eC46318785046ef68a0BA9, true);
29+
vm.deal(0x0fe884546476dDd290eC46318785046ef68a0BA9, 0 ether);
30+
(bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
31+
vm.stopPrank();
32+
require(success, "Delegate call should succeed");
33+
}
34+
35+
function testDelegateCallSucceeds() external {
36+
vm.createSelectFork("sepolia");
37+
A a = new A();
38+
vm.startPrank(0xd363339eE47775888Df411A163c586a8BdEA9dbf, true);
39+
(bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
40+
vm.stopPrank();
41+
require(success, "Delegate call should succeed");
42+
}
43+
44+
function testDelegateFailsWhenBalanceGtZero() external {
45+
vm.createSelectFork("sepolia");
46+
A a = new A();
47+
vm.startPrank(0xd363339eE47775888Df411A163c586a8BdEA9dbf, true);
48+
vm.deal(0xd363339eE47775888Df411A163c586a8BdEA9dbf, 1 ether);
49+
(bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
50+
vm.stopPrank();
51+
require(success, "Delegate call should succeed");
52+
}
53+
}

0 commit comments

Comments
 (0)