The nitty-gritty of Ethereum and Solidity : Transactions.

Alberto Molina
5 min readSep 9, 2023

--

Transactions underpin the entire Ethereum ecosystem, enabling the transfer of Ether (ETH) and the execution of smart contracts. In this blog we will uncover their structure, content, usage, the different types of transactions, and their crucial relationship with gas, as well as how Ethereum clients and the Ethereum Virtual Machine (EVM) handle them.

Transaction Fields

Sender

The externally own account (EOA) that signs the transaction using its private key. The sender pays all the transaction fees and, if the transaction transfers funds, those will be deducted from the sender’s balance as well.

Recipient

The EOA or smart contract that receives the transaction, in other words, its balance gets credited the transaction value.

If it is a smart contract and the transaction contains some data, the smart contract bytecode will be executed.

The recipient can be the address “0” (0x000…00) if the transaction is meant to deploy a new smart contract, more on that later.

Value

Ether, Ethereum’s native cryptocurrency, is sent in transactions. The amount of Ether transferred is specified in the transaction. This is where the transaction’s value is recorded, indicating how much Ether is being sent from the sender to the recipient.

Data

Ethereum transactions can do more than just transfer Ether; they can also carry data. This data is usually associated with smart contract execution. When sending Ether to a standard Ethereum address, the data field is typically empty. However, when interacting with a smart contract, this field contains the necessary instructions and data to execute the contract’s functions.

Gas

Here’s where the relationship between transactions and gas comes into play. To prevent spam and ensure fair usage of the network, Ethereum introduces the concept of gas. Every operation on the Ethereum network, from transferring Ether to executing smart contracts, consumes a certain amount of gas. The following fields must be included in a transaction:

  • Gas Limit: Maximum amount of gas that can be consumed by the transaction. It is set by the sender to control the transaction’s resource usage.
  • Max priority fee per Gas: Maximum amount of Gwei per unit of gas the sender is willing to pay as tips (to the miner mining the transaction).
  • Max Fee per Gas: Maximum amount of Gwei per unit of gas the sender is willing to pay for the whole transaction (base fee and miner tip included).

Nonce

A sequentially incrementing counter which indicates the transaction number from the sender. This is used to prevent anyone from resubmitting the same transaction to the network.

Signature

Generated with the sender’s ECDSA private key, it confirms the sender has authorized this transaction. The signature has the following components.

  • v : recover Id, used to determine the public key from the signature. Includes also the chain ID (Id that uniquely identifies an EVM blockchain, used to prevent cross-chain replay attacks).
  • r : x-coordinate of the resulting point (R) on the elliptic curve used in Ethereum’s elliptic curve digital signature algorithm (ECDSA). It is part of the signature’s cryptographic data and is used in the verification process.
  • s : the second part of the signature. It represents the modulo of a large prime number and is also a critical component in the ECDSA algorithm. It is used in combination with “r” in the verification process.

Transactions Types

Ethereum supports various types of transactions, each serving a specific purpose:

  1. Transfers: These are the simplest transactions where Ether is transferred from one address to another. They have an empty data field. They are initiated by an EOA and can be sent either to another EOA or a smart contract.
  2. Contract Calls: These transactions are just like “Transfers” but they contain data and are sent to smart contracts (sending it to an EOA will basically do the same as a “Transfer” transaction storing data in the blockchain). They will typically trigger the smart contract logic and must be initiated by an EOA.
  3. Contract Creation Transactions: When a new smart contract is deployed on the Ethereum network, a special type of transaction called a contract creation transaction is used. These transactions contain the bytecode of the new smart contract in the data field,must be initiated by an EOA and sent to address 0 (recipient = 0x000…00).
  4. Message (Internal “Transactions”): These are not actual transactions because the are not initiated by an EOA directly, they are instead initiated by a smart contract as a response to a “Contract Call” transaction. They enable interactions between existing smart contracts and include data for contract execution. These interactions are internal to the Ethereum Virtual Machine (EVM).

Transaction Lifecycle

Understanding how Ethereum clients and the EVM handle transactions is essential to grasp the transaction lifecycle:

  1. Transaction Creation: Users generate transactions through their wallets, specifying all the necessary fields and signing it. The transaction is then broadcast to the Ethereum network.
  2. Transaction Memory Pool: Transactions enter the memory pool, where miners select them for inclusion in a block. They generally prioritize transactions with higher gas prices, but MEV plays also an important role when it comes to selecting transactions to include in a block.
  3. Transaction Execution: Miners process transactions in the order they choose, executing the smart contracts and transferring Ether as specified. Gas is consumed during this stage, and unused gas is refunded to the sender.
  4. Block Confirmation: Once a block containing the transaction is mined and added to the blockchain, the transaction is considered confirmed. This confirmation provides security against double-spending. If for some reason the transaction fails (sender does not have enough funds, the executed smart contract reverts, …) the transaction is not included in a block but its failed execution yes, meaning that the state of the blockchain will not change except for the gas fees that will be debited from the sender’s balance.

The Role of Gas in Ethereum Transactions

Transactions need to allocate sufficient gas to cover their execution, or they will fail.

The total cost of a transaction is calculated as follows:

Total Transaction Cost = Gas Cost × Gas Price

  • Gas Cost : The actual amount of Gas consumed by the transaction. This depends on the smart contract been executed or deployed. If the transaction is a “Transfer” the Gas cost is constant : 21'000 Gas.
  • Gas Price : The amount of Gwei the sender pays for unit of Gas.

This cost is deducted from the sender’s account balance. If a transaction runs out of gas during execution, all changes made by the transaction are rolled back, but the gas used is not refunded (just like when a transaction fails for any other reason). This ensures that miners are compensated for their computational work.

Smart Contracts and the EVM

Ethereum transactions are the vehicles through which smart contracts are deployed and interacted with. When a transaction includes data that specifies a smart contract’s function to execute, the Ethereum Virtual Machine (EVM) comes into action. The EVM interprets and executes the bytecode of smart contracts, ensuring deterministic and consensus-based computation across all nodes in the Ethereum network.

Ethereum Clients and Transaction Management

Ethereum clients, like Geth and Parity, play a vital role in managing transactions. They are responsible for broadcasting transactions, maintaining transaction pools, and participating in the consensus process.

  • Transaction Broadcasting: Clients facilitate the creation and broadcasting of transactions, ensuring they reach the network.
  • Transaction Pool Management: Ethereum clients maintain transaction pools, where they keep track of pending transactions, prioritize them based on gas price, and pass them to miners for inclusion in blocks.
  • Consensus: Ethereum clients validate transactions, execute smart contracts, and reach consensus on the state of the Ethereum blockchain.

--

--