moved CalcGasLimit to eth; selectable miner strategies

This commit is contained in:
zsfelfoldi 2015-08-09 02:42:01 +02:00
parent 1cbd53add8
commit 9b904067b0
14 changed files with 163 additions and 49 deletions

View file

@ -333,6 +333,11 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
utils.GpobaseStepDownFlag, utils.GpobaseStepDownFlag,
utils.GpobaseStepUpFlag, utils.GpobaseStepUpFlag,
utils.GpobaseCorrectionFactorFlag, utils.GpobaseCorrectionFactorFlag,
utils.GlsFlag,
utils.GlsTargetFlag,
utils.GlsBlkUtilFlag,
utils.GlsMinFlag,
utils.GlsMaxFlag,
} }
app.Before = func(ctx *cli.Context) error { app.Before = func(ctx *cli.Context) error {
utils.SetupLogger(ctx) utils.SetupLogger(ctx)

View file

@ -361,6 +361,31 @@ var (
Usage: "Suggested gas price base correction factor (%)", Usage: "Suggested gas price base correction factor (%)",
Value: 110, Value: 110,
} }
GlsFlag = cli.StringFlag{
Name: "gls",
Usage: "Gas limit adjustment strategy ( none | target | blkutil )",
Value: "target",
}
GlsTargetFlag = cli.StringFlag{
Name: "glstarget",
Usage: "Constant gas limit target",
Value: "3141592",
}
GlsBlkUtilFlag = cli.IntFlag{
Name: "glsblkutil",
Usage: "Block utilization target (%)",
Value: 67,
}
GlsMinFlag = cli.StringFlag{
Name: "glsmin",
Usage: "Gas limit adjustment range minimum (applies to all strategies)",
Value: "1000000",
}
GlsMaxFlag = cli.StringFlag{
Name: "glsmax",
Usage: "Gas limit adjustment range maximum (applies to all strategies)",
Value: "10000000",
}
) )
// MakeNAT creates a port mapper from set command line flags. // MakeNAT creates a port mapper from set command line flags.
@ -437,6 +462,11 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
GpobaseStepDown: ctx.GlobalInt(GpobaseStepDownFlag.Name), GpobaseStepDown: ctx.GlobalInt(GpobaseStepDownFlag.Name),
GpobaseStepUp: ctx.GlobalInt(GpobaseStepUpFlag.Name), GpobaseStepUp: ctx.GlobalInt(GpobaseStepUpFlag.Name),
GpobaseCorrectionFactor: ctx.GlobalInt(GpobaseCorrectionFactorFlag.Name), GpobaseCorrectionFactor: ctx.GlobalInt(GpobaseCorrectionFactorFlag.Name),
Gls: ctx.GlobalString(GlsFlag.Name),
GlsTarget: common.String2Big(ctx.GlobalString(GlsTargetFlag.Name)),
GlsBlkUtil: ctx.GlobalInt(GlsBlkUtilFlag.Name),
GlsMin: common.String2Big(ctx.GlobalString(GlsMinFlag.Name)),
GlsMax: common.String2Big(ctx.GlobalString(GlsMaxFlag.Name)),
SolcPath: ctx.GlobalString(SolcPathFlag.Name), SolcPath: ctx.GlobalString(SolcPathFlag.Name),
AutoDAG: ctx.GlobalBool(AutoDAGFlag.Name) || ctx.GlobalBool(MiningEnabledFlag.Name), AutoDAG: ctx.GlobalBool(AutoDAGFlag.Name) || ctx.GlobalBool(MiningEnabledFlag.Name),
} }
@ -483,7 +513,7 @@ func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, ex
eventMux := new(event.TypeMux) eventMux := new(event.TypeMux)
pow := ethash.New() pow := ethash.New()
//genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB) //genesis := core.GenesisBlock(uint64(ctx.GlobalInt(GenesisNonceFlag.Name)), blockDB)
chain, err = core.NewChainManager(blockDB, stateDB, extraDB, pow, eventMux) chain, err = core.NewChainManager(blockDB, stateDB, extraDB, pow, eventMux, nil)
if err != nil { if err != nil {
Fatalf("Could not start chainmanager: %v", err) Fatalf("Could not start chainmanager: %v", err)
} }

View file

@ -108,7 +108,7 @@ func init() {
func genTxRing(naccounts int) func(int, *BlockGen) { func genTxRing(naccounts int) func(int, *BlockGen) {
from := 0 from := 0
return func(i int, gen *BlockGen) { return func(i int, gen *BlockGen) {
gas := CalcGasLimit(gen.PrevBlock(i - 1)) gas := gen.PrevBlock(i - 1).GasLimit() // CalcGasLimit(gen.PrevBlock(i - 1))
for { for {
gas.Sub(gas, params.TxGas) gas.Sub(gas, params.TxGas)
if gas.Cmp(params.TxGas) < 0 { if gas.Cmp(params.TxGas) < 0 {
@ -168,7 +168,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
// Time the insertion of the new chain. // Time the insertion of the new chain.
// State and blocks are stored in the same DB. // State and blocks are stored in the same DB.
evmux := new(event.TypeMux) evmux := new(event.TypeMux)
chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux, nil)
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
defer chainman.Stop() defer chainman.Stop()
b.ReportAllocs() b.ReportAllocs()

View file

@ -34,7 +34,7 @@ func proc() (*BlockProcessor, *ChainManager) {
var mux event.TypeMux var mux event.TypeMux
WriteTestNetGenesisBlock(db, db, 0) WriteTestNetGenesisBlock(db, db, 0)
chainMan, err := NewChainManager(db, db, db, thePow(), &mux) chainMan, err := NewChainManager(db, db, db, thePow(), &mux, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

View file

@ -172,7 +172,7 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
ParentHash: parent.Hash(), ParentHash: parent.Hash(),
Coinbase: parent.Coinbase(), Coinbase: parent.Coinbase(),
Difficulty: CalcDifficulty(time, parent.Time(), parent.Number(), parent.Difficulty()), Difficulty: CalcDifficulty(time, parent.Time(), parent.Number(), parent.Difficulty()),
GasLimit: CalcGasLimit(parent), GasLimit: parent.GasLimit(),
GasUsed: new(big.Int), GasUsed: new(big.Int),
Number: new(big.Int).Add(parent.Number(), common.Big1), Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: uint64(time), Time: uint64(time),
@ -185,7 +185,7 @@ func newCanonical(n int, db common.Database) (*BlockProcessor, error) {
evmux := &event.TypeMux{} evmux := &event.TypeMux{}
WriteTestNetGenesisBlock(db, db, 0) WriteTestNetGenesisBlock(db, db, 0)
chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux, nil)
bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux) bman := NewBlockProcessor(db, db, FakePow{}, chainman, evmux)
bman.bc.SetProcessor(bman) bman.bc.SetProcessor(bman)
parent := bman.bc.CurrentBlock() parent := bman.bc.CurrentBlock()

View file

@ -77,7 +77,7 @@ func ExampleGenerateChain() {
// Import the chain. This runs all block validation rules. // Import the chain. This runs all block validation rules.
evmux := &event.TypeMux{} evmux := &event.TypeMux{}
chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux) chainman, _ := NewChainManager(db, db, db, FakePow{}, evmux, nil)
chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux)) chainman.SetProcessor(NewBlockProcessor(db, db, FakePow{}, chainman, evmux))
if i, err := chainman.InsertChain(chain); err != nil { if i, err := chainman.InsertChain(chain); err != nil {
fmt.Printf("insert error (block %d): %v\n", i, err) fmt.Printf("insert error (block %d): %v\n", i, err)

View file

@ -54,8 +54,11 @@ const (
checkpointLimit = 200 checkpointLimit = 200
) )
type GlsFunc func(*types.Block) *big.Int
type ChainManager struct { type ChainManager struct {
//eth EthManager //eth EthManager
glsfunc GlsFunc
blockDb common.Database blockDb common.Database
stateDb common.Database stateDb common.Database
extraDb common.Database extraDb common.Database
@ -85,9 +88,10 @@ type ChainManager struct {
pow pow.PoW pow pow.PoW
} }
func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux *event.TypeMux) (*ChainManager, error) { func NewChainManager(blockDb, stateDb, extraDb common.Database, pow pow.PoW, mux *event.TypeMux, glsfunc GlsFunc) (*ChainManager, error) {
cache, _ := lru.New(blockCacheLimit) cache, _ := lru.New(blockCacheLimit)
bc := &ChainManager{ bc := &ChainManager{
glsfunc: glsfunc,
blockDb: blockDb, blockDb: blockDb,
stateDb: stateDb, stateDb: stateDb,
extraDb: extraDb, extraDb: extraDb,
@ -235,7 +239,7 @@ func (bc *ChainManager) setLastState() error {
bc.Reset() bc.Reset()
} }
bc.td = bc.currentBlock.Td bc.td = bc.currentBlock.Td
bc.currentGasLimit = CalcGasLimit(bc.currentBlock) bc.currentGasLimit = bc.glsfunc(bc.currentBlock)
if glog.V(logger.Info) { if glog.V(logger.Info) {
glog.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td) glog.Infof("Last block (#%v) %x TD=%v\n", bc.currentBlock.Number(), bc.currentBlock.Hash(), bc.td)
@ -757,7 +761,7 @@ out:
// We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long // We need some control over the mining operation. Acquiring locks and waiting for the miner to create new block takes too long
// and in most cases isn't even necessary. // and in most cases isn't even necessary.
if self.lastBlockHash == event.Hash { if self.lastBlockHash == event.Hash {
self.currentGasLimit = CalcGasLimit(event.Block) self.currentGasLimit = self.glsfunc(event.Block)
self.eventMux.Post(ChainHeadEvent{event.Block}) self.eventMux.Post(ChainHeadEvent{event.Block})
} }
} }

View file

@ -49,7 +49,7 @@ func thePow() pow.PoW {
func theChainManager(db common.Database, t *testing.T) *ChainManager { func theChainManager(db common.Database, t *testing.T) *ChainManager {
var eventMux event.TypeMux var eventMux event.TypeMux
WriteTestNetGenesisBlock(db, db, 0) WriteTestNetGenesisBlock(db, db, 0)
chainMan, err := NewChainManager(db, db, db, thePow(), &eventMux) chainMan, err := NewChainManager(db, db, db, thePow(), &eventMux, nil)
if err != nil { if err != nil {
t.Error("failed creating chainmanager:", err) t.Error("failed creating chainmanager:", err)
t.FailNow() t.FailNow()

View file

@ -79,39 +79,6 @@ func CalcTD(block, parent *types.Block) *big.Int {
return d return d
} }
// CalcGasLimit computes the gas limit of the next block after parent.
// The result may be modified by the caller.
// This is miner strategy, not consensus protocol.
func CalcGasLimit(parent *types.Block) *big.Int {
// contrib = (parentGasUsed * 3 / 2) / 1024
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
contrib = contrib.Div(contrib, big.NewInt(2))
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
// 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.
*/
gl := new(big.Int).Sub(parent.GasLimit(), decay)
gl = gl.Add(gl, contrib)
gl.Set(common.BigMax(gl, params.MinGasLimit))
// however, if we're now below the target (GenesisGasLimit) we increase the
// limit as much as we can (parentGasLimit / 1024 -1)
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 // GetBlockByHash returns the block corresponding to the hash or nil if not found
func GetBlockByHash(db common.Database, hash common.Hash) *types.Block { func GetBlockByHash(db common.Database, hash common.Hash) *types.Block {
data, _ := db.Get(append(blockHashPre, hash[:]...)) data, _ := db.Get(append(blockHashPre, hash[:]...))

View file

@ -17,8 +17,11 @@
package core package core
import ( import (
"math/big"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
) )
@ -32,4 +35,5 @@ type Backend interface {
StateDb() common.Database StateDb() common.Database
ExtraDb() common.Database ExtraDb() common.Database
EventMux() *event.TypeMux EventMux() *event.TypeMux
CalcGasLimit(*types.Block) *big.Int
} }

View file

@ -122,6 +122,12 @@ type Config struct {
GpobaseStepUp int GpobaseStepUp int
GpobaseCorrectionFactor int GpobaseCorrectionFactor int
Gls string
GlsTarget *big.Int
GlsBlkUtil int
GlsMin *big.Int
GlsMax *big.Int
// NewDB is used to create databases. // NewDB is used to create databases.
// If nil, the default is to create leveldb databases on disk. // If nil, the default is to create leveldb databases on disk.
NewDB func(path string) (common.Database, error) NewDB func(path string) (common.Database, error)
@ -232,6 +238,12 @@ type Ethereum struct {
GpobaseStepUp int GpobaseStepUp int
GpobaseCorrectionFactor int GpobaseCorrectionFactor int
Gls string
GlsTarget *big.Int
GlsBlkUtil int
GlsMin *big.Int
GlsMax *big.Int
net *p2p.Server net *p2p.Server
eventMux *event.TypeMux eventMux *event.TypeMux
miner *miner.Miner miner *miner.Miner
@ -350,6 +362,11 @@ func New(config *Config) (*Ethereum, error) {
GpobaseStepDown: config.GpobaseStepDown, GpobaseStepDown: config.GpobaseStepDown,
GpobaseStepUp: config.GpobaseStepUp, GpobaseStepUp: config.GpobaseStepUp,
GpobaseCorrectionFactor: config.GpobaseCorrectionFactor, GpobaseCorrectionFactor: config.GpobaseCorrectionFactor,
Gls: config.Gls,
GlsTarget: config.GlsTarget,
GlsBlkUtil: config.GlsBlkUtil,
GlsMin: config.GlsMin,
GlsMax: config.GlsMax,
} }
if config.PowTest { if config.PowTest {
@ -362,7 +379,8 @@ func New(config *Config) (*Ethereum, error) {
eth.pow = ethash.New() eth.pow = ethash.New()
} }
//genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb) //genesis := core.GenesisBlock(uint64(config.GenesisNonce), stateDb)
eth.chainManager, err = core.NewChainManager(blockDb, stateDb, extraDb, eth.pow, eth.EventMux())
eth.chainManager, err = core.NewChainManager(blockDb, stateDb, extraDb, eth.pow, eth.EventMux(), eth.CalcGasLimit)
if err != nil { if err != nil {
if err == core.ErrNoGenesis { if err == core.ErrNoGenesis {
return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`)

86
eth/gaslimit.go Normal file
View file

@ -0,0 +1,86 @@
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// 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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package eth
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
)
// CalcGasLimit computes the gas limit of the next block after parent.
// The result may be modified by the caller.
func (eth *Ethereum) CalcGasLimit(parent *types.Block) *big.Int {
var limit, step, min, max *big.Int
step = parent.GasLimit()
step = step.Div(step, params.GasLimitBoundDivisor)
step = step.Sub(step, big.NewInt(1))
min = parent.GasLimit()
min = min.Sub(min, step)
max = parent.GasLimit()
max = max.Add(max, step)
switch eth.Gls {
case "target":
limit = eth.GlsTarget
case "blkutil":
limit = eth.CalcGlsBlkUtil(parent)
default:
limit = parent.GasLimit()
}
if limit.Cmp(min) < 0 {
limit = min
}
if limit.Cmp(max) > 0 {
limit = max
}
return limit
}
func (eth *Ethereum) CalcGlsBlkUtil(parent *types.Block) *big.Int {
// contrib = (parentGasUsed * 100 / GlsBlkUtil) / 1024
contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(100))
contrib = contrib.Div(contrib, big.NewInt(int64(eth.GlsBlkUtil)))
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
// 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.
*/
gl := new(big.Int).Sub(parent.GasLimit(), decay)
gl = gl.Add(gl, contrib)
gl.Set(common.BigMax(gl, params.MinGasLimit))
// however, if we're now below the target (GenesisGasLimit) we increase the
// limit as much as we can (parentGasLimit / 1024 -1)
if gl.Cmp(params.GenesisGasLimit) < 0 {
gl.Add(parent.GasLimit(), decay)
gl.Set(common.BigMin(gl, params.GenesisGasLimit))
}
return gl
}

View file

@ -182,7 +182,7 @@ func newProtocolManagerForTesting(txAdded chan<- []*types.Transaction) *Protocol
core.WriteTestNetGenesisBlock(db, db, 0) core.WriteTestNetGenesisBlock(db, db, 0)
var ( var (
em = new(event.TypeMux) em = new(event.TypeMux)
chain, _ = core.NewChainManager(db, db, db, core.FakePow{}, em) chain, _ = core.NewChainManager(db, db, db, core.FakePow{}, em, nil)
txpool = &fakeTxPool{added: txAdded} txpool = &fakeTxPool{added: txAdded}
pm = NewProtocolManager(NetworkId, em, txpool, core.FakePow{}, chain) pm = NewProtocolManager(NetworkId, em, txpool, core.FakePow{}, chain)
) )

View file

@ -446,7 +446,7 @@ func (self *worker) commitNewWork() {
ParentHash: parent.Hash(), ParentHash: parent.Hash(),
Number: num.Add(num, common.Big1), Number: num.Add(num, common.Big1),
Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Number(), parent.Difficulty()), Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Number(), parent.Difficulty()),
GasLimit: core.CalcGasLimit(parent), GasLimit: self.eth.CalcGasLimit(parent),
GasUsed: new(big.Int), GasUsed: new(big.Int),
Coinbase: self.coinbase, Coinbase: self.coinbase,
Extra: self.extra, Extra: self.extra,