chain_util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "bytes"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/params"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. )
  27. // CalcDifficulty is the difficulty adjustment algorithm. It returns
  28. // the difficulty that a new block b should have when created at time
  29. // given the parent block's time and difficulty.
  30. func CalcDifficulty(time, parentTime uint64, parentDiff *big.Int) *big.Int {
  31. diff := new(big.Int)
  32. adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
  33. bigTime := new(big.Int)
  34. bigParentTime := new(big.Int)
  35. bigTime.SetUint64(time)
  36. bigParentTime.SetUint64(parentTime)
  37. if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
  38. diff.Add(parentDiff, adjust)
  39. } else {
  40. diff.Sub(parentDiff, adjust)
  41. }
  42. if diff.Cmp(params.MinimumDifficulty) < 0 {
  43. return params.MinimumDifficulty
  44. }
  45. return diff
  46. }
  47. // CalcTD computes the total difficulty of block.
  48. func CalcTD(block, parent *types.Block) *big.Int {
  49. if parent == nil {
  50. return block.Difficulty()
  51. }
  52. d := block.Difficulty()
  53. d.Add(d, parent.Td)
  54. return d
  55. }
  56. // CalcGasLimit computes the gas limit of the next block after parent.
  57. // The result may be modified by the caller.
  58. func CalcGasLimit(parent *types.Block) *big.Int {
  59. decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
  60. contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
  61. contrib = contrib.Div(contrib, big.NewInt(2))
  62. contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
  63. gl := new(big.Int).Sub(parent.GasLimit(), decay)
  64. gl = gl.Add(gl, contrib)
  65. gl = gl.Add(gl, big.NewInt(1))
  66. gl.Set(common.BigMax(gl, params.MinGasLimit))
  67. if gl.Cmp(params.GenesisGasLimit) < 0 {
  68. gl.Add(parent.GasLimit(), decay)
  69. gl.Set(common.BigMin(gl, params.GenesisGasLimit))
  70. }
  71. return gl
  72. }
  73. // GetBlockByHash returns the block corresponding to the hash or nil if not found
  74. func GetBlockByHash(db common.Database, hash common.Hash) *types.Block {
  75. data, _ := db.Get(append(blockHashPre, hash[:]...))
  76. if len(data) == 0 {
  77. return nil
  78. }
  79. var block types.StorageBlock
  80. if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
  81. glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
  82. return nil
  83. }
  84. return (*types.Block)(&block)
  85. }
  86. // GetBlockByHash returns the canonical block by number or nil if not found
  87. func GetBlockByNumber(db common.Database, number uint64) *types.Block {
  88. key, _ := db.Get(append(blockNumPre, big.NewInt(int64(number)).Bytes()...))
  89. if len(key) == 0 {
  90. return nil
  91. }
  92. return GetBlockByHash(db, common.BytesToHash(key))
  93. }
  94. // WriteHead force writes the current head
  95. func WriteHead(db common.Database, block *types.Block) error {
  96. key := append(blockNumPre, block.Number().Bytes()...)
  97. err := db.Put(key, block.Hash().Bytes())
  98. if err != nil {
  99. return err
  100. }
  101. err = db.Put([]byte("LastBlock"), block.Hash().Bytes())
  102. if err != nil {
  103. return err
  104. }
  105. return nil
  106. }