Skip to content

Commit ce2efdb

Browse files
committed
Add IsBlobConsensusEnabledAtHeight helper to reduce code duplication
1 parent 5e9e4f3 commit ce2efdb

8 files changed

Lines changed: 48 additions & 52 deletions

File tree

beacon/blockchain/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (s *Service) ParseProcessProposalRequest(req *cmtabci.ProcessProposalReques
3535
types.BlobSidecars,
3636
error,
3737
) {
38-
blobConsensusEnabled := s.chainSpec.BlobConsensusEnableHeight() > 0 && req.Height >= s.chainSpec.BlobConsensusEnableHeight()
38+
blobConsensusEnabled := s.chainSpec.IsBlobConsensusEnabledAtHeight(req.Height)
3939

4040
maxTxCount := MaxConsensusTxsCount
4141
if blobConsensusEnabled {
@@ -62,7 +62,7 @@ func (s *Service) ParseProcessProposalRequest(req *cmtabci.ProcessProposalReques
6262
req.GetTxs(),
6363
req.GetBlob(),
6464
req.Height,
65-
s.chainSpec.BlobConsensusEnableHeight(),
65+
s.chainSpec,
6666
)
6767
if err != nil {
6868
return nil, nil, fmt.Errorf("failed to extract blob sidecars at height %d: %w", req.Height, err)

beacon/blockchain/finalize_block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *Service) FinalizeBlock(
4343
blobs datypes.BlobSidecars,
4444
) (transition.ValidatorUpdates, error) {
4545
maxTxCount := MaxConsensusTxsCount
46-
if s.chainSpec.BlobConsensusEnableHeight() > 0 && req.Height >= s.chainSpec.BlobConsensusEnableHeight() {
46+
if s.chainSpec.IsBlobConsensusEnabledAtHeight(req.Height) {
4747
maxTxCount = 1
4848
}
4949

chain/spec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ func (s spec) BlobMaxBytes() int64 {
345345
return s.Data.BlobConfig.MaxBytes
346346
}
347347

348+
func (s spec) IsBlobConsensusEnabledAtHeight(height int64) bool {
349+
return s.BlobConsensusEnableHeight() > 0 && height >= s.BlobConsensusEnableHeight()
350+
}
351+
348352
// MaxEffectiveBalance returns the maximum effective balance.
349353
func (s spec) MaxEffectiveBalance() math.Gwei {
350354
return math.Gwei(s.Data.MaxEffectiveBalance)

consensus/cometbft/service/blobreactor/config.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type ConfigGetter interface {
4343
BlobConsensusEnableHeight() int64
4444
// BlobMaxBytes returns the maximum size of blob data in bytes for BlobReactor consensus params.
4545
BlobMaxBytes() int64
46+
// IsBlobConsensusEnabledAtHeight returns true if blob consensus is enabled for the given height.
47+
// Returns false if BlobConsensusEnableHeight is 0 (disabled) or if height is before enable height.
48+
IsBlobConsensusEnabledAtHeight(height int64) bool
4649
}
4750

4851
// Config contains configuration for the P2P BlobReactor component.
@@ -57,16 +60,6 @@ type Config struct {
5760
MaxBytes int64 `mapstructure:"max-bytes"`
5861
}
5962

60-
// BlobConsensusEnableHeight returns the height when blob processing is enabled.
61-
func (c Config) BlobConsensusEnableHeight() int64 {
62-
return c.ConsensusEnableHeight
63-
}
64-
65-
// BlobMaxBytes returns the maximum blob size in bytes.
66-
func (c Config) BlobMaxBytes() int64 {
67-
return c.MaxBytes
68-
}
69-
7063
func DefaultConfig() Config {
7164
return Config{
7265
ConsensusUpdateHeight: blobConsensusUpdateHeight,

consensus/cometbft/service/encoding/encoding.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525

2626
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
27+
"github.com/berachain/beacon-kit/consensus/cometbft/service/blobreactor"
2728
datypes "github.com/berachain/beacon-kit/da/types"
2829
"github.com/berachain/beacon-kit/primitives/common"
2930
"github.com/berachain/beacon-kit/primitives/encoding/ssz"
@@ -66,34 +67,33 @@ func UnmarshalBeaconBlockFromABCIRequest(
6667
}
6768

6869
// UnmarshalBlobSidecarsFromABCIRequest extracts blob sidecars from an ABCI FinalizeBlockRequest based on blob consensus parameters.
69-
func UnmarshalBlobSidecarsFromABCIRequest(req *cmtabci.FinalizeBlockRequest, blobEnableHeight int64) (datypes.BlobSidecars, error) {
70+
func UnmarshalBlobSidecarsFromABCIRequest(req *cmtabci.FinalizeBlockRequest, cfg blobreactor.ConfigGetter) (datypes.BlobSidecars, error) {
7071
if req == nil {
7172
return nil, ErrNilABCIRequest
7273
}
7374

74-
// Before or at BlobEnableHeight: blobs are in Txs[1] if present
75-
if blobEnableHeight <= 0 || req.Height < blobEnableHeight {
76-
txs := req.GetTxs()
77-
if len(txs) <= 1 {
78-
// No sidecars in this block
79-
return datypes.BlobSidecars{}, nil
80-
}
75+
// If we are at or after BlobEnableHeight, then blobs must be retrieved from cache or via blobreactor p2p
76+
if cfg.IsBlobConsensusEnabledAtHeight(req.Height) {
77+
return datypes.BlobSidecars{}, nil
78+
}
8179

82-
sidecarBz := txs[1]
83-
if len(sidecarBz) == 0 {
84-
return datypes.BlobSidecars{}, nil
85-
}
80+
// Otherwise, blobs are in Txs[1] if present in this block
81+
txs := req.GetTxs()
82+
if len(txs) <= 1 {
83+
return datypes.BlobSidecars{}, nil
84+
}
8685

87-
var sidecars datypes.BlobSidecars
88-
if err := ssz.Unmarshal(sidecarBz, &sidecars); err != nil {
89-
return nil, fmt.Errorf("failed to unmarshal blobs from Txs[1]: %w", err)
90-
}
86+
sidecarBz := txs[1]
87+
if len(sidecarBz) == 0 {
88+
return datypes.BlobSidecars{}, nil
89+
}
9190

92-
return sidecars, nil
91+
var sidecars datypes.BlobSidecars
92+
if err := ssz.Unmarshal(sidecarBz, &sidecars); err != nil {
93+
return nil, fmt.Errorf("failed to unmarshal blobs from Txs[1]: %w", err)
9394
}
9495

95-
// After BlobEnableHeight: blobs are distributed via BlobReactor and must be retrieved from cache or storage
96-
return datypes.BlobSidecars{}, nil
96+
return sidecars, nil
9797
}
9898

9999
// ExtractBlobSidecarsFromRequest is a generic helper that extracts blob sidecars from either
@@ -103,36 +103,35 @@ func ExtractBlobSidecarsFromRequest(
103103
txs [][]byte,
104104
blobData []byte,
105105
height int64,
106-
blobEnableHeight int64,
106+
cfg blobreactor.ConfigGetter,
107107
) (datypes.BlobSidecars, error) {
108-
// Before BlobEnableHeight: blobs are in Txs[1]
109-
if blobEnableHeight <= 0 || height < blobEnableHeight {
110-
if len(txs) <= 1 {
111-
// No sidecars in this block
112-
return datypes.BlobSidecars{}, nil
113-
}
114-
115-
sidecarBz := txs[1]
116-
if len(sidecarBz) == 0 {
108+
// If we are at or after BlobEnableHeight, then we use blobData for blobs if present
109+
if cfg.IsBlobConsensusEnabledAtHeight(height) {
110+
if len(blobData) == 0 {
117111
return datypes.BlobSidecars{}, nil
118112
}
119113

120114
var sidecars datypes.BlobSidecars
121-
if err := ssz.Unmarshal(sidecarBz, &sidecars); err != nil {
122-
return nil, fmt.Errorf("failed to unmarshal blobs from Txs[1] at height %d: %w", height, err)
115+
if err := ssz.Unmarshal(blobData, &sidecars); err != nil {
116+
return nil, fmt.Errorf("failed to unmarshal blobs from Blob field at height %d: %w", height, err)
123117
}
124118

125119
return sidecars, nil
126120
}
127121

128-
// After BlobEnableHeight: blobs are in the Blob field
129-
if len(blobData) == 0 {
122+
// Otherwise, blobs are in Txs[1] if present
123+
if len(txs) <= 1 {
124+
return datypes.BlobSidecars{}, nil
125+
}
126+
127+
sidecarBz := txs[1]
128+
if len(sidecarBz) == 0 {
130129
return datypes.BlobSidecars{}, nil
131130
}
132131

133132
var sidecars datypes.BlobSidecars
134-
if err := ssz.Unmarshal(blobData, &sidecars); err != nil {
135-
return nil, fmt.Errorf("failed to unmarshal blobs from Blob field at height %d: %w", height, err)
133+
if err := ssz.Unmarshal(sidecarBz, &sidecars); err != nil {
134+
return nil, fmt.Errorf("failed to unmarshal blobs from Txs[1] at height %d: %w", height, err)
136135
}
137136

138137
return sidecars, nil

consensus/cometbft/service/finalize_block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func (s *Service) finalizeBlock(
5151

5252
getBlobsFunc := func(cachedBlobData []byte) (datypes.BlobSidecars, error) {
5353
var sidecars datypes.BlobSidecars
54-
if s.chainSpec.BlobConsensusEnableHeight() <= 0 || req.Height < s.chainSpec.BlobConsensusEnableHeight() {
54+
if !s.chainSpec.IsBlobConsensusEnabledAtHeight(req.Height) {
5555
var err error
56-
sidecars, err = encoding.UnmarshalBlobSidecarsFromABCIRequest(req, s.chainSpec.BlobConsensusEnableHeight())
56+
sidecars, err = encoding.UnmarshalBlobSidecarsFromABCIRequest(req, s.chainSpec)
5757
if err != nil {
5858
return nil, fmt.Errorf("finalize block: failed parsing blobs from request: %w", err)
5959
}

consensus/cometbft/service/prepare_proposal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *Service) prepareProposal(
8484
return &cmtabci.PrepareProposalResponse{Txs: [][]byte{}}, nil
8585
}
8686

87-
if s.chainSpec.BlobConsensusEnableHeight() <= 0 || req.Height < s.chainSpec.BlobConsensusEnableHeight() {
87+
if !s.chainSpec.IsBlobConsensusEnabledAtHeight(req.Height) {
8888
// Before BlobEnableHeight: return blobs as second transaction
8989
return &cmtabci.PrepareProposalResponse{Txs: [][]byte{blkBz, sidecarsBz}}, nil
9090
}

consensus/cometbft/service/process_proposal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (s *Service) processProposal(
6262
)
6363

6464
// Reject proposal if blobs are present before they are allowed
65-
if s.chainSpec.BlobConsensusEnableHeight() > 0 && req.Height < s.chainSpec.BlobConsensusEnableHeight() {
65+
if s.chainSpec.BlobConsensusEnableHeight() > 0 && !s.chainSpec.IsBlobConsensusEnabledAtHeight(req.Height) {
6666
if len(req.Blob) > 0 {
6767
status := cmtabci.PROCESS_PROPOSAL_STATUS_REJECT
6868
s.logger.Error(

0 commit comments

Comments
 (0)