diff --git a/consensus/ethash/algorithm_test.go b/consensus/ethash/algorithm_test.go index ca17ae019c..38477f0f71 100644 --- a/consensus/ethash/algorithm_test.go +++ b/consensus/ethash/algorithm_test.go @@ -703,7 +703,7 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) { go func(idx int) { defer pend.Done() - ethash := New(Config{cachedir, 0, 1, "", 0, 0, false, false, false, false}) + ethash := New(Config{cachedir, 0, 1, "", 0, 0, Normal}) if err := ethash.VerifySeal(nil, block.Header()); err != nil { t.Errorf("proc %d: block verification failed: %v", idx, err) } diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 684f470764..7a598e8e81 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -68,7 +68,7 @@ func (ethash *Ethash) Author(header *types.Header) (common.Address, error) { // stock Ethereum ethash engine. func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { // If we're running a full engine faking, accept any input as valid - if ethash.config.FullFake { + if ethash.config.PowMode == FullFake { return nil } // Short circuit if the header is known, or it's parent not @@ -89,7 +89,7 @@ func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.He // a results channel to retrieve the async verifications. func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { // If we're running a full engine faking, accept any input as valid - if ethash.config.FullFake || len(headers) == 0 { + if ethash.config.PowMode == FullFake || len(headers) == 0 { abort, results := make(chan struct{}), make(chan error, len(headers)) for i := 0; i < len(headers); i++ { results <- nil @@ -169,7 +169,7 @@ func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers [] // rules of the stock Ethereum ethash engine. func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { // If we're running a full engine faking, accept any input as valid - if ethash.config.FullFake { + if ethash.config.PowMode == FullFake { return nil } // Verify that there are at most 2 uncles included in this block @@ -455,7 +455,7 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int { // the PoW difficulty requirements. func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error { // If we're running a fake PoW, accept any seal as valid - if ethash.config.Fake { + if ethash.config.PowMode == Fake || ethash.config.PowMode == FullFake { time.Sleep(ethash.fakeDelay) if ethash.fakeFail == header.Number.Uint64() { return errInvalidPoW @@ -480,7 +480,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head cache := ethash.cache(number) size := datasetSize(number) - if ethash.config.Test { + if ethash.config.PowMode == Test { size = 32 * 1024 } digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64()) diff --git a/consensus/ethash/ethash.go b/consensus/ethash/ethash.go index b3d4b2b4b6..78efae1d48 100644 --- a/consensus/ethash/ethash.go +++ b/consensus/ethash/ethash.go @@ -45,7 +45,7 @@ var ( maxUint256 = 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 = New(Config{"", 3, 0, "", 1, 0, false, false, false, false}) + sharedEthash = New(Config{"", 3, 0, "", 1, 0, Normal}) // algorithmRevision is the data structure version used for file naming. algorithmRevision = 23 @@ -320,6 +320,16 @@ func MakeDataset(block uint64, dir string) { d.release() } +type Mode uint + +const ( + Normal Mode = iota + Shared + Test + Fake + FullFake +) + // Config are the configuration parameters of the ethash. type Config struct { CacheDir string @@ -328,12 +338,7 @@ type Config struct { DatasetDir string DatasetsInMem int DatasetsOnDisk int - - // The fields below are configurations for testing - Fake bool - FullFake bool - Test bool - Shared bool + PowMode Mode } // Ethash is a consensus engine based on proot-of-work implementing the ethash @@ -387,7 +392,7 @@ func NewTester() *Ethash { return &Ethash{ config: Config{ CachesInMem: 1, - Test: true, + PowMode: Test, }, caches: make(map[uint64]*cache), datasets: make(map[uint64]*dataset), @@ -402,7 +407,7 @@ func NewTester() *Ethash { func NewFaker() *Ethash { return &Ethash{ config: Config{ - Fake: true, + PowMode: Fake, }, } } @@ -413,7 +418,7 @@ func NewFaker() *Ethash { func NewFakeFailer(fail uint64) *Ethash { return &Ethash{ config: Config{ - Fake: true, + PowMode: Fake, }, fakeFail: fail, } @@ -425,7 +430,7 @@ func NewFakeFailer(fail uint64) *Ethash { func NewFakeDelayer(delay time.Duration) *Ethash { return &Ethash{ config: Config{ - Fake: true, + PowMode: Fake, }, fakeDelay: delay, } @@ -436,8 +441,7 @@ func NewFakeDelayer(delay time.Duration) *Ethash { func NewFullFaker() *Ethash { return &Ethash{ config: Config{ - Fake: true, - FullFake: true, + PowMode: FullFake, }, } } @@ -497,7 +501,7 @@ func (ethash *Ethash) cache(block uint64) []uint32 { ethash.lock.Unlock() // Wait for generation finish, bump the timestamp and finalize the cache - current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.Test) + current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == Test) current.lock.Lock() current.used = time.Now() @@ -505,7 +509,7 @@ func (ethash *Ethash) cache(block uint64) []uint32 { // If we exhausted the future cache, now's a good time to regenerate it if future != nil { - go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.Test) + go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == Test) } return current.cache } @@ -560,7 +564,7 @@ func (ethash *Ethash) dataset(block uint64) []uint32 { ethash.lock.Unlock() // Wait for generation finish, bump the timestamp and finalize the cache - current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.Test) + current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == Test) current.lock.Lock() current.used = time.Now() @@ -568,7 +572,7 @@ func (ethash *Ethash) dataset(block uint64) []uint32 { // If we exhausted the future dataset, now's a good time to regenerate it if future != nil { - go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.Test) + go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == Test) } return current.dataset } diff --git a/consensus/ethash/sealer.go b/consensus/ethash/sealer.go index 62d056dd09..d7cceb11d4 100644 --- a/consensus/ethash/sealer.go +++ b/consensus/ethash/sealer.go @@ -34,7 +34,7 @@ import ( // the block's difficulty requirements. func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { // If we're running a fake PoW, simply return a 0 nonce immediately - if ethash.config.Fake { + if ethash.config.PowMode == Fake || ethash.config.PowMode == FullFake { header := block.Header() header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{} return block.WithSeal(header), nil diff --git a/console/console_test.go b/console/console_test.go index 492a55733f..98d0feddac 100644 --- a/console/console_test.go +++ b/console/console_test.go @@ -98,7 +98,7 @@ func newTester(t *testing.T, confOverride func(*eth.Config)) *tester { Genesis: core.DeveloperGenesisBlock(15, common.Address{}), Etherbase: common.HexToAddress(testAddress), Ethash: ethash.Config{ - Test: true, + PowMode: ethash.Test, }, } if confOverride != nil { diff --git a/eth/backend.go b/eth/backend.go index 1104e8909e..8d08feca60 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -216,13 +216,13 @@ func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chai } // Otherwise assume proof-of-work switch { - case config.Fake: + case config.PowMode == ethash.Fake: log.Warn("Ethash used in fake mode") return ethash.NewFaker() - case config.Test: + case config.PowMode == ethash.Test: log.Warn("Ethash used in test mode") return ethash.NewTester() - case config.Shared: + case config.PowMode == ethash.Shared: log.Warn("Ethash used in shared mode") return ethash.NewShared() default: diff --git a/eth/gen_config.go b/eth/gen_config.go index 134fae7465..e2d50e1f66 100644 --- a/eth/gen_config.go +++ b/eth/gen_config.go @@ -7,6 +7,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/gasprice" @@ -36,10 +37,8 @@ func (c Config) MarshalTOML() (interface{}, error) { TxPool core.TxPoolConfig GPO gasprice.Config EnablePreimageRecording bool - DocRoot string `toml:"-"` - PowFake bool `toml:"-"` - PowTest bool `toml:"-"` - PowShared bool `toml:"-"` + DocRoot string `toml:"-"` + PowMode ethash.Mode `toml:"-"` } var enc Config enc.Genesis = c.Genesis @@ -64,9 +63,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.GPO = c.GPO enc.EnablePreimageRecording = c.EnablePreimageRecording enc.DocRoot = c.DocRoot - enc.PowFake = c.Ethash.Fake - enc.PowTest = c.Ethash.Test - enc.PowShared = c.Ethash.Shared + enc.PowMode = c.Ethash.PowMode return &enc, nil } @@ -94,10 +91,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { TxPool *core.TxPoolConfig GPO *gasprice.Config EnablePreimageRecording *bool - DocRoot *string `toml:"-"` - PowFake *bool `toml:"-"` - PowTest *bool `toml:"-"` - PowShared *bool `toml:"-"` + DocRoot *string `toml:"-"` + PowMode *ethash.Mode `toml:"-"` } var dec Config if err := unmarshal(&dec); err != nil { @@ -169,14 +164,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.DocRoot != nil { c.DocRoot = *dec.DocRoot } - if dec.PowFake != nil { - c.Ethash.Fake = *dec.PowFake - } - if dec.PowTest != nil { - c.Ethash.Test = *dec.PowTest - } - if dec.PowShared != nil { - c.Ethash.Shared = *dec.PowShared + if dec.PowMode != nil { + c.Ethash.PowMode = *dec.PowMode } return nil }