From d93a1b671cc5cbfa0ac546ede74fd53859c98789 Mon Sep 17 00:00:00 2001 From: Rupam Dey Date: Fri, 13 Dec 2024 02:09:49 +0530 Subject: [PATCH] process lc finality update only for new finalized checkpoints (#14713) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add checks for finalized checkpoint * implement `EmptyExecutionPayloadHeader()` function * changelog * fix error message * revert `process_block.go` * fix error message * testing * Update CHANGELOG.md Co-authored-by: Radosław Kapka * revert "testing" --------- Co-authored-by: Radosław Kapka Co-authored-by: Radosław Kapka --- CHANGELOG.md | 2 + .../blockchain/process_block_helpers.go | 6 +++ beacon-chain/core/light-client/BUILD.bazel | 1 + beacon-chain/core/light-client/lightclient.go | 43 ++++++++--------- beacon-chain/execution/engine_client.go | 47 +++++++++++++++++++ 5 files changed, 75 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1686754e85..5dc61322ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve - Save light client updates and bootstraps in DB. - Added more comprehensive tests for `BlockToLightClientHeader`. [PR](https://github.com/prysmaticlabs/prysm/pull/14699) - Added an error field to log `Finished building block`. +- Implemented a new `EmptyExecutionPayloadHeader` function. ### Changed @@ -78,6 +79,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve - Check kzg commitments align with blobs and proofs for beacon api end point. - Revert "Proposer checks gas limit before accepting builder's bid". - Updated quic-go to v0.48.2 . +- Process light client finality updates only for new finalized epochs instead of doing it for every block. ### Deprecated diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index d39e50cb53..97c195d113 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -1,6 +1,7 @@ package blockchain import ( + "bytes" "context" "fmt" "time" @@ -240,6 +241,11 @@ func (s *Service) processLightClientFinalityUpdate( } } + // Check if the finalized checkpoint has changed + if finalizedCheckPoint == nil || bytes.Equal(finalizedCheckPoint.GetRoot(), postState.FinalizedCheckpoint().Root) { + return nil + } + update, err := lightclient.NewLightClientFinalityUpdateFromBeaconState( ctx, postState.Slot(), diff --git a/beacon-chain/core/light-client/BUILD.bazel b/beacon-chain/core/light-client/BUILD.bazel index 2757a1325b..25bfc44320 100644 --- a/beacon-chain/core/light-client/BUILD.bazel +++ b/beacon-chain/core/light-client/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/light-client", visibility = ["//visibility:public"], deps = [ + "//beacon-chain/execution:go_default_library", "//beacon-chain/state:go_default_library", "//config/fieldparams:go_default_library", "//config/params:go_default_library", diff --git a/beacon-chain/core/light-client/lightclient.go b/beacon-chain/core/light-client/lightclient.go index a179bc5b50..cb4b7191d4 100644 --- a/beacon-chain/core/light-client/lightclient.go +++ b/beacon-chain/core/light-client/lightclient.go @@ -7,6 +7,7 @@ import ( "reflect" "github.com/pkg/errors" + "github.com/prysmaticlabs/prysm/v5/beacon-chain/execution" "github.com/prysmaticlabs/prysm/v5/beacon-chain/state" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" @@ -404,18 +405,15 @@ func BlockToLightClientHeader( var payloadProof [][]byte if blockEpoch < params.BeaconConfig().CapellaForkEpoch { - payloadHeader = &enginev1.ExecutionPayloadHeaderCapella{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, fieldparams.LogsBloomLength), - PrevRandao: make([]byte, fieldparams.RootLength), - ExtraData: make([]byte, 0), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - TransactionsRoot: make([]byte, fieldparams.RootLength), - WithdrawalsRoot: make([]byte, fieldparams.RootLength), + var ok bool + + p, err := execution.EmptyExecutionPayloadHeader(version.Capella) + if err != nil { + return nil, errors.Wrap(err, "could not get payload header") + } + payloadHeader, ok = p.(*enginev1.ExecutionPayloadHeaderCapella) + if !ok { + return nil, fmt.Errorf("payload header type %T is not %T", p, &enginev1.ExecutionPayloadHeaderCapella{}) } payloadProof = emptyPayloadProof() } else { @@ -472,18 +470,15 @@ func BlockToLightClientHeader( var payloadProof [][]byte if blockEpoch < params.BeaconConfig().CapellaForkEpoch { - payloadHeader = &enginev1.ExecutionPayloadHeaderDeneb{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, fieldparams.LogsBloomLength), - PrevRandao: make([]byte, fieldparams.RootLength), - ExtraData: make([]byte, 0), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - TransactionsRoot: make([]byte, fieldparams.RootLength), - WithdrawalsRoot: make([]byte, fieldparams.RootLength), + var ok bool + + p, err := execution.EmptyExecutionPayloadHeader(version.Deneb) + if err != nil { + return nil, errors.Wrap(err, "could not get payload header") + } + payloadHeader, ok = p.(*enginev1.ExecutionPayloadHeaderDeneb) + if !ok { + return nil, fmt.Errorf("payload header type %T is not %T", p, &enginev1.ExecutionPayloadHeaderDeneb{}) } payloadProof = emptyPayloadProof() } else { diff --git a/beacon-chain/execution/engine_client.go b/beacon-chain/execution/engine_client.go index 1424ab9375..002378ad5b 100644 --- a/beacon-chain/execution/engine_client.go +++ b/beacon-chain/execution/engine_client.go @@ -849,6 +849,53 @@ func EmptyExecutionPayload(v int) (proto.Message, error) { } } +func EmptyExecutionPayloadHeader(v int) (proto.Message, error) { + switch v { + case version.Bellatrix: + return &pb.ExecutionPayloadHeader{ + ParentHash: make([]byte, fieldparams.RootLength), + FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), + StateRoot: make([]byte, fieldparams.RootLength), + ReceiptsRoot: make([]byte, fieldparams.RootLength), + LogsBloom: make([]byte, fieldparams.LogsBloomLength), + PrevRandao: make([]byte, fieldparams.RootLength), + ExtraData: make([]byte, 0), + BaseFeePerGas: make([]byte, fieldparams.RootLength), + BlockHash: make([]byte, fieldparams.RootLength), + }, nil + case version.Capella: + return &pb.ExecutionPayloadHeaderCapella{ + ParentHash: make([]byte, fieldparams.RootLength), + FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), + StateRoot: make([]byte, fieldparams.RootLength), + ReceiptsRoot: make([]byte, fieldparams.RootLength), + LogsBloom: make([]byte, fieldparams.LogsBloomLength), + PrevRandao: make([]byte, fieldparams.RootLength), + ExtraData: make([]byte, 0), + BaseFeePerGas: make([]byte, fieldparams.RootLength), + BlockHash: make([]byte, fieldparams.RootLength), + TransactionsRoot: make([]byte, fieldparams.RootLength), + WithdrawalsRoot: make([]byte, fieldparams.RootLength), + }, nil + case version.Deneb, version.Electra: + return &pb.ExecutionPayloadHeaderDeneb{ + ParentHash: make([]byte, fieldparams.RootLength), + FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), + StateRoot: make([]byte, fieldparams.RootLength), + ReceiptsRoot: make([]byte, fieldparams.RootLength), + LogsBloom: make([]byte, fieldparams.LogsBloomLength), + PrevRandao: make([]byte, fieldparams.RootLength), + ExtraData: make([]byte, 0), + BaseFeePerGas: make([]byte, fieldparams.RootLength), + BlockHash: make([]byte, fieldparams.RootLength), + TransactionsRoot: make([]byte, fieldparams.RootLength), + WithdrawalsRoot: make([]byte, fieldparams.RootLength), + }, nil + default: + return nil, errors.Wrapf(ErrUnsupportedVersion, "version=%s", version.String(v)) + } +} + func toBlockNumArg(number *big.Int) string { if number == nil { return "latest"