2024-03-06 10:50:22 -06:00
|
|
|
// Copyright 2024 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2024-11-08 08:21:00 -06:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/params"
|
2024-03-06 10:50:22 -06:00
|
|
|
"github.com/ethereum/go-ethereum/beacon/types"
|
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2024-03-20 13:22:44 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2024-03-06 10:50:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2024-03-20 13:22:44 -05:00
|
|
|
urls []string
|
|
|
|
customHeader map[string]string
|
2024-11-08 08:21:00 -06:00
|
|
|
config *params.ClientConfig
|
2024-03-20 13:22:44 -05:00
|
|
|
scheduler *request.Scheduler
|
|
|
|
blockSync *beaconBlockSync
|
|
|
|
engineRPC *rpc.Client
|
|
|
|
|
|
|
|
chainHeadSub event.Subscription
|
|
|
|
engineClient *engineClient
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
2024-11-08 08:21:00 -06:00
|
|
|
func NewClient(config params.ClientConfig) *Client {
|
2024-03-06 10:50:22 -06:00
|
|
|
// create data structures
|
|
|
|
var (
|
|
|
|
db = memorydb.New()
|
2024-11-08 08:21:00 -06:00
|
|
|
committeeChain = light.NewCommitteeChain(db, &config.ChainConfig, config.Threshold, !config.NoFilter)
|
|
|
|
headTracker = light.NewHeadTracker(committeeChain, config.Threshold)
|
2024-03-06 10:50:22 -06:00
|
|
|
)
|
|
|
|
headSync := sync.NewHeadSync(headTracker, committeeChain)
|
|
|
|
|
|
|
|
// set up scheduler and sync modules
|
|
|
|
scheduler := request.NewScheduler()
|
2024-11-08 08:21:00 -06:00
|
|
|
checkpointInit := sync.NewCheckpointInit(committeeChain, config.Checkpoint)
|
2024-03-06 10:50:22 -06:00
|
|
|
forwardSync := sync.NewForwardUpdateSync(committeeChain)
|
2024-03-20 13:22:44 -05:00
|
|
|
beaconBlockSync := newBeaconBlockSync(headTracker)
|
2024-03-06 10:50:22 -06:00
|
|
|
scheduler.RegisterTarget(headTracker)
|
|
|
|
scheduler.RegisterTarget(committeeChain)
|
|
|
|
scheduler.RegisterModule(checkpointInit, "checkpointInit")
|
|
|
|
scheduler.RegisterModule(forwardSync, "forwardSync")
|
|
|
|
scheduler.RegisterModule(headSync, "headSync")
|
|
|
|
scheduler.RegisterModule(beaconBlockSync, "beaconBlockSync")
|
|
|
|
|
|
|
|
return &Client{
|
2024-03-20 13:22:44 -05:00
|
|
|
scheduler: scheduler,
|
2024-11-08 08:21:00 -06:00
|
|
|
urls: config.Apis,
|
|
|
|
customHeader: config.CustomHeader,
|
|
|
|
config: &config,
|
2024-03-20 13:22:44 -05:00
|
|
|
blockSync: beaconBlockSync,
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
func (c *Client) SetEngineRPC(engine *rpc.Client) {
|
|
|
|
c.engineRPC = engine
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
func (c *Client) Start() error {
|
|
|
|
headCh := make(chan types.ChainHeadEvent, 16)
|
|
|
|
c.chainHeadSub = c.blockSync.SubscribeChainHead(headCh)
|
2024-11-08 08:21:00 -06:00
|
|
|
c.engineClient = startEngineClient(c.config, c.engineRPC, headCh)
|
2024-03-20 13:22:44 -05:00
|
|
|
|
2024-03-06 10:50:22 -06:00
|
|
|
c.scheduler.Start()
|
|
|
|
for _, url := range c.urls {
|
|
|
|
beaconApi := api.NewBeaconLightApi(url, c.customHeader)
|
|
|
|
c.scheduler.RegisterServer(request.NewServer(api.NewApiServer(beaconApi), &mclock.System{}))
|
|
|
|
}
|
2024-03-20 13:22:44 -05:00
|
|
|
return nil
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|
|
|
|
|
2024-03-20 13:22:44 -05:00
|
|
|
func (c *Client) Stop() error {
|
|
|
|
c.engineClient.stop()
|
|
|
|
c.chainHeadSub.Unsubscribe()
|
2024-03-06 10:50:22 -06:00
|
|
|
c.scheduler.Stop()
|
2024-03-20 13:22:44 -05:00
|
|
|
return nil
|
2024-03-06 10:50:22 -06:00
|
|
|
}
|