Skip to content

Commit

Permalink
E3: Remove Proof-Of-Work Consensus code (#11556)
Browse files Browse the repository at this point in the history
Notable things:
Removed all testing on Total Difficulty or Difficulty as that is a PoW
concept
Integration tests about difficulty removed as they only test ETH PoW
Remove difficulty checks in a test
I had to tweak some hashes as I have removed difficulty computation so
some hashes were different is some tests (now hardcoded to 1)

- It is running on the tip of the chain too, YAY
  • Loading branch information
Giulio2002 authored Aug 10, 2024
1 parent b0a2654 commit af4dc9d
Show file tree
Hide file tree
Showing 53 changed files with 525 additions and 5,694 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/ethash"
"github.com/erigontech/erigon/consensus/mainnet"
"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 := ethash.NewFaker()
engine := mainnet.NewMainnetConsensus()
checkStateRoot := true
m := mock.MockWithGenesisEngine(t, &genesis, engine, false, checkStateRoot)

Expand Down
4 changes: 0 additions & 4 deletions cmd/evm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ 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 @@ -534,9 +533,6 @@ 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: 0 additions & 21 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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 @@ -117,23 +116,3 @@ 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: 3 additions & 14 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/ethash"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/consensus/merge"
"github.com/erigontech/erigon/core"
"github.com/erigontech/erigon/core/state"
Expand Down Expand Up @@ -262,18 +262,7 @@ func Main(ctx *cli.Context) error {
}
prestate.Env.Difficulty = nil
} else if env.Difficulty == nil {
// 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)
return NewError(ErrorVMConfig, errors.New("POW not supported, difficulty must be defined in env"))
}

// manufacture block from above inputs
Expand Down Expand Up @@ -320,7 +309,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(&ethash.FakeEthash{})
engine := merge.New(&mainnet.MainnetConsensus{})

t8logger := log.New("t8ntool")
chainReader := consensuschain.NewReader(chainConfig, tx, nil, t8logger)
Expand Down
3 changes: 2 additions & 1 deletion cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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 @@ -1517,7 +1518,7 @@ func initConsensusEngine(ctx context.Context, cc *chain2.Config, dir string, db
heimdallClient = heimdall.NewHeimdallClient(config.HeimdallURL, logger)
}
} else {
consensusConfig = &config.Ethash
consensusConfig = &mainnet.MainnetConfig{}
}
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: 5 additions & 4 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/ethash"
"github.com/erigontech/erigon/consensus/mainnet"
"github.com/erigontech/erigon/core/rawdb"
"github.com/erigontech/erigon/core/state"
"github.com/erigontech/erigon/core/types"
Expand All @@ -82,6 +82,7 @@ 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 @@ -517,11 +518,11 @@ func RemoteServices(ctx context.Context, cfg *httpcfg.HttpCfg, logger log.Logger
bor.NewGenesisContractsClient(cc, borConfig.ValidatorContract, borConfig.StateReceiverContract, logger), logger)

default:
engine = ethash.NewFaker()
engine = mainnet.NewMainnetConsensus()
}

default:
engine = ethash.NewFaker()
engine = mainnet.NewMainnetConsensus()
}
} else {
remoteCE = &remoteConsensusEngine{}
Expand Down Expand Up @@ -940,7 +941,7 @@ func (e *remoteConsensusEngine) init(db kv.RoDB, blockReader services.FullBlockR
bor.NewChainSpanner(bor.GenesisContractValidatorSetABI(), cc, true, logger),
bor.NewGenesisContractsClient(cc, borConfig.ValidatorContract, borConfig.StateReceiverContract, logger), logger)
} else {
e.engine = ethash.NewFaker()
e.engine = mainnet.NewMainnetConsensus()
}

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/ethash"
"github.com/erigontech/erigon/consensus/merge"
"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)
if len(apis) < 1 {
_, isEngineAPI := m.Engine.(*merge.Merge)
if len(apis) < 1 && isEngineAPI {
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{}, ethashApi, m.Log))
txpool.RegisterMiningServer(server, privateapi.NewMiningServer(ctx, &IsMiningMock{}, 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/ethash"
"github.com/erigontech/erigon/consensus/mainnet"
"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(ethash.NewFullFaker(), intraBlockState, noOpWriter, noOpWriter, chainConfig, getHeader, block, vmConfig, false, logger)
receipts, err1 := runBlock(mainnet.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{}, nil, logger)
miningGrpcServer := privateapi.NewMiningServer(ctx, &rpcdaemontest.IsMiningMock{}, logger)

grpcServer, err := txpool.StartGrpc(txpoolGrpcServer, miningGrpcServer, txpoolApiAddr, nil, logger)
if err != nil {
Expand Down
41 changes: 0 additions & 41 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ 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 @@ -121,25 +120,6 @@ 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 @@ -1535,26 +1515,6 @@ 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 @@ -1834,7 +1794,6 @@ 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/ethash"
"github.com/erigontech/erigon/consensus/mainnet"
"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 ethash.VerifyHeaderBasics(chain, header, parent, true /*checkTimestamp*/, c.HasGasLimitContract() /*skipGasLimit*/)
return mainnet.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 af4dc9d

Please sign in to comment.