eth, consenus, console: compress pow testing config field to single one

This commit is contained in:
rjl493456442 2017-11-23 21:58:21 +08:00
parent 8913f67f8e
commit 2a0d4a6b96
7 changed files with 40 additions and 47 deletions

View file

@ -703,7 +703,7 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) {
go func(idx int) { go func(idx int) {
defer pend.Done() 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 { if err := ethash.VerifySeal(nil, block.Header()); err != nil {
t.Errorf("proc %d: block verification failed: %v", idx, err) t.Errorf("proc %d: block verification failed: %v", idx, err)
} }

View file

@ -68,7 +68,7 @@ func (ethash *Ethash) Author(header *types.Header) (common.Address, error) {
// stock Ethereum ethash engine. // stock Ethereum ethash engine.
func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error { 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 we're running a full engine faking, accept any input as valid
if ethash.config.FullFake { if ethash.config.PowMode == FullFake {
return nil return nil
} }
// Short circuit if the header is known, or it's parent not // 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. // a results channel to retrieve the async verifications.
func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) { 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 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)) abort, results := make(chan struct{}), make(chan error, len(headers))
for i := 0; i < len(headers); i++ { for i := 0; i < len(headers); i++ {
results <- nil results <- nil
@ -169,7 +169,7 @@ func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []
// rules of the stock Ethereum ethash engine. // rules of the stock Ethereum ethash engine.
func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Block) error { 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 we're running a full engine faking, accept any input as valid
if ethash.config.FullFake { if ethash.config.PowMode == FullFake {
return nil return nil
} }
// Verify that there are at most 2 uncles included in this block // 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. // the PoW difficulty requirements.
func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error { func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
// If we're running a fake PoW, accept any seal as valid // 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) time.Sleep(ethash.fakeDelay)
if ethash.fakeFail == header.Number.Uint64() { if ethash.fakeFail == header.Number.Uint64() {
return errInvalidPoW return errInvalidPoW
@ -480,7 +480,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
cache := ethash.cache(number) cache := ethash.cache(number)
size := datasetSize(number) size := datasetSize(number)
if ethash.config.Test { if ethash.config.PowMode == Test {
size = 32 * 1024 size = 32 * 1024
} }
digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64()) digest, result := hashimotoLight(size, cache, header.HashNoNonce().Bytes(), header.Nonce.Uint64())

View file

@ -45,7 +45,7 @@ var (
maxUint256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0)) 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 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 is the data structure version used for file naming.
algorithmRevision = 23 algorithmRevision = 23
@ -320,6 +320,16 @@ func MakeDataset(block uint64, dir string) {
d.release() d.release()
} }
type Mode uint
const (
Normal Mode = iota
Shared
Test
Fake
FullFake
)
// Config are the configuration parameters of the ethash. // Config are the configuration parameters of the ethash.
type Config struct { type Config struct {
CacheDir string CacheDir string
@ -328,12 +338,7 @@ type Config struct {
DatasetDir string DatasetDir string
DatasetsInMem int DatasetsInMem int
DatasetsOnDisk int DatasetsOnDisk int
PowMode Mode
// The fields below are configurations for testing
Fake bool
FullFake bool
Test bool
Shared bool
} }
// Ethash is a consensus engine based on proot-of-work implementing the ethash // Ethash is a consensus engine based on proot-of-work implementing the ethash
@ -387,7 +392,7 @@ func NewTester() *Ethash {
return &Ethash{ return &Ethash{
config: Config{ config: Config{
CachesInMem: 1, CachesInMem: 1,
Test: true, PowMode: Test,
}, },
caches: make(map[uint64]*cache), caches: make(map[uint64]*cache),
datasets: make(map[uint64]*dataset), datasets: make(map[uint64]*dataset),
@ -402,7 +407,7 @@ func NewTester() *Ethash {
func NewFaker() *Ethash { func NewFaker() *Ethash {
return &Ethash{ return &Ethash{
config: Config{ config: Config{
Fake: true, PowMode: Fake,
}, },
} }
} }
@ -413,7 +418,7 @@ func NewFaker() *Ethash {
func NewFakeFailer(fail uint64) *Ethash { func NewFakeFailer(fail uint64) *Ethash {
return &Ethash{ return &Ethash{
config: Config{ config: Config{
Fake: true, PowMode: Fake,
}, },
fakeFail: fail, fakeFail: fail,
} }
@ -425,7 +430,7 @@ func NewFakeFailer(fail uint64) *Ethash {
func NewFakeDelayer(delay time.Duration) *Ethash { func NewFakeDelayer(delay time.Duration) *Ethash {
return &Ethash{ return &Ethash{
config: Config{ config: Config{
Fake: true, PowMode: Fake,
}, },
fakeDelay: delay, fakeDelay: delay,
} }
@ -436,8 +441,7 @@ func NewFakeDelayer(delay time.Duration) *Ethash {
func NewFullFaker() *Ethash { func NewFullFaker() *Ethash {
return &Ethash{ return &Ethash{
config: Config{ config: Config{
Fake: true, PowMode: FullFake,
FullFake: true,
}, },
} }
} }
@ -497,7 +501,7 @@ func (ethash *Ethash) cache(block uint64) []uint32 {
ethash.lock.Unlock() ethash.lock.Unlock()
// Wait for generation finish, bump the timestamp and finalize the cache // 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.lock.Lock()
current.used = time.Now() 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 we exhausted the future cache, now's a good time to regenerate it
if future != nil { 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 return current.cache
} }
@ -560,7 +564,7 @@ func (ethash *Ethash) dataset(block uint64) []uint32 {
ethash.lock.Unlock() ethash.lock.Unlock()
// Wait for generation finish, bump the timestamp and finalize the cache // 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.lock.Lock()
current.used = time.Now() 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 we exhausted the future dataset, now's a good time to regenerate it
if future != nil { 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 return current.dataset
} }

View file

@ -34,7 +34,7 @@ import (
// the block's difficulty requirements. // the block's difficulty requirements.
func (ethash *Ethash) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) { 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 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 := block.Header()
header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{} header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{}
return block.WithSeal(header), nil return block.WithSeal(header), nil

View file

@ -98,7 +98,7 @@ func newTester(t *testing.T, confOverride func(*eth.Config)) *tester {
Genesis: core.DeveloperGenesisBlock(15, common.Address{}), Genesis: core.DeveloperGenesisBlock(15, common.Address{}),
Etherbase: common.HexToAddress(testAddress), Etherbase: common.HexToAddress(testAddress),
Ethash: ethash.Config{ Ethash: ethash.Config{
Test: true, PowMode: ethash.Test,
}, },
} }
if confOverride != nil { if confOverride != nil {

View file

@ -216,13 +216,13 @@ func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chai
} }
// Otherwise assume proof-of-work // Otherwise assume proof-of-work
switch { switch {
case config.Fake: case config.PowMode == ethash.Fake:
log.Warn("Ethash used in fake mode") log.Warn("Ethash used in fake mode")
return ethash.NewFaker() return ethash.NewFaker()
case config.Test: case config.PowMode == ethash.Test:
log.Warn("Ethash used in test mode") log.Warn("Ethash used in test mode")
return ethash.NewTester() return ethash.NewTester()
case config.Shared: case config.PowMode == ethash.Shared:
log.Warn("Ethash used in shared mode") log.Warn("Ethash used in shared mode")
return ethash.NewShared() return ethash.NewShared()
default: default:

View file

@ -7,6 +7,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "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/core"
"github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/eth/gasprice"
@ -36,10 +37,8 @@ func (c Config) MarshalTOML() (interface{}, error) {
TxPool core.TxPoolConfig TxPool core.TxPoolConfig
GPO gasprice.Config GPO gasprice.Config
EnablePreimageRecording bool EnablePreimageRecording bool
DocRoot string `toml:"-"` DocRoot string `toml:"-"`
PowFake bool `toml:"-"` PowMode ethash.Mode `toml:"-"`
PowTest bool `toml:"-"`
PowShared bool `toml:"-"`
} }
var enc Config var enc Config
enc.Genesis = c.Genesis enc.Genesis = c.Genesis
@ -64,9 +63,7 @@ func (c Config) MarshalTOML() (interface{}, error) {
enc.GPO = c.GPO enc.GPO = c.GPO
enc.EnablePreimageRecording = c.EnablePreimageRecording enc.EnablePreimageRecording = c.EnablePreimageRecording
enc.DocRoot = c.DocRoot enc.DocRoot = c.DocRoot
enc.PowFake = c.Ethash.Fake enc.PowMode = c.Ethash.PowMode
enc.PowTest = c.Ethash.Test
enc.PowShared = c.Ethash.Shared
return &enc, nil return &enc, nil
} }
@ -94,10 +91,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
TxPool *core.TxPoolConfig TxPool *core.TxPoolConfig
GPO *gasprice.Config GPO *gasprice.Config
EnablePreimageRecording *bool EnablePreimageRecording *bool
DocRoot *string `toml:"-"` DocRoot *string `toml:"-"`
PowFake *bool `toml:"-"` PowMode *ethash.Mode `toml:"-"`
PowTest *bool `toml:"-"`
PowShared *bool `toml:"-"`
} }
var dec Config var dec Config
if err := unmarshal(&dec); err != nil { if err := unmarshal(&dec); err != nil {
@ -169,14 +164,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error {
if dec.DocRoot != nil { if dec.DocRoot != nil {
c.DocRoot = *dec.DocRoot c.DocRoot = *dec.DocRoot
} }
if dec.PowFake != nil { if dec.PowMode != nil {
c.Ethash.Fake = *dec.PowFake c.Ethash.PowMode = *dec.PowMode
}
if dec.PowTest != nil {
c.Ethash.Test = *dec.PowTest
}
if dec.PowShared != nil {
c.Ethash.Shared = *dec.PowShared
} }
return nil return nil
} }