How is it different than networkID?
ChainID was introduced in EIP-155 to prevent replay attacks between the main ETH and ETC chains, which both have a networkID of 1
.
It's basically just an additional way to tell chains apart. Subsequent to EIP-155, ETH has a chainID of 1
, while ETC has a chainID of 61
(even though they still have the same networkID of 1
).
Is chainID and networkID needed in every block or just the genesis block?
It's required for the chain to operate in general - e.g. it's required when signing transactions, meaning transactions signed on the ETH network end up with a different hash than those signed on ETC. Before EIP-155, signed transactions on each network would look the same, and could be replayed.
Edit:
A specific example of how chainId is used.
As per the EIP-155 page, the v
value of a transaction's signature is dependent on the value of chainID.
If
block.number >= FORK_BLKNUM
andv = CHAIN_ID * 2 + 35
orv = CHAIN_ID * 2 + 36
, then when computing the hash of a transaction for purposes of signing or recovering, instead of hashing only the first six elements (i.e. nonce, gasprice, startgas, to, value, data), hash nine elements, withv
replaced byCHAIN_ID
,r = 0
ands = 0
. The currently existing signature scheme usingv = 27
andv = 28
remains valid and continues to operate under the same rules as it does now.
There is a detailed example of how this is applied on the EIP-155 page.