Skip to content

Commit e6689fe

Browse files
zsfelfoldifjl
andauthored
beacon/light/sync: print error log if checkpoint retrieval fails (#29532)
Co-authored-by: Felix Lange <[email protected]>
1 parent 1ec7af2 commit e6689fe

File tree

11 files changed

+175
-40
lines changed

11 files changed

+175
-40
lines changed

beacon/blsync/block_sync_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
)
2929

3030
var (
31-
testServer1 = "testServer1"
32-
testServer2 = "testServer2"
31+
testServer1 = testServer("testServer1")
32+
testServer2 = testServer("testServer2")
3333

3434
testBlock1 = types.NewBeaconBlock(&deneb.BeaconBlock{
3535
Slot: 123,
@@ -51,6 +51,12 @@ var (
5151
})
5252
)
5353

54+
type testServer string
55+
56+
func (t testServer) Name() string {
57+
return string(t)
58+
}
59+
5460
func TestBlockSync(t *testing.T) {
5561
ht := &testHeadTracker{}
5662
blockSync := newBeaconBlockSync(ht)

beacon/light/api/api_server.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ func (s *ApiServer) SendRequest(id request.ID, req request.Request) {
7373
r.Updates, r.Committees, err = s.api.GetBestUpdatesAndCommittees(data.FirstPeriod, data.Count)
7474
resp = r
7575
case sync.ReqHeader:
76+
var r sync.RespHeader
7677
log.Debug("Beacon API: requesting header", "reqid", id, "hash", common.Hash(data))
77-
resp, err = s.api.GetHeader(common.Hash(data))
78+
r.Header, r.Canonical, r.Finalized, err = s.api.GetHeader(common.Hash(data))
79+
resp = r
7880
case sync.ReqCheckpointData:
7981
log.Debug("Beacon API: requesting checkpoint data", "reqid", id, "hash", common.Hash(data))
8082
resp, err = s.api.GetCheckpointData(common.Hash(data))
@@ -101,3 +103,8 @@ func (s *ApiServer) Unsubscribe() {
101103
s.unsubscribe = nil
102104
}
103105
}
106+
107+
// Name implements request.Server
108+
func (s *ApiServer) Name() string {
109+
return s.api.url
110+
}

beacon/light/api/light_api.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ func decodeFinalityUpdate(enc []byte) (types.FinalityUpdate, error) {
291291

292292
// GetHeader fetches and validates the beacon header with the given blockRoot.
293293
// If blockRoot is null hash then the latest head header is fetched.
294-
func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error) {
294+
// The values of the canonical and finalized flags are also returned. Note that
295+
// these flags are not validated.
296+
func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, bool, bool, error) {
295297
var blockId string
296298
if blockRoot == (common.Hash{}) {
297299
blockId = "head"
@@ -300,11 +302,12 @@ func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error
300302
}
301303
resp, err := api.httpGetf("/eth/v1/beacon/headers/%s", blockId)
302304
if err != nil {
303-
return types.Header{}, err
305+
return types.Header{}, false, false, err
304306
}
305307

306308
var data struct {
307-
Data struct {
309+
Finalized bool `json:"finalized"`
310+
Data struct {
308311
Root common.Hash `json:"root"`
309312
Canonical bool `json:"canonical"`
310313
Header struct {
@@ -314,16 +317,16 @@ func (api *BeaconLightApi) GetHeader(blockRoot common.Hash) (types.Header, error
314317
} `json:"data"`
315318
}
316319
if err := json.Unmarshal(resp, &data); err != nil {
317-
return types.Header{}, err
320+
return types.Header{}, false, false, err
318321
}
319322
header := data.Data.Header.Message
320323
if blockRoot == (common.Hash{}) {
321324
blockRoot = data.Data.Root
322325
}
323326
if header.Hash() != blockRoot {
324-
return types.Header{}, errors.New("retrieved beacon header root does not match")
327+
return types.Header{}, false, false, errors.New("retrieved beacon header root does not match")
325328
}
326-
return header, nil
329+
return header, data.Data.Canonical, data.Finalized, nil
327330
}
328331

329332
// GetCheckpointData fetches and validates bootstrap data belonging to the given checkpoint.
@@ -446,7 +449,7 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func()
446449
defer wg.Done()
447450

448451
// Request initial data.
449-
if head, err := api.GetHeader(common.Hash{}); err == nil {
452+
if head, _, _, err := api.GetHeader(common.Hash{}); err == nil {
450453
listener.OnNewHead(head.Slot, head.Hash())
451454
}
452455
if signedHead, err := api.GetOptimisticHeadUpdate(); err == nil {

beacon/light/request/scheduler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ type (
9393
// the modules that do not interact with them directly.
9494
// In order to make module testing easier, Server interface is used in
9595
// events and modules.
96-
Server any
96+
Server interface {
97+
Name() string
98+
}
9799
Request any
98100
Response any
99101
ID uint64

beacon/light/request/scheduler_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ type testServer struct {
7070
canRequest int
7171
}
7272

73+
func (s *testServer) Name() string {
74+
return ""
75+
}
76+
7377
func (s *testServer) subscribe(eventCb func(Event)) {
7478
s.eventCb = eventCb
7579
}

beacon/light/request/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const (
5858
// EvResponse or EvFail. Additionally, it may also send application-defined
5959
// events that the Modules can interpret.
6060
type requestServer interface {
61+
Name() string
6162
Subscribe(eventCallback func(Event))
6263
SendRequest(ID, Request)
6364
Unsubscribe()
@@ -69,6 +70,7 @@ type requestServer interface {
6970
// limit the number of parallel in-flight requests and temporarily disable
7071
// new requests based on timeouts and response failures.
7172
type server interface {
73+
Server
7274
subscribe(eventCallback func(Event))
7375
canRequestNow() bool
7476
sendRequest(Request) ID
@@ -138,6 +140,11 @@ type serverWithTimeout struct {
138140
lastID ID
139141
}
140142

143+
// Name implements request.Server
144+
func (s *serverWithTimeout) Name() string {
145+
return s.parent.Name()
146+
}
147+
141148
// init initializes serverWithTimeout
142149
func (s *serverWithTimeout) init(clock mclock.Clock) {
143150
s.clock = clock

beacon/light/request/server_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ type testRequestServer struct {
153153
eventCb func(Event)
154154
}
155155

156+
func (rs *testRequestServer) Name() string { return "" }
156157
func (rs *testRequestServer) Subscribe(eventCb func(Event)) { rs.eventCb = eventCb }
157158
func (rs *testRequestServer) SendRequest(ID, Request) {}
158159
func (rs *testRequestServer) Unsubscribe() {}

beacon/light/sync/head_sync_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
)
2525

2626
var (
27-
testServer1 = "testServer1"
28-
testServer2 = "testServer2"
29-
testServer3 = "testServer3"
30-
testServer4 = "testServer4"
27+
testServer1 = testServer("testServer1")
28+
testServer2 = testServer("testServer2")
29+
testServer3 = testServer("testServer3")
30+
testServer4 = testServer("testServer4")
3131

3232
testHead0 = types.HeadInfo{}
3333
testHead1 = types.HeadInfo{Slot: 123, BlockRoot: common.Hash{1}}
@@ -42,6 +42,12 @@ var (
4242
testSHead4 = types.SignedHeader{SignatureSlot: 0x6444, Header: types.Header{Slot: 0x6443, StateRoot: common.Hash{4}}}
4343
)
4444

45+
type testServer string
46+
47+
func (t testServer) Name() string {
48+
return string(t)
49+
}
50+
4551
func TestValidatedHead(t *testing.T) {
4652
chain := &TestCommitteeChain{}
4753
ht := &TestHeadTracker{}

beacon/light/sync/test_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (ts *TestScheduler) Run(testIndex int, exp ...any) {
7575
if count == 0 {
7676
continue
7777
}
78-
ts.t.Errorf("Missing %d Server.Fail(s) from server %s in test case #%d", count, server.(string), testIndex)
78+
ts.t.Errorf("Missing %d Server.Fail(s) from server %s in test case #%d", count, server.Name(), testIndex)
7979
}
8080

8181
if !reflect.DeepEqual(ts.sent[testIndex], expReqs) {
@@ -104,7 +104,7 @@ func (ts *TestScheduler) Send(server request.Server, req request.Request) reques
104104

105105
func (ts *TestScheduler) Fail(server request.Server, desc string) {
106106
if ts.expFail[server] == 0 {
107-
ts.t.Errorf("Unexpected Fail from server %s in test case #%d: %s", server.(string), ts.testIndex, desc)
107+
ts.t.Errorf("Unexpected Fail from server %s in test case #%d: %s", server.Name(), ts.testIndex, desc)
108108
return
109109
}
110110
ts.expFail[server]--

beacon/light/sync/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ type (
3636
Updates []*types.LightClientUpdate
3737
Committees []*types.SerializedSyncCommittee
3838
}
39-
ReqHeader common.Hash
39+
ReqHeader common.Hash
40+
RespHeader struct {
41+
Header types.Header
42+
Canonical, Finalized bool
43+
}
4044
ReqCheckpointData common.Hash
4145
ReqBeaconBlock common.Hash
4246
)

0 commit comments

Comments
 (0)