forkid_test.go 12 KB

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