Skip to content

Commit 622e11a

Browse files
zsfelfoldizfy0701
authored andcommitted
beacon/blsync: remove cli dependencies (ethereum#30720)
This PR moves chain config related code (config file processing, fork logic, network defaults) from `beacon/types` and `beacon/blsync` into `beacon/params` while the command line flag logic of the chain config is moved into `cmd/utils`, thereby removing the cli dependencies from package `beacon` and its sub-packages.
1 parent 887d634 commit 622e11a

File tree

11 files changed

+254
-260
lines changed

11 files changed

+254
-260
lines changed

beacon/blsync/client.go

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@
1717
package blsync
1818

1919
import (
20-
"strings"
21-
2220
"github.com/ethereum/go-ethereum/beacon/light"
2321
"github.com/ethereum/go-ethereum/beacon/light/api"
2422
"github.com/ethereum/go-ethereum/beacon/light/request"
2523
"github.com/ethereum/go-ethereum/beacon/light/sync"
24+
"github.com/ethereum/go-ethereum/beacon/params"
2625
"github.com/ethereum/go-ethereum/beacon/types"
27-
"github.com/ethereum/go-ethereum/cmd/utils"
2826
"github.com/ethereum/go-ethereum/common/mclock"
2927
"github.com/ethereum/go-ethereum/ethdb/memorydb"
3028
"github.com/ethereum/go-ethereum/event"
3129
"github.com/ethereum/go-ethereum/rpc"
32-
"github.com/urfave/cli/v2"
3330
)
3431

3532
type Client struct {
3633
urls []string
3734
customHeader map[string]string
38-
chainConfig *lightClientConfig
35+
config *params.ClientConfig
3936
scheduler *request.Scheduler
4037
blockSync *beaconBlockSync
4138
engineRPC *rpc.Client
@@ -44,34 +41,18 @@ type Client struct {
4441
engineClient *engineClient
4542
}
4643

47-
func NewClient(ctx *cli.Context) *Client {
48-
if !ctx.IsSet(utils.BeaconApiFlag.Name) {
49-
utils.Fatalf("Beacon node light client API URL not specified")
50-
}
51-
var (
52-
chainConfig = makeChainConfig(ctx)
53-
customHeader = make(map[string]string)
54-
)
55-
for _, s := range ctx.StringSlice(utils.BeaconApiHeaderFlag.Name) {
56-
kv := strings.Split(s, ":")
57-
if len(kv) != 2 {
58-
utils.Fatalf("Invalid custom API header entry: %s", s)
59-
}
60-
customHeader[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
61-
}
62-
44+
func NewClient(config params.ClientConfig) *Client {
6345
// create data structures
6446
var (
6547
db = memorydb.New()
66-
threshold = ctx.Int(utils.BeaconThresholdFlag.Name)
67-
committeeChain = light.NewCommitteeChain(db, chainConfig.ChainConfig, threshold, !ctx.Bool(utils.BeaconNoFilterFlag.Name))
68-
headTracker = light.NewHeadTracker(committeeChain, threshold)
48+
committeeChain = light.NewCommitteeChain(db, &config.ChainConfig, config.Threshold, !config.NoFilter)
49+
headTracker = light.NewHeadTracker(committeeChain, config.Threshold)
6950
)
7051
headSync := sync.NewHeadSync(headTracker, committeeChain)
7152

7253
// set up scheduler and sync modules
7354
scheduler := request.NewScheduler()
74-
checkpointInit := sync.NewCheckpointInit(committeeChain, chainConfig.Checkpoint)
55+
checkpointInit := sync.NewCheckpointInit(committeeChain, config.Checkpoint)
7556
forwardSync := sync.NewForwardUpdateSync(committeeChain)
7657
beaconBlockSync := newBeaconBlockSync(headTracker)
7758
scheduler.RegisterTarget(headTracker)
@@ -83,9 +64,9 @@ func NewClient(ctx *cli.Context) *Client {
8364

8465
return &Client{
8566
scheduler: scheduler,
86-
urls: ctx.StringSlice(utils.BeaconApiFlag.Name),
87-
customHeader: customHeader,
88-
chainConfig: &chainConfig,
67+
urls: config.Apis,
68+
customHeader: config.CustomHeader,
69+
config: &config,
8970
blockSync: beaconBlockSync,
9071
}
9172
}
@@ -97,7 +78,7 @@ func (c *Client) SetEngineRPC(engine *rpc.Client) {
9778
func (c *Client) Start() error {
9879
headCh := make(chan types.ChainHeadEvent, 16)
9980
c.chainHeadSub = c.blockSync.SubscribeChainHead(headCh)
100-
c.engineClient = startEngineClient(c.chainConfig, c.engineRPC, headCh)
81+
c.engineClient = startEngineClient(c.config, c.engineRPC, headCh)
10182

10283
c.scheduler.Start()
10384
for _, url := range c.urls {

beacon/blsync/config.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

beacon/blsync/engineclient.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/ethereum/go-ethereum/beacon/engine"
26+
"github.com/ethereum/go-ethereum/beacon/params"
2627
"github.com/ethereum/go-ethereum/beacon/types"
2728
"github.com/ethereum/go-ethereum/common"
2829
ctypes "github.com/ethereum/go-ethereum/core/types"
@@ -31,14 +32,14 @@ import (
3132
)
3233

3334
type engineClient struct {
34-
config *lightClientConfig
35+
config *params.ClientConfig
3536
rpc *rpc.Client
3637
rootCtx context.Context
3738
cancelRoot context.CancelFunc
3839
wg sync.WaitGroup
3940
}
4041

41-
func startEngineClient(config *lightClientConfig, rpc *rpc.Client, headCh <-chan types.ChainHeadEvent) *engineClient {
42+
func startEngineClient(config *params.ClientConfig, rpc *rpc.Client, headCh <-chan types.ChainHeadEvent) *engineClient {
4243
ctx, cancel := context.WithCancel(context.Background())
4344
ec := &engineClient{
4445
config: config,

beacon/light/committee_chain.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,24 @@ type CommitteeChain struct {
7676
unixNano func() int64 // system clock (simulated clock in tests)
7777
sigVerifier committeeSigVerifier // BLS sig verifier (dummy verifier in tests)
7878

79-
config *types.ChainConfig
79+
config *params.ChainConfig
8080
minimumUpdateScore types.UpdateScore
8181
enforceTime bool // enforceTime specifies whether the age of a signed header should be checked
8282
}
8383

8484
// NewCommitteeChain creates a new CommitteeChain.
85-
func NewCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool) *CommitteeChain {
85+
func NewCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool) *CommitteeChain {
8686
return newCommitteeChain(db, config, signerThreshold, enforceTime, blsVerifier{}, &mclock.System{}, func() int64 { return time.Now().UnixNano() })
8787
}
8888

8989
// NewTestCommitteeChain creates a new CommitteeChain for testing.
90-
func NewTestCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool, clock *mclock.Simulated) *CommitteeChain {
90+
func NewTestCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool, clock *mclock.Simulated) *CommitteeChain {
9191
return newCommitteeChain(db, config, signerThreshold, enforceTime, dummyVerifier{}, clock, func() int64 { return int64(clock.Now()) })
9292
}
9393

9494
// newCommitteeChain creates a new CommitteeChain with the option of replacing the
9595
// clock source and signature verification for testing purposes.
96-
func newCommitteeChain(db ethdb.KeyValueStore, config *types.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain {
96+
func newCommitteeChain(db ethdb.KeyValueStore, config *params.ChainConfig, signerThreshold int, enforceTime bool, sigVerifier committeeSigVerifier, clock mclock.Clock, unixNano func() int64) *CommitteeChain {
9797
s := &CommitteeChain{
9898
committeeCache: lru.NewCache[uint64, syncCommittee](10),
9999
db: db,
@@ -505,7 +505,7 @@ func (s *CommitteeChain) verifySignedHeader(head types.SignedHeader) (bool, time
505505
if committee == nil {
506506
return false, age, nil
507507
}
508-
if signingRoot, err := s.config.Forks.SigningRoot(head.Header); err == nil {
508+
if signingRoot, err := s.config.Forks.SigningRoot(head.Header.Epoch(), head.Header.Hash()); err == nil {
509509
return s.sigVerifier.verifySignature(committee, signingRoot, &head.Signature), age, nil
510510
}
511511
return false, age, nil

beacon/light/committee_chain_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ var (
3131
testGenesis = newTestGenesis()
3232
testGenesis2 = newTestGenesis()
3333

34-
tfBase = newTestForks(testGenesis, types.Forks{
35-
&types.Fork{Epoch: 0, Version: []byte{0}},
34+
tfBase = newTestForks(testGenesis, params.Forks{
35+
&params.Fork{Epoch: 0, Version: []byte{0}},
3636
})
37-
tfAlternative = newTestForks(testGenesis, types.Forks{
38-
&types.Fork{Epoch: 0, Version: []byte{0}},
39-
&types.Fork{Epoch: 0x700, Version: []byte{1}},
37+
tfAlternative = newTestForks(testGenesis, params.Forks{
38+
&params.Fork{Epoch: 0, Version: []byte{0}},
39+
&params.Fork{Epoch: 0x700, Version: []byte{1}},
4040
})
41-
tfAnotherGenesis = newTestForks(testGenesis2, types.Forks{
42-
&types.Fork{Epoch: 0, Version: []byte{0}},
41+
tfAnotherGenesis = newTestForks(testGenesis2, params.Forks{
42+
&params.Fork{Epoch: 0, Version: []byte{0}},
4343
})
4444

4545
tcBase = newTestCommitteeChain(nil, tfBase, true, 0, 10, 400, false)
@@ -226,13 +226,13 @@ type committeeChainTest struct {
226226
t *testing.T
227227
db *memorydb.Database
228228
clock *mclock.Simulated
229-
config types.ChainConfig
229+
config params.ChainConfig
230230
signerThreshold int
231231
enforceTime bool
232232
chain *CommitteeChain
233233
}
234234

235-
func newCommitteeChainTest(t *testing.T, config types.ChainConfig, signerThreshold int, enforceTime bool) *committeeChainTest {
235+
func newCommitteeChainTest(t *testing.T, config params.ChainConfig, signerThreshold int, enforceTime bool) *committeeChainTest {
236236
c := &committeeChainTest{
237237
t: t,
238238
db: memorydb.New(),
@@ -298,20 +298,20 @@ func (c *committeeChainTest) verifyRange(tc *testCommitteeChain, begin, end uint
298298
c.verifySignedHeader(tc, float64(end)+1.5, false)
299299
}
300300

301-
func newTestGenesis() types.ChainConfig {
302-
var config types.ChainConfig
301+
func newTestGenesis() params.ChainConfig {
302+
var config params.ChainConfig
303303
rand.Read(config.GenesisValidatorsRoot[:])
304304
return config
305305
}
306306

307-
func newTestForks(config types.ChainConfig, forks types.Forks) types.ChainConfig {
307+
func newTestForks(config params.ChainConfig, forks params.Forks) params.ChainConfig {
308308
for _, fork := range forks {
309309
config.AddFork(fork.Name, fork.Epoch, fork.Version)
310310
}
311311
return config
312312
}
313313

314-
func newTestCommitteeChain(parent *testCommitteeChain, config types.ChainConfig, newCommittees bool, begin, end int, signerCount int, finalizedHeader bool) *testCommitteeChain {
314+
func newTestCommitteeChain(parent *testCommitteeChain, config params.ChainConfig, newCommittees bool, begin, end int, signerCount int, finalizedHeader bool) *testCommitteeChain {
315315
tc := &testCommitteeChain{
316316
config: config,
317317
}
@@ -337,7 +337,7 @@ type testPeriod struct {
337337

338338
type testCommitteeChain struct {
339339
periods []testPeriod
340-
config types.ChainConfig
340+
config params.ChainConfig
341341
}
342342

343343
func (tc *testCommitteeChain) fillCommittees(begin, end int) {

beacon/light/test_helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func GenerateTestCommittee() *types.SerializedSyncCommittee {
3333
return s
3434
}
3535

36-
func GenerateTestUpdate(config *types.ChainConfig, period uint64, committee, nextCommittee *types.SerializedSyncCommittee, signerCount int, finalizedHeader bool) *types.LightClientUpdate {
36+
func GenerateTestUpdate(config *params.ChainConfig, period uint64, committee, nextCommittee *types.SerializedSyncCommittee, signerCount int, finalizedHeader bool) *types.LightClientUpdate {
3737
update := new(types.LightClientUpdate)
3838
update.NextSyncCommitteeRoot = nextCommittee.Root()
3939
var attestedHeader types.Header
@@ -48,9 +48,9 @@ func GenerateTestUpdate(config *types.ChainConfig, period uint64, committee, nex
4848
return update
4949
}
5050

51-
func GenerateTestSignedHeader(header types.Header, config *types.ChainConfig, committee *types.SerializedSyncCommittee, signatureSlot uint64, signerCount int) types.SignedHeader {
51+
func GenerateTestSignedHeader(header types.Header, config *params.ChainConfig, committee *types.SerializedSyncCommittee, signatureSlot uint64, signerCount int) types.SignedHeader {
5252
bitmask := makeBitmask(signerCount)
53-
signingRoot, _ := config.Forks.SigningRoot(header)
53+
signingRoot, _ := config.Forks.SigningRoot(header.Epoch(), header.Hash())
5454
c, _ := dummyVerifier{}.deserializeSyncCommittee(committee)
5555
return types.SignedHeader{
5656
Header: header,

0 commit comments

Comments
 (0)