chain_util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 int64, parentTime int64, parentDiff *big.Int) *big.Int {
  31. diff := new(big.Int)
  32. adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
  33. if big.NewInt(time-parentTime).Cmp(params.DurationLimit) < 0 {
  34. diff.Add(parentDiff, adjust)
  35. } else {
  36. diff.Sub(parentDiff, adjust)
  37. }
  38. if diff.Cmp(params.MinimumDifficulty) < 0 {
  39. return params.MinimumDifficulty
  40. }
  41. return diff
  42. }
  43. // CalcTD computes the total difficulty of block.
  44. func CalcTD(block, parent *types.Block) *big.Int {
  45. if parent == nil {
  46. return block.Difficulty()
  47. }
  48. d := block.Difficulty()
  49. d.Add(d, parent.Td)
  50. return d
  51. }
  52. // CalcGasLimit computes the gas limit of the next block after parent.
  53. // The result may be modified by the caller.
  54. func CalcGasLimit(parent *types.Block) *big.Int {
  55. decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
  56. contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
  57. contrib = contrib.Div(contrib, big.NewInt(2))
  58. contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
  59. gl := new(big.Int).Sub(parent.GasLimit(), decay)
  60. gl = gl.Add(gl, contrib)
  61. gl = gl.Add(gl, big.NewInt(1))
  62. gl.Set(common.BigMax(gl, params.MinGasLimit))
  63. if gl.Cmp(params.GenesisGasLimit) < 0 {
  64. gl.Add(parent.GasLimit(), decay)
  65. gl.Set(common.BigMin(gl, params.GenesisGasLimit))
  66. }
  67. return gl
  68. }
  69. // GetBlockByHash returns the block corresponding to the hash or nil if not found
  70. func GetBlockByHash(db common.Database, hash common.Hash) *types.Block {
  71. data, _ := db.Get(append(blockHashPre, hash[:]...))
  72. if len(data) == 0 {
  73. return nil
  74. }
  75. var block types.StorageBlock
  76. if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
  77. glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
  78. return nil
  79. }
  80. return (*types.Block)(&block)
  81. }
  82. // GetBlockByHash returns the canonical block by number or nil if not found
  83. func GetBlockByNumber(db common.Database, number uint64) *types.Block {
  84. key, _ := db.Get(append(blockNumPre, big.NewInt(int64(number)).Bytes()...))
  85. if len(key) == 0 {
  86. return nil
  87. }
  88. return GetBlockByHash(db, common.BytesToHash(key))
  89. }
  90. // WriteHead force writes the current head
  91. func WriteHead(db common.Database, block *types.Block) error {
  92. key := append(blockNumPre, block.Number().Bytes()...)
  93. err := db.Put(key, block.Hash().Bytes())
  94. if err != nil {
  95. return err
  96. }
  97. err = db.Put([]byte("LastBlock"), block.Hash().Bytes())
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }