From f3314d2d24c3f8e52be9ce593ee3538f36e49d6e Mon Sep 17 00:00:00 2001 From: Bastin <43618253+Inspector-Butters@users.noreply.github.com> Date: Thu, 17 Jul 2025 16:58:18 +0200 Subject: [PATCH] Abstract validation logic for saving LC updates into the store function (#15504) * Unify LC API (updates) * Remove unused fields in LC beacon API server * refactor lc logic into core * fix tests --- .../blockchain/process_block_helpers.go | 28 +---------------- beacon-chain/core/light-client/BUILD.bazel | 1 + beacon-chain/core/light-client/store.go | 30 +++++++++++++++++-- beacon-chain/sync/rpc_light_client_test.go | 2 +- changelog/bastin_abstract-save-update.md | 3 ++ 5 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 changelog/bastin_abstract-save-update.md diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index a74518a15f..70a1823aa2 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -189,33 +189,7 @@ func (s *Service) processLightClientUpdate(cfg *postBlockProcessConfig) error { period := slots.SyncCommitteePeriod(slots.ToEpoch(attestedState.Slot())) - oldUpdate, err := s.lcStore.LightClientUpdate(cfg.ctx, period) - if err != nil { - return errors.Wrapf(err, "could not get current light client update") - } - - if oldUpdate == nil { - if err := s.lcStore.SaveLightClientUpdate(cfg.ctx, period, update); err != nil { - return errors.Wrapf(err, "could not save light client update") - } - log.WithField("period", period).Debug("Saved new light client update") - return nil - } - - isNewUpdateBetter, err := lightclient.IsBetterUpdate(update, oldUpdate) - if err != nil { - return errors.Wrapf(err, "could not compare light client updates") - } - - if isNewUpdateBetter { - if err := s.lcStore.SaveLightClientUpdate(cfg.ctx, period, update); err != nil { - return errors.Wrapf(err, "could not save light client update") - } - log.WithField("period", period).Debug("Saved new light client update") - return nil - } - log.WithField("period", period).Debug("New light client update is not better than the current one, skipping save") - return nil + return s.lcStore.SaveLightClientUpdate(cfg.ctx, period, update) } // processLightClientBootstrap saves a light client bootstrap for this block diff --git a/beacon-chain/core/light-client/BUILD.bazel b/beacon-chain/core/light-client/BUILD.bazel index c3ce3f88eb..6d3d664d87 100644 --- a/beacon-chain/core/light-client/BUILD.bazel +++ b/beacon-chain/core/light-client/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//runtime/version:go_default_library", "//time/slots:go_default_library", "@com_github_pkg_errors//:go_default_library", + "@com_github_sirupsen_logrus//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], ) diff --git a/beacon-chain/core/light-client/store.go b/beacon-chain/core/light-client/store.go index 337ddbebbb..aa56c605a2 100644 --- a/beacon-chain/core/light-client/store.go +++ b/beacon-chain/core/light-client/store.go @@ -7,6 +7,7 @@ import ( "github.com/OffchainLabs/prysm/v6/beacon-chain/db/iface" "github.com/OffchainLabs/prysm/v6/consensus-types/interfaces" "github.com/pkg/errors" + log "github.com/sirupsen/logrus" ) var ErrLightClientBootstrapNotFound = errors.New("light client bootstrap not found") @@ -92,8 +93,33 @@ func (s *Store) SaveLightClientUpdate(ctx context.Context, period uint64, update s.mu.Lock() defer s.mu.Unlock() - // Save the light client update to the database - return s.beaconDB.SaveLightClientUpdate(ctx, period, update) + oldUpdate, err := s.beaconDB.LightClientUpdate(ctx, period) + if err != nil { + return errors.Wrapf(err, "could not get current light client update") + } + + if oldUpdate == nil { + if err := s.beaconDB.SaveLightClientUpdate(ctx, period, update); err != nil { + return errors.Wrapf(err, "could not save light client update") + } + log.WithField("period", period).Debug("Saved new light client update") + return nil + } + + isNewUpdateBetter, err := IsBetterUpdate(update, oldUpdate) + if err != nil { + return errors.Wrapf(err, "could not compare light client updates") + } + + if isNewUpdateBetter { + if err := s.beaconDB.SaveLightClientUpdate(ctx, period, update); err != nil { + return errors.Wrapf(err, "could not save light client update") + } + log.WithField("period", period).Debug("Saved new light client update") + return nil + } + log.WithField("period", period).Debug("New light client update is not better than the current one, skipping save") + return nil } func (s *Store) SetLastFinalityUpdate(update interfaces.LightClientFinalityUpdate) { diff --git a/beacon-chain/sync/rpc_light_client_test.go b/beacon-chain/sync/rpc_light_client_test.go index f1820773e5..acd42f1c9f 100644 --- a/beacon-chain/sync/rpc_light_client_test.go +++ b/beacon-chain/sync/rpc_light_client_test.go @@ -441,7 +441,7 @@ func TestRPC_LightClientUpdatesByRange(t *testing.T) { l := util.NewTestLightClient(t, i, util.WithIncreasedAttestedSlot(uint64(j))) update, err := lightClient.NewLightClientUpdateFromBeaconState(ctx, l.State.Slot(), l.State, l.Block, l.AttestedState, l.AttestedBlock, l.FinalizedBlock) require.NoError(t, err) - require.NoError(t, r.lcStore.SaveLightClientUpdate(ctx, uint64(j), update)) + require.NoError(t, r.cfg.beaconDB.SaveLightClientUpdate(ctx, uint64(j), update)) } var wg sync.WaitGroup diff --git a/changelog/bastin_abstract-save-update.md b/changelog/bastin_abstract-save-update.md new file mode 100644 index 0000000000..ae9233b6af --- /dev/null +++ b/changelog/bastin_abstract-save-update.md @@ -0,0 +1,3 @@ +### Ignored + +- Moved the validation logic for saving LC updates to the store function. \ No newline at end of file