Merge pull request #19 from ethereum/master

Merge
This commit is contained in:
Caesar Chad 2018-08-29 20:15:01 +08:00 committed by GitHub
commit 375da65eeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 196 additions and 128 deletions

View file

@ -101,6 +101,7 @@ var (
utils.MinerNotifyFlag, utils.MinerNotifyFlag,
utils.MinerGasTargetFlag, utils.MinerGasTargetFlag,
utils.MinerLegacyGasTargetFlag, utils.MinerLegacyGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerGasPriceFlag, utils.MinerGasPriceFlag,
utils.MinerLegacyGasPriceFlag, utils.MinerLegacyGasPriceFlag,
utils.MinerEtherbaseFlag, utils.MinerEtherbaseFlag,
@ -236,7 +237,6 @@ func init() {
// Start system runtime metrics collection // Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second) go metrics.CollectProcessMetrics(3 * time.Second)
utils.SetupNetwork(ctx)
return nil return nil
} }

View file

@ -189,6 +189,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.MinerNotifyFlag, utils.MinerNotifyFlag,
utils.MinerGasPriceFlag, utils.MinerGasPriceFlag,
utils.MinerGasTargetFlag, utils.MinerGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerEtherbaseFlag, utils.MinerEtherbaseFlag,
utils.MinerExtraDataFlag, utils.MinerExtraDataFlag,
utils.MinerRecommitIntervalFlag, utils.MinerRecommitIntervalFlag,

View file

@ -42,7 +42,7 @@ ADD genesis.json /genesis.json
RUN \ RUN \
echo 'geth --cache 512 init /genesis.json' > geth.sh && \{{if .Unlock}} echo 'geth --cache 512 init /genesis.json' > geth.sh && \{{if .Unlock}}
echo 'mkdir -p /root/.ethereum/keystore/ && cp /signer.json /root/.ethereum/keystore/' >> geth.sh && \{{end}} echo 'mkdir -p /root/.ethereum/keystore/ && cp /signer.json /root/.ethereum/keystore/' >> geth.sh && \{{end}}
echo $'exec geth --networkid {{.NetworkID}} --cache 512 --port {{.Port}} --maxpeers {{.Peers}} {{.LightFlag}} --ethstats \'{{.Ethstats}}\' {{if .Bootnodes}}--bootnodes {{.Bootnodes}}{{end}} {{if .Etherbase}}--miner.etherbase {{.Etherbase}} --mine --miner.threads 1{{end}} {{if .Unlock}}--unlock 0 --password /signer.pass --mine{{end}} --miner.gastarget {{.GasTarget}} --miner.gasprice {{.GasPrice}}' >> geth.sh echo $'exec geth --networkid {{.NetworkID}} --cache 512 --port {{.Port}} --maxpeers {{.Peers}} {{.LightFlag}} --ethstats \'{{.Ethstats}}\' {{if .Bootnodes}}--bootnodes {{.Bootnodes}}{{end}} {{if .Etherbase}}--miner.etherbase {{.Etherbase}} --mine --miner.threads 1{{end}} {{if .Unlock}}--unlock 0 --password /signer.pass --mine{{end}} --miner.gastarget {{.GasTarget}} --miner.gaslimit {{.GasLimit}} --miner.gasprice {{.GasPrice}}' >> geth.sh
ENTRYPOINT ["/bin/sh", "geth.sh"] ENTRYPOINT ["/bin/sh", "geth.sh"]
` `
@ -68,6 +68,7 @@ services:
- STATS_NAME={{.Ethstats}} - STATS_NAME={{.Ethstats}}
- MINER_NAME={{.Etherbase}} - MINER_NAME={{.Etherbase}}
- GAS_TARGET={{.GasTarget}} - GAS_TARGET={{.GasTarget}}
- GAS_LIMIT={{.GasLimit}}
- GAS_PRICE={{.GasPrice}} - GAS_PRICE={{.GasPrice}}
logging: logging:
driver: "json-file" driver: "json-file"
@ -104,6 +105,7 @@ func deployNode(client *sshClient, network string, bootnodes []string, config *n
"Ethstats": config.ethstats, "Ethstats": config.ethstats,
"Etherbase": config.etherbase, "Etherbase": config.etherbase,
"GasTarget": uint64(1000000 * config.gasTarget), "GasTarget": uint64(1000000 * config.gasTarget),
"GasLimit": uint64(1000000 * config.gasLimit),
"GasPrice": uint64(1000000000 * config.gasPrice), "GasPrice": uint64(1000000000 * config.gasPrice),
"Unlock": config.keyJSON != "", "Unlock": config.keyJSON != "",
}) })
@ -122,6 +124,7 @@ func deployNode(client *sshClient, network string, bootnodes []string, config *n
"Ethstats": config.ethstats[:strings.Index(config.ethstats, ":")], "Ethstats": config.ethstats[:strings.Index(config.ethstats, ":")],
"Etherbase": config.etherbase, "Etherbase": config.etherbase,
"GasTarget": config.gasTarget, "GasTarget": config.gasTarget,
"GasLimit": config.gasLimit,
"GasPrice": config.gasPrice, "GasPrice": config.gasPrice,
}) })
files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes() files[filepath.Join(workdir, "docker-compose.yaml")] = composefile.Bytes()
@ -160,6 +163,7 @@ type nodeInfos struct {
keyJSON string keyJSON string
keyPass string keyPass string
gasTarget float64 gasTarget float64
gasLimit float64
gasPrice float64 gasPrice float64
} }
@ -175,8 +179,9 @@ func (info *nodeInfos) Report() map[string]string {
} }
if info.gasTarget > 0 { if info.gasTarget > 0 {
// Miner or signer node // Miner or signer node
report["Gas limit (baseline target)"] = fmt.Sprintf("%0.3f MGas", info.gasTarget)
report["Gas price (minimum accepted)"] = fmt.Sprintf("%0.3f GWei", info.gasPrice) report["Gas price (minimum accepted)"] = fmt.Sprintf("%0.3f GWei", info.gasPrice)
report["Gas floor (baseline target)"] = fmt.Sprintf("%0.3f MGas", info.gasTarget)
report["Gas ceil (target maximum)"] = fmt.Sprintf("%0.3f MGas", info.gasLimit)
if info.etherbase != "" { if info.etherbase != "" {
// Ethash proof-of-work miner // Ethash proof-of-work miner
@ -217,6 +222,7 @@ func checkNode(client *sshClient, network string, boot bool) (*nodeInfos, error)
totalPeers, _ := strconv.Atoi(infos.envvars["TOTAL_PEERS"]) totalPeers, _ := strconv.Atoi(infos.envvars["TOTAL_PEERS"])
lightPeers, _ := strconv.Atoi(infos.envvars["LIGHT_PEERS"]) lightPeers, _ := strconv.Atoi(infos.envvars["LIGHT_PEERS"])
gasTarget, _ := strconv.ParseFloat(infos.envvars["GAS_TARGET"], 64) gasTarget, _ := strconv.ParseFloat(infos.envvars["GAS_TARGET"], 64)
gasLimit, _ := strconv.ParseFloat(infos.envvars["GAS_LIMIT"], 64)
gasPrice, _ := strconv.ParseFloat(infos.envvars["GAS_PRICE"], 64) gasPrice, _ := strconv.ParseFloat(infos.envvars["GAS_PRICE"], 64)
// Container available, retrieve its node ID and its genesis json // Container available, retrieve its node ID and its genesis json
@ -256,6 +262,7 @@ func checkNode(client *sshClient, network string, boot bool) (*nodeInfos, error)
keyJSON: keyJSON, keyJSON: keyJSON,
keyPass: keyPass, keyPass: keyPass,
gasTarget: gasTarget, gasTarget: gasTarget,
gasLimit: gasLimit,
gasPrice: gasPrice, gasPrice: gasPrice,
} }
stats.enode = fmt.Sprintf("enode://%s@%s:%d", id, client.address, stats.port) stats.enode = fmt.Sprintf("enode://%s@%s:%d", id, client.address, stats.port)

View file

@ -50,7 +50,7 @@ func (w *wizard) deployNode(boot bool) {
if boot { if boot {
infos = &nodeInfos{port: 30303, peersTotal: 512, peersLight: 256} infos = &nodeInfos{port: 30303, peersTotal: 512, peersLight: 256}
} else { } else {
infos = &nodeInfos{port: 30303, peersTotal: 50, peersLight: 0, gasTarget: 4.7, gasPrice: 18} infos = &nodeInfos{port: 30303, peersTotal: 50, peersLight: 0, gasTarget: 7.5, gasLimit: 10, gasPrice: 1}
} }
} }
existed := err == nil existed := err == nil
@ -152,6 +152,10 @@ func (w *wizard) deployNode(boot bool) {
fmt.Printf("What gas limit should empty blocks target (MGas)? (default = %0.3f)\n", infos.gasTarget) fmt.Printf("What gas limit should empty blocks target (MGas)? (default = %0.3f)\n", infos.gasTarget)
infos.gasTarget = w.readDefaultFloat(infos.gasTarget) infos.gasTarget = w.readDefaultFloat(infos.gasTarget)
fmt.Println()
fmt.Printf("What gas limit should full blocks target (MGas)? (default = %0.3f)\n", infos.gasLimit)
infos.gasLimit = w.readDefaultFloat(infos.gasLimit)
fmt.Println() fmt.Println()
fmt.Printf("What gas price should the signer require (GWei)? (default = %0.3f)\n", infos.gasPrice) fmt.Printf("What gas price should the signer require (GWei)? (default = %0.3f)\n", infos.gasPrice)
infos.gasPrice = w.readDefaultFloat(infos.gasPrice) infos.gasPrice = w.readDefaultFloat(infos.gasPrice)

View file

@ -13,6 +13,9 @@
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
// +build !windows
package main package main
import ( import (

View file

@ -329,12 +329,17 @@ var (
MinerGasTargetFlag = cli.Uint64Flag{ MinerGasTargetFlag = cli.Uint64Flag{
Name: "miner.gastarget", Name: "miner.gastarget",
Usage: "Target gas floor for mined blocks", Usage: "Target gas floor for mined blocks",
Value: params.GenesisGasLimit, Value: eth.DefaultConfig.MinerGasFloor,
} }
MinerLegacyGasTargetFlag = cli.Uint64Flag{ MinerLegacyGasTargetFlag = cli.Uint64Flag{
Name: "targetgaslimit", Name: "targetgaslimit",
Usage: "Target gas floor for mined blocks (deprecated, use --miner.gastarget)", Usage: "Target gas floor for mined blocks (deprecated, use --miner.gastarget)",
Value: params.GenesisGasLimit, Value: eth.DefaultConfig.MinerGasFloor,
}
MinerGasLimitFlag = cli.Uint64Flag{
Name: "miner.gaslimit",
Usage: "Target gas ceiling for mined blocks",
Value: eth.DefaultConfig.MinerGasCeil,
} }
MinerGasPriceFlag = BigFlag{ MinerGasPriceFlag = BigFlag{
Name: "miner.gasprice", Name: "miner.gasprice",
@ -1146,6 +1151,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
if ctx.GlobalIsSet(MinerExtraDataFlag.Name) { if ctx.GlobalIsSet(MinerExtraDataFlag.Name) {
cfg.MinerExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name)) cfg.MinerExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
} }
if ctx.GlobalIsSet(MinerLegacyGasTargetFlag.Name) {
cfg.MinerGasFloor = ctx.GlobalUint64(MinerLegacyGasTargetFlag.Name)
}
if ctx.GlobalIsSet(MinerGasTargetFlag.Name) {
cfg.MinerGasFloor = ctx.GlobalUint64(MinerGasTargetFlag.Name)
}
if ctx.GlobalIsSet(MinerGasLimitFlag.Name) {
cfg.MinerGasCeil = ctx.GlobalUint64(MinerGasLimitFlag.Name)
}
if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) { if ctx.GlobalIsSet(MinerLegacyGasPriceFlag.Name) {
cfg.MinerGasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name) cfg.MinerGasPrice = GlobalBig(ctx, MinerLegacyGasPriceFlag.Name)
} }
@ -1270,15 +1284,6 @@ func RegisterEthStatsService(stack *node.Node, url string) {
} }
} }
// SetupNetwork configures the system for either the main net or some test network.
func SetupNetwork(ctx *cli.Context) {
// TODO(fjl): move target gas limit into config
params.TargetGasLimit = ctx.GlobalUint64(MinerLegacyGasTargetFlag.Name)
if ctx.GlobalIsSet(MinerGasTargetFlag.Name) {
params.TargetGasLimit = ctx.GlobalUint64(MinerGasTargetFlag.Name)
}
}
func SetupMetrics(ctx *cli.Context) { func SetupMetrics(ctx *cli.Context) {
if metrics.Enabled { if metrics.Enabled {
log.Info("Enabling metrics collection") log.Info("Enabling metrics collection")

View file

@ -111,7 +111,8 @@ 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)) block := gen.PrevBlock(i - 1)
gas := CalcGasLimit(block, block.GasLimit(), block.GasLimit())
for { for {
gas -= params.TxGas gas -= params.TxGas
if gas < params.TxGas { if gas < params.TxGas {

View file

@ -101,9 +101,11 @@ func (v *BlockValidator) ValidateState(block, parent *types.Block, statedb *stat
return nil return nil
} }
// CalcGasLimit computes the gas limit of the next block after parent. // CalcGasLimit computes the gas limit of the next block after parent. It aims
// This is miner strategy, not consensus protocol. // to keep the baseline gas above the provided floor, and increase it towards the
func CalcGasLimit(parent *types.Block) uint64 { // ceil if the blocks are full. If the ceil is exceeded, it will always decrease
// the gas allowance.
func CalcGasLimit(parent *types.Block, gasFloor, gasCeil uint64) uint64 {
// contrib = (parentGasUsed * 3 / 2) / 1024 // contrib = (parentGasUsed * 3 / 2) / 1024
contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor contrib := (parent.GasUsed() + parent.GasUsed()/2) / params.GasLimitBoundDivisor
@ -121,12 +123,16 @@ func CalcGasLimit(parent *types.Block) uint64 {
if limit < params.MinGasLimit { if limit < params.MinGasLimit {
limit = params.MinGasLimit limit = params.MinGasLimit
} }
// however, if we're now below the target (TargetGasLimit) we increase the // If we're outside our allowed gas range, we try to hone towards them
// limit as much as we can (parentGasLimit / 1024 -1) if limit < gasFloor {
if limit < params.TargetGasLimit {
limit = parent.GasLimit() + decay limit = parent.GasLimit() + decay
if limit > params.TargetGasLimit { if limit > gasFloor {
limit = params.TargetGasLimit limit = gasFloor
}
} else if limit > gasCeil {
limit = parent.GasLimit() - decay
if limit < gasCeil {
limit = gasCeil
} }
} }
return limit return limit

View file

@ -240,7 +240,7 @@ func makeHeader(chain consensus.ChainReader, parent *types.Block, state *state.S
Difficulty: parent.Difficulty(), Difficulty: parent.Difficulty(),
UncleHash: parent.UncleHash(), UncleHash: parent.UncleHash(),
}), }),
GasLimit: CalcGasLimit(parent), GasLimit: CalcGasLimit(parent, parent.GasLimit(), parent.GasLimit()),
Number: new(big.Int).Add(parent.Number(), common.Big1), Number: new(big.Int).Add(parent.Number(), common.Big1),
Time: time, Time: time,
} }

View file

@ -173,7 +173,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
return nil, err return nil, err
} }
eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit) eth.miner = miner.New(eth, eth.chainConfig, eth.EventMux(), eth.engine, config.MinerRecommit, config.MinerGasFloor, config.MinerGasCeil)
eth.miner.SetExtra(makeExtraData(config.MinerExtraData)) eth.miner.SetExtra(makeExtraData(config.MinerExtraData))
eth.APIBackend = &EthAPIBackend{eth, nil} eth.APIBackend = &EthAPIBackend{eth, nil}

View file

@ -48,7 +48,9 @@ var DefaultConfig = Config{
DatabaseCache: 768, DatabaseCache: 768,
TrieCache: 256, TrieCache: 256,
TrieTimeout: 60 * time.Minute, TrieTimeout: 60 * time.Minute,
MinerGasPrice: big.NewInt(18 * params.Shannon), MinerGasFloor: 8000000,
MinerGasCeil: 8000000,
MinerGasPrice: big.NewInt(params.GWei),
MinerRecommit: 3 * time.Second, MinerRecommit: 3 * time.Second,
TxPool: core.DefaultTxPoolConfig, TxPool: core.DefaultTxPoolConfig,
@ -99,6 +101,8 @@ type Config struct {
Etherbase common.Address `toml:",omitempty"` Etherbase common.Address `toml:",omitempty"`
MinerNotify []string `toml:",omitempty"` MinerNotify []string `toml:",omitempty"`
MinerExtraData []byte `toml:",omitempty"` MinerExtraData []byte `toml:",omitempty"`
MinerGasFloor uint64
MinerGasCeil uint64
MinerGasPrice *big.Int MinerGasPrice *big.Int
MinerRecommit time.Duration MinerRecommit time.Duration
MinerNoverify bool MinerNoverify bool

View file

@ -29,7 +29,7 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
var maxPrice = big.NewInt(500 * params.Shannon) var maxPrice = big.NewInt(500 * params.GWei)
type Config struct { type Config struct {
Blocks int Blocks int

View file

@ -33,6 +33,8 @@ func (c Config) MarshalTOML() (interface{}, error) {
Etherbase common.Address `toml:",omitempty"` Etherbase common.Address `toml:",omitempty"`
MinerNotify []string `toml:",omitempty"` MinerNotify []string `toml:",omitempty"`
MinerExtraData hexutil.Bytes `toml:",omitempty"` MinerExtraData hexutil.Bytes `toml:",omitempty"`
MinerGasFloor uint64
MinerGasCeil uint64
MinerGasPrice *big.Int MinerGasPrice *big.Int
MinerRecommit time.Duration MinerRecommit time.Duration
MinerNoverify bool MinerNoverify bool
@ -57,6 +59,8 @@ func (c Config) MarshalTOML() (interface{}, error) {
enc.Etherbase = c.Etherbase enc.Etherbase = c.Etherbase
enc.MinerNotify = c.MinerNotify enc.MinerNotify = c.MinerNotify
enc.MinerExtraData = c.MinerExtraData enc.MinerExtraData = c.MinerExtraData
enc.MinerGasFloor = c.MinerGasFloor
enc.MinerGasCeil = c.MinerGasCeil
enc.MinerGasPrice = c.MinerGasPrice enc.MinerGasPrice = c.MinerGasPrice
enc.MinerRecommit = c.MinerRecommit enc.MinerRecommit = c.MinerRecommit
enc.MinerNoverify = c.MinerNoverify enc.MinerNoverify = c.MinerNoverify
@ -85,6 +89,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
Etherbase *common.Address `toml:",omitempty"` Etherbase *common.Address `toml:",omitempty"`
MinerNotify []string `toml:",omitempty"` MinerNotify []string `toml:",omitempty"`
MinerExtraData *hexutil.Bytes `toml:",omitempty"` MinerExtraData *hexutil.Bytes `toml:",omitempty"`
MinerGasFloor *uint64
MinerGasCeil *uint64
MinerGasPrice *big.Int MinerGasPrice *big.Int
MinerRecommit *time.Duration MinerRecommit *time.Duration
MinerNoverify *bool MinerNoverify *bool
@ -140,6 +146,12 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
if dec.MinerExtraData != nil { if dec.MinerExtraData != nil {
c.MinerExtraData = *dec.MinerExtraData c.MinerExtraData = *dec.MinerExtraData
} }
if dec.MinerGasFloor != nil {
c.MinerGasFloor = *dec.MinerGasFloor
}
if dec.MinerGasCeil != nil {
c.MinerGasCeil = *dec.MinerGasCeil
}
if dec.MinerGasPrice != nil { if dec.MinerGasPrice != nil {
c.MinerGasPrice = dec.MinerGasPrice c.MinerGasPrice = dec.MinerGasPrice
} }

View file

@ -47,7 +47,7 @@ import (
) )
const ( const (
defaultGasPrice = 50 * params.Shannon defaultGasPrice = params.GWei
) )
// PublicEthereumAPI provides an API to access Ethereum related information. // PublicEthereumAPI provides an API to access Ethereum related information.

View file

@ -52,13 +52,13 @@ type Miner struct {
shouldStart int32 // should start indicates whether we should start after sync shouldStart int32 // should start indicates whether we should start after sync
} }
func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, recommit time.Duration) *Miner { func New(eth Backend, config *params.ChainConfig, mux *event.TypeMux, engine consensus.Engine, recommit time.Duration, gasFloor, gasCeil uint64) *Miner {
miner := &Miner{ miner := &Miner{
eth: eth, eth: eth,
mux: mux, mux: mux,
engine: engine, engine: engine,
exitCh: make(chan struct{}), exitCh: make(chan struct{}),
worker: newWorker(config, engine, eth, mux, recommit), worker: newWorker(config, engine, eth, mux, recommit, gasFloor, gasCeil),
canStart: 1, canStart: 1,
} }
go miner.update() go miner.update()

View file

@ -206,6 +206,8 @@ func makeSealer(genesis *core.Genesis, nodes []string) (*node.Node, error) {
DatabaseHandles: 256, DatabaseHandles: 256,
TxPool: core.DefaultTxPoolConfig, TxPool: core.DefaultTxPoolConfig,
GPO: eth.DefaultConfig.GPO, GPO: eth.DefaultConfig.GPO,
MinerGasFloor: genesis.GasLimit * 9 / 10,
MinerGasCeil: genesis.GasLimit * 11 / 10,
MinerGasPrice: big.NewInt(1), MinerGasPrice: big.NewInt(1),
MinerRecommit: time.Second, MinerRecommit: time.Second,
}) })

View file

@ -186,6 +186,8 @@ func makeMiner(genesis *core.Genesis, nodes []string) (*node.Node, error) {
TxPool: core.DefaultTxPoolConfig, TxPool: core.DefaultTxPoolConfig,
GPO: eth.DefaultConfig.GPO, GPO: eth.DefaultConfig.GPO,
Ethash: eth.DefaultConfig.Ethash, Ethash: eth.DefaultConfig.Ethash,
MinerGasFloor: genesis.GasLimit * 9 / 10,
MinerGasCeil: genesis.GasLimit * 11 / 10,
MinerGasPrice: big.NewInt(1), MinerGasPrice: big.NewInt(1),
MinerRecommit: time.Second, MinerRecommit: time.Second,
}) })

View file

@ -18,7 +18,7 @@ package miner
import ( import (
"bytes" "bytes"
"fmt" "errors"
"math/big" "math/big"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -127,6 +127,9 @@ type worker struct {
eth Backend eth Backend
chain *core.BlockChain chain *core.BlockChain
gasFloor uint64
gasCeil uint64
// Subscriptions // Subscriptions
mux *event.TypeMux mux *event.TypeMux
txsCh chan core.NewTxsEvent txsCh chan core.NewTxsEvent
@ -171,13 +174,15 @@ type worker struct {
resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval. resubmitHook func(time.Duration, time.Duration) // Method to call upon updating resubmitting interval.
} }
func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, recommit time.Duration) *worker { func newWorker(config *params.ChainConfig, engine consensus.Engine, eth Backend, mux *event.TypeMux, recommit time.Duration, gasFloor, gasCeil uint64) *worker {
worker := &worker{ worker := &worker{
config: config, config: config,
engine: engine, engine: engine,
eth: eth, eth: eth,
mux: mux, mux: mux,
chain: eth.BlockChain(), chain: eth.BlockChain(),
gasFloor: gasFloor,
gasCeil: gasCeil,
possibleUncles: make(map[common.Hash]*types.Block), possibleUncles: make(map[common.Hash]*types.Block),
unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth), unconfirmed: newUnconfirmedBlocks(eth.BlockChain(), miningLogAtDepth),
pendingTasks: make(map[common.Hash]*task), pendingTasks: make(map[common.Hash]*task),
@ -615,13 +620,16 @@ func (w *worker) makeCurrent(parent *types.Block, header *types.Header) error {
func (w *worker) commitUncle(env *environment, uncle *types.Header) error { func (w *worker) commitUncle(env *environment, uncle *types.Header) error {
hash := uncle.Hash() hash := uncle.Hash()
if env.uncles.Contains(hash) { if env.uncles.Contains(hash) {
return fmt.Errorf("uncle not unique") return errors.New("uncle not unique")
}
if env.header.ParentHash == uncle.ParentHash {
return errors.New("uncle is sibling")
} }
if !env.ancestors.Contains(uncle.ParentHash) { if !env.ancestors.Contains(uncle.ParentHash) {
return fmt.Errorf("uncle's parent unknown (%x)", uncle.ParentHash[0:4]) return errors.New("uncle's parent unknown")
} }
if env.family.Contains(hash) { if env.family.Contains(hash) {
return fmt.Errorf("uncle already in family (%x)", hash) return errors.New("uncle already included")
} }
env.uncles.Add(uncle.Hash()) env.uncles.Add(uncle.Hash())
return nil return nil
@ -807,7 +815,7 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
header := &types.Header{ header := &types.Header{
ParentHash: parent.Hash(), ParentHash: parent.Hash(),
Number: num.Add(num, common.Big1), Number: num.Add(num, common.Big1),
GasLimit: core.CalcGasLimit(parent), GasLimit: core.CalcGasLimit(parent, w.gasFloor, w.gasCeil),
Extra: w.extra, Extra: w.extra,
Time: big.NewInt(tstamp), Time: big.NewInt(tstamp),
} }
@ -847,29 +855,24 @@ func (w *worker) commitNewWork(interrupt *int32, noempty bool) {
if w.config.DAOForkSupport && w.config.DAOForkBlock != nil && w.config.DAOForkBlock.Cmp(header.Number) == 0 { if w.config.DAOForkSupport && w.config.DAOForkBlock != nil && w.config.DAOForkBlock.Cmp(header.Number) == 0 {
misc.ApplyDAOHardFork(env.state) misc.ApplyDAOHardFork(env.state)
} }
// Accumulate the uncles for the current block
// compute uncles for the new block. for hash, uncle := range w.possibleUncles {
var ( if uncle.NumberU64()+staleThreshold <= header.Number.Uint64() {
uncles []*types.Header delete(w.possibleUncles, hash)
badUncles []common.Hash }
) }
uncles := make([]*types.Header, 0, 2)
for hash, uncle := range w.possibleUncles { for hash, uncle := range w.possibleUncles {
if len(uncles) == 2 { if len(uncles) == 2 {
break break
} }
if err := w.commitUncle(env, uncle.Header()); err != nil { if err := w.commitUncle(env, uncle.Header()); err != nil {
log.Trace("Bad uncle found and will be removed", "hash", hash) log.Trace("Possible uncle rejected", "hash", hash, "reason", err)
log.Trace(fmt.Sprint(uncle))
badUncles = append(badUncles, hash)
} else { } else {
log.Debug("Committing new uncle to block", "hash", hash) log.Debug("Committing new uncle to block", "hash", hash)
uncles = append(uncles, uncle.Header()) uncles = append(uncles, uncle.Header())
} }
} }
for _, hash := range badUncles {
delete(w.possibleUncles, hash)
}
if !noempty { if !noempty {
// Create an empty block based on temporary copied state for sealing in advance without waiting block // Create an empty block based on temporary copied state for sealing in advance without waiting block

View file

@ -45,8 +45,8 @@ var (
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey) testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
testBankFunds = big.NewInt(1000000000000000000) testBankFunds = big.NewInt(1000000000000000000)
acc1Key, _ = crypto.GenerateKey() testUserKey, _ = crypto.GenerateKey()
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey) testUserAddress = crypto.PubkeyToAddress(testUserKey.PublicKey)
// Test transactions // Test transactions
pendingTxs []*types.Transaction pendingTxs []*types.Transaction
@ -62,9 +62,9 @@ func init() {
Period: 10, Period: 10,
Epoch: 30000, Epoch: 30000,
} }
tx1, _ := types.SignTx(types.NewTransaction(0, acc1Addr, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) tx1, _ := types.SignTx(types.NewTransaction(0, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
pendingTxs = append(pendingTxs, tx1) pendingTxs = append(pendingTxs, tx1)
tx2, _ := types.SignTx(types.NewTransaction(1, acc1Addr, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey) tx2, _ := types.SignTx(types.NewTransaction(1, testUserAddress, big.NewInt(1000), params.TxGas, nil, nil), types.HomesteadSigner{}, testBankKey)
newTxs = append(newTxs, tx2) newTxs = append(newTxs, tx2)
} }
@ -77,7 +77,7 @@ type testWorkerBackend struct {
uncleBlock *types.Block uncleBlock *types.Block
} }
func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) *testWorkerBackend { func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, n int) *testWorkerBackend {
var ( var (
db = ethdb.NewMemDatabase() db = ethdb.NewMemDatabase()
gspec = core.Genesis{ gspec = core.Genesis{
@ -92,14 +92,28 @@ func newTestWorkerBackend(t *testing.T, chainConfig *params.ChainConfig, engine
copy(gspec.ExtraData[32:], testBankAddress[:]) copy(gspec.ExtraData[32:], testBankAddress[:])
case *ethash.Ethash: case *ethash.Ethash:
default: default:
t.Fatal("unexpect consensus engine type") t.Fatalf("unexpected consensus engine type: %T", engine)
} }
genesis := gspec.MustCommit(db) genesis := gspec.MustCommit(db)
chain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{}) chain, _ := core.NewBlockChain(db, nil, gspec.Config, engine, vm.Config{})
txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain) txpool := core.NewTxPool(testTxPoolConfig, chainConfig, chain)
blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(acc1Addr) // Generate a small n-block chain and an uncle block for it
if n > 0 {
blocks, _ := core.GenerateChain(chainConfig, genesis, engine, db, n, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testBankAddress)
})
if _, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("failed to insert origin chain: %v", err)
}
}
parent := genesis
if n > 0 {
parent = chain.GetBlockByHash(chain.CurrentBlock().ParentHash())
}
blocks, _ := core.GenerateChain(chainConfig, parent, engine, db, 1, func(i int, gen *core.BlockGen) {
gen.SetCoinbase(testUserAddress)
}) })
return &testWorkerBackend{ return &testWorkerBackend{
@ -116,10 +130,10 @@ func (b *testWorkerBackend) PostChainEvents(events []interface{}) {
b.chain.PostChainEvents(events, nil) b.chain.PostChainEvents(events, nil)
} }
func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) (*worker, *testWorkerBackend) { func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, blocks int) (*worker, *testWorkerBackend) {
backend := newTestWorkerBackend(t, chainConfig, engine) backend := newTestWorkerBackend(t, chainConfig, engine, blocks)
backend.txPool.AddLocals(pendingTxs) backend.txPool.AddLocals(pendingTxs)
w := newWorker(chainConfig, engine, backend, new(event.TypeMux), time.Second) w := newWorker(chainConfig, engine, backend, new(event.TypeMux), time.Second, params.GenesisGasLimit, params.GenesisGasLimit)
w.setEtherbase(testBankAddress) w.setEtherbase(testBankAddress)
return w, backend return w, backend
} }
@ -134,24 +148,24 @@ func TestPendingStateAndBlockClique(t *testing.T) {
func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testPendingStateAndBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, b := newTestWorker(t, chainConfig, engine) w, b := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
// Ensure snapshot has been updated. // Ensure snapshot has been updated.
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
block, state := w.pending() block, state := w.pending()
if block.NumberU64() != 1 { if block.NumberU64() != 1 {
t.Errorf("block number mismatch, has %d, want %d", block.NumberU64(), 1) t.Errorf("block number mismatch: have %d, want %d", block.NumberU64(), 1)
} }
if balance := state.GetBalance(acc1Addr); balance.Cmp(big.NewInt(1000)) != 0 { if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(1000)) != 0 {
t.Errorf("account balance mismatch, has %d, want %d", balance, 1000) t.Errorf("account balance mismatch: have %d, want %d", balance, 1000)
} }
b.txPool.AddLocals(newTxs) b.txPool.AddLocals(newTxs)
// Ensure the new tx events has been processed // Ensure the new tx events has been processed
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
block, state = w.pending() block, state = w.pending()
if balance := state.GetBalance(acc1Addr); balance.Cmp(big.NewInt(2000)) != 0 { if balance := state.GetBalance(testUserAddress); balance.Cmp(big.NewInt(2000)) != 0 {
t.Errorf("account balance mismatch, has %d, want %d", balance, 2000) t.Errorf("account balance mismatch: have %d, want %d", balance, 2000)
} }
} }
@ -165,7 +179,7 @@ func TestEmptyWorkClique(t *testing.T) {
func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, _ := newTestWorker(t, chainConfig, engine) w, _ := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
var ( var (
@ -179,10 +193,10 @@ func testEmptyWork(t *testing.T, chainConfig *params.ChainConfig, engine consens
receiptLen, balance = 1, big.NewInt(1000) receiptLen, balance = 1, big.NewInt(1000)
} }
if len(task.receipts) != receiptLen { if len(task.receipts) != receiptLen {
t.Errorf("receipt number mismatch has %d, want %d", len(task.receipts), receiptLen) t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
} }
if task.state.GetBalance(acc1Addr).Cmp(balance) != 0 { if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
t.Errorf("account balance mismatch has %d, want %d", task.state.GetBalance(acc1Addr), balance) t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
} }
} }
@ -219,19 +233,19 @@ func TestStreamUncleBlock(t *testing.T) {
ethash := ethash.NewFaker() ethash := ethash.NewFaker()
defer ethash.Close() defer ethash.Close()
w, b := newTestWorker(t, ethashChainConfig, ethash) w, b := newTestWorker(t, ethashChainConfig, ethash, 1)
defer w.close() defer w.close()
var taskCh = make(chan struct{}) var taskCh = make(chan struct{})
taskIndex := 0 taskIndex := 0
w.newTaskHook = func(task *task) { w.newTaskHook = func(task *task) {
if task.block.NumberU64() == 1 { if task.block.NumberU64() == 2 {
if taskIndex == 2 { if taskIndex == 2 {
has := task.block.Header().UncleHash have := task.block.Header().UncleHash
want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()}) want := types.CalcUncleHash([]*types.Header{b.uncleBlock.Header()})
if has != want { if have != want {
t.Errorf("uncle hash mismatch, has %s, want %s", has.Hex(), want.Hex()) t.Errorf("uncle hash mismatch: have %s, want %s", have.Hex(), want.Hex())
} }
} }
taskCh <- struct{}{} taskCh <- struct{}{}
@ -248,12 +262,12 @@ func TestStreamUncleBlock(t *testing.T) {
// Ensure worker has finished initialization // Ensure worker has finished initialization
for { for {
b := w.pendingBlock() b := w.pendingBlock()
if b != nil && b.NumberU64() == 1 { if b != nil && b.NumberU64() == 2 {
break break
} }
} }
w.start() w.start()
// Ignore the first two works // Ignore the first two works
for i := 0; i < 2; i += 1 { for i := 0; i < 2; i += 1 {
select { select {
@ -282,7 +296,7 @@ func TestRegenerateMiningBlockClique(t *testing.T) {
func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, b := newTestWorker(t, chainConfig, engine) w, b := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
var taskCh = make(chan struct{}) var taskCh = make(chan struct{})
@ -293,10 +307,10 @@ func testRegenerateMiningBlock(t *testing.T, chainConfig *params.ChainConfig, en
if taskIndex == 2 { if taskIndex == 2 {
receiptLen, balance := 2, big.NewInt(2000) receiptLen, balance := 2, big.NewInt(2000)
if len(task.receipts) != receiptLen { if len(task.receipts) != receiptLen {
t.Errorf("receipt number mismatch has %d, want %d", len(task.receipts), receiptLen) t.Errorf("receipt number mismatch: have %d, want %d", len(task.receipts), receiptLen)
} }
if task.state.GetBalance(acc1Addr).Cmp(balance) != 0 { if task.state.GetBalance(testUserAddress).Cmp(balance) != 0 {
t.Errorf("account balance mismatch has %d, want %d", task.state.GetBalance(acc1Addr), balance) t.Errorf("account balance mismatch: have %d, want %d", task.state.GetBalance(testUserAddress), balance)
} }
} }
taskCh <- struct{}{} taskCh <- struct{}{}
@ -347,7 +361,7 @@ func TestAdjustIntervalClique(t *testing.T) {
func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) { func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine) {
defer engine.Close() defer engine.Close()
w, _ := newTestWorker(t, chainConfig, engine) w, _ := newTestWorker(t, chainConfig, engine, 0)
defer w.close() defer w.close()
w.skipSealHook = func(task *task) bool { w.skipSealHook = func(task *task) bool {
@ -387,10 +401,10 @@ func testAdjustInterval(t *testing.T, chainConfig *params.ChainConfig, engine co
// Check interval // Check interval
if minInterval != wantMinInterval { if minInterval != wantMinInterval {
t.Errorf("resubmit min interval mismatch want %s has %s", wantMinInterval, minInterval) t.Errorf("resubmit min interval mismatch: have %v, want %v ", minInterval, wantMinInterval)
} }
if recommitInterval != wantRecommitInterval { if recommitInterval != wantRecommitInterval {
t.Errorf("resubmit interval mismatch want %s has %s", wantRecommitInterval, recommitInterval) t.Errorf("resubmit interval mismatch: have %v, want %v", recommitInterval, wantRecommitInterval)
} }
result = append(result, float64(recommitInterval.Nanoseconds())) result = append(result, float64(recommitInterval.Nanoseconds()))
index += 1 index += 1

View file

@ -17,18 +17,12 @@
package params package params
// These are the multipliers for ether denominations. // These are the multipliers for ether denominations.
// Example: To get the wei value of an amount in 'douglas', use // Example: To get the wei value of an amount in 'gwei', use
// //
// new(big.Int).Mul(value, big.NewInt(params.Douglas)) // new(big.Int).Mul(value, big.NewInt(params.GWei))
// //
const ( const (
Wei = 1 Wei = 1
Ada = 1e3 GWei = 1e9
Babbage = 1e6 Ether = 1e18
Shannon = 1e9
Szabo = 1e12
Finney = 1e15
Ether = 1e18
Einstein = 1e21
Douglas = 1e42
) )

View file

@ -18,10 +18,6 @@ package params
import "math/big" import "math/big"
var (
TargetGasLimit = GenesisGasLimit // The artificial target
)
const ( const (
GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations. GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be. MinGasLimit uint64 = 5000 // Minimum the gas limit may ever be.

View file

@ -717,7 +717,7 @@ func (w *Whisper) expire() {
w.stats.messagesCleared++ w.stats.messagesCleared++
w.stats.memoryCleared += sz w.stats.memoryCleared += sz
w.stats.memoryUsed -= sz w.stats.memoryUsed -= sz
return true return false
}) })
w.expirations[expiry].Clear() w.expirations[expiry].Clear()
delete(w.expirations, expiry) delete(w.expirations, expiry)

View file

@ -487,27 +487,34 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
} }
params.TTL = 1 params.TTL = 1
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
env, err := msg.Wrap(params)
if err != nil {
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
}
err = w.Send(env) messagesCount := 5
if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) // Send a few messages one after another. Due to low PoW and expiration buckets
// with one second resolution, it covers a case when there are multiple items
// in a single expiration bucket.
for i := 0; i < messagesCount; i++ {
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
env, err := msg.Wrap(params)
if err != nil {
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
}
err = w.Send(env)
if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
}
} }
// wait till received or timeout // wait till received or timeout
var received, expired bool var received, expired bool
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if len(w.Envelopes()) > 0 { if len(w.Envelopes()) == messagesCount {
received = true received = true
break break
} }

View file

@ -900,7 +900,7 @@ func (whisper *Whisper) expire() {
whisper.stats.messagesCleared++ whisper.stats.messagesCleared++
whisper.stats.memoryCleared += sz whisper.stats.memoryCleared += sz
whisper.stats.memoryUsed -= sz whisper.stats.memoryUsed -= sz
return true return false
}) })
whisper.expirations[expiry].Clear() whisper.expirations[expiry].Clear()
delete(whisper.expirations, expiry) delete(whisper.expirations, expiry)

View file

@ -465,27 +465,34 @@ func TestExpiry(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
} }
params.TTL = 1 params.TTL = 1
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
env, err := msg.Wrap(params)
if err != nil {
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
}
err = w.Send(env) messagesCount := 5
if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err) // Send a few messages one after another. Due to low PoW and expiration buckets
// with one second resolution, it covers a case when there are multiple items
// in a single expiration bucket.
for i := 0; i < messagesCount; i++ {
msg, err := NewSentMessage(params)
if err != nil {
t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
}
env, err := msg.Wrap(params)
if err != nil {
t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
}
err = w.Send(env)
if err != nil {
t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
}
} }
// wait till received or timeout // wait till received or timeout
var received, expired bool var received, expired bool
for j := 0; j < 20; j++ { for j := 0; j < 20; j++ {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if len(w.Envelopes()) > 0 { if len(w.Envelopes()) == messagesCount {
received = true received = true
break break
} }