Skip to content

Commit f5a9394

Browse files
authored
Reorganize processing of light client updates (#15383)
* Reorganize processing of light client updates * changelog <3
1 parent 4095da8 commit f5a9394

File tree

4 files changed

+49
-52
lines changed

4 files changed

+49
-52
lines changed

beacon-chain/blockchain/process_block.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ func (s *Service) postBlockProcess(cfg *postBlockProcessConfig) error {
7272
}
7373
if features.Get().EnableLightClient && slots.ToEpoch(s.CurrentSlot()) >= params.BeaconConfig().AltairForkEpoch {
7474
defer s.processLightClientUpdates(cfg)
75-
defer s.saveLightClientUpdate(cfg)
76-
defer s.saveLightClientBootstrap(cfg)
7775
}
7876
defer s.sendStateFeedOnBlock(cfg)
7977
defer reportProcessingTime(startTime)

beacon-chain/blockchain/process_block_helpers.go

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ func (s *Service) sendStateFeedOnBlock(cfg *postBlockProcessConfig) {
131131
}
132132

133133
func (s *Service) processLightClientUpdates(cfg *postBlockProcessConfig) {
134+
if err := s.processLightClientUpdate(cfg); err != nil {
135+
log.WithError(err).Error("Failed to process light client update")
136+
}
137+
if err := s.processLightClientBootstrap(cfg); err != nil {
138+
log.WithError(err).Error("Failed to process light client bootstrap")
139+
}
134140
if err := s.processLightClientOptimisticUpdate(cfg.ctx, cfg.roblock, cfg.postState); err != nil {
135141
log.WithError(err).Error("Failed to process light client optimistic update")
136142
}
@@ -139,38 +145,33 @@ func (s *Service) processLightClientUpdates(cfg *postBlockProcessConfig) {
139145
}
140146
}
141147

142-
// saveLightClientUpdate saves the light client update for this block
148+
// processLightClientUpdate saves the light client update for this block
143149
// if it's better than the already saved one, when feature flag is enabled.
144-
func (s *Service) saveLightClientUpdate(cfg *postBlockProcessConfig) {
150+
func (s *Service) processLightClientUpdate(cfg *postBlockProcessConfig) error {
145151
attestedRoot := cfg.roblock.Block().ParentRoot()
146152
attestedBlock, err := s.getBlock(cfg.ctx, attestedRoot)
147153
if err != nil {
148-
log.WithError(err).Errorf("Saving light client update failed: Could not get attested block for root %#x", attestedRoot)
149-
return
154+
return errors.Wrapf(err, "could not get attested block for root %#x", attestedRoot)
150155
}
151156
if attestedBlock == nil || attestedBlock.IsNil() {
152-
log.Error("Saving light client update failed: Attested block is nil")
153-
return
157+
return errors.New("attested block is nil")
154158
}
155159
attestedState, err := s.cfg.StateGen.StateByRoot(cfg.ctx, attestedRoot)
156160
if err != nil {
157-
log.WithError(err).Errorf("Saving light client update failed: Could not get attested state for root %#x", attestedRoot)
158-
return
161+
return errors.Wrapf(err, "could not get attested state for root %#x", attestedRoot)
159162
}
160163
if attestedState == nil || attestedState.IsNil() {
161-
log.Error("Saving light client update failed: Attested state is nil")
162-
return
164+
return errors.New("attested state is nil")
163165
}
164166

165167
finalizedRoot := attestedState.FinalizedCheckpoint().Root
166168
finalizedBlock, err := s.getBlock(cfg.ctx, [32]byte(finalizedRoot))
167169
if err != nil {
168170
if errors.Is(err, errBlockNotFoundInCacheOrDB) {
169-
log.Debugf("Skipping saving light client update: Finalized block is nil for root %#x", finalizedRoot)
170-
} else {
171-
log.WithError(err).Errorf("Saving light client update failed: Could not get finalized block for root %#x", finalizedRoot)
171+
log.Debugf("Skipping saving light client update because finalized block is nil for root %#x", finalizedRoot)
172+
return nil
172173
}
173-
return
174+
return errors.Wrapf(err, "could not get finalized block for root %#x", finalizedRoot)
174175
}
175176

176177
update, err := lightclient.NewLightClientUpdateFromBeaconState(
@@ -183,57 +184,52 @@ func (s *Service) saveLightClientUpdate(cfg *postBlockProcessConfig) {
183184
finalizedBlock,
184185
)
185186
if err != nil {
186-
log.WithError(err).Error("Saving light client update failed: Could not create light client update")
187-
return
187+
return errors.Wrapf(err, "could not create light client update")
188188
}
189189

190190
period := slots.SyncCommitteePeriod(slots.ToEpoch(attestedState.Slot()))
191191

192192
oldUpdate, err := s.cfg.BeaconDB.LightClientUpdate(cfg.ctx, period)
193193
if err != nil {
194-
log.WithError(err).Error("Saving light client update failed: Could not get current light client update")
195-
return
194+
return errors.Wrapf(err, "could not get current light client update")
196195
}
197196

198197
if oldUpdate == nil {
199198
if err := s.cfg.BeaconDB.SaveLightClientUpdate(cfg.ctx, period, update); err != nil {
200-
log.WithError(err).Error("Saving light client update failed: Could not save light client update")
201-
} else {
202-
log.WithField("period", period).Debug("Saving light client update: Saved new update")
199+
return errors.Wrapf(err, "could not save light client update")
203200
}
204-
return
201+
log.WithField("period", period).Debug("Saved new light client update")
202+
return nil
205203
}
206204

207205
isNewUpdateBetter, err := lightclient.IsBetterUpdate(update, oldUpdate)
208206
if err != nil {
209-
log.WithError(err).Error("Saving light client update failed: Could not compare light client updates")
210-
return
207+
return errors.Wrapf(err, "could not compare light client updates")
211208
}
212209

213210
if isNewUpdateBetter {
214211
if err := s.cfg.BeaconDB.SaveLightClientUpdate(cfg.ctx, period, update); err != nil {
215-
log.WithError(err).Error("Saving light client update failed: Could not save light client update")
216-
} else {
217-
log.WithField("period", period).Debug("Saving light client update: Saved new update")
212+
return errors.Wrapf(err, "could not save light client update")
218213
}
219-
} else {
220-
log.WithField("period", period).Debug("Saving light client update: New update is not better than the current one. Skipping save.")
214+
log.WithField("period", period).Debug("Saved new light client update")
215+
return nil
221216
}
217+
log.WithField("period", period).Debug("New light client update is not better than the current one, skipping save")
218+
return nil
222219
}
223220

224-
// saveLightClientBootstrap saves a light client bootstrap for this block
221+
// processLightClientBootstrap saves a light client bootstrap for this block
225222
// when feature flag is enabled.
226-
func (s *Service) saveLightClientBootstrap(cfg *postBlockProcessConfig) {
223+
func (s *Service) processLightClientBootstrap(cfg *postBlockProcessConfig) error {
227224
blockRoot := cfg.roblock.Root()
228225
bootstrap, err := lightclient.NewLightClientBootstrapFromBeaconState(cfg.ctx, s.CurrentSlot(), cfg.postState, cfg.roblock)
229226
if err != nil {
230-
log.WithError(err).Error("Saving light client bootstrap failed: Could not create light client bootstrap")
231-
return
227+
return errors.Wrapf(err, "could not create light client bootstrap")
232228
}
233-
err = s.cfg.BeaconDB.SaveLightClientBootstrap(cfg.ctx, blockRoot[:], bootstrap)
234-
if err != nil {
235-
log.WithError(err).Error("Saving light client bootstrap failed: Could not save light client bootstrap in DB")
229+
if err := s.cfg.BeaconDB.SaveLightClientBootstrap(cfg.ctx, blockRoot[:], bootstrap); err != nil {
230+
return errors.Wrapf(err, "could not save light client bootstrap")
236231
}
232+
return nil
237233
}
238234

239235
func (s *Service) processLightClientFinalityUpdate(

beacon-chain/blockchain/process_block_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,7 @@ func fakeResult(missing []uint64) map[uint64]struct{} {
27062706
return r
27072707
}
27082708

2709-
func TestSaveLightClientUpdate(t *testing.T) {
2709+
func TestProcessLightClientUpdate(t *testing.T) {
27102710
featCfg := &features.Flags{}
27112711
featCfg.EnableLightClient = true
27122712
reset := features.InitWithReset(featCfg)
@@ -2747,7 +2747,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
27472747
isValidPayload: true,
27482748
}
27492749

2750-
s.saveLightClientUpdate(cfg)
2750+
require.NoError(t, s.processLightClientUpdate(cfg))
27512751

27522752
// Check that the light client update is saved
27532753
period := slots.SyncCommitteePeriod(slots.ToEpoch(l.AttestedState.Slot()))
@@ -2802,7 +2802,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
28022802
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
28032803
require.NoError(t, err)
28042804

2805-
s.saveLightClientUpdate(cfg)
2805+
require.NoError(t, s.processLightClientUpdate(cfg))
28062806

28072807
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
28082808
require.NoError(t, err)
@@ -2863,7 +2863,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
28632863
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
28642864
require.NoError(t, err)
28652865

2866-
s.saveLightClientUpdate(cfg)
2866+
require.NoError(t, s.processLightClientUpdate(cfg))
28672867

28682868
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
28692869
require.NoError(t, err)
@@ -2906,7 +2906,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
29062906
isValidPayload: true,
29072907
}
29082908

2909-
s.saveLightClientUpdate(cfg)
2909+
require.NoError(t, s.processLightClientUpdate(cfg))
29102910

29112911
// Check that the light client update is saved
29122912
period := slots.SyncCommitteePeriod(slots.ToEpoch(l.AttestedState.Slot()))
@@ -2960,7 +2960,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
29602960
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
29612961
require.NoError(t, err)
29622962

2963-
s.saveLightClientUpdate(cfg)
2963+
require.NoError(t, s.processLightClientUpdate(cfg))
29642964

29652965
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
29662966
require.NoError(t, err)
@@ -3021,7 +3021,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
30213021
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
30223022
require.NoError(t, err)
30233023

3024-
s.saveLightClientUpdate(cfg)
3024+
require.NoError(t, s.processLightClientUpdate(cfg))
30253025

30263026
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
30273027
require.NoError(t, err)
@@ -3064,7 +3064,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
30643064
isValidPayload: true,
30653065
}
30663066

3067-
s.saveLightClientUpdate(cfg)
3067+
require.NoError(t, s.processLightClientUpdate(cfg))
30683068

30693069
// Check that the light client update is saved
30703070
period := slots.SyncCommitteePeriod(slots.ToEpoch(l.AttestedState.Slot()))
@@ -3118,7 +3118,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
31183118
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
31193119
require.NoError(t, err)
31203120

3121-
s.saveLightClientUpdate(cfg)
3121+
require.NoError(t, s.processLightClientUpdate(cfg))
31223122

31233123
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
31243124
require.NoError(t, err)
@@ -3179,7 +3179,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
31793179
err = s.cfg.BeaconDB.SaveLightClientUpdate(ctx, period, oldUpdate)
31803180
require.NoError(t, err)
31813181

3182-
s.saveLightClientUpdate(cfg)
3182+
require.NoError(t, s.processLightClientUpdate(cfg))
31833183

31843184
u, err := s.cfg.BeaconDB.LightClientUpdate(ctx, period)
31853185
require.NoError(t, err)
@@ -3192,7 +3192,7 @@ func TestSaveLightClientUpdate(t *testing.T) {
31923192
reset()
31933193
}
31943194

3195-
func TestSaveLightClientBootstrap(t *testing.T) {
3195+
func TestProcessLightClientBootstrap(t *testing.T) {
31963196
featCfg := &features.Flags{}
31973197
featCfg.EnableLightClient = true
31983198
reset := features.InitWithReset(featCfg)
@@ -3222,7 +3222,7 @@ func TestSaveLightClientBootstrap(t *testing.T) {
32223222
isValidPayload: true,
32233223
}
32243224

3225-
s.saveLightClientBootstrap(cfg)
3225+
require.NoError(t, s.processLightClientBootstrap(cfg))
32263226

32273227
// Check that the light client bootstrap is saved
32283228
b, err := s.cfg.BeaconDB.LightClientBootstrap(ctx, currentBlockRoot[:])
@@ -3257,7 +3257,7 @@ func TestSaveLightClientBootstrap(t *testing.T) {
32573257
isValidPayload: true,
32583258
}
32593259

3260-
s.saveLightClientBootstrap(cfg)
3260+
require.NoError(t, s.processLightClientBootstrap(cfg))
32613261

32623262
// Check that the light client bootstrap is saved
32633263
b, err := s.cfg.BeaconDB.LightClientBootstrap(ctx, currentBlockRoot[:])
@@ -3292,7 +3292,7 @@ func TestSaveLightClientBootstrap(t *testing.T) {
32923292
isValidPayload: true,
32933293
}
32943294

3295-
s.saveLightClientBootstrap(cfg)
3295+
require.NoError(t, s.processLightClientBootstrap(cfg))
32963296

32973297
// Check that the light client bootstrap is saved
32983298
b, err := s.cfg.BeaconDB.LightClientBootstrap(ctx, currentBlockRoot[:])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Ignored
2+
3+
- Reorganize processing of light client updates.

0 commit comments

Comments
 (0)