Ethereum transaction
A transaction on an Ethereum blockchain is required to transfer ETH from EOA or for smart contracts to change state, perform an action, or read a state of a contract. A transaction can only be initiated from an EOA and it can further initiate other internal transactions.
For example, an EOA wallet initiates a transaction to call a function of contract A, and this contract calls another function of contract B. In this case, a transaction is initiated from an EOA to contract A and then an internal transaction is initiated from contract A to contract B.
Transaction Creation and Signing Process
A user begins by filling in the required fields to create a new transaction. Afterward, the transaction is signed using the private key associated with their Externally Owned Account (EOA). Once signed, the transaction is broadcast to the Ethereum blockchain via an Ethereum client. The client first verifies the transaction’s validity and ensures it is signed with the EOA’s private key, matching the address in the from field.
From
The from field contains the public address of the EOA that initiated the transaction. In Ethereum, only EOAs can initiate transactions, while contract accounts cannot. Transactions triggered by contracts are known as internal transactions (commonly referred to as "message calls" in Ethereum's yellow paper), which are triggered as a result of contract interactions.
To
The to field contains the public address of either an EOA or a smart contract. If the to address belongs to an EOA, the transaction is typically meant to transfer Ether (ETH). If it belongs to a contract, the transaction is either a function call to the contract or a transfer of ETH to the contract.
Value
The value field represents the amount of ETH being transferred in the transaction. For transactions between EOAs, this field is usually non-zero, indicating the amount of ETH being sent. In contract interactions, the value field can either be zero or non-zero, depending on the contract’s function.
Gas Limit
The gas limit is the maximum amount of computational effort (gas units) that the Ethereum Virtual Machine (EVM) is allowed to spend on the transaction’s execution.
Gas Price
The gas price is the amount of Ether that the user is willing to pay per unit of gas. It determines how much the user is paying to execute the transaction.
Nonce
The nonce is a counter that starts at 0 for every EOA and increments by 1 with each new transaction initiated by that account. It ensures transactions are executed in order and prevents replay attacks. When multiple transactions are pending, the one with the lowest nonce for that EOA is executed first.
Data
The data field contains the hexadecimal-encoded data required for invoking a smart contract method. When transferring ETH between EOAs, this field is empty (represented by 0x
). However, when interacting with a contract, it contains the method signature and parameters in hexadecimal form.
For example, when calling the transfer()
function on an ERC-20 token contract, the data field includes the method signature and the function's parameters. The hexadecimal code 0xa9059cbb
represents the transfer()
method, and the following bytes represent the parameters (recipient address and amount):
Types of Transactions
Let us tie the information we just learned about accounts with the different types of transactions:
Message call transaction: A message call derives from an externally owned account that wants to interact with another EOA or contract account. An example of a message call would be sending Ether from one account to another, or interacting with a smart contract (e.g, swapping tokens on Uniswap).
Contract creation transaction: A contract creation derives from an EOA to create a smart contract account (generally to store code and storage). An example of this type of transaction would be deploying a storage smart contract to store data.
Transaction Status
The transaction status lets the user know the status of a transaction. Transaction change or dropping a transaction is allowed in different states. A transaction can be in any of the following three statuses:
As shown in the preceding diagram, transactions are always initiated by an EOA. Let's go through each of the statuses.
Pending status
When a transaction is submitted to the Ethereum network, it first goes into the pending status, waiting to be executed by the nodes. A transaction can be in the pending state for a longer duration if the gas price is set very low in the transaction and the nodes are busy processing other higher gas price transactions. During the pending state, the transaction initiator is allowed to change the transaction fields at any time. They can do so by sending another transaction with the same nonce.
Success status
A transaction that is executed successfully without any failure is called a successful transaction. A successful transaction means the intended operation is executed successfully by the Ethereum blockchain and verified by other nodes present in the Ethereum blockchain network:
Fail status
A transaction is executed and, during the execution, it may fail due to an error in the contract method execution. Transaction failure also depends on the contract definition. Transaction execution failure could also occur in case of a low gas limit, and transactions will fail with the OutOfGasException
error:
Dropped status
A transaction is sometimes dropped from the Ethereum network for several reasons:
If the transaction was in the pending state for a very long duration.
It has a very low gas price as compared to other pending transactions
The maximum number of pending transactions the Ethereum blockchain can handle are in the pending transaction pool
Once a transaction is dropped, all the gas and ETH is returned to the originating wallet:
Transaction Operations
There are two primary operations that can be performed on a transaction that has already been initiated, but remains in a pending status:
Replace/Update
If a user wants to modify certain fields of a pending transaction, they can do so by submitting a new transaction with the updated values while keeping the same nonce. This type of transaction is typically used when a transaction is pending due to a low gas price. By increasing the gas price in the new transaction (while keeping the nonce unchanged), the likelihood of the transaction being processed by the Ethereum network is increased, as miners prioritize transactions with higher gas fees.
Cancel
To cancel a pending transaction, the same EOA (Externally Owned Account) that initiated the original transaction can submit a new transaction with the same nonce. In this new transaction, the To field is set to the EOA’s own address, effectively sending a self-transaction with no impact other than canceling the pending one. The gas price for this new transaction must be higher than the original one to ensure that it gets processed first, allowing the user to override the pending transaction and prevent it from being executed. This is referred to as a self-transaction.
Transaction Lifecycle
The transaction lifecycle can be simplified to:
An external account creates a transaction object.
The account sending the transaction verifies it by signing which creates a transaction hash
The transaction is broadcasted across the network using an Ethereum node
The transaction execution is idle until the transaction is mined and added to the block or replaced/canceled.
Understanding Ethereum Transaction Signing and Validation
In the Ethereum blockchain, transactions are at the heart of how accounts interact, whether it’s transferring Ether or executing smart contracts. For these interactions to be secure and trustworthy, Ethereum uses public-key cryptography to ensure that only the owner of an account (Externally Owned Account or EOA) can authorize transactions from that account. This is accomplished by signing the transaction with a private key, which ensures that the signature was created by a specific private key holder and that the transaction can be verified.
The Role of Public-Key Cryptography
Public-key cryptography plays a vital role in securing Ethereum transactions. Here's a quick breakdown:
Private Key: A private key is the secret half of a key pair, used to sign transactions. This key must remain confidential because it grants control over the EOA.
Public Key: The public key is derived from the private key and is used to verify the authenticity of a transaction. The address of an EOA is a truncated hash of its public key, providing a unique identifier for the account.
When an Ethereum transaction is signed with the private key, anyone can use the public key (derived from the transaction’s address) to confirm that the signature is valid. This ensures that only the account owner can authorize transactions.
Why Private Keys Matter
Control of the Account: Knowing the private key of an EOA means that you have full control over that account and can sign transactions from it. This is why protecting private keys is crucial.
Signatures as Proof: The private key is used to generate a unique cryptographic signature for each transaction, proving that the transaction was authorized by the correct account holder. Without this signature, there’s no way for the network to authenticate the transaction.
Delegating Private Key Management
When developing smart contracts and decentralized applications (dApps), developers rarely deal with private keys directly. Managing private keys is sensitive and can lead to significant security risks if done improperly. Instead, developers use wallet software like MetaMask, which handles private key management and cryptographic signatures.
Wallets like MetaMask offer:
Security: By securely storing private keys.
Convenience: By handling transaction signing in the background.
Interfacing: They make it easy to interact with dApps, sign transactions, and access the Ethereum network without exposing the private key to the developer or the dApp.
Transaction Attributes: What Is Signed?
When a user initiates an Ethereum transaction, several key pieces of data are compiled and signed. Here are the core attributes included in the transaction and what they represent:
Nonce
The nonce is a sequential number representing the count of transactions sent from a specific EOA. This ensures that transactions are processed in order and prevents duplicate transactions from being replayed.
Gas Price
The gas price specifies the amount of wei (smallest unit of Ether) that the transaction will pay for each unit of gas consumed during execution. A higher gas price offers an incentive for miners to prioritize the transaction.
Gas Limit
This represents the maximum amount of gas the transaction is allowed to consume. If the transaction exceeds this limit, it will fail, but the gas will still be spent.
To Address
This is the recipient of the transaction. It could be the address of another EOA or a contract account. If deploying a new smart contract, the To field is left blank.
Value
The amount of wei being transferred as part of the transaction. This can be zero in the case of contract calls or function executions.
Data
This field holds additional data for the transaction. If the transaction is calling a function on a smart contract, the data field contains the function name and parameters. For contract deployment, this field stores the contract’s bytecode. In simple ETH transfers, this field is often left empty.
Chain ID
Ethereum supports multiple networks, each with a unique identifier, called a chain ID. The Mainnet has a chain ID of
1
, while testnets like Kovan use a different chain ID, such as42
. This ensures transactions are sent to the correct network.
How Ethereum Validates Transactions
Once the transaction attributes are concatenated and signed using the private key, the cryptographic signature is added to the transaction. The process works as follows:
Transaction Signed by the Private Key: After creating the transaction, the user’s private key is used to generate a unique cryptographic signature that’s appended to the transaction.
Broadcast to Ethereum Nodes: The signed transaction is broadcast to the Ethereum network, where nodes begin validating it.
Signature Verification: Ethereum nodes use the sender's address (derived from the public key) to verify the transaction’s signature. This ensures that the transaction was actually authorized by the owner of the private key associated with the sending account.
Rejection of Invalid Signatures: If the signature doesn’t match the public key or has been tampered with, the nodes reject the transaction, ensuring that unauthorized transactions do not enter the network.
Security of Ethereum Transactions
Ethereum’s transaction model is secure because:
Private Key Control: Only the owner of the private key can sign transactions, meaning unauthorized parties cannot move funds or execute actions on the account.
Signature Validation: Each transaction includes a signature that the network verifies before accepting the transaction. This ensures the authenticity of every interaction on the blockchain.
Using Web3 libraries, wallets, and cryptographic algorithms, Ethereum maintains the security and trustless nature of its decentralized system.
Understanding how Ethereum uses public-key cryptography, private keys, and signatures provides insight into the security of the blockchain. Developers are encouraged to use wallets like MetaMask to handle private key management securely. The signing process ensures that only the rightful owner of an account can authorize transactions, and Ethereum’s validation mechanism ensures the network only processes legitimate transactions. By properly securing private keys and leveraging wallet services, users and developers can safely interact with the Ethereum network.
Last updated