2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-07-01 17:15:02 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/big"
|
2015-07-10 15:29:40 +03:00
|
|
|
"time"
|
2015-07-01 17:15:02 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2015-07-10 15:29:40 +03:00
|
|
|
var (
|
2015-08-05 00:46:38 +03:00
|
|
|
blockHashPre = []byte("block-hash-")
|
|
|
|
blockNumPre = []byte("block-num-")
|
|
|
|
expDiffPeriod = big.NewInt(100000)
|
2015-07-10 15:29:40 +03:00
|
|
|
)
|
|
|
|
|
2015-07-01 17:15:02 +03:00
|
|
|
// CalcDifficulty is the difficulty adjustment algorithm. It returns
|
|
|
|
// the difficulty that a new block b should have when created at time
|
|
|
|
// given the parent block's time and difficulty.
|
2015-08-05 00:46:38 +03:00
|
|
|
func CalcDifficulty(time, parentTime uint64, parentNumber, parentDiff *big.Int) *big.Int {
|
2015-07-01 17:15:02 +03:00
|
|
|
diff := new(big.Int)
|
|
|
|
adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor)
|
2015-07-08 14:21:06 +03:00
|
|
|
bigTime := new(big.Int)
|
|
|
|
bigParentTime := new(big.Int)
|
|
|
|
|
|
|
|
bigTime.SetUint64(time)
|
|
|
|
bigParentTime.SetUint64(parentTime)
|
|
|
|
|
|
|
|
if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 {
|
2015-07-01 17:15:02 +03:00
|
|
|
diff.Add(parentDiff, adjust)
|
|
|
|
} else {
|
|
|
|
diff.Sub(parentDiff, adjust)
|
|
|
|
}
|
|
|
|
if diff.Cmp(params.MinimumDifficulty) < 0 {
|
2015-08-05 00:46:38 +03:00
|
|
|
diff = params.MinimumDifficulty
|
2015-07-01 17:15:02 +03:00
|
|
|
}
|
2015-08-05 00:46:38 +03:00
|
|
|
|
|
|
|
periodCount := new(big.Int).Add(parentNumber, common.Big1)
|
|
|
|
periodCount.Div(periodCount, expDiffPeriod)
|
|
|
|
if periodCount.Cmp(common.Big1) > 0 {
|
|
|
|
// diff = diff + 2^(periodCount - 2)
|
|
|
|
expDiff := periodCount.Sub(periodCount, common.Big2)
|
|
|
|
expDiff.Exp(common.Big2, expDiff, nil)
|
|
|
|
diff.Add(diff, expDiff)
|
|
|
|
diff = common.BigMax(diff, params.MinimumDifficulty)
|
|
|
|
}
|
|
|
|
|
2015-07-01 17:15:02 +03:00
|
|
|
return diff
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcTD computes the total difficulty of block.
|
|
|
|
func CalcTD(block, parent *types.Block) *big.Int {
|
|
|
|
if parent == nil {
|
|
|
|
return block.Difficulty()
|
|
|
|
}
|
|
|
|
d := block.Difficulty()
|
|
|
|
d.Add(d, parent.Td)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcGasLimit computes the gas limit of the next block after parent.
|
|
|
|
// The result may be modified by the caller.
|
2015-08-03 03:46:34 +03:00
|
|
|
// This is miner strategy, not consensus protocol.
|
2015-07-01 17:15:02 +03:00
|
|
|
func CalcGasLimit(parent *types.Block) *big.Int {
|
2015-08-03 03:46:34 +03:00
|
|
|
// contrib = (parentGasUsed * 3 / 2) / 1024
|
2015-07-01 17:15:02 +03:00
|
|
|
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
|
|
|
|
contrib = contrib.Div(contrib, big.NewInt(2))
|
|
|
|
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
|
|
|
|
|
2015-08-03 03:46:34 +03:00
|
|
|
// decay = parentGasLimit / 1024 -1
|
|
|
|
decay := new(big.Int).Div(parent.GasLimit(), params.GasLimitBoundDivisor)
|
|
|
|
decay.Sub(decay, big.NewInt(1))
|
|
|
|
|
|
|
|
/*
|
|
|
|
strategy: gasLimit of block-to-mine is set based on parent's
|
|
|
|
gasUsed value. if parentGasUsed > parentGasLimit * (2/3) then we
|
|
|
|
increase it, otherwise lower it (or leave it unchanged if it's right
|
|
|
|
at that usage) the amount increased/decreased depends on how far away
|
|
|
|
from parentGasLimit * (2/3) parentGasUsed is.
|
|
|
|
*/
|
2015-07-01 17:15:02 +03:00
|
|
|
gl := new(big.Int).Sub(parent.GasLimit(), decay)
|
|
|
|
gl = gl.Add(gl, contrib)
|
|
|
|
gl.Set(common.BigMax(gl, params.MinGasLimit))
|
|
|
|
|
2015-08-03 03:46:34 +03:00
|
|
|
// however, if we're now below the target (GenesisGasLimit) we increase the
|
|
|
|
// limit as much as we can (parentGasLimit / 1024 -1)
|
2015-07-01 17:15:02 +03:00
|
|
|
if gl.Cmp(params.GenesisGasLimit) < 0 {
|
|
|
|
gl.Add(parent.GasLimit(), decay)
|
|
|
|
gl.Set(common.BigMin(gl, params.GenesisGasLimit))
|
|
|
|
}
|
|
|
|
return gl
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the block corresponding to the hash or nil if not found
|
|
|
|
func GetBlockByHash(db common.Database, hash common.Hash) *types.Block {
|
|
|
|
data, _ := db.Get(append(blockHashPre, hash[:]...))
|
|
|
|
if len(data) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var block types.StorageBlock
|
|
|
|
if err := rlp.Decode(bytes.NewReader(data), &block); err != nil {
|
|
|
|
glog.V(logger.Error).Infof("invalid block RLP for hash %x: %v", hash, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return (*types.Block)(&block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBlockByHash returns the canonical block by number or nil if not found
|
|
|
|
func GetBlockByNumber(db common.Database, number uint64) *types.Block {
|
|
|
|
key, _ := db.Get(append(blockNumPre, big.NewInt(int64(number)).Bytes()...))
|
|
|
|
if len(key) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetBlockByHash(db, common.BytesToHash(key))
|
|
|
|
}
|
|
|
|
|
2015-07-25 22:48:53 +03:00
|
|
|
// WriteCanonNumber writes the canonical hash for the given block
|
|
|
|
func WriteCanonNumber(db common.Database, block *types.Block) error {
|
2015-07-01 17:15:02 +03:00
|
|
|
key := append(blockNumPre, block.Number().Bytes()...)
|
|
|
|
err := db.Put(key, block.Hash().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-25 22:48:53 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHead force writes the current head
|
|
|
|
func WriteHead(db common.Database, block *types.Block) error {
|
|
|
|
err := WriteCanonNumber(db, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-01 17:15:02 +03:00
|
|
|
err = db.Put([]byte("LastBlock"), block.Hash().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-10 15:29:40 +03:00
|
|
|
|
|
|
|
// WriteBlock writes a block to the database
|
|
|
|
func WriteBlock(db common.Database, block *types.Block) error {
|
|
|
|
tstart := time.Now()
|
|
|
|
|
|
|
|
enc, _ := rlp.EncodeToBytes((*types.StorageBlock)(block))
|
|
|
|
key := append(blockHashPre, block.Hash().Bytes()...)
|
|
|
|
err := db.Put(key, enc)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatal("db write fail:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if glog.V(logger.Debug) {
|
|
|
|
glog.Infof("wrote block #%v %s. Took %v\n", block.Number(), common.PP(block.Hash().Bytes()), time.Since(tstart))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|