Skip to content

Commit 1fc0732

Browse files
committed
comment about precond of account debit method
1 parent 57cea41 commit 1fc0732

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

category/execution/ethereum/state3/state.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,32 @@ void State::add_to_balance(Address const &address, uint256_t const &delta)
325325
account_state.touch();
326326
}
327327

328+
// It is the caller's duty to ensure (precondition 1) that the to-be-debited
329+
// account has sufficient balance. However, that is not enough: in case of
330+
// relaxed validation of balances, the caller is also responsible for ensuring
331+
// (precondition 2) that min_balance has been adjusted to account for this
332+
// debit, e.g. by calling State::record_balance_constraint_for_debit().
333+
// Otherwise, the MONAD_ASSERT guarding subtraction underflow in the body of
334+
// try_fix_account_mismatch() may fail when can_merge() happens later.
335+
//
336+
// State/AccountState disables direct accesses to balances and thus ensures that
337+
// any external non-friend caller who guarantees the precondition 1 also ends up
338+
// guaranteeing precondition 2: there are only 2 ways the external non-friend
339+
// caller can ensure precondition 1: either call
340+
// State::record_balance_constraint_for_debit() and check the returned bool is
341+
// true or call State::get_current_balance_pessimistic() and check that the
342+
// result is >= delta. The former correctly adjusts min_balance and latter will
343+
// disable relaxed validation for the account `address`.
344+
//
345+
// Care must be taken to also ensure precondition 2 at call sites of this method
346+
// in methods of State/AccountState or their friends. Alternatively, the call to
347+
// record_balance_constraint_for_debit() below can be commented out. That call
348+
// is probably cheap and if the caller already did it just before, the second
349+
// call will be a no-op.
328350
void State::subtract_from_balance(
329351
Address const &address, uint256_t const &delta)
330352
{
353+
// record_balance_constraint_for_debit(address, delta);
331354
auto &account_state = current_account_state(address);
332355
auto &account = account_state.account_;
333356
if (MONAD_UNLIKELY(!account.has_value())) {

0 commit comments

Comments
 (0)