config.go 13 KB

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