config.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. )
  21. // MainnetChainConfig is the chain parameters to run a node on the main network.
  22. var MainnetChainConfig = &ChainConfig{
  23. HomesteadBlock: MainNetHomesteadBlock,
  24. DAOForkBlock: MainNetDAOForkBlock,
  25. DAOForkSupport: true,
  26. EIP150Block: MainNetHomesteadGasRepriceBlock,
  27. EIP150Hash: MainNetHomesteadGasRepriceHash,
  28. EIP155Block: MainNetSpuriousDragon,
  29. EIP158Block: MainNetSpuriousDragon,
  30. }
  31. // TestnetChainConfig is the chain parameters to run a node on the test network.
  32. var TestnetChainConfig = &ChainConfig{
  33. HomesteadBlock: TestNetHomesteadBlock,
  34. DAOForkBlock: TestNetDAOForkBlock,
  35. DAOForkSupport: false,
  36. EIP150Block: TestNetHomesteadGasRepriceBlock,
  37. EIP150Hash: TestNetHomesteadGasRepriceHash,
  38. EIP155Block: TestNetSpuriousDragon,
  39. EIP158Block: TestNetSpuriousDragon,
  40. }
  41. // ChainConfig is the core config which determines the blockchain settings.
  42. //
  43. // ChainConfig is stored in the database on a per block basis. This means
  44. // that any network, identified by its genesis block, can have its own
  45. // set of configuration options.
  46. type ChainConfig struct {
  47. ChainId *big.Int `json:"chainId"` // Chain id identifies the current chain and is used for replay protection
  48. HomesteadBlock *big.Int `json:"homesteadBlock"` // Homestead switch block (nil = no fork, 0 = already homestead)
  49. DAOForkBlock *big.Int `json:"daoForkBlock"` // TheDAO hard-fork switch block (nil = no fork)
  50. DAOForkSupport bool `json:"daoForkSupport"` // Whether the nodes supports or opposes the DAO hard-fork
  51. // EIP150 implements the Gas price changes (https://github.com/ethereum/EIPs/issues/150)
  52. EIP150Block *big.Int `json:"eip150Block"` // EIP150 HF block (nil = no fork)
  53. EIP150Hash common.Hash `json:"eip150Hash"` // EIP150 HF hash (fast sync aid)
  54. EIP155Block *big.Int `json:"eip155Block"` // EIP155 HF block
  55. EIP158Block *big.Int `json:"eip158Block"` // EIP158 HF block
  56. }
  57. var (
  58. TestChainConfig = &ChainConfig{big.NewInt(1), new(big.Int), new(big.Int), true, new(big.Int), common.Hash{}, new(big.Int), new(big.Int)}
  59. TestRules = TestChainConfig.Rules(new(big.Int))
  60. )
  61. // IsHomestead returns whether num is either equal to the homestead block or greater.
  62. func (c *ChainConfig) IsHomestead(num *big.Int) bool {
  63. if c.HomesteadBlock == nil || num == nil {
  64. return false
  65. }
  66. return num.Cmp(c.HomesteadBlock) >= 0
  67. }
  68. // GasTable returns the gas table corresponding to the current phase (homestead or homestead reprice).
  69. //
  70. // The returned GasTable's fields shouldn't, under any circumstances, be changed.
  71. func (c *ChainConfig) GasTable(num *big.Int) GasTable {
  72. if num == nil {
  73. return GasTableHomestead
  74. }
  75. switch {
  76. case c.EIP158Block != nil && num.Cmp(c.EIP158Block) >= 0:
  77. return GasTableEIP158
  78. case c.EIP150Block != nil && num.Cmp(c.EIP150Block) >= 0:
  79. return GasTableHomesteadGasRepriceFork
  80. default:
  81. return GasTableHomestead
  82. }
  83. }
  84. func (c *ChainConfig) IsEIP150(num *big.Int) bool {
  85. if c.EIP150Block == nil || num == nil {
  86. return false
  87. }
  88. return num.Cmp(c.EIP150Block) >= 0
  89. }
  90. func (c *ChainConfig) IsEIP155(num *big.Int) bool {
  91. if c.EIP155Block == nil || num == nil {
  92. return false
  93. }
  94. return num.Cmp(c.EIP155Block) >= 0
  95. }
  96. func (c *ChainConfig) IsEIP158(num *big.Int) bool {
  97. if c.EIP158Block == nil || num == nil {
  98. return false
  99. }
  100. return num.Cmp(c.EIP158Block) >= 0
  101. }
  102. // Rules wraps ChainConfig and is merely syntatic sugar or can be used for functions
  103. // that do not have or require information about the block.
  104. //
  105. // Rules is a one time interface meaning that it shouldn't be used in between transition
  106. // phases.
  107. type Rules struct {
  108. ChainId *big.Int
  109. IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
  110. }
  111. func (c *ChainConfig) Rules(num *big.Int) Rules {
  112. return Rules{ChainId: new(big.Int).Set(c.ChainId), IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), IsEIP155: c.IsEIP155(num), IsEIP158: c.IsEIP158(num)}
  113. }