Skip to content
Merged
26 changes: 20 additions & 6 deletions crates/revive-strategy/src/cheatcodes/mock_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use alloy_primitives::{Address, Bytes, map::foldhash::HashMap, ruint::aliases::U256};
use foundry_cheatcodes::{Ecx, MockCallDataContext, MockCallReturnData};
use foundry_cheatcodes::{DealRecord, Ecx, MockCallDataContext, MockCallReturnData};
use foundry_evm::constants::CHEATCODE_ADDRESS;
use polkadot_sdk::{
frame_system,
Expand Down Expand Up @@ -60,12 +60,23 @@ impl MockHandlerImpl {
state.mocked_functions = mock_inner.mocked_functions.clone();
}

/// Funds pranked fuzz addresses with u128::MAX so they can make calls in pallet-revive.
/// Skips accounts that were explicitly dealt to via vm.deal() to preserve balance assertions.
///
/// Note: This only affects accounts that have never been dealt to. Accounts created from
/// within a test contract (e.g., via `self` calls) are not skipped and will be funded if
/// their balance is 0. This is intentional - vm.deal() is an explicit user action that
/// should be respected, while internal contract creations should still get auto-funding.
pub(crate) fn fund_pranked_accounts(&self, account: Address) {
// Fuzzed prank addresses have no balance, so they won't exist in revive, and
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does it work in REVM if prank addresses do not have any balance?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works, in REVM they don't need balance to execute

// calls will fail, this is not a problem when running in REVM.
// TODO: Figure it out why this is still needed.
let balance = Pallet::<Runtime>::evm_balance(&H160::from_slice(account.as_slice()));
if balance == 0.into() {
let mock_inner = self.inner.borrow();

// Skip accounts that were explicitly dealt to via vm.deal()
if mock_inner.eth_deals.iter().any(|deal| deal.address == account) {
return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should check if the balance aligns between foundry's REVM and pallet-revive and then align them if they are divergent in case of e.g vm.deal execution within a callback as it will not set the balance within pallet-revive but will be present in eth_deals.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also addressed, and we default to max if there is no deal

}

let pvm_balance = Pallet::<Runtime>::evm_balance(&H160::from_slice(account.as_slice()));
if pvm_balance == 0.into() {
Pallet::<Runtime>::set_evm_balance(
&H160::from_slice(account.as_slice()),
u128::MAX.into(),
Expand Down Expand Up @@ -193,6 +204,8 @@ struct MockHandlerInner<T: frame_system::Config + pallet_revive::Config> {

pub mocked_calls: HashMap<Address, BTreeMap<MockCallDataContext, VecDeque<MockCallReturnData>>>,
pub mocked_functions: HashMap<Address, HashMap<Bytes, Address>>,
/// Records of accounts that were explicitly dealt to via vm.deal().
pub eth_deals: Vec<DealRecord>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this field if we can just pass it to fund_pranked_accounts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

}

impl MockHandlerInner<Runtime> {
Expand Down Expand Up @@ -222,6 +235,7 @@ impl MockHandlerInner<Runtime> {
mocked_calls: state.mocked_calls.clone(),
callee: callee.map(|addr| H160::from_slice(addr.as_slice())).unwrap_or_default(),
mocked_functions: state.mocked_functions.clone(),
eth_deals: state.eth_deals.clone(),
}
}
}
37 changes: 37 additions & 0 deletions testdata/default/revive/Prank.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,40 @@ contract Counter {
number++;
}
}

contract FundPrankedAccountsReproTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);

address alice = address(0xA11CE);

function test_PrankDealtAccountWithZeroBalance() public {
vm.deal(alice, 3 ether);
assertEq(alice.balance, 3 ether, "alice should have 3 ether after deal");

vm.deal(alice, 0);
assertEq(alice.balance, 0, "alice should have 0 balance after second deal");

vm.startPrank(alice);
assertEq(alice.balance, 0, "alice balance should remain 0, not be reset to u128::MAX");
vm.stopPrank();
}

function test_PrankFuzzedAddressGetsFunded() public {
address fuzzedAddr = address(0xF022ED);
uint256 initialBalance = fuzzedAddr.balance;

vm.prank(fuzzedAddr);

assertTrue(fuzzedAddr.balance >= initialBalance, "fuzzed address should be auto-funded");
}

function test_PrankDealtAccountWithBalance() public {
vm.deal(alice, 5 ether);
assertEq(alice.balance, 5 ether, "alice should have 5 ether");

vm.startPrank(alice);
assertEq(alice.balance, 5 ether, "alice balance should remain 5 ether");
vm.stopPrank();
}

}
Loading