>/D_
Published on

Understanding Ethereum Transactions: How State Changes Happen on the Blockchain

Authors
  • avatar
    Name
    Frank
    Twitter

Understanding Ethereum Transactions

Ethereum transactions are the core mechanism that changes the blockchain's state. Each transaction is essentially a signed message originating from an Externally Owned Account (EOA), transmitted across the Ethereum network, and eventually recorded on the blockchain. Transactions are the only way to make changes to Ethereum's state or execute smart contracts in the Ethereum Virtual Machine (EVM).

Ethereum as a Finite State Machine

To understand Ethereum, it helps to think of it as a global, shared state machine. A state machine is a system that transitions between different states based on inputs. Ethereum, specifically, is a finite state machine, meaning it has a defined set of states and transitions between them. Each state represents the current status of all accounts and balances, while transactions are the inputs that drive state transitions.

For example, think of a simple turnstile as a state machine. It has two states: locked and unlocked. Depending on the inputs (e.g., inserting a coin or pushing the turnstile), it transitions between these states. Similarly, in Ethereum, transactions are the inputs that change the state of the blockchain.

Ethereum transactions are what keep this state machine evolving, making them essential for all operations on the blockchain.

Parts of an Ethereum Transaction

Each Ethereum transaction contains several components:

  • Nonce: A sequence number issued by the originating EOA, which prevents message replay. It also helps in ensuring the correct order of transactions.
  • Gas Price: The amount of Ether (in wei) that the sender is willing to pay for each unit of gas used by the transaction.
  • Gas Limit: The maximum amount of gas that the sender is willing to purchase for the transaction.
  • Recipient: The destination Ethereum address.
  • Value: The amount of Ether (in wei) to send to the recipient.
  • Data: The variable-length data payload for contract interaction.
  • v, r, s: The three components of an Elliptic Curve Digital Signature Algorithm (ECDSA) signature from the sender.

Understanding Transaction Components

Nonce

The nonce serves two primary purposes: transaction order and preventing replay attacks.

  • Transaction Order: If an account sends multiple transactions, the nonce ensures they are executed in the correct order. Even if nodes receive the transactions out of order, the nonce determines which transaction should be processed first.
  • Transaction Replay: The nonce also prevents the same transaction from being executed multiple times. Once a transaction is broadcast, its nonce is used to make sure duplicate attempts are rejected.

Gas

Gas is the fuel of Ethereum, but it is not Ether. It is a separate, virtual currency with its own exchange rate against Ether. Gas controls the amount of computational resources that a transaction can use, ensuring fair resource allocation.

  • Gas Price: Set by the sender to indicate how much they are willing to pay for each gas unit. It is measured in wei per gas unit, for example, 3 gwei.
  • Gas Limit: The maximum number of gas units a transaction can consume. For simple payments between EOAs, the gas limit is typically fixed at 21,000 gas units.

Recipient

The recipient field indicates the destination address of the transaction. Ethereum does not validate whether the recipient address exists, so any typo here could result in lost Ether.

Transaction Value and Data

The payload of a transaction is represented in two fields: value and data.

  • Value: This represents the amount of Ether being transferred. Transactions containing a value differ in behavior based on the recipient:

    • If the recipient is an EOA, the balance at that address is simply increased.
    • If the recipient is a contract, the EVM will execute the contract.
      • If there is data in the transaction, the EVM will call the specified function.
      • If there is no data, and a fallback function exists, the fallback will be executed.
      • If no fallback function exists, the transaction will be reverted.
  • Data: The data payload is typically used for smart contract interaction. If the recipient is a contract, the data represents function selectors and arguments in hex-encoded form.

  • Special Case: Contract Creation: Contract creation transactions have a recipient set to the zero address (0x0). The data field contains the compiled bytecode for the contract, and the value represents an optional starting balance.

Digital Signatures

Ethereum transactions include a digital signature to prove ownership of the private key without exposing it. A digital signature ensures:

  • Authentication: The message was created by the sender.
  • Non-repudiation: The sender cannot deny sending the message.
  • Integrity: The message was not altered during transit.

Ethereum uses EIP-155 to prevent transaction replay on other blockchains, by adding a chain identifier to the transaction data.

Notably, transactions do not include a "from" field, as the sender's public key can be derived from the signature.

Transaction Propagation

Ethereum uses a flood routing protocol to propagate transactions through its peer-to-peer network. When a node receives a signed transaction, it validates it and forwards it to all its neighbors, who in turn validate and propagate it further. This process results in the transaction reaching every node in the network.

Handling Errors in Transactions

Ethereum transactions are atomic, meaning they either complete successfully or are fully reverted.

Successful Termination Scenarios

  • EOA to EOA Transaction: Balance changes are recorded.
  • EOA to Contract (No Nested Contracts): Balance changes and contract state updates are recorded.
  • EOA to Contract (With Nested Contracts): All state changes are recorded if no errors occur.
  • EOA to Contract (With Partial Errors): If certain contracts error, only the non-erroring changes are recorded.

Reverted Transactions

When a transaction is reverted, all effects are rolled back, as if the transaction never occurred. However, the gas used is deducted from the sender's account.

Error Handling Strategies

  • Error Propagation: By default, if a contract call fails during a nested operation, the entire transaction is reverted to maintain consistency.
  • Catching Errors: Smart contracts can be coded to catch and handle errors using mechanisms like try/catch blocks, allowing for more sophisticated error management.
  • Strategic Error Handling: Developers can choose to selectively handle or propagate errors, depending on the contract's intended logic.

In summary, Ethereum transactions are at the heart of all state changes and interactions on the blockchain. They are a powerful mechanism, with strict rules to ensure consistency, transparency, and security in the decentralized ecosystem.

My shorthand notes were the source material for this article produced by generative AI.