Skip to content

Commit 3accf6b

Browse files
authored
fix(forge): apply startPrank with delegate only for top calls (#10069)
1 parent f45781d commit 3accf6b

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

crates/cheatcodes/src/inspector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,8 @@ where {
10131013
// Apply our prank
10141014
if let Some(prank) = &self.get_prank(curr_depth) {
10151015
// Apply delegate call, `call.caller`` will not equal `prank.prank_caller`
1016-
if let CallScheme::DelegateCall | CallScheme::ExtDelegateCall = call.scheme {
1017-
if prank.delegate_call {
1016+
if prank.delegate_call && curr_depth == prank.depth {
1017+
if let CallScheme::DelegateCall | CallScheme::ExtDelegateCall = call.scheme {
10181018
call.target_address = prank.new_caller;
10191019
call.caller = prank.new_caller;
10201020
let acc = ecx.journaled_state.account(prank.new_caller);

testdata/default/cheats/Prank.t.sol

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,44 @@ contract PrankTest is DSTest {
550550
);
551551
}
552552
}
553+
554+
contract Issue9990 is DSTest {
555+
Vm constant vm = Vm(address(bytes20(uint160(uint256(keccak256("hevm cheat code"))))));
556+
557+
function testDelegatePrank() external {
558+
A a = new A();
559+
vm.etch(address(0x11111), hex"11");
560+
vm.startPrank(address(0x11111), true);
561+
(bool success,) = address(a).delegatecall(abi.encodeWithSelector(A.foo.selector));
562+
require(success, "MyTest: error calling foo on A");
563+
vm.stopPrank();
564+
}
565+
}
566+
567+
// Contracts for DELEGATECALL test case: testDelegatePrank
568+
contract A {
569+
function foo() external {
570+
require(address(0x11111) == msg.sender, "wrong msg.sender in A");
571+
require(address(0x11111) == address(this), "wrong address(this) in A");
572+
B b = new B();
573+
(bool success,) = address(b).call(abi.encodeWithSelector(B.bar.selector));
574+
require(success, "A: error calling B.bar");
575+
}
576+
}
577+
578+
contract B {
579+
function bar() external {
580+
require(address(0x11111) == msg.sender, "wrong msg.sender in B");
581+
require(0x769A6A5f81bD725e4302751162A7cb30482A222d == address(this), "wrong address(this) in B");
582+
C c = new C();
583+
(bool success,) = address(c).delegatecall(abi.encodeWithSelector(C.bar.selector));
584+
require(success, "B: error calling C.bar");
585+
}
586+
}
587+
588+
contract C {
589+
function bar() external view {
590+
require(address(0x11111) == msg.sender, "wrong msg.sender in C");
591+
require(0x769A6A5f81bD725e4302751162A7cb30482A222d == address(this), "wrong address(this) in C");
592+
}
593+
}

0 commit comments

Comments
 (0)