Skip to content

Commit

Permalink
added separable lightclient (erigontech#6800)
Browse files Browse the repository at this point in the history
How to run:
Erigon flags needed: --private.api.addr <ADDR> --externalcl
Lightclient flags needed: --private.api.addr <ADDR>
  • Loading branch information
Giulio2002 authored and finiteops committed Apr 10, 2023
1 parent 1c22a4c commit af87a2a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 38 deletions.
48 changes: 37 additions & 11 deletions cmd/lightclient/lightclient/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func convertLightrpcExecutionPayloadToEthbacked(e *cltypes.Eth1Block) *types.Exe
}

func (l *LightClient) processBeaconBlock(beaconBlock *cltypes.BeaconBlock) error {
if l.execution == nil {
if l.execution == nil && l.executionClient == nil {
return nil
}
// If we recently imported the beacon block, skip.
Expand All @@ -71,19 +71,45 @@ func (l *LightClient) processBeaconBlock(beaconBlock *cltypes.BeaconBlock) error

payload := convertLightrpcExecutionPayloadToEthbacked(beaconBlock.Body.ExecutionPayload)

_, err = l.execution.EngineNewPayload(l.ctx, payload)
if err != nil {
return err
if l.execution != nil {
_, err = l.execution.EngineNewPayload(l.ctx, payload)
if err != nil {
return err
}
}

if l.executionClient != nil {
_, err = l.executionClient.EngineNewPayload(l.ctx, payload)
if err != nil {
return err
}
}

// Wait a bit
time.Sleep(500 * time.Millisecond)
_, err = l.execution.EngineForkChoiceUpdated(l.ctx, &remote.EngineForkChoiceUpdatedRequest{
ForkchoiceState: &remote.EngineForkChoiceState{
HeadBlockHash: payloadHash,
SafeBlockHash: payloadHash,
FinalizedBlockHash: gointerfaces.ConvertHashToH256(l.finalizedEth1Hash),
},
})
if l.execution != nil {
_, err = l.execution.EngineForkChoiceUpdated(l.ctx, &remote.EngineForkChoiceUpdatedRequest{
ForkchoiceState: &remote.EngineForkChoiceState{
HeadBlockHash: payloadHash,
SafeBlockHash: payloadHash,
FinalizedBlockHash: gointerfaces.ConvertHashToH256(l.finalizedEth1Hash),
},
})
if err != nil {
return err
}
}
if l.executionClient != nil {
_, err = l.executionClient.EngineForkChoiceUpdated(l.ctx, &remote.EngineForkChoiceUpdatedRequest{
ForkchoiceState: &remote.EngineForkChoiceState{
HeadBlockHash: payloadHash,
SafeBlockHash: payloadHash,
FinalizedBlockHash: gointerfaces.ConvertHashToH256(l.finalizedEth1Hash),
},
})
if err != nil {
return err
}
}
return err
}
9 changes: 6 additions & 3 deletions cmd/lightclient/lightclient/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ type LightClient struct {
recentHashesCache *lru.Cache
db kv.RwDB
rpc *rpc.BeaconRpcP2P
execution remote.ETHBACKENDServer
store *LightClientStore
// Either execution server or client
execution remote.ETHBACKENDServer
executionClient remote.ETHBACKENDClient
store *LightClientStore
}

func NewLightClient(ctx context.Context, db kv.RwDB, genesisConfig *clparams.GenesisConfig, beaconConfig *clparams.BeaconChainConfig,
execution remote.ETHBACKENDServer, sentinel sentinel.SentinelClient,
execution remote.ETHBACKENDServer, executionClient remote.ETHBACKENDClient, sentinel sentinel.SentinelClient,
highestSeen uint64, verbose bool) (*LightClient, error) {
recentHashesCache, err := lru.New(maxRecentHashes)
return &LightClient{
Expand All @@ -74,6 +76,7 @@ func NewLightClient(ctx context.Context, db kv.RwDB, genesisConfig *clparams.Gen
verbose: verbose,
highestSeen: highestSeen,
db: db,
executionClient: executionClient,
}, err
}

Expand Down
22 changes: 13 additions & 9 deletions cmd/lightclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import (
"fmt"
"os"

"github.com/ledgerwatch/erigon-lib/gointerfaces/remote"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/mdbx"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/log/v3"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"

"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/fork"
Expand All @@ -37,7 +39,7 @@ import (
)

func main() {
app := lightclientapp.MakeApp(runLightClientNode, flags.CLDefaultFlags)
app := lightclientapp.MakeApp(runLightClientNode, flags.LCDefaultFlags)
if err := app.Run(os.Args); err != nil {
_, printErr := fmt.Fprintln(os.Stderr, err)
if printErr != nil {
Expand Down Expand Up @@ -96,18 +98,20 @@ func runLightClientNode(cliCtx *cli.Context) error {
}
log.Info("Sentinel started", "addr", cfg.ServerAddr)

tx, err := db.BeginRo(ctx)
if err != nil {
return err
}

tx.Rollback()

if err != nil {
log.Error("[Checkpoint Sync] Failed", "reason", err)
return err
}
lc, err := lightclient.NewLightClient(ctx, db, cfg.GenesisCfg, cfg.BeaconCfg, nil, sentinel, 0, true)
var execution remote.ETHBACKENDClient
if cfg.ErigonPrivateApi != "" {
cc, err := grpc.Dial(cfg.ErigonPrivateApi, grpc.WithInsecure())
if err != nil {
log.Error("could not connect to erigon private api", "err", err)
}
defer cc.Close()
execution = remote.NewETHBACKENDClient(cc)
}
lc, err := lightclient.NewLightClient(ctx, db, cfg.GenesisCfg, cfg.BeaconCfg, nil, execution, sentinel, 0, true)
if err != nil {
log.Error("Could not make Lightclient", "err", err)
return err
Expand Down
30 changes: 16 additions & 14 deletions cmd/sentinel/cli/cliSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import (
)

type ConsensusClientCliCfg struct {
GenesisCfg *clparams.GenesisConfig `json:"genesisCfg"`
BeaconCfg *clparams.BeaconChainConfig `json:"beaconCfg"`
NetworkCfg *clparams.NetworkConfig `json:"networkCfg"`
BeaconDataCfg *rawdb.BeaconDataConfig `json:"beaconDataConfig"`
Port uint `json:"port"`
Addr string `json:"address"`
ServerAddr string `json:"serverAddr"`
ServerProtocol string `json:"serverProtocol"`
ServerTcpPort uint `json:"serverTcpPort"`
LogLvl uint `json:"logLevel"`
NoDiscovery bool `json:"noDiscovery"`
CheckpointUri string `json:"checkpointUri"`
Chaindata string `json:"chaindata"`
ELEnabled bool `json:"elEnabled"`
GenesisCfg *clparams.GenesisConfig `json:"genesisCfg"`
BeaconCfg *clparams.BeaconChainConfig `json:"beaconCfg"`
NetworkCfg *clparams.NetworkConfig `json:"networkCfg"`
BeaconDataCfg *rawdb.BeaconDataConfig `json:"beaconDataConfig"`
Port uint `json:"port"`
Addr string `json:"address"`
ServerAddr string `json:"serverAddr"`
ServerProtocol string `json:"serverProtocol"`
ServerTcpPort uint `json:"serverTcpPort"`
LogLvl uint `json:"logLevel"`
NoDiscovery bool `json:"noDiscovery"`
CheckpointUri string `json:"checkpointUri"`
Chaindata string `json:"chaindata"`
ELEnabled bool `json:"elEnabled"`
ErigonPrivateApi string `json:"erigonPrivateApi"`
}

func SetupConsensusClientCfg(ctx *cli.Context) (*ConsensusClientCliCfg, error) {
Expand All @@ -37,6 +38,7 @@ func SetupConsensusClientCfg(ctx *cli.Context) (*ConsensusClientCliCfg, error) {
if err != nil {
return nil, err
}
cfg.ErigonPrivateApi = ctx.String(flags.ErigonPrivateApiFlag.Name)
if ctx.String(flags.BeaconConfigFlag.Name) != "" {
cfg.BeaconCfg = new(clparams.BeaconChainConfig)
if *cfg.BeaconCfg, err = clparams.CustomConfig(ctx.String(flags.BeaconConfigFlag.Name)); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions cmd/sentinel/cli/flags/defaultFlags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ var CLDefaultFlags = []cli.Flag{
&GenesisSSZFlag,
&CheckpointSyncUrlFlag,
}

var LCDefaultFlags = []cli.Flag{
&SentinelDiscoveryPort,
&SentinelDiscoveryAddr,
&SentinelServerPort,
&SentinelServerAddr,
&Chain,
&Verbosity,
&SentinelTcpPort,
&NoDiscovery,
&ChaindataFlag,
&ELEnabledFlag,
&BeaconDBModeFlag,
&BootnodesFlag,
&BeaconConfigFlag,
&GenesisSSZFlag,
&CheckpointSyncUrlFlag,
&ErigonPrivateApiFlag,
}
5 changes: 5 additions & 0 deletions cmd/sentinel/cli/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ var (
Usage: "checkpoint sync endpoint",
Value: "",
}
ErigonPrivateApiFlag = cli.StringFlag{
Name: "private.api.addr",
Usage: "connect to existing erigon instance",
Value: "",
}
)
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func New(stack *node.Node, config *ethconfig.Config, logger log.Logger) (*Ethere
return nil, err
}

lc, err := lightclient.NewLightClient(ctx, memdb.New(), genesisCfg, beaconCfg, ethBackendRPC, client, currentBlockNumber, false)
lc, err := lightclient.NewLightClient(ctx, memdb.New(), genesisCfg, beaconCfg, ethBackendRPC, nil, client, currentBlockNumber, false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit af87a2a

Please sign in to comment.