From 8bd421fb0b7400da95517203bf398e8a55cf71bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Wed, 28 Sep 2016 13:17:17 +0300 Subject: [PATCH] cmd, core, params: use gas-per-second block limiting --- cmd/geth/main.go | 2 +- cmd/geth/usage.go | 2 +- cmd/utils/flags.go | 10 +++++----- core/blockchain.go | 22 ++++++++++++---------- params/miner_strategy.go | 7 +++---- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 6606fbac6c..6d6dee45e0 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -150,7 +150,7 @@ participating. utils.MiningGPUFlag, utils.AutoDAGFlag, utils.TargetGasLimitFlag, - utils.BlockTimeLimitFlag, + utils.GasPerSecondFlag, utils.NATFlag, utils.NatspecEnabledFlag, utils.NoDiscoverFlag, diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 4bb7936669..84a30f3752 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -126,7 +126,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.AutoDAGFlag, utils.EtherbaseFlag, utils.TargetGasLimitFlag, - utils.BlockTimeLimitFlag, + utils.GasPerSecondFlag, utils.GasPriceFlag, utils.ExtraDataFlag, }, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 6ae3f26d44..a6d489e1e7 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -183,10 +183,10 @@ var ( Usage: "Sets the artificial target gas floor for the blocks to mine", Value: params.GenesisGasLimit.String(), } - BlockTimeLimitFlag = cli.IntFlag{ - Name: "blocktimelimit", - Usage: "Time limit for processing a block after which to halve the hard gas limit", - Value: 5, + GasPerSecondFlag = cli.StringFlag{ + Name: "gaspersec", + Usage: "Gas consumption per second below which to tighten the block limits", + Value: params.GasPerSecondThreshold.String(), } AutoDAGFlag = cli.BoolFlag{ Name: "autodag", @@ -752,7 +752,7 @@ func SetupNetwork(ctx *cli.Context) { } params.TargetGasLimit = common.String2Big(ctx.GlobalString(TargetGasLimitFlag.Name)) params.CurrentGasCeil.Set(params.TargetGasLimit) - params.BlockTimeLimit = time.Duration(ctx.GlobalInt(BlockTimeLimitFlag.Name)) * time.Second + params.GasPerSecondThreshold = common.String2Big(ctx.GlobalString(GasPerSecondFlag.Name)) } // MustMakeChainConfig reads the chain configuration from the database in ctx.Datadir. diff --git a/core/blockchain.go b/core/blockchain.go index 179ccc1925..9500d93747 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -980,17 +980,19 @@ func (self *BlockChain) InsertChain(chain types.Blocks) (int, error) { stats.processed++ // Update the current gas limit (mining only) based on import times - params.CurrentGasCeilLock.Lock() - if time.Since(bstart) > params.BlockTimeLimit { - ceil := new(big.Int).Div(params.CurrentGasCeil, params.CurrentGasCeilCutDiv) - params.CurrentGasCeil.Set(common.BigMax(ceil, params.MinGasLimit)) - } else { - ceil := new(big.Int).Add(params.CurrentGasCeil, new(big.Int).Div(params.CurrentGasCeil, params.CurrentGasCeilIncDiv)) - limit := new(big.Int).Mul(chain[i].GasLimit(), big.NewInt(2)) - params.CurrentGasCeil.Set(common.BigMin(ceil, limit)) + if gasUsed := chain[i].GasUsed(); gasUsed.Cmp(common.Big0) > 0 { + params.CurrentGasCeilLock.Lock() + if gasPerSecond := gasUsed.Uint64() * uint64(time.Second) / uint64(time.Since(bstart)); gasPerSecond < params.GasPerSecondThreshold.Uint64() { + ceil := new(big.Int).Div(params.CurrentGasCeil, params.CurrentGasCeilCutDiv) + params.CurrentGasCeil.Set(common.BigMax(ceil, params.MinGasLimit)) + } else { + ceil := new(big.Int).Add(params.CurrentGasCeil, new(big.Int).Div(params.CurrentGasCeil, params.CurrentGasCeilIncDiv)) + limit := new(big.Int).Mul(chain[i].GasLimit(), big.NewInt(2)) + params.CurrentGasCeil.Set(common.BigMin(ceil, limit)) + } + glog.V(logger.Debug).Infof("block gas ceiling changed to %v", params.CurrentGasCeil) + params.CurrentGasCeilLock.Unlock() } - glog.V(logger.Debug).Infof("block gas ceiling changed to %v", params.CurrentGasCeil) - params.CurrentGasCeilLock.Unlock() } if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) { diff --git a/params/miner_strategy.go b/params/miner_strategy.go index c5c1cabfc0..4ce483aad1 100644 --- a/params/miner_strategy.go +++ b/params/miner_strategy.go @@ -19,13 +19,12 @@ package params import ( "math/big" "sync" - "time" ) var ( - GenesisGasLimit = big.NewInt(4712388) // Gas limit of the Genesis block. - TargetGasLimit = new(big.Int).Set(GenesisGasLimit) // The artificial target - BlockTimeLimit = 5 * time.Second // Block processing time limit to reduce gas after + GenesisGasLimit = big.NewInt(4712388) // Gas limit of the Genesis block. + TargetGasLimit = new(big.Int).Set(GenesisGasLimit) // The artificial target + GasPerSecondThreshold = big.NewInt(4712388 / 2) // Threshold below which to start reducing block gas limits // Temp hack to get a dynamic gas limit in palace (clean up!!!) CurrentGasCeil = new(big.Int).Set(GenesisGasLimit)