EIP-1959 · Stagnant · Core · 2019-04-20
EIP-1959: New Opcode to check if a chainID is part of the history of chainIDs
Proposed a VALID_CHAINID opcode that tells a contract whether a chain ID has ever been valid for the current chain, so signatures made before a chain-ID change keep working.
What problem does it solve?
Off-chain signed messages, such as EIP-712 orders, meta-transactions and state-channel updates, include the chain ID so they cannot be replayed on another chain. A contract then needs to check that chain ID.
There are two naive options, and both break around a hard fork that changes the chain ID:
- Hard-code the chain ID at deploy time: after the fork, messages signed with the new ID are rejected.
- Compare with the current chain ID (the
CHAINIDopcode of EIP-1344): after the fork, every message signed before it is rejected. For a state channel that can mean funds become unreachable.
EIP-1959 argues that a message valid when signed should stay valid, so a contract should accept any chain ID from the chain's own history.
How it works
- New opcode
VALID_CHAINID, proposed at0x46. - Takes one stack item: a 256-bit chain ID to test.
- Pushes
1if that value has been a chain ID of this chain at any point since genesis (including the current one), else0. - Gas cost:
G_blockhash, with a note that it may need to rise as the history grows.
Wallets still sign with the latest chain ID, so two chains that split and both changed their IDs cannot replay each other's new messages. Messages signed before the split stay valid on both, the same way pre-fork state is shared.
The spec admits one gap. If a small minority forks off and the majority does not change its chain ID, the majority's new messages can be replayed on the minority fork. The author accepts this, assuming such forks gain little traction.
Sample code
How a contract would use it, written as hypothetical Solidity (no compiler supports this opcode):
// Hypothetical: VALID_CHAINID was never added to the EVM or to Solidity.
function execute(Message calldata m, bytes calldata sig) external {
require(validChainId(m.chainId), "wrong chain"); // would compile to opcode VALID_CHAINID
// ... verify EIP-712 signature over m, then act
}
EVM-level view:
PUSH32 <chainId> // value to test
VALID_CHAINID // pops 1 item, pushes 1 if chainId is in this chain's history, else 0
What contracts actually do today, using EIP-1344:
require(m.chainId == block.chainid, "wrong chain");
Things to know
- Status: Stagnant. It was never adopted.
- Opcode
0x46was later taken byCHAINIDfrom EIP-1344, activated in Istanbul. Any revival would need a different opcode number. - The same author's EIP-1965 is a related, also stagnant, variant that checks whether a chain ID was valid at a specific block number.
- In practice, contracts read
block.chainidand, when a domain separator includes it, recompute the separator if the chain ID changes. Mainnet has not changed its chain ID, so the problem stayed largely theoretical. - Builds on EIP-155, which introduced chain IDs for transaction replay protection.