mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
tmp fix ethash unittests
This commit is contained in:
parent
5a5e2dbc1a
commit
9a9796a1a2
7 changed files with 61 additions and 58 deletions
|
|
@ -33,7 +33,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/common/fdlimit"
|
"github.com/ethereum/go-ethereum/common/fdlimit"
|
||||||
"github.com/ethereum/go-ethereum/consensus"
|
"github.com/ethereum/go-ethereum/consensus"
|
||||||
//"github.com/ethereum/go-ethereum/consensus/ethash"
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
||||||
"github.com/ethereum/go-ethereum/consensus/posv"
|
"github.com/ethereum/go-ethereum/consensus/posv"
|
||||||
"github.com/ethereum/go-ethereum/core"
|
"github.com/ethereum/go-ethereum/core"
|
||||||
"github.com/ethereum/go-ethereum/core/state"
|
"github.com/ethereum/go-ethereum/core/state"
|
||||||
|
|
@ -1245,17 +1245,17 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
|
||||||
if config.Posv != nil {
|
if config.Posv != nil {
|
||||||
engine = posv.New(config.Posv, chainDb)
|
engine = posv.New(config.Posv, chainDb)
|
||||||
} else {
|
} else {
|
||||||
//engine = ethash.NewFaker()
|
engine = ethash.NewFaker()
|
||||||
//if !ctx.GlobalBool(FakePoWFlag.Name) {
|
if !ctx.GlobalBool(FakePoWFlag.Name) {
|
||||||
// engine = ethash.New(ethash.Config{
|
engine = ethash.New(ethash.Config{
|
||||||
// CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
|
CacheDir: stack.ResolvePath(eth.DefaultConfig.Ethash.CacheDir),
|
||||||
// CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
|
CachesInMem: eth.DefaultConfig.Ethash.CachesInMem,
|
||||||
// CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
|
CachesOnDisk: eth.DefaultConfig.Ethash.CachesOnDisk,
|
||||||
// DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
|
DatasetDir: stack.ResolvePath(eth.DefaultConfig.Ethash.DatasetDir),
|
||||||
// DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
|
DatasetsInMem: eth.DefaultConfig.Ethash.DatasetsInMem,
|
||||||
// DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
|
DatasetsOnDisk: eth.DefaultConfig.Ethash.DatasetsOnDisk,
|
||||||
// })
|
})
|
||||||
//}
|
}
|
||||||
Fatalf("Only support posv consensus")
|
Fatalf("Only support posv consensus")
|
||||||
}
|
}
|
||||||
if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
|
if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ func (ethash *Ethash) Author(header *types.Header) (common.Address, error) {
|
||||||
|
|
||||||
// VerifyHeader checks whether a header conforms to the consensus rules of the
|
// VerifyHeader checks whether a header conforms to the consensus rules of the
|
||||||
// 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, state *state.StateDB, 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 == ModeFullFake {
|
if ethash.config.PowMode == ModeFullFake {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -81,13 +81,13 @@ func (ethash *Ethash) VerifyHeader(chain consensus.ChainReader, header *types.He
|
||||||
return consensus.ErrUnknownAncestor
|
return consensus.ErrUnknownAncestor
|
||||||
}
|
}
|
||||||
// Sanity checks passed, do a proper verification
|
// Sanity checks passed, do a proper verification
|
||||||
return ethash.verifyHeader(chain, header, parent, false, seal)
|
return ethash.verifyHeader(chain, state, header, parent, false, seal)
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
|
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
|
||||||
// concurrently. The method returns a quit channel to abort the operations and
|
// concurrently. The method returns a quit channel to abort the operations and
|
||||||
// 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, state *state.StateDB, 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 == ModeFullFake || 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))
|
||||||
|
|
@ -113,7 +113,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*type
|
||||||
for i := 0; i < workers; i++ {
|
for i := 0; i < workers; i++ {
|
||||||
go func() {
|
go func() {
|
||||||
for index := range inputs {
|
for index := range inputs {
|
||||||
errors[index] = ethash.verifyHeaderWorker(chain, headers, seals, index)
|
errors[index] = ethash.verifyHeaderWorker(chain, state, headers, seals, index)
|
||||||
done <- index
|
done <- index
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
@ -149,7 +149,7 @@ func (ethash *Ethash) VerifyHeaders(chain consensus.ChainReader, headers []*type
|
||||||
return abort, errorsOut
|
return abort, errorsOut
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []*types.Header, seals []bool, index int) error {
|
func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, state *state.StateDB, headers []*types.Header, seals []bool, index int) error {
|
||||||
var parent *types.Header
|
var parent *types.Header
|
||||||
if index == 0 {
|
if index == 0 {
|
||||||
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
|
parent = chain.GetHeader(headers[0].ParentHash, headers[0].Number.Uint64()-1)
|
||||||
|
|
@ -162,7 +162,7 @@ func (ethash *Ethash) verifyHeaderWorker(chain consensus.ChainReader, headers []
|
||||||
if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
|
if chain.GetHeader(headers[index].Hash(), headers[index].Number.Uint64()) != nil {
|
||||||
return nil // known block
|
return nil // known block
|
||||||
}
|
}
|
||||||
return ethash.verifyHeader(chain, headers[index], parent, false, seals[index])
|
return ethash.verifyHeader(chain, state, headers[index], parent, false, seals[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyUncles verifies that the given block's uncles conform to the consensus
|
// VerifyUncles verifies that the given block's uncles conform to the consensus
|
||||||
|
|
@ -210,7 +210,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
|
||||||
if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
|
if ancestors[uncle.ParentHash] == nil || uncle.ParentHash == block.ParentHash() {
|
||||||
return errDanglingUncle
|
return errDanglingUncle
|
||||||
}
|
}
|
||||||
if err := ethash.verifyHeader(chain, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
|
if err := ethash.verifyHeader(chain, nil, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -220,7 +220,7 @@ func (ethash *Ethash) VerifyUncles(chain consensus.ChainReader, block *types.Blo
|
||||||
// verifyHeader checks whether a header conforms to the consensus rules of the
|
// verifyHeader checks whether a header conforms to the consensus rules of the
|
||||||
// stock Ethereum ethash engine.
|
// stock Ethereum ethash engine.
|
||||||
// See YP section 4.3.4. "Block Header Validity"
|
// See YP section 4.3.4. "Block Header Validity"
|
||||||
func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent *types.Header, uncle bool, seal bool) error {
|
func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, state *state.StateDB, header, parent *types.Header, uncle bool, seal bool) error {
|
||||||
// Ensure that the header's extra-data section is of a reasonable size
|
// Ensure that the header's extra-data section is of a reasonable size
|
||||||
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
|
if uint64(len(header.Extra)) > params.MaximumExtraDataSize {
|
||||||
return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
|
return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize)
|
||||||
|
|
@ -502,7 +502,7 @@ func (ethash *Ethash) VerifySeal(chain consensus.ChainReader, header *types.Head
|
||||||
|
|
||||||
// Prepare implements consensus.Engine, initializing the difficulty field of a
|
// Prepare implements consensus.Engine, initializing the difficulty field of a
|
||||||
// header to conform to the ethash protocol. The changes are done inline.
|
// header to conform to the ethash protocol. The changes are done inline.
|
||||||
func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) error {
|
func (ethash *Ethash) Prepare(chain consensus.ChainReader, state *state.StateDB, header *types.Header) error {
|
||||||
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
|
parent := chain.GetHeader(header.ParentHash, header.Number.Uint64()-1)
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
return consensus.ErrUnknownAncestor
|
return consensus.ErrUnknownAncestor
|
||||||
|
|
|
||||||
|
|
@ -144,7 +144,7 @@ func TestRewardBalance(t *testing.T) {
|
||||||
|
|
||||||
foundationAddr := common.HexToAddress(common.FoudationAddr)
|
foundationAddr := common.HexToAddress(common.FoudationAddr)
|
||||||
totalReward := new(big.Int).SetInt64(15 * 1000)
|
totalReward := new(big.Int).SetInt64(15 * 1000)
|
||||||
rewards, err := contracts.GetRewardBalancesRate(foundationAddr, acc3Addr, totalReward, baseValidator)
|
rewards, err := contracts.GetRewardBalancesRate(foundationAddr, nil, acc3Addr, totalReward)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Fail to get reward balances rate.", err)
|
t.Error("Fail to get reward balances rate.", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,13 +48,13 @@ func TestHeaderVerification(t *testing.T) {
|
||||||
for i := 0; i < len(blocks); i++ {
|
for i := 0; i < len(blocks); i++ {
|
||||||
for j, valid := range []bool{true, false} {
|
for j, valid := range []bool{true, false} {
|
||||||
var results <-chan error
|
var results <-chan error
|
||||||
|
state, _ := chain.State()
|
||||||
if valid {
|
if valid {
|
||||||
engine := ethash.NewFaker()
|
engine := ethash.NewFaker()
|
||||||
_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
|
_, results = engine.VerifyHeaders(chain, state, []*types.Header{headers[i]}, []bool{true})
|
||||||
} else {
|
} else {
|
||||||
engine := ethash.NewFakeFailer(headers[i].Number.Uint64())
|
engine := ethash.NewFakeFailer(headers[i].Number.Uint64())
|
||||||
_, results = engine.VerifyHeaders(chain, []*types.Header{headers[i]}, []bool{true})
|
_, results = engine.VerifyHeaders(chain, state, []*types.Header{headers[i]}, []bool{true})
|
||||||
}
|
}
|
||||||
// Wait for the verification result
|
// Wait for the verification result
|
||||||
select {
|
select {
|
||||||
|
|
@ -104,14 +104,15 @@ func testHeaderConcurrentVerification(t *testing.T, threads int) {
|
||||||
// also an invalid chain (enough if one arbitrary block is invalid).
|
// also an invalid chain (enough if one arbitrary block is invalid).
|
||||||
for i, valid := range []bool{true, false} {
|
for i, valid := range []bool{true, false} {
|
||||||
var results <-chan error
|
var results <-chan error
|
||||||
|
|
||||||
if valid {
|
if valid {
|
||||||
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFaker(), vm.Config{})
|
||||||
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
|
state, _ := chain.State()
|
||||||
|
_, results = chain.engine.VerifyHeaders(chain, state, headers, seals)
|
||||||
chain.Stop()
|
chain.Stop()
|
||||||
} else {
|
} else {
|
||||||
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), vm.Config{})
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeFailer(uint64(len(headers)-1)), vm.Config{})
|
||||||
_, results = chain.engine.VerifyHeaders(chain, headers, seals)
|
state, _ := chain.State()
|
||||||
|
_, results = chain.engine.VerifyHeaders(chain, state, headers, seals)
|
||||||
chain.Stop()
|
chain.Stop()
|
||||||
}
|
}
|
||||||
// Wait for all the verification results
|
// Wait for all the verification results
|
||||||
|
|
@ -175,8 +176,8 @@ func testHeaderConcurrentAbortion(t *testing.T, threads int) {
|
||||||
// Start the verifications and immediately abort
|
// Start the verifications and immediately abort
|
||||||
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), vm.Config{})
|
chain, _ := NewBlockChain(testdb, nil, params.TestChainConfig, ethash.NewFakeDelayer(time.Millisecond), vm.Config{})
|
||||||
defer chain.Stop()
|
defer chain.Stop()
|
||||||
|
state, _ := chain.State()
|
||||||
abort, results := chain.engine.VerifyHeaders(chain, headers, seals)
|
abort, results := chain.engine.VerifyHeaders(chain, state, headers, seals)
|
||||||
close(abort)
|
close(abort)
|
||||||
|
|
||||||
// Deplete the results channel
|
// Deplete the results channel
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,8 @@ func printChain(bc *BlockChain) {
|
||||||
func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
|
func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
|
||||||
for _, block := range chain {
|
for _, block := range chain {
|
||||||
// Try and process the block
|
// Try and process the block
|
||||||
err := blockchain.engine.VerifyHeader(blockchain, block.Header(), true)
|
st, _ := blockchain.State()
|
||||||
|
err := blockchain.engine.VerifyHeader(blockchain, st, block.Header(), true)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = blockchain.validator.ValidateBody(block)
|
err = blockchain.validator.ValidateBody(block)
|
||||||
}
|
}
|
||||||
|
|
@ -141,7 +142,8 @@ func testBlockChainImport(chain types.Blocks, blockchain *BlockChain) error {
|
||||||
func testHeaderChainImport(chain []*types.Header, blockchain *BlockChain) error {
|
func testHeaderChainImport(chain []*types.Header, blockchain *BlockChain) error {
|
||||||
for _, header := range chain {
|
for _, header := range chain {
|
||||||
// Try and validate the header
|
// Try and validate the header
|
||||||
if err := blockchain.engine.VerifyHeader(blockchain, header, false); err != nil {
|
state, _ := blockchain.State()
|
||||||
|
if err := blockchain.engine.VerifyHeader(blockchain, state, header, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Manually insert the header into the database, but don't reorganise (allows subsequent testing)
|
// Manually insert the header into the database, but don't reorganise (allows subsequent testing)
|
||||||
|
|
|
||||||
|
|
@ -393,31 +393,30 @@ func CreateConsensusEngine(ctx *node.ServiceContext, config *ethash.Config, chai
|
||||||
if chainConfig.Posv != nil {
|
if chainConfig.Posv != nil {
|
||||||
return posv.New(chainConfig.Posv, db)
|
return posv.New(chainConfig.Posv, db)
|
||||||
}
|
}
|
||||||
// Otherwise, return nil
|
|
||||||
return nil
|
// Otherwise assume proof-of-work
|
||||||
//// Otherwise assume proof-of-work
|
switch {
|
||||||
//switch {
|
case config.PowMode == ethash.ModeFake:
|
||||||
//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.ModeTest:
|
||||||
//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.ModeShared:
|
||||||
//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:
|
engine := ethash.New(ethash.Config{
|
||||||
// engine := ethash.New(ethash.Config{
|
CacheDir: ctx.ResolvePath(config.CacheDir),
|
||||||
// CacheDir: ctx.ResolvePath(config.CacheDir),
|
CachesInMem: config.CachesInMem,
|
||||||
// CachesInMem: config.CachesInMem,
|
CachesOnDisk: config.CachesOnDisk,
|
||||||
// CachesOnDisk: config.CachesOnDisk,
|
DatasetDir: config.DatasetDir,
|
||||||
// DatasetDir: config.DatasetDir,
|
DatasetsInMem: config.DatasetsInMem,
|
||||||
// DatasetsInMem: config.DatasetsInMem,
|
DatasetsOnDisk: config.DatasetsOnDisk,
|
||||||
// DatasetsOnDisk: config.DatasetsOnDisk,
|
})
|
||||||
// })
|
engine.SetThreads(-1) // Disable CPU mining
|
||||||
// engine.SetThreads(-1) // Disable CPU mining
|
return engine
|
||||||
// return engine
|
}
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIs returns the collection of RPC services the ethereum package offers.
|
// APIs returns the collection of RPC services the ethereum package offers.
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,8 @@ func testFork(t *testing.T, LightChain *LightChain, i, n int, comparator func(td
|
||||||
func testHeaderChainImport(chain []*types.Header, lightchain *LightChain) error {
|
func testHeaderChainImport(chain []*types.Header, lightchain *LightChain) error {
|
||||||
for _, header := range chain {
|
for _, header := range chain {
|
||||||
// Try and validate the header
|
// Try and validate the header
|
||||||
if err := lightchain.engine.VerifyHeader(lightchain.hc, header, true); err != nil {
|
state, _ := lightchain.State()
|
||||||
|
if err := lightchain.engine.VerifyHeader(lightchain.hc, state, header, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Manually insert the header into the database, but don't reorganize (allows subsequent testing)
|
// Manually insert the header into the database, but don't reorganize (allows subsequent testing)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue