forkid_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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
  17. import (
  18. "bytes"
  19. "math"
  20. "math/big"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/params"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. // TestCreation tests that different genesis and fork rule combinations result in
  27. // the correct fork ID.
  28. func TestCreation(t *testing.T) {
  29. mergeConfig := *params.MainnetChainConfig
  30. mergeConfig.MergeNetsplitBlock = big.NewInt(18000000)
  31. type testcase struct {
  32. head uint64
  33. want ID
  34. }
  35. tests := []struct {
  36. config *params.ChainConfig
  37. genesis common.Hash
  38. cases []testcase
  39. }{
  40. // Mainnet test cases
  41. {
  42. params.MainnetChainConfig,
  43. params.MainnetGenesisHash,
  44. []testcase{
  45. {0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced
  46. {1149999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block
  47. {1150000, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block
  48. {1919999, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block
  49. {1920000, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block
  50. {2462999, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block
  51. {2463000, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block
  52. {2674999, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block
  53. {2675000, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block
  54. {4369999, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block
  55. {4370000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block
  56. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block
  57. {7280000, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block
  58. {9068999, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block
  59. {9069000, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // First Istanbul and first Muir Glacier block
  60. {9199999, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // Last Istanbul and first Muir Glacier block
  61. {9200000, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // First Muir Glacier block
  62. {12243999, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
  63. {12244000, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
  64. {12964999, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
  65. {12965000, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
  66. {13772999, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
  67. {13773000, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
  68. {15049999, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
  69. {15050000, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0xf42400}}, // First Gray Glacier block
  70. {20000000, ID{Hash: checksumToBytes(0x20e53762), Next: 0}}, // Future Gray Glacier block
  71. },
  72. },
  73. // Ropsten test cases
  74. {
  75. params.RopstenChainConfig,
  76. params.RopstenGenesisHash,
  77. []testcase{
  78. {0, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}}, // Unsynced, last Frontier, Homestead and first Tangerine block
  79. {9, ID{Hash: checksumToBytes(0x30c7ddbc), Next: 10}}, // Last Tangerine block
  80. {10, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}}, // First Spurious block
  81. {1699999, ID{Hash: checksumToBytes(0x63760190), Next: 1700000}}, // Last Spurious block
  82. {1700000, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}}, // First Byzantium block
  83. {4229999, ID{Hash: checksumToBytes(0x3ea159c7), Next: 4230000}}, // Last Byzantium block
  84. {4230000, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}}, // First Constantinople block
  85. {4939393, ID{Hash: checksumToBytes(0x97b544f3), Next: 4939394}}, // Last Constantinople block
  86. {4939394, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}}, // First Petersburg block
  87. {6485845, ID{Hash: checksumToBytes(0xd6e2149b), Next: 6485846}}, // Last Petersburg block
  88. {6485846, ID{Hash: checksumToBytes(0x4bc66396), Next: 7117117}}, // First Istanbul block
  89. {7117116, ID{Hash: checksumToBytes(0x4bc66396), Next: 7117117}}, // Last Istanbul block
  90. {7117117, ID{Hash: checksumToBytes(0x6727ef90), Next: 9812189}}, // First Muir Glacier block
  91. {9812188, ID{Hash: checksumToBytes(0x6727ef90), Next: 9812189}}, // Last Muir Glacier block
  92. {9812189, ID{Hash: checksumToBytes(0xa157d377), Next: 10499401}}, // First Berlin block
  93. {10499400, ID{Hash: checksumToBytes(0xa157d377), Next: 10499401}}, // Last Berlin block
  94. {10499401, ID{Hash: checksumToBytes(0x7119b6b3), Next: 0}}, // First London block
  95. {11000000, ID{Hash: checksumToBytes(0x7119b6b3), Next: 0}}, // Future London block
  96. },
  97. },
  98. // Rinkeby test cases
  99. {
  100. params.RinkebyChainConfig,
  101. params.RinkebyGenesisHash,
  102. []testcase{
  103. {0, ID{Hash: checksumToBytes(0x3b8e0691), Next: 1}}, // Unsynced, last Frontier block
  104. {1, ID{Hash: checksumToBytes(0x60949295), Next: 2}}, // First and last Homestead block
  105. {2, ID{Hash: checksumToBytes(0x8bde40dd), Next: 3}}, // First and last Tangerine block
  106. {3, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // First Spurious block
  107. {1035300, ID{Hash: checksumToBytes(0xcb3a64bb), Next: 1035301}}, // Last Spurious block
  108. {1035301, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // First Byzantium block
  109. {3660662, ID{Hash: checksumToBytes(0x8d748b57), Next: 3660663}}, // Last Byzantium block
  110. {3660663, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // First Constantinople block
  111. {4321233, ID{Hash: checksumToBytes(0xe49cab14), Next: 4321234}}, // Last Constantinople block
  112. {4321234, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // First Petersburg block
  113. {5435344, ID{Hash: checksumToBytes(0xafec6b27), Next: 5435345}}, // Last Petersburg block
  114. {5435345, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // First Istanbul block
  115. {8290927, ID{Hash: checksumToBytes(0xcbdb8838), Next: 8290928}}, // Last Istanbul block
  116. {8290928, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // First Berlin block
  117. {8897987, ID{Hash: checksumToBytes(0x6910c8bd), Next: 8897988}}, // Last Berlin block
  118. {8897988, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}}, // First London block
  119. {10000000, ID{Hash: checksumToBytes(0x8E29F2F3), Next: 0}}, // Future London block
  120. },
  121. },
  122. // Goerli test cases
  123. {
  124. params.GoerliChainConfig,
  125. params.GoerliGenesisHash,
  126. []testcase{
  127. {0, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople and first Petersburg block
  128. {1561650, ID{Hash: checksumToBytes(0xa3f5ab08), Next: 1561651}}, // Last Petersburg block
  129. {1561651, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // First Istanbul block
  130. {4460643, ID{Hash: checksumToBytes(0xc25efa5c), Next: 4460644}}, // Last Istanbul block
  131. {4460644, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // First Berlin block
  132. {5000000, ID{Hash: checksumToBytes(0x757a1c47), Next: 5062605}}, // Last Berlin block
  133. {5062605, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}}, // First London block
  134. {6000000, ID{Hash: checksumToBytes(0xB8C6299D), Next: 0}}, // Future London block
  135. },
  136. },
  137. // Sepolia test cases
  138. {
  139. params.SepoliaChainConfig,
  140. params.SepoliaGenesisHash,
  141. []testcase{
  142. {0, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}}, // Unsynced, last Frontier, Homestead, Tangerine, Spurious, Byzantium, Constantinople, Petersburg, Istanbul, Berlin and first London block
  143. {1735370, ID{Hash: checksumToBytes(0xfe3366e7), Next: 1735371}}, // Last London block
  144. {1735371, ID{Hash: checksumToBytes(0xb96cbd13), Next: 0}}, // First MergeNetsplit block
  145. },
  146. },
  147. // Merge test cases
  148. {
  149. &mergeConfig,
  150. params.MainnetGenesisHash,
  151. []testcase{
  152. {0, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Unsynced
  153. {1149999, ID{Hash: checksumToBytes(0xfc64ec04), Next: 1150000}}, // Last Frontier block
  154. {1150000, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // First Homestead block
  155. {1919999, ID{Hash: checksumToBytes(0x97c2c34c), Next: 1920000}}, // Last Homestead block
  156. {1920000, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // First DAO block
  157. {2462999, ID{Hash: checksumToBytes(0x91d1f948), Next: 2463000}}, // Last DAO block
  158. {2463000, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // First Tangerine block
  159. {2674999, ID{Hash: checksumToBytes(0x7a64da13), Next: 2675000}}, // Last Tangerine block
  160. {2675000, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // First Spurious block
  161. {4369999, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}}, // Last Spurious block
  162. {4370000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // First Byzantium block
  163. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}}, // Last Byzantium block
  164. {7280000, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // First and last Constantinople, first Petersburg block
  165. {9068999, ID{Hash: checksumToBytes(0x668db0af), Next: 9069000}}, // Last Petersburg block
  166. {9069000, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // First Istanbul and first Muir Glacier block
  167. {9199999, ID{Hash: checksumToBytes(0x879d6e30), Next: 9200000}}, // Last Istanbul and first Muir Glacier block
  168. {9200000, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // First Muir Glacier block
  169. {12243999, ID{Hash: checksumToBytes(0xe029e991), Next: 12244000}}, // Last Muir Glacier block
  170. {12244000, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // First Berlin block
  171. {12964999, ID{Hash: checksumToBytes(0x0eb440f6), Next: 12965000}}, // Last Berlin block
  172. {12965000, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // First London block
  173. {13772999, ID{Hash: checksumToBytes(0xb715077d), Next: 13773000}}, // Last London block
  174. {13773000, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // First Arrow Glacier block
  175. {15049999, ID{Hash: checksumToBytes(0x20c327fc), Next: 15050000}}, // Last Arrow Glacier block
  176. {15050000, ID{Hash: checksumToBytes(0xf0afd0e3), Next: 0xf42400}}, // First Gray Glacier block
  177. {18000000, ID{Hash: checksumToBytes(0x3118e3fc), Next: 0}}, // First Merge Start block
  178. {20000000, ID{Hash: checksumToBytes(0x3118e3fc), Next: 0}}, // Future Merge Start block
  179. },
  180. },
  181. }
  182. for i, tt := range tests {
  183. for j, ttt := range tt.cases {
  184. if have := NewID(tt.config, tt.genesis, ttt.head); have != ttt.want {
  185. t.Errorf("test %d, case %d: fork ID mismatch: have %x, want %x", i, j, have, ttt.want)
  186. }
  187. }
  188. }
  189. }
  190. // TestValidation tests that a local peer correctly validates and accepts a remote
  191. // fork ID.
  192. func TestValidation(t *testing.T) {
  193. tests := []struct {
  194. head uint64
  195. id ID
  196. err error
  197. }{
  198. // Local is mainnet Petersburg, remote announces the same. No future fork is announced.
  199. {7987396, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil},
  200. // Local is mainnet Petersburg, remote announces the same. Remote also announces a next fork
  201. // at block 0xffffffff, but that is uncertain.
  202. {7987396, ID{Hash: checksumToBytes(0x668db0af), Next: math.MaxUint64}, nil},
  203. // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
  204. // also Byzantium, but it's not yet aware of Petersburg (e.g. non updated node before the fork).
  205. // In this case we don't know if Petersburg passed yet or not.
  206. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil},
  207. // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
  208. // also Byzantium, and it's also aware of Petersburg (e.g. updated node before the fork). We
  209. // don't know if Petersburg passed yet (will pass) or not.
  210. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
  211. // Local is mainnet currently in Byzantium only (so it's aware of Petersburg), remote announces
  212. // also Byzantium, and it's also aware of some random fork (e.g. misconfigured Petersburg). As
  213. // neither forks passed at neither nodes, they may mismatch, but we still connect for now.
  214. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: math.MaxUint64}, nil},
  215. // Local is mainnet exactly on Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
  216. // is simply out of sync, accept.
  217. {7280000, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
  218. // Local is mainnet Petersburg, remote announces Byzantium + knowledge about Petersburg. Remote
  219. // is simply out of sync, accept.
  220. {7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 7280000}, nil},
  221. // Local is mainnet Petersburg, remote announces Spurious + knowledge about Byzantium. Remote
  222. // is definitely out of sync. It may or may not need the Petersburg update, we don't know yet.
  223. {7987396, ID{Hash: checksumToBytes(0x3edd5b10), Next: 4370000}, nil},
  224. // Local is mainnet Byzantium, remote announces Petersburg. Local is out of sync, accept.
  225. {7279999, ID{Hash: checksumToBytes(0x668db0af), Next: 0}, nil},
  226. // Local is mainnet Spurious, remote announces Byzantium, but is not aware of Petersburg. Local
  227. // out of sync. Local also knows about a future fork, but that is uncertain yet.
  228. {4369999, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, nil},
  229. // Local is mainnet Petersburg. remote announces Byzantium but is not aware of further forks.
  230. // Remote needs software update.
  231. {7987396, ID{Hash: checksumToBytes(0xa00bc324), Next: 0}, ErrRemoteStale},
  232. // Local is mainnet Petersburg, and isn't aware of more forks. Remote announces Petersburg +
  233. // 0xffffffff. Local needs software update, reject.
  234. {7987396, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale},
  235. // Local is mainnet Byzantium, and is aware of Petersburg. Remote announces Petersburg +
  236. // 0xffffffff. Local needs software update, reject.
  237. {7279999, ID{Hash: checksumToBytes(0x5cddc0e1), Next: 0}, ErrLocalIncompatibleOrStale},
  238. // Local is mainnet Petersburg, remote is Rinkeby Petersburg.
  239. {7987396, ID{Hash: checksumToBytes(0xafec6b27), Next: 0}, ErrLocalIncompatibleOrStale},
  240. // Local is mainnet Gray Glacier, far in the future. Remote announces Gopherium (non existing fork)
  241. // at some future block 88888888, for itself, but past block for local. Local is incompatible.
  242. //
  243. // This case detects non-upgraded nodes with majority hash power (typical Ropsten mess).
  244. {88888888, ID{Hash: checksumToBytes(0x20e53762), Next: 88888888}, ErrLocalIncompatibleOrStale},
  245. // Local is mainnet Byzantium. Remote is also in Byzantium, but announces Gopherium (non existing
  246. // fork) at block 7279999, before Petersburg. Local is incompatible.
  247. {7279999, ID{Hash: checksumToBytes(0xa00bc324), Next: 7279999}, ErrLocalIncompatibleOrStale},
  248. }
  249. for i, tt := range tests {
  250. filter := newFilter(params.MainnetChainConfig, params.MainnetGenesisHash, func() uint64 { return tt.head })
  251. if err := filter(tt.id); err != tt.err {
  252. t.Errorf("test %d: validation error mismatch: have %v, want %v", i, err, tt.err)
  253. }
  254. }
  255. }
  256. // Tests that IDs are properly RLP encoded (specifically important because we
  257. // use uint32 to store the hash, but we need to encode it as [4]byte).
  258. func TestEncoding(t *testing.T) {
  259. tests := []struct {
  260. id ID
  261. want []byte
  262. }{
  263. {ID{Hash: checksumToBytes(0), Next: 0}, common.Hex2Bytes("c6840000000080")},
  264. {ID{Hash: checksumToBytes(0xdeadbeef), Next: 0xBADDCAFE}, common.Hex2Bytes("ca84deadbeef84baddcafe,")},
  265. {ID{Hash: checksumToBytes(math.MaxUint32), Next: math.MaxUint64}, common.Hex2Bytes("ce84ffffffff88ffffffffffffffff")},
  266. }
  267. for i, tt := range tests {
  268. have, err := rlp.EncodeToBytes(tt.id)
  269. if err != nil {
  270. t.Errorf("test %d: failed to encode forkid: %v", i, err)
  271. continue
  272. }
  273. if !bytes.Equal(have, tt.want) {
  274. t.Errorf("test %d: RLP mismatch: have %x, want %x", i, have, tt.want)
  275. }
  276. }
  277. }