EIP TXID Precompile
Abstract
This EIP proposes a new precompiled contract that provides an opcode, TXID, which pushes the index
(nonce) of the current origin transaction onto the stack. This functionality allows smart contracts
to access the nonce of the transaction that originated the current call stack, providing enhanced
capabilities for tracking and verifying transaction sequences.
Motivation
In the current Ethereum Virtual Machine (EVM), smart contracts lack the ability to directly access
the nonce of the transaction that initiated the current execution context. This information can be
valuable for various use cases, including transaction tracing, enhanced security checks, and complex
state-dependent logic. The TXID precompile aims to fill this gap by providing a standardized and
efficient way to access this information.
Specification
Precompile Address
The TXID precompile will be assigned a specific address, which will be determined at the time of
finalizing this EIP.
Opcode: TXID
The TXID opcode, when called, pushes the nonce of the origin transaction onto the stack.
Gas Costs
The gas cost for invoking the TXID opcode will be determined after benchmarking its computational
overhead. It is expected to have a low fixed gas cost.
Return Value
The TXID precompile returns a single value: the nonce of the transaction that originated the
current call stack.
Error Handling
If the TXID precompile is invoked in a context where the transaction nonce is not available (e.g.,
in a local execution environment outside the blockchain), it should return zero.
Rationale
The choice of a precompiled contract for this functionality is due to the low-level nature of the operation. Accessing the transaction nonce is not feasible with Solidity alone, as it requires interaction with the EVM internals. A precompiled contract is a suitable method for extending the EVM's capabilities in a performance-efficient manner.
Backwards Compatibility
This EIP is fully backward compatible as it introduces a new precompiled contract without modifying the behavior of existing opcodes or structures.
Reference Implementation
The following Solidity code provides a conceptual implementation of the TXID precompile. Note that
in practice, the implementation of this functionality would require modifications to the EVM and
cannot be fully represented in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TXIDPrecompile {
// The address of the precompile (to be determined)
address constant precompileAddress = address(1); // Example address
/**
* @dev Returns the nonce of the transaction that originated the current call stack.
* @return nonce The nonce of the origin transaction.
*/
function getTransactionNonce() public returns (uint256 nonce) {
assembly {
// Call the precompile contract
// The actual implementation of this will depend on the assigned opcode
let success := staticcall(gas(), precompileAddress, 0, 0, nonce, 32)
// Check if the call was successful
if iszero(success) {
revert(0, 0)
}
}
}
}
Security Considerations
The introduction of the TXID precompile should not have adverse security implications, as it only
provides read access to an already publicly available piece of information (the transaction nonce).
However, it is crucial to ensure that the implementation in the EVM does not introduce unexpected
behaviors, especially in edge cases such as transactions with high nonces or in the context of chain
reorganizations.
This EIP draft outlines the proposed functionality and provides a reference for how it might be implemented in Solidity. The actual implementation would require changes at the EVM level, which are beyond the scope of Solidity and would need to be addressed in the EIP's final form.