Skip to content

Commit

Permalink
consensus/ethash, cmd/puppeth: address review concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Apr 10, 2019
1 parent 95dd8c3 commit e822a0d
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/puppeth/module_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ func deployDashboard(client *sshClient, network string, conf *config, config *da
"Byzantium": conf.Genesis.Config.ByzantiumBlock,
"Constantinople": conf.Genesis.Config.ConstantinopleBlock,
"ConstantinopleFix": conf.Genesis.Config.PetersburgBlock,
"ProgPow": conf.Genesis.Config.ProgpowBlock,
"ProgPoW": conf.Genesis.Config.ProgpowBlock,
})
files[filepath.Join(workdir, "index.html")] = indexfile.Bytes()

Expand Down
8 changes: 5 additions & 3 deletions cmd/puppeth/wizard_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ func (w *wizard) manageGenesis() {
fmt.Printf("Which block should Constantinople-Fix (remove EIP-1283) come into effect? (default = %v)\n", w.conf.Genesis.Config.PetersburgBlock)
w.conf.Genesis.Config.PetersburgBlock = w.readDefaultBigInt(w.conf.Genesis.Config.PetersburgBlock)

fmt.Println()
fmt.Printf("Which block should ProgPow come into effect? (default = %v)\n", w.conf.Genesis.Config.ProgpowBlock)
w.conf.Genesis.Config.ProgpowBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ProgpowBlock)
if w.conf.Genesis.Config.Clique == nil {
fmt.Println()
fmt.Printf("Which block should ProgPow come into effect? (default = %v)\n", w.conf.Genesis.Config.ProgpowBlock)
w.conf.Genesis.Config.ProgpowBlock = w.readDefaultBigInt(w.conf.Genesis.Config.ProgpowBlock)
}

out, _ := json.MarshalIndent(w.conf.Genesis.Config, "", " ")
fmt.Printf("Chain configuration updated:\n\n%s\n", out)
Expand Down
14 changes: 7 additions & 7 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,13 +1573,13 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
engine = ethash.NewFaker()
if !ctx.GlobalBool(FakePoWFlag.Name) {
engine = ethash.New(ethash.Config{
CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
ProgpowBlockNumber: config.ProgpowBlock,
CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
ProgpowBlock: config.ProgpowBlock,
}, nil, false)
}
}
Expand Down
12 changes: 6 additions & 6 deletions consensus/ethash/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,15 +739,15 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) {
pend.Wait()
}

// Benchmarks the cache generation performance.
// BenchmarkCacheGeneration benchmarks the cache generation performance.
func BenchmarkCacheGeneration(b *testing.B) {
for i := 0; i < b.N; i++ {
cache := make([]uint32, cacheSize(1)/4)
generateCache(cache, 0, make([]byte, 32))
}
}

// Benchmarks the dataset (small) generation performance.
// BenchmarkSmallDatasetGeneration benchmarks the dataset (small) generation performance.
func BenchmarkSmallDatasetGeneration(b *testing.B) {
cache := make([]uint32, 65536/4)
generateCache(cache, 0, make([]byte, 32))
Expand All @@ -759,7 +759,7 @@ func BenchmarkSmallDatasetGeneration(b *testing.B) {
}
}

// Benchmarks the light verification performance.
// BenchmarkHashimotoLight benchmarks the light verification performance.
func BenchmarkHashimotoLight(b *testing.B) {
cache := make([]uint32, cacheSize(1)/4)
generateCache(cache, 0, make([]byte, 32))
Expand All @@ -772,7 +772,7 @@ func BenchmarkHashimotoLight(b *testing.B) {
}
}

// BenchmarkProgpowLight Benchmarks the light verification performance (not counting cDag generation).
// BenchmarkProgpowLight benchmarks the light verification performance (not counting cDag generation).
func BenchmarkProgpowLight(b *testing.B) {
cache := make([]uint32, cacheSize(1)/4)
generateCache(cache, 0, make([]byte, 32))
Expand All @@ -787,7 +787,7 @@ func BenchmarkProgpowLight(b *testing.B) {
}
}

// Benchmarks the full (small) verification performance.
// BenchmarkHashimotoFullSmall benchmarks the full (small) verification performance.
func BenchmarkHashimotoFullSmall(b *testing.B) {
cache := make([]uint32, 65536/4)
generateCache(cache, 0, make([]byte, 32))
Expand All @@ -803,7 +803,7 @@ func BenchmarkHashimotoFullSmall(b *testing.B) {
}
}

// Benchmarks the full (small) verification performance.
// BenchmarkProgpowFullSmall benchmarks the full (small) verification performance.
func BenchmarkProgpowFullSmall(b *testing.B) {
cache := make([]uint32, 65536/4)
generateCache(cache, 0, make([]byte, 32))
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestCalcDifficulty(t *testing.T) {
// DatasetDir: "",
// DatasetsInMem: 1,
// DatasetsOnDisk: 1,
// ProgpowBlockNumber: config.ProgpowBlock,
// ProgpowBlock: config.ProgpowBlock,
// }, nil, false)
// bc, err := core.NewBlockChain(db, nil, config, engine, vm.Config{}, nil)
// //fmt.Printf("Genesis hash %x\n", bc.Genesis().Hash())
Expand Down
37 changes: 21 additions & 16 deletions consensus/ethash/ethash.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ var (
// two256 is a big integer representing 2^256
two256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))

// sharedEthash is a full instance that can be shared between multiple users.
sharedEthash *Ethash
ethashMu sync.Mutex // lock for initializing sharedEthash
// sharedEngines contains ethash instances which are mapped by progpow blocknumber
sharedEngines map[uint64]*Ethash
ethashMu sync.Mutex // lock for modifying sharedEngines

// algorithmRevision is the data structure version used for file naming.
algorithmRevision = 23
Expand Down Expand Up @@ -405,14 +405,14 @@ const (

// Config are the configuration parameters of the ethash.
type Config struct {
CacheDir string
CachesInMem int
CachesOnDisk int
DatasetDir string
DatasetsInMem int
DatasetsOnDisk int
PowMode Mode
ProgpowBlockNumber *big.Int // Block number at which to use progpow instead of hashimoto
CacheDir string
CachesInMem int
CachesOnDisk int
DatasetDir string
DatasetsInMem int
DatasetsOnDisk int
PowMode Mode
ProgpowBlock *big.Int // Block number at which to use progpow instead of hashimoto
}

// sealTask wraps a seal block with relative result channel for remote sealer thread.
Expand Down Expand Up @@ -576,11 +576,16 @@ func NewFullFaker() *Ethash {
// in the same process.
func NewShared(progpowNumber *big.Int) *Ethash {
ethashMu.Lock()
if sharedEthash == nil {
sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal, progpowNumber}, nil, false)
if progpowNumber == nil {
progpowNumber = new(big.Int).SetUint64(uint64(math.MaxUint64))
}
sharedEngine, exist := sharedEngines[progpowNumber.Uint64()]
if !exist {
sharedEngine = New(Config{"", 3, 0, "", 1, 0, ModeNormal, progpowNumber}, nil, false)
sharedEngines[progpowNumber.Uint64()] = sharedEngine
}
ethashMu.Unlock()
return &Ethash{shared: sharedEthash}
return &Ethash{shared: sharedEngine}
}

// Close closes the exit channel to notify all backend threads exiting.
Expand Down Expand Up @@ -736,7 +741,7 @@ type powLight func(size uint64, cache []uint32, hash []byte, nonce, number uint6

// fullPow returns either hashimoto or progpow full checker depending on number
func (ethash *Ethash) fullPow(number *big.Int) powFull {
if progpowNumber := ethash.config.ProgpowBlockNumber; progpowNumber != nil && progpowNumber.Cmp(number) <= 0 {
if progpowNumber := ethash.config.ProgpowBlock; progpowNumber != nil && progpowNumber.Cmp(number) <= 0 {
ethashCache := ethash.cache(number.Uint64())
if ethashCache.cDag == nil {
log.Warn("cDag is nil, suboptimal performance")
Expand All @@ -762,7 +767,7 @@ func (ethash *Ethash) fullPow(number *big.Int) powFull {

// lightPow returns either hashimoto or progpow depending on number
func (ethash *Ethash) lightPow(number *big.Int) powLight {
if progpowNumber := ethash.config.ProgpowBlockNumber; progpowNumber != nil && progpowNumber.Cmp(number) <= 0 {
if progpowNumber := ethash.config.ProgpowBlock; progpowNumber != nil && progpowNumber.Cmp(number) <= 0 {
return func(size uint64, cache []uint32, hash []byte, nonce uint64, blockNumber uint64) ([]byte, []byte) {
ethashCache := ethash.cache(blockNumber)
if ethashCache.cDag == nil {
Expand Down
17 changes: 17 additions & 0 deletions consensus/ethash/progpow.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// Copyright 2019 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 ethash implements the ethash proof-of-work consensus engine.
package ethash

import (
Expand Down
2 changes: 1 addition & 1 deletion consensus/ethash/progpow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (n *progpowHashTestcase) UnmarshalJSON(buf []byte) error {
return nil
}
func TestProgpowHashes(t *testing.T) {
data, err := ioutil.ReadFile(filepath.Join("..", "..", "tests", "progpow_testvectors.json"))
data, err := ioutil.ReadFile(filepath.Join(".", "testdata", "progpow_testvectors.json"))
if err != nil {
t.Fatal(err)
}
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainCo
return ethash.NewShared(chainConfig.ProgpowBlock)
default:
engine := ethash.New(ethash.Config{
CacheDir: ctx.ResolvePath(config.CacheDir),
CachesInMem: config.CachesInMem,
CachesOnDisk: config.CachesOnDisk,
DatasetDir: config.DatasetDir,
DatasetsInMem: config.DatasetsInMem,
DatasetsOnDisk: config.DatasetsOnDisk,
ProgpowBlockNumber: chainConfig.ProgpowBlock,
CacheDir: ctx.ResolvePath(config.CacheDir),
CachesInMem: config.CachesInMem,
CachesOnDisk: config.CachesOnDisk,
DatasetDir: config.DatasetDir,
DatasetsInMem: config.DatasetsInMem,
DatasetsOnDisk: config.DatasetsOnDisk,
ProgpowBlock: chainConfig.ProgpowBlock,
}, notify, noverify)
engine.SetThreads(-1) // Disable CPU mining
return engine
Expand Down

0 comments on commit e822a0d

Please sign in to comment.