Skip to content

Lesson 1 of 9 · 25m

EVM Execution, State, and Trust Assumptions

Model transactions, msg.sender, atomicity, gas, reverts, and contract composition before assuming a Solidity function does what its name claims.

The first smart contract audit I ever watched go badly treated Solidity like a web backend with wallet login. It wasn't. A public blockchain is an adversarial shared computer: anyone can call your external functions, inspect pending transactions, compose your contract with theirs, and pay to make state transitions permanent. The right mental model is not "users interact with an API." It is "untrusted code executes mine atomically, in public, with money attached."

Transactions are atomic, but composition is hostile

A transaction either succeeds as a whole or reverts its state changes. That sounds safe until you realize an attacker can call multiple protocols inside the same transaction:

function attack() external {
    tokenA.approve(address(lender), type(uint256).max);
    lender.flashLoan(amount, address(this));
    victim.deposit(amount);          // same transaction
    victim.withdraw(victimShares);   // before invariants catch up
    lender.repayFlashLoan(amount + fee);
}

Flash loans are not magic. They expose assumptions that were already false: that liquidity cannot appear instantly, that one transaction cannot manipulate a price and consume it, or that no caller can arrange contracts in an unfavorable order.

msg.sender is not a user identity

msg.sender is the immediate caller. It may be an externally owned account, a multisig, another contract, a proxy, a batcher, an account-abstraction wallet, or an attacker contract. tx.origin is worse: it tells you the original externally owned account, and using it for authorization creates phishing-style intermediary attacks.

// Bad: a malicious contract can sit between the victim and this contract.
require(tx.origin == owner, "not owner");

// Better: authorize the immediate caller and design for contract callers.
require(msg.sender == owner, "not owner");

Even msg.sender is only an authentication input. It does not answer whether the caller owns a vault share, controls a token amount, or should be allowed to execute a privileged workflow.

External calls are trust boundaries

Every external call can execute attacker-controlled code. Token contracts can be malicious or nonstandard. Receiver hooks can reenter. A call can consume gas, revert unexpectedly, return malformed data, or silently fail in older interfaces. Before integrating a token or protocol, document what you assume: decimals, transfer fees, rebasing behavior, pause states, upgradeability, and return-value semantics.

(bool ok, bytes memory data) = token.call(
    abi.encodeWithSelector(IERC20.transfer.selector, to, amount)
);
require(ok && (data.length == 0 || abi.decode(data, (bool))), "transfer failed");

Modern libraries such as OpenZeppelin SafeERC20 package this reality. They are not decorative wrappers; they encode a decade of ugly token behavior.

Reverts erase state, not the outside world

A revert rolls back contract state inside the failed transaction, but the gas is still spent, the transaction may still be public evidence, and side effects in other successful transactions remain. Crosschain bridges and asynchronous messaging break the simpler mental model even further: one chain's execution may succeed while a destination action fails or arrives out of order.

Field notes

  • Never reason about one function in isolation; reason about a transaction containing attacker-chosen contract calls before and after it.
  • view is a convenience label, not a security boundary; other contracts can be stateful even when your call site is read-only.
  • A failed transaction still costs gas and can still hurt user experience or bot economics.
  • Assume mempool visibility. Publicly broadcasted profitable transactions can be copied, front-run, or sandwiched.
  • Contract interactions are adversarial APIs with atomic rollback, not trusted library calls.

Was this lesson helpful?

Sign in to rate