forkid.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package forkid implements EIP-2124 (https://eips.ethereum.org/EIPS/eip-2124).
  17. package forkid
  18. import (
  19. "encoding/binary"
  20. "errors"
  21. "hash/crc32"
  22. "math"
  23. "math/big"
  24. "reflect"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/core/types"
  28. "github.com/ethereum/go-ethereum/log"
  29. "github.com/ethereum/go-ethereum/params"
  30. )
  31. var (
  32. // ErrRemoteStale is returned by the validator if a remote fork checksum is a
  33. // subset of our already applied forks, but the announced next fork block is
  34. // not on our already passed chain.
  35. ErrRemoteStale = errors.New("remote needs update")
  36. // ErrLocalIncompatibleOrStale is returned by the validator if a remote fork
  37. // checksum does not match any local checksum variation, signalling that the
  38. // two chains have diverged in the past at some point (possibly at genesis).
  39. ErrLocalIncompatibleOrStale = errors.New("local incompatible or needs update")
  40. )
  41. // Blockchain defines all necessary method to build a forkID.
  42. type Blockchain interface {
  43. // Config retrieves the chain's fork configuration.
  44. Config() *params.ChainConfig
  45. // Genesis retrieves the chain's genesis block.
  46. Genesis() *types.Block
  47. // CurrentHeader retrieves the current head header of the canonical chain.
  48. CurrentHeader() *types.Header
  49. }
  50. // ID is a fork identifier as defined by EIP-2124.
  51. type ID struct {
  52. Hash [4]byte // CRC32 checksum of the genesis block and passed fork block numbers
  53. Next uint64 // Block number of the next upcoming fork, or 0 if no forks are known
  54. }
  55. // Filter is a fork id filter to validate a remotely advertised ID.
  56. type Filter func(id ID) error
  57. // NewID calculates the Ethereum fork ID from the chain config, genesis hash, and head.
  58. func NewID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {
  59. // Calculate the starting checksum from the genesis hash
  60. hash := crc32.ChecksumIEEE(genesis[:])
  61. // Calculate the current fork checksum and the next fork block
  62. var next uint64
  63. for _, fork := range gatherForks(config) {
  64. if fork <= head {
  65. // Fork already passed, checksum the previous hash and the fork number
  66. hash = checksumUpdate(hash, fork)
  67. continue
  68. }
  69. next = fork
  70. break
  71. }
  72. return ID{Hash: checksumToBytes(hash), Next: next}
  73. }
  74. func NextForkHash(config *params.ChainConfig, genesis common.Hash, head uint64) [4]byte {
  75. // Calculate the starting checksum from the genesis hash
  76. hash := crc32.ChecksumIEEE(genesis[:])
  77. // Calculate the current fork checksum and the next fork block
  78. var next uint64
  79. for _, fork := range gatherForks(config) {
  80. if fork <= head {
  81. // Fork already passed, checksum the previous hash and the fork number
  82. hash = checksumUpdate(hash, fork)
  83. continue
  84. }
  85. next = fork
  86. break
  87. }
  88. if next == 0 {
  89. return checksumToBytes(hash)
  90. } else {
  91. return checksumToBytes(checksumUpdate(hash, next))
  92. }
  93. }
  94. // NewIDWithChain calculates the Ethereum fork ID from an existing chain instance.
  95. func NewIDWithChain(chain Blockchain) ID {
  96. return NewID(
  97. chain.Config(),
  98. chain.Genesis().Hash(),
  99. chain.CurrentHeader().Number.Uint64(),
  100. )
  101. }
  102. // NewFilter creates a filter that returns if a fork ID should be rejected or not
  103. // based on the local chain's status.
  104. func NewFilter(chain Blockchain) Filter {
  105. return newFilter(
  106. chain.Config(),
  107. chain.Genesis().Hash(),
  108. func() uint64 {
  109. return chain.CurrentHeader().Number.Uint64()
  110. },
  111. )
  112. }
  113. // NewStaticFilter creates a filter at block zero.
  114. func NewStaticFilter(config *params.ChainConfig, genesis common.Hash) Filter {
  115. head := func() uint64 { return 0 }
  116. return newFilter(config, genesis, head)
  117. }
  118. // newFilter is the internal version of NewFilter, taking closures as its arguments
  119. // instead of a chain. The reason is to allow testing it without having to simulate
  120. // an entire blockchain.
  121. func newFilter(config *params.ChainConfig, genesis common.Hash, headfn func() uint64) Filter {
  122. // Calculate the all the valid fork hash and fork next combos
  123. var (
  124. forks = gatherForks(config)
  125. sums = make([][4]byte, len(forks)+1) // 0th is the genesis
  126. )
  127. hash := crc32.ChecksumIEEE(genesis[:])
  128. sums[0] = checksumToBytes(hash)
  129. for i, fork := range forks {
  130. hash = checksumUpdate(hash, fork)
  131. sums[i+1] = checksumToBytes(hash)
  132. }
  133. // Add two sentries to simplify the fork checks and don't require special
  134. // casing the last one.
  135. forks = append(forks, math.MaxUint64) // Last fork will never be passed
  136. // Create a validator that will filter out incompatible chains
  137. return func(id ID) error {
  138. // Run the fork checksum validation ruleset:
  139. // 1. If local and remote FORK_CSUM matches, compare local head to FORK_NEXT.
  140. // The two nodes are in the same fork state currently. They might know
  141. // of differing future forks, but that's not relevant until the fork
  142. // triggers (might be postponed, nodes might be updated to match).
  143. // 1a. A remotely announced but remotely not passed block is already passed
  144. // locally, disconnect, since the chains are incompatible.
  145. // 1b. No remotely announced fork; or not yet passed locally, connect.
  146. // 2. If the remote FORK_CSUM is a subset of the local past forks and the
  147. // remote FORK_NEXT matches with the locally following fork block number,
  148. // connect.
  149. // Remote node is currently syncing. It might eventually diverge from
  150. // us, but at this current point in time we don't have enough information.
  151. // 3. If the remote FORK_CSUM is a superset of the local past forks and can
  152. // be completed with locally known future forks, connect.
  153. // Local node is currently syncing. It might eventually diverge from
  154. // the remote, but at this current point in time we don't have enough
  155. // information.
  156. // 4. Reject in all other cases.
  157. head := headfn()
  158. for i, fork := range forks {
  159. // If our head is beyond this fork, continue to the next (we have a dummy
  160. // fork of maxuint64 as the last item to always fail this check eventually).
  161. if head > fork {
  162. continue
  163. }
  164. // Found the first unpassed fork block, check if our current state matches
  165. // the remote checksum (rule #1).
  166. if sums[i] == id.Hash {
  167. // Fork checksum matched, check if a remote future fork block already passed
  168. // locally without the local node being aware of it (rule #1a).
  169. if id.Next > 0 && head >= id.Next {
  170. return ErrLocalIncompatibleOrStale
  171. }
  172. // Haven't passed locally a remote-only fork, accept the connection (rule #1b).
  173. return nil
  174. }
  175. // The local and remote nodes are in different forks currently, check if the
  176. // remote checksum is a subset of our local forks (rule #2).
  177. for j := 0; j < i; j++ {
  178. if sums[j] == id.Hash {
  179. // Remote checksum is a subset, validate based on the announced next fork
  180. if forks[j] != id.Next {
  181. return ErrRemoteStale
  182. }
  183. return nil
  184. }
  185. }
  186. // Remote chain is not a subset of our local one, check if it's a superset by
  187. // any chance, signalling that we're simply out of sync (rule #3).
  188. for j := i + 1; j < len(sums); j++ {
  189. if sums[j] == id.Hash {
  190. // Yay, remote checksum is a superset, ignore upcoming forks
  191. return nil
  192. }
  193. }
  194. // No exact, subset or superset match. We are on differing chains, reject.
  195. return ErrLocalIncompatibleOrStale
  196. }
  197. log.Error("Impossible fork ID validation", "id", id)
  198. return nil // Something's very wrong, accept rather than reject
  199. }
  200. }
  201. // checksumUpdate calculates the next IEEE CRC32 checksum based on the previous
  202. // one and a fork block number (equivalent to CRC32(original-blob || fork)).
  203. func checksumUpdate(hash uint32, fork uint64) uint32 {
  204. var blob [8]byte
  205. binary.BigEndian.PutUint64(blob[:], fork)
  206. return crc32.Update(hash, crc32.IEEETable, blob[:])
  207. }
  208. // checksumToBytes converts a uint32 checksum into a [4]byte array.
  209. func checksumToBytes(hash uint32) [4]byte {
  210. var blob [4]byte
  211. binary.BigEndian.PutUint32(blob[:], hash)
  212. return blob
  213. }
  214. // gatherForks gathers all the known forks and creates a sorted list out of them.
  215. func gatherForks(config *params.ChainConfig) []uint64 {
  216. // Gather all the fork block numbers via reflection
  217. kind := reflect.TypeOf(params.ChainConfig{})
  218. conf := reflect.ValueOf(config).Elem()
  219. var forks []uint64
  220. for i := 0; i < kind.NumField(); i++ {
  221. // Fetch the next field and skip non-fork rules
  222. field := kind.Field(i)
  223. if !strings.HasSuffix(field.Name, "Block") {
  224. continue
  225. }
  226. if field.Type != reflect.TypeOf(new(big.Int)) {
  227. continue
  228. }
  229. // Extract the fork rule block number and aggregate it
  230. rule := conf.Field(i).Interface().(*big.Int)
  231. if rule != nil {
  232. forks = append(forks, rule.Uint64())
  233. }
  234. }
  235. // Sort the fork block numbers to permit chronological XOR
  236. for i := 0; i < len(forks); i++ {
  237. for j := i + 1; j < len(forks); j++ {
  238. if forks[i] > forks[j] {
  239. forks[i], forks[j] = forks[j], forks[i]
  240. }
  241. }
  242. }
  243. // Deduplicate block numbers applying multiple forks
  244. for i := 1; i < len(forks); i++ {
  245. if forks[i] == forks[i-1] {
  246. forks = append(forks[:i], forks[i+1:]...)
  247. i--
  248. }
  249. }
  250. // Skip any forks in block 0, that's the genesis ruleset
  251. if len(forks) > 0 && forks[0] == 0 {
  252. forks = forks[1:]
  253. }
  254. return forks
  255. }