mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
consensus, eth: document pow mode
This commit is contained in:
parent
2a0d4a6b96
commit
ac6908d71f
6 changed files with 27 additions and 26 deletions
|
|
@ -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, Normal})
|
ethash := New(Config{cachedir, 0, 1, "", 0, 0, ModeNormal})
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.PowMode == FullFake {
|
if ethash.config.PowMode == ModeFullFake {
|
||||||
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.PowMode == FullFake || len(headers) == 0 {
|
if ethash.config.PowMode == ModeFullFake || 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.PowMode == FullFake {
|
if ethash.config.PowMode == ModeFullFake {
|
||||||
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.PowMode == Fake || ethash.config.PowMode == FullFake {
|
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
|
||||||
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.PowMode == Test {
|
if ethash.config.PowMode == ModeTest {
|
||||||
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())
|
||||||
|
|
|
||||||
|
|
@ -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, Normal})
|
sharedEthash = New(Config{"", 3, 0, "", 1, 0, ModeNormal})
|
||||||
|
|
||||||
// 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,14 +320,15 @@ func MakeDataset(block uint64, dir string) {
|
||||||
d.release()
|
d.release()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mode defines the type and amount of PoW verification an ethash engine makes.
|
||||||
type Mode uint
|
type Mode uint
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Normal Mode = iota
|
ModeNormal Mode = iota
|
||||||
Shared
|
ModeShared
|
||||||
Test
|
ModeTest
|
||||||
Fake
|
ModeFake
|
||||||
FullFake
|
ModeFullFake
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config are the configuration parameters of the ethash.
|
// Config are the configuration parameters of the ethash.
|
||||||
|
|
@ -392,7 +393,7 @@ func NewTester() *Ethash {
|
||||||
return &Ethash{
|
return &Ethash{
|
||||||
config: Config{
|
config: Config{
|
||||||
CachesInMem: 1,
|
CachesInMem: 1,
|
||||||
PowMode: Test,
|
PowMode: ModeTest,
|
||||||
},
|
},
|
||||||
caches: make(map[uint64]*cache),
|
caches: make(map[uint64]*cache),
|
||||||
datasets: make(map[uint64]*dataset),
|
datasets: make(map[uint64]*dataset),
|
||||||
|
|
@ -407,7 +408,7 @@ func NewTester() *Ethash {
|
||||||
func NewFaker() *Ethash {
|
func NewFaker() *Ethash {
|
||||||
return &Ethash{
|
return &Ethash{
|
||||||
config: Config{
|
config: Config{
|
||||||
PowMode: Fake,
|
PowMode: ModeFake,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -418,7 +419,7 @@ func NewFaker() *Ethash {
|
||||||
func NewFakeFailer(fail uint64) *Ethash {
|
func NewFakeFailer(fail uint64) *Ethash {
|
||||||
return &Ethash{
|
return &Ethash{
|
||||||
config: Config{
|
config: Config{
|
||||||
PowMode: Fake,
|
PowMode: ModeFake,
|
||||||
},
|
},
|
||||||
fakeFail: fail,
|
fakeFail: fail,
|
||||||
}
|
}
|
||||||
|
|
@ -430,7 +431,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{
|
||||||
PowMode: Fake,
|
PowMode: ModeFake,
|
||||||
},
|
},
|
||||||
fakeDelay: delay,
|
fakeDelay: delay,
|
||||||
}
|
}
|
||||||
|
|
@ -441,7 +442,7 @@ func NewFakeDelayer(delay time.Duration) *Ethash {
|
||||||
func NewFullFaker() *Ethash {
|
func NewFullFaker() *Ethash {
|
||||||
return &Ethash{
|
return &Ethash{
|
||||||
config: Config{
|
config: Config{
|
||||||
PowMode: FullFake,
|
PowMode: ModeFullFake,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -501,7 +502,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.PowMode == Test)
|
current.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
|
||||||
|
|
||||||
current.lock.Lock()
|
current.lock.Lock()
|
||||||
current.used = time.Now()
|
current.used = time.Now()
|
||||||
|
|
@ -509,7 +510,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.PowMode == Test)
|
go future.generate(ethash.config.CacheDir, ethash.config.CachesOnDisk, ethash.config.PowMode == ModeTest)
|
||||||
}
|
}
|
||||||
return current.cache
|
return current.cache
|
||||||
}
|
}
|
||||||
|
|
@ -564,7 +565,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.PowMode == Test)
|
current.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
|
||||||
|
|
||||||
current.lock.Lock()
|
current.lock.Lock()
|
||||||
current.used = time.Now()
|
current.used = time.Now()
|
||||||
|
|
@ -572,7 +573,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.PowMode == Test)
|
go future.generate(ethash.config.DatasetDir, ethash.config.DatasetsOnDisk, ethash.config.PowMode == ModeTest)
|
||||||
}
|
}
|
||||||
return current.dataset
|
return current.dataset
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.PowMode == Fake || ethash.config.PowMode == FullFake {
|
if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake {
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -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{
|
||||||
PowMode: ethash.Test,
|
PowMode: ethash.ModeTest,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if confOverride != nil {
|
if confOverride != nil {
|
||||||
|
|
|
||||||
|
|
@ -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.PowMode == ethash.Fake:
|
case config.PowMode == ethash.ModeFake:
|
||||||
log.Warn("Ethash used in fake mode")
|
log.Warn("Ethash used in fake mode")
|
||||||
return ethash.NewFaker()
|
return ethash.NewFaker()
|
||||||
case config.PowMode == ethash.Test:
|
case config.PowMode == ethash.ModeTest:
|
||||||
log.Warn("Ethash used in test mode")
|
log.Warn("Ethash used in test mode")
|
||||||
return ethash.NewTester()
|
return ethash.NewTester()
|
||||||
case config.PowMode == ethash.Shared:
|
case config.PowMode == ethash.ModeShared:
|
||||||
log.Warn("Ethash used in shared mode")
|
log.Warn("Ethash used in shared mode")
|
||||||
return ethash.NewShared()
|
return ethash.NewShared()
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue