Skip to content

Commit

Permalink
Revert "E3: Remove Proof-Of-Work Consensus code" (#11628)
Browse files Browse the repository at this point in the history
This reverts PR #11556, which caused Hive tests (e.g. "Bad Hash on
NewPayload") to crash.
  • Loading branch information
yperbasis committed Aug 15, 2024
1 parent ef233ed commit c746942
Show file tree
Hide file tree
Showing 53 changed files with 5,700 additions and 523 deletions.
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
"github.com/erigontech/erigon/common/math"
"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/consensus/misc"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
Expand Down Expand Up @@ -96,7 +96,7 @@ type SimulatedBackend struct {
// for testing purposes.
func NewSimulatedBackendWithConfig(t *testing.T, alloc types.GenesisAlloc, config *chain.Config, gasLimit uint64) *SimulatedBackend {
genesis := types.Genesis{Config: config, GasLimit: gasLimit, Alloc: alloc}
engine := mainnet.NewMainnetConsensus()
engine := ethash.NewFaker()
checkStateRoot := true
m := mock.MockWithGenesisEngine(t, &genesis, engine, false, checkStateRoot)

Expand Down
4 changes: 4 additions & 0 deletions cmd/evm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Mining rewards and ommer rewards might need to be added. This is how those are a
To make `t8n` apply these, the following inputs are required:

- `--state.reward`
- For ethash, it is `5000000000000000000` `wei`,
- If this is not defined, mining rewards are not applied,
- A value of `0` is valid, and causes accounts to be 'touched'.
- For each ommer, the tool needs to be given an `address\` and a `delta`. This
Expand Down Expand Up @@ -533,6 +534,9 @@ Command line params that need to be supported are:
`stdout` - into the stdout output
`stderr` - into the stderr output
--seal.clique value Seal block with Clique. `stdin` or file name of where to find the Clique sealing data.
--seal.ethash Seal block with ethash. (default: false)
--seal.ethash.dir value Path to ethash DAG. If none exists, a new DAG will be generated.
--seal.ethash.mode value Defines the type and amount of PoW verification an ethash engine makes. (default: "normal")
--verbosity value Sets the verbosity level. (default: 3)
```

Expand Down
21 changes: 21 additions & 0 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
state3 "github.com/erigontech/erigon-lib/state"
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/math"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/tracing"
"github.com/erigontech/erigon/core/types"
Expand Down Expand Up @@ -116,3 +117,23 @@ func MakePreState(chainRules *chain.Rules, tx kv.RwTx, sd *state3.SharedDomains,
}
return stateReader, stateWriter
}

// calcDifficulty is based on ethash.CalcDifficulty. This method is used in case
// the caller does not provide an explicit difficulty, but instead provides only
// parent timestamp + difficulty.
// Note: this method only works for ethash engine.
func calcDifficulty(config *chain.Config, number, currentTime, parentTime uint64,
parentDifficulty *big.Int, parentUncleHash libcommon.Hash) *big.Int {
uncleHash := parentUncleHash
if uncleHash == (libcommon.Hash{}) {
uncleHash = types.EmptyUncleHash
}
parent := &types.Header{
ParentHash: libcommon.Hash{},
UncleHash: uncleHash,
Difficulty: parentDifficulty,
Number: new(big.Int).SetUint64(number - 1),
Time: parentTime,
}
return ethash.CalcDifficulty(config, currentTime, parent.Time, parent.Difficulty, number-1, parent.UncleHash)
}
17 changes: 14 additions & 3 deletions cmd/evm/internal/t8ntool/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (
"github.com/erigontech/erigon-lib/kv"
libstate "github.com/erigontech/erigon-lib/state"
"github.com/erigontech/erigon/common/math"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/consensus/merge"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/state"
Expand Down Expand Up @@ -262,7 +262,18 @@ func Main(ctx *cli.Context) error {
}
prestate.Env.Difficulty = nil
} else if env.Difficulty == nil {
return NewError(ErrorVMConfig, errors.New("POW not supported, difficulty must be defined in env"))
// If difficulty was not provided by caller, we need to calculate it.
switch {
case env.ParentDifficulty == nil:
return NewError(ErrorVMConfig, errors.New("currentDifficulty was not provided, and cannot be calculated due to missing parentDifficulty"))
case env.Number == 0:
return NewError(ErrorVMConfig, errors.New("currentDifficulty needs to be provided for block number 0"))
case env.Timestamp <= env.ParentTimestamp:
return NewError(ErrorVMConfig, fmt.Errorf("currentDifficulty cannot be calculated -- currentTime (%d) needs to be after parent time (%d)",
env.Timestamp, env.ParentTimestamp))
}
prestate.Env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp,
env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash)
}

// manufacture block from above inputs
Expand Down Expand Up @@ -309,7 +320,7 @@ func Main(ctx *cli.Context) error {
reader, writer := MakePreState(chainConfig.Rules(0, 0), tx, sd, prestate.Pre)
// Merge engine can be used for pre-merge blocks as well, as it
// redirects to the ethash engine based on the block number
engine := merge.New(&mainnet.MainnetConsensus{})
engine := merge.New(&ethash.FakeEthash{})

t8logger := log.New("t8ntool")
chainReader := consensuschain.NewReader(chainConfig, tx, nil, t8logger)
Expand Down
3 changes: 1 addition & 2 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
"github.com/erigontech/erigon/cl/clparams"
"github.com/erigontech/erigon/cmd/hack/tool/fromdb"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/rawdb/blockio"
Expand Down Expand Up @@ -1518,7 +1517,7 @@ func initConsensusEngine(ctx context.Context, cc *chain2.Config, dir string, db
heimdallClient = heimdall.NewHeimdallClient(config.HeimdallURL, logger)
}
} else {
consensusConfig = &mainnet.MainnetConfig{}
consensusConfig = &config.Ethash
}
return ethconsensusconfig.CreateConsensusEngine(ctx, &nodecfg.Config{Dirs: datadir.New(dir)}, cc, consensusConfig, config.Miner.Notify, config.Miner.Noverify,
heimdallClient, config.WithoutHeimdall, blockReader, db.ReadOnly(), logger, nil, nil), heimdallClient
Expand Down
9 changes: 4 additions & 5 deletions cmd/rpcdaemon/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import (
"github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/paths"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
Expand All @@ -81,7 +81,6 @@ import (
"github.com/erigontech/erigon/turbo/rpchelper"
"github.com/erigontech/erigon/turbo/services"
"github.com/erigontech/erigon/turbo/snapshotsync/freezeblocks"

// Force-load native and js packages, to trigger registration
_ "github.com/erigontech/erigon/eth/tracers/js"
_ "github.com/erigontech/erigon/eth/tracers/native"
Expand Down Expand Up @@ -511,11 +510,11 @@ func RemoteServices(ctx context.Context, cfg *httpcfg.HttpCfg, logger log.Logger
// Skip the compatibility check, until we have a schema in erigon-lib
engine = bor.NewRo(cc, borKv, blockReader, logger)
default:
engine = mainnet.NewMainnetConsensus()
engine = ethash.NewFaker()
}

default:
engine = mainnet.NewMainnetConsensus()
engine = ethash.NewFaker()
}
} else {
remoteCE = &remoteConsensusEngine{}
Expand Down Expand Up @@ -930,7 +929,7 @@ func (e *remoteConsensusEngine) init(db kv.RoDB, blockReader services.FullBlockR

e.engine = bor.NewRo(cc, borKv, blockReader, logger)
} else {
e.engine = mainnet.NewMainnetConsensus()
e.engine = ethash.NewFaker()
}

return true
Expand Down
8 changes: 4 additions & 4 deletions cmd/rpcdaemon/rpcdaemontest/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"github.com/erigontech/erigon/accounts/abi/bind/backends"
"github.com/erigontech/erigon/common/u256"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/merge"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/types"
"github.com/erigontech/erigon/core/vm"
Expand Down Expand Up @@ -304,17 +304,17 @@ func CreateTestGrpcConn(t *testing.T, m *mock.MockSentry) (context.Context, *grp
ctx, cancel := context.WithCancel(context.Background())

apis := m.Engine.APIs(nil)
_, isEngineAPI := m.Engine.(*merge.Merge)
if len(apis) < 1 && isEngineAPI {
if len(apis) < 1 {
t.Fatal("couldn't instantiate Engine api")
}

ethashApi := apis[1].Service.(*ethash.API)
server := grpc.NewServer()

remote.RegisterETHBACKENDServer(server, privateapi.NewEthBackendServer(ctx, nil, m.DB, m.Notifications.Events,
m.BlockReader, log.New(), builder.NewLatestBlockBuiltStore()))
txpool.RegisterTxpoolServer(server, m.TxPoolGrpcServer)
txpool.RegisterMiningServer(server, privateapi.NewMiningServer(ctx, &IsMiningMock{}, m.Log))
txpool.RegisterMiningServer(server, privateapi.NewMiningServer(ctx, &IsMiningMock{}, ethashApi, m.Log))
listener := bufconn.Listen(1024 * 1024)

dialer := func() func(context.Context, string) (net.Conn, error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

"github.com/erigontech/erigon/common/debug"
"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/state"
Expand Down Expand Up @@ -603,7 +603,7 @@ func OpcodeTracer(genesis *types.Genesis, blockNum uint64, chaindata string, num
getHeader := func(hash libcommon.Hash, number uint64) *types.Header {
return rawdb.ReadHeader(historyTx, hash, number)
}
receipts, err1 := runBlock(mainnet.NewFullFaker(), intraBlockState, noOpWriter, noOpWriter, chainConfig, getHeader, block, vmConfig, false, logger)
receipts, err1 := runBlock(ethash.NewFullFaker(), intraBlockState, noOpWriter, noOpWriter, chainConfig, getHeader, block, vmConfig, false, logger)
if err1 != nil {
return err1
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/txpool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func doTxpool(ctx context.Context, logger log.Logger) error {
fetch.ConnectCore()
fetch.ConnectSentries()

miningGrpcServer := privateapi.NewMiningServer(ctx, &rpcdaemontest.IsMiningMock{}, logger)
miningGrpcServer := privateapi.NewMiningServer(ctx, &rpcdaemontest.IsMiningMock{}, nil, logger)

grpcServer, err := txpool.StartGrpc(txpoolGrpcServer, miningGrpcServer, txpoolApiAddr, nil, logger)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/erigontech/erigon/cmd/utils/flags"
common2 "github.com/erigontech/erigon/common"
"github.com/erigontech/erigon/common/paths"
"github.com/erigontech/erigon/consensus/ethash/ethashcfg"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/crypto"
"github.com/erigontech/erigon/eth/ethconfig"
Expand Down Expand Up @@ -120,6 +121,25 @@ var (
Name: "trusted-setup-file",
Usage: "Absolute path to trusted_setup.json file",
}
// Ethash settings
EthashCachesInMemoryFlag = cli.IntFlag{
Name: "ethash.cachesinmem",
Usage: "Number of recent ethash caches to keep in memory (16MB each)",
Value: ethconfig.Defaults.Ethash.CachesInMem,
}
EthashCachesLockMmapFlag = cli.BoolFlag{
Name: "ethash.cacheslockmmap",
Usage: "Lock memory maps of recent ethash caches",
}
EthashDatasetDirFlag = flags.DirectoryFlag{
Name: "ethash.dagdir",
Usage: "Directory to store the ethash mining DAGs",
Value: flags.DirectoryString(ethconfig.Defaults.Ethash.DatasetDir),
}
EthashDatasetsLockMmapFlag = cli.BoolFlag{
Name: "ethash.dagslockmmap",
Usage: "Lock memory maps for recent ethash mining DAGs",
}
ExternalConsensusFlag = cli.BoolFlag{
Name: "externalcl",
Usage: "Enables the external consensus layer",
Expand Down Expand Up @@ -1515,6 +1535,26 @@ func setTxPool(ctx *cli.Context, fullCfg *ethconfig.Config) {
cfg.CommitEvery = common2.RandomizeDuration(ctx.Duration(TxPoolCommitEveryFlag.Name))
}

func setEthash(ctx *cli.Context, datadir string, cfg *ethconfig.Config) {
if ctx.IsSet(EthashDatasetDirFlag.Name) {
cfg.Ethash.DatasetDir = ctx.String(EthashDatasetDirFlag.Name)
} else {
cfg.Ethash.DatasetDir = filepath.Join(datadir, "ethash-dags")
}
if ctx.IsSet(EthashCachesInMemoryFlag.Name) {
cfg.Ethash.CachesInMem = ctx.Int(EthashCachesInMemoryFlag.Name)
}
if ctx.IsSet(EthashCachesLockMmapFlag.Name) {
cfg.Ethash.CachesLockMmap = ctx.Bool(EthashCachesLockMmapFlag.Name)
}
if ctx.IsSet(FakePoWFlag.Name) {
cfg.Ethash.PowMode = ethashcfg.ModeFake
}
if ctx.IsSet(EthashDatasetsLockMmapFlag.Name) {
cfg.Ethash.DatasetsLockMmap = ctx.Bool(EthashDatasetsLockMmapFlag.Name)
}
}

func SetupMinerCobra(cmd *cobra.Command, cfg *params.MiningConfig) {
flags := cmd.Flags()
var err error
Expand Down Expand Up @@ -1794,6 +1834,7 @@ func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.C
cfg.TxPool = ethconfig.DefaultTxPool2Config(cfg)
cfg.TxPool.DBDir = nodeConfig.Dirs.TxPool

setEthash(ctx, nodeConfig.Dirs.DataDir, cfg)
setClique(ctx, &cfg.Clique, nodeConfig.Dirs.DataDir)
setMiner(ctx, &cfg.Miner)
setWhitelist(ctx, cfg)
Expand Down
4 changes: 2 additions & 2 deletions consensus/aura/aura.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

"github.com/erigontech/erigon/consensus"
"github.com/erigontech/erigon/consensus/clique"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/ethash"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/tracing"
"github.com/erigontech/erigon/core/types"
Expand Down Expand Up @@ -371,7 +371,7 @@ func (c *AuRa) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Hea
log.Error("consensus.ErrUnknownAncestor", "parentNum", number-1, "hash", header.ParentHash.String())
return consensus.ErrUnknownAncestor
}
return mainnet.VerifyHeaderBasics(chain, header, parent, true /*checkTimestamp*/, c.HasGasLimitContract() /*skipGasLimit*/)
return ethash.VerifyHeaderBasics(chain, header, parent, true /*checkTimestamp*/, c.HasGasLimitContract() /*skipGasLimit*/)
}

// nolint
Expand Down
10 changes: 5 additions & 5 deletions consensus/clique/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func TestClique(t *testing.T) {
}
}
if failed {
_ = engine.Close()
engine.Close()
return
}
chainX := &core.ChainPack{Blocks: batches[len(batches)-1]}
Expand All @@ -507,7 +507,7 @@ func TestClique(t *testing.T) {
t.Errorf("test %d: unexpected failure: %v", i, err)
}
if tt.failure != nil {
_ = engine.Close()
engine.Close()
return
}
// No failure was produced or requested, generate the final voting snapshot
Expand All @@ -529,7 +529,7 @@ func TestClique(t *testing.T) {
}); err != nil {
t.Errorf("test %d: failed to retrieve voting snapshot %d(%s): %v",
i, head.NumberU64(), head.Hash().Hex(), err)
_ = engine.Close()
engine.Close()
return
}

Expand All @@ -548,15 +548,15 @@ func TestClique(t *testing.T) {
result := snap.GetSigners()
if len(result) != len(signers) {
t.Errorf("test %d: signers mismatch: have %x, want %x", i, result, signers)
_ = engine.Close()
engine.Close()
return
}
for j := 0; j < len(result); j++ {
if !bytes.Equal(result[j][:], signers[j][:]) {
t.Errorf("test %d, signer %d: signer mismatch: have %x, want %x", i, j, result[j], signers[j])
}
}
_ = engine.Close()
engine.Close()
})
}
}
Loading

0 comments on commit c746942

Please sign in to comment.