config.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2016 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 params
  17. import (
  18. "fmt"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. // Genesis hashes to enforce below configs on.
  23. var (
  24. MainnetGenesisHash = common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3")
  25. TestnetGenesisHash = common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d")
  26. )
  27. var (
  28. // MainnetChainConfig is the chain parameters to run a node on the main network.
  29. MainnetChainConfig = &ChainConfig{
  30. ChainID: big.NewInt(1),
  31. HomesteadBlock: big.NewInt(1150000),
  32. DAOForkBlock: big.NewInt(1920000),
  33. DAOForkSupport: true,
  34. EIP150Block: big.NewInt(2463000),
  35. EIP150Hash: common.HexToHash("0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0"),
  36. EIP155Block: big.NewInt(2675000),
  37. EIP158Block: big.NewInt(2675000),
  38. ByzantiumBlock: big.NewInt(4370000),
  39. ConstantinopleBlock: nil,
  40. Ethash: new(EthashConfig),
  41. }
  42. // TestnetChainConfig contains the chain parameters to run a node on the Ropsten test network.
  43. TestnetChainConfig = &ChainConfig{
  44. ChainID: big.NewInt(3),
  45. HomesteadBlock: big.NewInt(0),
  46. DAOForkBlock: nil,
  47. DAOForkSupport: true,
  48. EIP150Block: big.NewInt(0),
  49. EIP150Hash: common.HexToHash("0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"),
  50. EIP155Block: big.NewInt(10),
  51. EIP158Block: big.NewInt(10),
  52. ByzantiumBlock: big.NewInt(1700000),
  53. ConstantinopleBlock: nil,
  54. Ethash: new(EthashConfig),
  55. }
  56. // RinkebyChainConfig contains the chain parameters to run a node on the Rinkeby test network.
  57. RinkebyChainConfig = &ChainConfig{
  58. ChainID: big.NewInt(4),
  59. HomesteadBlock: big.NewInt(1),
  60. DAOForkBlock: nil,
  61. DAOForkSupport: true,
  62. EIP150Block: big.NewInt(2),
  63. EIP150Hash: common.HexToHash("0x9b095b36c15eaf13044373aef8ee0bd3a382a5abb92e402afa44b8249c3a90e9"),
  64. EIP155Block: big.NewInt(3),
  65. EIP158Block: big.NewInt(3),
  66. ByzantiumBlock: big.NewInt(1035301),
  67. ConstantinopleBlock: nil,
  68. Clique: &CliqueConfig{
  69. Period: 15,
  70. Epoch: 30000,
  71. },
  72. }
  73. // AllEthashProtocolChanges contains every protocol change (EIPs) introduced
  74. // and accepted by the Ethereum core developers into the Ethash consensus.
  75. //
  76. // This configuration is intentionally not using keyed fields to force anyone
  77. // adding flags to the config to also have to set these fields.
  78. AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
  79. // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced
  80. // and accepted by the Ethereum core developers into the Clique consensus.
  81. //
  82. // This configuration is intentionally not using keyed fields to force anyone
  83. // adding flags to the config to also have to set these fields.
  84. AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}}
  85. TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil}
  86. TestRules = TestChainConfig.Rules(new(big.Int))
  87. )
  88. // ChainConfig is the core config which determines the blockchain settings.
  89. //
  90. // ChainConfig is stored in the database on a per block basis. This means
  91. // that any network, identified by its genesis block, can have its own
  92. // set of configuration options.
  93. type ChainConfig struct {
  94. ChainID *big.Int `json:"chainId"` // chainId identifies the current chain and is used for replay protection
  95. HomesteadBlock *big.Int `json:"homesteadBlock,omitempty"` // Homestead switch block (nil = no fork, 0 = already homestead)
  96. DAOForkBlock *big.Int `json:"daoForkBlock,omitempty"` // TheDAO hard-fork switch block (nil = no fork)
  97. DAOForkSupport bool `json:"daoForkSupport,omitempty"` // Whether the nodes supports or opposes the DAO hard-fork
  98. // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
  99. EIP150Block *big.Int `json:"eip150Block,omitempty"` // EIP150 HF block (nil = no fork)
  100. EIP150Hash common.Hash `json:"eip150Hash,omitempty"` // EIP150 HF hash (needed for header only clients as only gas pricing changed)
  101. EIP155Block *big.Int `json:"eip155Block,omitempty"` // EIP155 HF block
  102. EIP158Block *big.Int `json:"eip158Block,omitempty"` // EIP158 HF block
  103. ByzantiumBlock *big.Int `json:"byzantiumBlock,omitempty"` // Byzantium switch block (nil = no fork, 0 = already on byzantium)
  104. ConstantinopleBlock *big.Int `json:"constantinopleBlock,omitempty"` // Constantinople switch block (nil = no fork, 0 = already activated)
  105. // Various consensus engines
  106. Ethash *EthashConfig `json:"ethash,omitempty"`
  107. Clique *CliqueConfig `json:"clique,omitempty"`
  108. }
  109. // EthashConfig is the consensus engine configs for proof-of-work based sealing.
  110. type EthashConfig struct{}
  111. // String implements the stringer interface, returning the consensus engine details.
  112. func (c *EthashConfig) String() string {
  113. return "ethash"
  114. }
  115. // CliqueConfig is the consensus engine configs for proof-of-authority based sealing.
  116. type CliqueConfig struct {
  117. Period uint64 `json:"period"` // Number of seconds between blocks to enforce
  118. Epoch uint64 `json:"epoch"` // Epoch length to reset votes and checkpoint
  119. }
  120. // String implements the stringer interface, returning the consensus engine details.
  121. func (c *CliqueConfig) String() string {
  122. return "clique"
  123. }
  124. // String implements the fmt.Stringer interface.
  125. func (c *ChainConfig) String() string {
  126. var engine interface{}
  127. switch {
  128. case c.Ethash != nil:
  129. engine = c.Ethash
  130. case c.Clique != nil:
  131. engine = c.Clique
  132. default:
  133. engine = "unknown"
  134. }
  135. return fmt.Sprintf("{ChainID: %v Homestead: %v DAO: %v DAOSupport: %v EIP150: %v EIP155: %v EIP158: %v Byzantium: %v Constantinople: %v Engine: %v}",
  136. c.ChainID,
  137. c.HomesteadBlock,
  138. c.DAOForkBlock,
  139. c.DAOForkSupport,
  140. c.EIP150Block,
  141. c.EIP155Block,
  142. c.EIP158Block,
  143. c.ByzantiumBlock,
  144. c.ConstantinopleBlock,
  145. engine,
  146. )
  147. }
  148. // IsHomestead returns whether num is either equal to the homestead block or greater.
  149. func (c *ChainConfig) IsHomestead(num *big.Int) bool {
  150. return isForked(c.HomesteadBlock, num)
  151. }
  152. // IsDAOFork returns whether num is either equal to the DAO fork block or greater.
  153. func (c *ChainConfig) IsDAOFork(num *big.Int) bool {
  154. return isForked(c.DAOForkBlock, num)
  155. }
  156. // IsEIP150 returns whether num is either equal to the EIP150 fork block or greater.
  157. func (c *ChainConfig) IsEIP150(num *big.Int) bool {
  158. return isForked(c.EIP150Block, num)
  159. }
  160. // IsEIP155 returns whether num is either equal to the EIP155 fork block or greater.
  161. func (c *ChainConfig) IsEIP155(num *big.Int) bool {
  162. return isForked(c.EIP155Block, num)
  163. }
  164. // IsEIP158 returns whether num is either equal to the EIP158 fork block or greater.
  165. func (c *ChainConfig) IsEIP158(num *big.Int) bool {
  166. return isForked(c.EIP158Block, num)
  167. }
  168. // IsByzantium returns whether num is either equal to the Byzantium fork block or greater.
  169. func (c *ChainConfig) IsByzantium(num *big.Int) bool {
  170. return isForked(c.ByzantiumBlock, num)
  171. }
  172. // IsConstantinople returns whether num is either equal to the Constantinople fork block or greater.
  173. func (c *ChainConfig) IsConstantinople(num *big.Int) bool {
  174. return isForked(c.ConstantinopleBlock, num)
  175. }
  176. // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice).
  177. //
  178. // The returned GasTable's fields shouldn't, under any circumstances, be changed.
  179. func (c *ChainConfig) GasTable(num *big.Int) GasTable {
  180. if num == nil {
  181. return GasTableHomestead
  182. }
  183. switch {
  184. case c.IsConstantinople(num):
  185. return GasTableConstantinople
  186. case c.IsEIP158(num):
  187. return GasTableEIP158
  188. case c.IsEIP150(num):
  189. return GasTableEIP150
  190. default:
  191. return GasTableHomestead
  192. }
  193. }
  194. // CheckCompatible checks whether scheduled fork transitions have been imported
  195. // with a mismatching chain configuration.
  196. func (c *ChainConfig) CheckCompatible(newcfg *ChainConfig, height uint64) *ConfigCompatError {
  197. bhead := new(big.Int).SetUint64(height)
  198. // Iterate checkCompatible to find the lowest conflict.
  199. var lasterr *ConfigCompatError
  200. for {
  201. err := c.checkCompatible(newcfg, bhead)
  202. if err == nil || (lasterr != nil && err.RewindTo == lasterr.RewindTo) {
  203. break
  204. }
  205. lasterr = err
  206. bhead.SetUint64(err.RewindTo)
  207. }
  208. return lasterr
  209. }
  210. func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *ConfigCompatError {
  211. if isForkIncompatible(c.HomesteadBlock, newcfg.HomesteadBlock, head) {
  212. return newCompatError("Homestead fork block", c.HomesteadBlock, newcfg.HomesteadBlock)
  213. }
  214. if isForkIncompatible(c.DAOForkBlock, newcfg.DAOForkBlock, head) {
  215. return newCompatError("DAO fork block", c.DAOForkBlock, newcfg.DAOForkBlock)
  216. }
  217. if c.IsDAOFork(head) && c.DAOForkSupport != newcfg.DAOForkSupport {
  218. return newCompatError("DAO fork support flag", c.DAOForkBlock, newcfg.DAOForkBlock)
  219. }
  220. if isForkIncompatible(c.EIP150Block, newcfg.EIP150Block, head) {
  221. return newCompatError("EIP150 fork block", c.EIP150Block, newcfg.EIP150Block)
  222. }
  223. if isForkIncompatible(c.EIP155Block, newcfg.EIP155Block, head) {
  224. return newCompatError("EIP155 fork block", c.EIP155Block, newcfg.EIP155Block)
  225. }
  226. if isForkIncompatible(c.EIP158Block, newcfg.EIP158Block, head) {
  227. return newCompatError("EIP158 fork block", c.EIP158Block, newcfg.EIP158Block)
  228. }
  229. if c.IsEIP158(head) && !configNumEqual(c.ChainID, newcfg.ChainID) {
  230. return newCompatError("EIP158 chain ID", c.EIP158Block, newcfg.EIP158Block)
  231. }
  232. if isForkIncompatible(c.ByzantiumBlock, newcfg.ByzantiumBlock, head) {
  233. return newCompatError("Byzantium fork block", c.ByzantiumBlock, newcfg.ByzantiumBlock)
  234. }
  235. if isForkIncompatible(c.ConstantinopleBlock, newcfg.ConstantinopleBlock, head) {
  236. return newCompatError("Constantinople fork block", c.ConstantinopleBlock, newcfg.ConstantinopleBlock)
  237. }
  238. return nil
  239. }
  240. // isForkIncompatible returns true if a fork scheduled at s1 cannot be rescheduled to
  241. // block s2 because head is already past the fork.
  242. func isForkIncompatible(s1, s2, head *big.Int) bool {
  243. return (isForked(s1, head) || isForked(s2, head)) && !configNumEqual(s1, s2)
  244. }
  245. // isForked returns whether a fork scheduled at block s is active at the given head block.
  246. func isForked(s, head *big.Int) bool {
  247. if s == nil || head == nil {
  248. return false
  249. }
  250. return s.Cmp(head) <= 0
  251. }
  252. func configNumEqual(x, y *big.Int) bool {
  253. if x == nil {
  254. return y == nil
  255. }
  256. if y == nil {
  257. return x == nil
  258. }
  259. return x.Cmp(y) == 0
  260. }
  261. // ConfigCompatError is raised if the locally-stored blockchain is initialised with a
  262. // ChainConfig that would alter the past.
  263. type ConfigCompatError struct {
  264. What string
  265. // block numbers of the stored and new configurations
  266. StoredConfig, NewConfig *big.Int
  267. // the block number to which the local chain must be rewound to correct the error
  268. RewindTo uint64
  269. }
  270. func newCompatError(what string, storedblock, newblock *big.Int) *ConfigCompatError {
  271. var rew *big.Int
  272. switch {
  273. case storedblock == nil:
  274. rew = newblock
  275. case newblock == nil || storedblock.Cmp(newblock) < 0:
  276. rew = storedblock
  277. default:
  278. rew = newblock
  279. }
  280. err := &ConfigCompatError{what, storedblock, newblock, 0}
  281. if rew != nil && rew.Sign() > 0 {
  282. err.RewindTo = rew.Uint64() - 1
  283. }
  284. return err
  285. }
  286. func (err *ConfigCompatError) Error() string {
  287. return fmt.Sprintf("mismatching %s in database (have %d, want %d, rewindto %d)", err.What, err.StoredConfig, err.NewConfig, err.RewindTo)
  288. }
  289. // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
  290. // that do not have or require information about the block.
  291. //
  292. // Rules is a one time interface meaning that it shouldn't be used in between transition
  293. // phases.
  294. type Rules struct {
  295. ChainID *big.Int
  296. IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
  297. IsByzantium bool
  298. }
  299. // Rules ensures c's ChainID is not nil.
  300. func (c *ChainConfig) Rules(num *big.Int) Rules {
  301. chainID := c.ChainID
  302. if chainID == nil {
  303. chainID = new(big.Int)
  304. }
  305. return Rules{ChainID: new(big.Int).Set(chainID), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num), IsByzantium: c.IsByzantium(num)}
  306. }