2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521 | @pytest.mark.parametrize(
"delegated,target_is_warm,delegation_is_warm",
[
pytest.param(False, False, False, id="no_delegation-cold_target"),
pytest.param(False, True, False, id="no_delegation-warm_target"),
pytest.param(
True, False, False, id="delegated-cold_target-cold_delegation"
),
pytest.param(
True, True, False, id="delegated-warm_target-cold_delegation"
),
pytest.param(
True, False, True, id="delegated-cold_target-warm_delegation"
),
pytest.param(
True, True, True, id="delegated-warm_target-warm_delegation"
),
],
)
@pytest.mark.with_all_call_opcodes(
selector=lambda call_opcode: call_opcode in (Op.CALL, Op.CALLCODE)
)
def test_bal_call_revert_insufficient_funds(
pre: Alloc,
blockchain_test: BlockchainTestFiller,
call_opcode: Op,
delegated: bool,
target_is_warm: bool,
delegation_is_warm: bool,
) -> None:
"""
Test BAL with CALL/CALLCODE failure due to insufficient balance
(not OOG), with and without 7702 delegation.
Caller (balance=100): SLOAD(0x01) → call_opcode(target, value=1000)
→ SSTORE(0x02, result). The call fails because 1000 > 100. The
failure happens after delegation resolution, so when the target is
a 7702-delegated EOA both target and delegation target appear in
the BAL — distinct from the OOG case (see
test_bal_call_7702_delegation_and_oog) where the static-check
optimization keeps the delegation target out of the BAL.
Access-list warming does NOT add to BAL on its own — only EVM
access does — so the BAL is identical across warm/cold variants.
"""
alice = pre.fund_eoa()
caller_balance = 100
transfer_amount = 1000 # > caller_balance, transfer must fail
target_balance = 1 # non-zero balance keeps non-delegated target non-empty
delegation_target: Address | None = None
if delegated:
delegation_target = pre.deploy_contract(code=Op.STOP)
target = pre.fund_eoa(
amount=target_balance, delegation=delegation_target
)
else:
target = pre.fund_eoa(amount=target_balance)
caller_code = (
Op.SLOAD(0x01)
+ Op.POP
+ call_opcode(100_000, target, transfer_amount, 0, 0, 0, 0)
+ Op.PUSH1(0x02)
+ Op.SSTORE
+ Op.STOP
)
caller = pre.deploy_contract(
code=caller_code,
balance=caller_balance,
storage={0x02: 0xDEAD}, # non-zero so SSTORE(0) is a change
)
access_list: list[AccessList] = []
if target_is_warm:
access_list.append(AccessList(address=target, storage_keys=[]))
if delegated and delegation_is_warm:
assert delegation_target is not None
access_list.append(
AccessList(address=delegation_target, storage_keys=[])
)
tx = Transaction(
sender=alice,
to=caller,
gas_limit=1_000_000,
access_list=access_list,
)
account_expectations: Dict[Address, BalAccountExpectation] = {
alice: BalAccountExpectation(
nonce_changes=[BalNonceChange(block_access_index=1, post_nonce=1)],
),
caller: BalAccountExpectation(
storage_reads=[0x01],
storage_changes=[
BalStorageSlot(
slot=0x02,
slot_changes=[
BalStorageChange(block_access_index=1, post_value=0)
],
)
],
),
# Target accessed before balance check fails.
target: BalAccountExpectation.empty(),
}
if delegated:
assert delegation_target is not None
# Delegation resolved before balance check fails.
account_expectations[delegation_target] = BalAccountExpectation.empty()
block = Block(
txs=[tx],
expected_block_access_list=BlockAccessListExpectation(
account_expectations=account_expectations
),
)
blockchain_test(
pre=pre,
blocks=[block],
post={
alice: Account(nonce=1),
caller: Account(
balance=caller_balance, # unchanged - transfer failed
storage={0x02: 0}, # Failed call returned 0
),
target: Account(balance=target_balance), # unchanged
},
)
|