config.go 11 KB

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