Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethclient/lightclient: new light client (WIP) #29033

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 15 additions & 38 deletions beacon/blsync/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,40 @@
package blsync

import (
"strings"

"github.com/ethereum/go-ethereum/beacon/config"
"github.com/ethereum/go-ethereum/beacon/light"
"github.com/ethereum/go-ethereum/beacon/light/api"
"github.com/ethereum/go-ethereum/beacon/light/request"
"github.com/ethereum/go-ethereum/beacon/light/sync"
"github.com/ethereum/go-ethereum/beacon/types"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/rpc"
"github.com/urfave/cli/v2"
)

type Client struct {
urls []string
customHeader map[string]string
chainConfig *lightClientConfig
scheduler *request.Scheduler
blockSync *beaconBlockSync
engineRPC *rpc.Client
config config.LightClientConfig
scheduler *request.Scheduler
blockSync *beaconBlockSync
engineRPC *rpc.Client

chainHeadSub event.Subscription
engineClient *engineClient
}

func NewClient(ctx *cli.Context) *Client {
if !ctx.IsSet(utils.BeaconApiFlag.Name) {
utils.Fatalf("Beacon node light client API URL not specified")
}
var (
chainConfig = makeChainConfig(ctx)
customHeader = make(map[string]string)
)
for _, s := range ctx.StringSlice(utils.BeaconApiHeaderFlag.Name) {
kv := strings.Split(s, ":")
if len(kv) != 2 {
utils.Fatalf("Invalid custom API header entry: %s", s)
}
customHeader[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}

func NewClient(config config.LightClientConfig) *Client {
// create data structures
var (
db = memorydb.New()
threshold = ctx.Int(utils.BeaconThresholdFlag.Name)
committeeChain = light.NewCommitteeChain(db, chainConfig.ChainConfig, threshold, !ctx.Bool(utils.BeaconNoFilterFlag.Name))
headTracker = light.NewHeadTracker(committeeChain, threshold)
committeeChain = light.NewCommitteeChain(db, config.ChainConfig, config.SignerThreshold, config.EnforceTime)
headTracker = light.NewHeadTracker(committeeChain, config.SignerThreshold)
)
headSync := sync.NewHeadSync(headTracker, committeeChain)

// set up scheduler and sync modules
scheduler := request.NewScheduler()
checkpointInit := sync.NewCheckpointInit(committeeChain, chainConfig.Checkpoint)
checkpointInit := sync.NewCheckpointInit(committeeChain, config.Checkpoint)
forwardSync := sync.NewForwardUpdateSync(committeeChain)
beaconBlockSync := newBeaconBlockSync(headTracker)
scheduler.RegisterTarget(headTracker)
Expand All @@ -82,11 +61,9 @@ func NewClient(ctx *cli.Context) *Client {
scheduler.RegisterModule(beaconBlockSync, "beaconBlockSync")

return &Client{
scheduler: scheduler,
urls: ctx.StringSlice(utils.BeaconApiFlag.Name),
customHeader: customHeader,
chainConfig: &chainConfig,
blockSync: beaconBlockSync,
scheduler: scheduler,
config: config,
blockSync: beaconBlockSync,
}
}

Expand All @@ -97,11 +74,11 @@ func (c *Client) SetEngineRPC(engine *rpc.Client) {
func (c *Client) Start() error {
headCh := make(chan types.ChainHeadEvent, 16)
c.chainHeadSub = c.blockSync.SubscribeChainHead(headCh)
c.engineClient = startEngineClient(c.chainConfig, c.engineRPC, headCh)
c.engineClient = startEngineClient(c.config.ChainConfig, c.engineRPC, headCh)

c.scheduler.Start()
for _, url := range c.urls {
beaconApi := api.NewBeaconLightApi(url, c.customHeader)
for _, url := range c.config.ApiUrls {
beaconApi := api.NewBeaconLightApi(url, c.config.CustomHeader)
c.scheduler.RegisterServer(request.NewServer(api.NewApiServer(beaconApi), &mclock.System{}))
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions beacon/blsync/engineclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ import (
)

type engineClient struct {
config *lightClientConfig
config *types.ChainConfig
rpc *rpc.Client
rootCtx context.Context
cancelRoot context.CancelFunc
wg sync.WaitGroup
}

func startEngineClient(config *lightClientConfig, rpc *rpc.Client, headCh <-chan types.ChainHeadEvent) *engineClient {
func startEngineClient(config *types.ChainConfig, rpc *rpc.Client, headCh <-chan types.ChainHeadEvent) *engineClient {
ctx, cancel := context.WithCancel(context.Background())
ec := &engineClient{
config: config,
Expand Down
49 changes: 41 additions & 8 deletions beacon/blsync/config.go → beacon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package blsync
package config

import (
"strings"

"github.com/ethereum/go-ethereum/beacon/types"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/urfave/cli/v2"
)

// lightClientConfig contains beacon light client configuration
type lightClientConfig struct {
// LightChainConfig contains beacon light chain configuration
type LightChainConfig struct {
*types.ChainConfig
Checkpoint common.Hash
}

// LightClientConfig contains beacon light client configuration
type LightClientConfig struct {
LightChainConfig
ApiUrls []string
CustomHeader map[string]string
SignerThreshold int
EnforceTime bool
}

var (
MainnetConfig = lightClientConfig{
MainnetConfig = LightChainConfig{
ChainConfig: (&types.ChainConfig{
GenesisValidatorsRoot: common.HexToHash("0x4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"),
GenesisTime: 1606824023,
Expand All @@ -44,7 +55,7 @@ var (
Checkpoint: common.HexToHash("0x388be41594ec7d6a6894f18c73f3469f07e2c19a803de4755d335817ed8e2e5a"),
}

SepoliaConfig = lightClientConfig{
SepoliaConfig = LightChainConfig{
ChainConfig: (&types.ChainConfig{
GenesisValidatorsRoot: common.HexToHash("0xd8ea171f3c94aea21ebc42a1ed61052acf3f9209c00e4efbaaddac09ed9b8078"),
GenesisTime: 1655733600,
Expand All @@ -57,7 +68,7 @@ var (
Checkpoint: common.HexToHash("0x1005a6d9175e96bfbce4d35b80f468e9bff0b674e1e861d16e09e10005a58e81"),
}

GoerliConfig = lightClientConfig{
GoerliConfig = LightChainConfig{
ChainConfig: (&types.ChainConfig{
GenesisValidatorsRoot: common.HexToHash("0x043db0d9a83813551ee2f33450d23797757d430911a9320530ad8a0eabc43efb"),
GenesisTime: 1614588812,
Expand All @@ -71,8 +82,8 @@ var (
}
)

func makeChainConfig(ctx *cli.Context) lightClientConfig {
var config lightClientConfig
func MakeLightChainConfig(ctx *cli.Context) LightChainConfig {
var config LightChainConfig
customConfig := ctx.IsSet(utils.BeaconConfigFlag.Name)
utils.CheckExclusive(ctx, utils.MainnetFlag, utils.GoerliFlag, utils.SepoliaFlag, utils.BeaconConfigFlag)
switch {
Expand Down Expand Up @@ -127,3 +138,25 @@ func makeChainConfig(ctx *cli.Context) lightClientConfig {
}
return config
}

func MakeLightClientConfig(ctx *cli.Context) LightClientConfig {
if !ctx.IsSet(utils.BeaconApiFlag.Name) {
utils.Fatalf("Beacon node light client API URL not specified")
}
customHeader := make(map[string]string)
for _, s := range ctx.StringSlice(utils.BeaconApiHeaderFlag.Name) {
kv := strings.Split(s, ":")
if len(kv) != 2 {
utils.Fatalf("Invalid custom API header entry: %s", s)
}
customHeader[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
}

return LightClientConfig{
LightChainConfig: MakeLightChainConfig(ctx),
ApiUrls: ctx.StringSlice(utils.BeaconApiFlag.Name),
CustomHeader: customHeader,
SignerThreshold: ctx.Int(utils.BeaconThresholdFlag.Name),
EnforceTime: !ctx.Bool(utils.BeaconNoFilterFlag.Name),
}
}
33 changes: 33 additions & 0 deletions beacon/types/exec_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ func (eh *ExecutionHeader) PayloadRoot() merkle.Value {
return merkle.Value(eh.obj.HashTreeRoot(tree.GetHashFn()))
}

func (eh *ExecutionHeader) BlockNumber() uint64 {
switch obj := eh.obj.(type) {
case *capella.ExecutionPayloadHeader:
return uint64(obj.BlockNumber)
case *deneb.ExecutionPayloadHeader:
return uint64(obj.BlockNumber)
default:
panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
}
}

func (eh *ExecutionHeader) BlockHash() common.Hash {
switch obj := eh.obj.(type) {
case *capella.ExecutionPayloadHeader:
Expand All @@ -78,3 +89,25 @@ func (eh *ExecutionHeader) BlockHash() common.Hash {
panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
}
}

func (eh *ExecutionHeader) ParentHash() common.Hash {
switch obj := eh.obj.(type) {
case *capella.ExecutionPayloadHeader:
return common.Hash(obj.ParentHash)
case *deneb.ExecutionPayloadHeader:
return common.Hash(obj.ParentHash)
default:
panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
}
}

func (eh *ExecutionHeader) StateRoot() common.Hash {
switch obj := eh.obj.(type) {
case *capella.ExecutionPayloadHeader:
return common.Hash(obj.StateRoot)
case *deneb.ExecutionPayloadHeader:
return common.Hash(obj.StateRoot)
default:
panic(fmt.Errorf("unsupported ExecutionPayloadHeader type %T", obj))
}
}
3 changes: 2 additions & 1 deletion cmd/blsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"

"github.com/ethereum/go-ethereum/beacon/blsync"
"github.com/ethereum/go-ethereum/beacon/config"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/internal/flags"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -87,7 +88,7 @@ func sync(ctx *cli.Context) error {
log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(output, verbosity, usecolor)))

// set up blsync
client := blsync.NewClient(ctx)
client := blsync.NewClient(config.MakeLightClientConfig(ctx))
client.SetEngineRPC(makeRPCClient(ctx))
client.Start()

Expand Down