config.go 12 KB

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