From f8916e9226c8e90400443b97070a7a610ae29311 Mon Sep 17 00:00:00 2001 From: Giulio rebuffo Date: Thu, 10 Nov 2022 18:06:04 +0100 Subject: [PATCH] Made Lightclient default (#5813) lightclient cl is default, for external cl, run `--externalcl` --- README.md | 4 ++ cl/clparams/config.go | 8 +++ cmd/rpcdaemon/cli/httpcfg/http_cfg.go | 1 + cmd/rpcdaemon/commands/daemon.go | 2 +- cmd/rpcdaemon/commands/engine_api.go | 32 +++++++++--- cmd/utils/flags.go | 8 +-- eth/backend.go | 70 +++++++++++++-------------- turbo/cli/default_flags.go | 2 +- turbo/cli/flags.go | 1 + 9 files changed, 79 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 92cb4ceea..d25a619d3 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,10 @@ Don't start services as separated processes unless you have clear reason for it: your own implementation, security. How to start Erigon's services as separated processes, see in [docker-compose.yml](./docker-compose.yml). +### Embedded Consensus Layer + +By default the Engine API is disabled in favour of Erigon native Embedded Consensus Layer, if you want to either stake or sync an external Consensus Layer, run Erigon with flag `--externalcl`. + ### Optional stages There is an optional stage that can be enabled through flags: diff --git a/cl/clparams/config.go b/cl/clparams/config.go index 2510e211c..0ab5ee167 100644 --- a/cl/clparams/config.go +++ b/cl/clparams/config.go @@ -708,3 +708,11 @@ func GetCheckpointSyncEndpoint(net NetworkType) string { } return checkpoints[n.Int64()] } + +// Check if chain with a specific ID is supported or not +// 1 is Ethereum Mainnet +// 5 is Goerli Testnet +// 11155111 is Sepolia Testnet +func Supported(id uint64) bool { + return id == 1 || id == 5 || id == 11155111 +} diff --git a/cmd/rpcdaemon/cli/httpcfg/http_cfg.go b/cmd/rpcdaemon/cli/httpcfg/http_cfg.go index 7c7ce241b..ed5232ae7 100644 --- a/cmd/rpcdaemon/cli/httpcfg/http_cfg.go +++ b/cmd/rpcdaemon/cli/httpcfg/http_cfg.go @@ -50,6 +50,7 @@ type HttpCfg struct { HTTPTimeouts rpccfg.HTTPTimeouts AuthRpcTimeouts rpccfg.HTTPTimeouts EvmCallTimeout time.Duration + InternalCL bool LogDirVerbosity string LogDirPath string } diff --git a/cmd/rpcdaemon/commands/daemon.go b/cmd/rpcdaemon/commands/daemon.go index fe0f8045f..c0b9758a1 100644 --- a/cmd/rpcdaemon/commands/daemon.go +++ b/cmd/rpcdaemon/commands/daemon.go @@ -129,7 +129,7 @@ func AuthAPIList(db kv.RoDB, eth rpchelper.ApiBackend, txPool txpool.TxpoolClien base := NewBaseApi(filters, stateCache, blockReader, agg, cfg.WithDatadir, cfg.EvmCallTimeout) ethImpl := NewEthAPI(base, db, eth, txPool, mining, cfg.Gascap) - engineImpl := NewEngineAPI(base, db, eth) + engineImpl := NewEngineAPI(base, db, eth, cfg.InternalCL) list = append(list, rpc.API{ Namespace: "eth", diff --git a/cmd/rpcdaemon/commands/engine_api.go b/cmd/rpcdaemon/commands/engine_api.go index 541362023..2c49628a9 100644 --- a/cmd/rpcdaemon/commands/engine_api.go +++ b/cmd/rpcdaemon/commands/engine_api.go @@ -69,8 +69,9 @@ type EngineAPI interface { // EngineImpl is implementation of the EngineAPI interface type EngineImpl struct { *BaseAPI - db kv.RoDB - api rpchelper.ApiBackend + db kv.RoDB + api rpchelper.ApiBackend + internalCL bool } func convertPayloadStatus(x *remote.EnginePayloadStatus) map[string]interface{} { @@ -88,6 +89,10 @@ func convertPayloadStatus(x *remote.EnginePayloadStatus) map[string]interface{} } func (e *EngineImpl) ForkchoiceUpdatedV1(ctx context.Context, forkChoiceState *ForkChoiceState, payloadAttributes *PayloadAttributes) (map[string]interface{}, error) { + if e.internalCL { + log.Error("EXTERNAL CONSENSUS LAYER IS NOT ENABLED, PLEASE RESTART WITH FLAG --externalcl") + return nil, fmt.Errorf("engine api should not be used, restart with --externalcl") + } log.Debug("Received ForkchoiceUpdated", "head", forkChoiceState.HeadHash, "safe", forkChoiceState.HeadHash, "finalized", forkChoiceState.FinalizedBlockHash, "build", payloadAttributes != nil) @@ -143,6 +148,10 @@ func (e *EngineImpl) ForkchoiceUpdatedV1(ctx context.Context, forkChoiceState *F // NewPayloadV1 processes new payloads (blocks) from the beacon chain. // See https://github.com/ethereum/execution-apis/blob/main/src/engine/specification.md#engine_newpayloadv1 func (e *EngineImpl) NewPayloadV1(ctx context.Context, payload *ExecutionPayload) (map[string]interface{}, error) { + if e.internalCL { + log.Error("EXTERNAL CONSENSUS LAYER IS NOT ENABLED, PLEASE RESTART WITH FLAG --externalcl") + return nil, fmt.Errorf("engine api should not be used, restart with --externalcl") + } log.Debug("Received NewPayload", "height", uint64(payload.BlockNumber), "hash", payload.BlockHash) var baseFee *uint256.Int @@ -201,6 +210,11 @@ func (e *EngineImpl) NewPayloadV1(ctx context.Context, payload *ExecutionPayload } func (e *EngineImpl) GetPayloadV1(ctx context.Context, payloadID hexutil.Bytes) (*ExecutionPayload, error) { + if e.internalCL { + log.Error("EXTERNAL CONSENSUS LAYER IS NOT ENABLED, PLEASE RESTART WITH FLAG --externalcl") + return nil, fmt.Errorf("engine api should not be used, restart with --externalcl") + } + decodedPayloadId := binary.BigEndian.Uint64(payloadID) log.Info("Received GetPayload", "payloadId", decodedPayloadId) @@ -242,6 +256,11 @@ func (e *EngineImpl) GetPayloadV1(ctx context.Context, payloadID hexutil.Bytes) // Can also be used to ping the execution layer (heartbeats). // See https://github.com/ethereum/execution-apis/blob/v1.0.0-alpha.7/src/engine/specification.md#engine_exchangetransitionconfigurationv1 func (e *EngineImpl) ExchangeTransitionConfigurationV1(ctx context.Context, beaconConfig TransitionConfiguration) (TransitionConfiguration, error) { + if e.internalCL { + log.Error("EXTERNAL CONSENSUS LAYER IS NOT ENABLED, PLEASE RESTART WITH FLAG --externalcl") + return TransitionConfiguration{}, fmt.Errorf("engine api should not be used, restart with --externalcl") + } + tx, err := e.db.BeginRo(ctx) if err != nil { @@ -274,10 +293,11 @@ func (e *EngineImpl) ExchangeTransitionConfigurationV1(ctx context.Context, beac } // NewEngineAPI returns EngineImpl instance -func NewEngineAPI(base *BaseAPI, db kv.RoDB, api rpchelper.ApiBackend) *EngineImpl { +func NewEngineAPI(base *BaseAPI, db kv.RoDB, api rpchelper.ApiBackend, internalCL bool) *EngineImpl { return &EngineImpl{ - BaseAPI: base, - db: db, - api: api, + BaseAPI: base, + db: db, + api: api, + internalCL: internalCL, } } diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index d3a4adfea..7249d2d16 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -163,9 +163,9 @@ var ( Name: "snapshots", Usage: `Default: use snapshots "true" for BSC, Mainnet and Goerli. use snapshots "false" in all other cases`, } - LightClientFlag = cli.BoolFlag{ - Name: "experimental.lightclient", - Usage: "enables experimental CL lightclient.", + ExternalConsensusFlag = cli.BoolFlag{ + Name: "externalcl", + Usage: "enables external consensus", } // Transaction pool settings TxPoolDisableFlag = cli.BoolFlag{ @@ -1427,7 +1427,7 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) { // SetEthConfig applies eth-related command line flags to the config. func SetEthConfig(ctx *cli.Context, nodeConfig *nodecfg.Config, cfg *ethconfig.Config) { - cfg.CL = ctx.GlobalBool(LightClientFlag.Name) + cfg.CL = ctx.GlobalBool(ExternalConsensusFlag.Name) cfg.Sync.UseSnapshots = ctx.GlobalBoolT(SnapshotFlag.Name) cfg.Dirs = nodeConfig.Dirs cfg.Snapshot.KeepBlocks = ctx.GlobalBool(SnapKeepBlocksFlag.Name) diff --git a/eth/backend.go b/eth/backend.go index e578474e8..f0cfcaa88 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -453,44 +453,40 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere blockReader, chainConfig, assembleBlockPOS, backend.sentriesClient.Hd, config.Miner.EnabledPOS) miningRPC = privateapi.NewMiningServer(ctx, backend, ethashApi) - if config.CL { - // Chains supported are Sepolia, Mainnet and Goerli - if config.NetworkID == 1 || config.NetworkID == 5 || config.NetworkID == 11155111 { - genesisCfg, networkCfg, beaconCfg := clparams.GetConfigsByNetwork(clparams.NetworkType(config.NetworkID)) - if err != nil { - return nil, err - } - client, err := service.StartSentinelService(&sentinel.SentinelConfig{ - IpAddr: "127.0.0.1", - Port: 4000, - TCPPort: 4001, - GenesisConfig: genesisCfg, - NetworkConfig: networkCfg, - BeaconConfig: beaconCfg, - }, &service.ServerConfig{Network: "tcp", Addr: "localhost:7777"}) - if err != nil { - return nil, err - } - - lc, err := lightclient.NewLightClient(ctx, genesisCfg, beaconCfg, ethBackendRPC, client, currentBlockNumber, false) - if err != nil { - return nil, err - } - bs, err := clcore.RetrieveBeaconState(ctx, - clparams.GetCheckpointSyncEndpoint(clparams.NetworkType(config.NetworkID))) - - if err != nil { - return nil, err - } - - if err := lc.BootstrapCheckpoint(ctx, bs.FinalizedCheckpoint.Root); err != nil { - return nil, err - } - - go lc.Start() - } else { - log.Warn("Cannot run lightclient on a non-supported chain. only goerli, sepolia and mainnet are allowed") + // If we choose not to run a consensus layer, run our embedded. + if !config.CL && clparams.Supported(config.NetworkID) { + genesisCfg, networkCfg, beaconCfg := clparams.GetConfigsByNetwork(clparams.NetworkType(config.NetworkID)) + if err != nil { + return nil, err } + client, err := service.StartSentinelService(&sentinel.SentinelConfig{ + IpAddr: "127.0.0.1", + Port: 4000, + TCPPort: 4001, + GenesisConfig: genesisCfg, + NetworkConfig: networkCfg, + BeaconConfig: beaconCfg, + }, &service.ServerConfig{Network: "tcp", Addr: "localhost:7777"}) + if err != nil { + return nil, err + } + + lc, err := lightclient.NewLightClient(ctx, genesisCfg, beaconCfg, ethBackendRPC, client, currentBlockNumber, false) + if err != nil { + return nil, err + } + bs, err := clcore.RetrieveBeaconState(ctx, + clparams.GetCheckpointSyncEndpoint(clparams.NetworkType(config.NetworkID))) + + if err != nil { + return nil, err + } + + if err := lc.BootstrapCheckpoint(ctx, bs.FinalizedCheckpoint.Root); err != nil { + return nil, err + } + + go lc.Start() } if stack.Config().PrivateApiAddr != "" { diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go index bcada1247..4ba36a8e3 100644 --- a/turbo/cli/default_flags.go +++ b/turbo/cli/default_flags.go @@ -12,7 +12,7 @@ var DefaultFlags = []cli.Flag{ utils.DataDirFlag, utils.EthashDatasetDirFlag, utils.SnapshotFlag, - utils.LightClientFlag, + utils.ExternalConsensusFlag, utils.TxPoolDisableFlag, utils.TxPoolLocalsFlag, utils.TxPoolNoLocalsFlag, diff --git a/turbo/cli/flags.go b/turbo/cli/flags.go index dd8b01925..965f593dd 100644 --- a/turbo/cli/flags.go +++ b/turbo/cli/flags.go @@ -371,6 +371,7 @@ func setEmbeddedRpcDaemon(ctx *cli.Context, cfg *nodecfg.Config) { TxPoolApiAddr: ctx.GlobalString(utils.TxpoolApiAddrFlag.Name), StateCache: kvcache.DefaultCoherentConfig, + InternalCL: !ctx.GlobalBool(utils.ExternalConsensusFlag.Name), } if ctx.GlobalIsSet(utils.HttpCompressionFlag.Name) { c.HttpCompression = ctx.GlobalBool(utils.HttpCompressionFlag.Name)