Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(beacon): Hardened sidecars validation #2108

Open
wants to merge 18 commits into
base: harden-proposer-validation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions beacond/cmd/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ func DefaultComponents() []any {
components.ProvideBlsSigner,
components.ProvideBlobProcessor[
*AvailabilityStore, *BeaconBlockBody, *BeaconBlockHeader,
*BlobSidecar, *BlobSidecars, *Logger,
*ConsensusSidecars, *BlobSidecar, *BlobSidecars, *Logger,
],
components.ProvideBlobProofVerifier,
components.ProvideBlobVerifier[
*BeaconBlockHeader, *BlobSidecar, *BlobSidecars,
],
components.ProvideChainService[
*AvailabilityStore,
*ConsensusBlock, *BeaconBlock, *BeaconBlockBody,
Expand All @@ -77,8 +74,8 @@ func DefaultComponents() []any {
// *BeaconStateMarshallable, *BlockStore, *KVStore, *StorageBackend,
// ],
components.ProvideDAService[
*AvailabilityStore, *BeaconBlockBody, *BlobSidecar,
*BlobSidecars, *Logger,
*AvailabilityStore, *BeaconBlockBody, *BeaconBlockHeader,
*ConsensusSidecars, *BlobSidecar, *BlobSidecars, *Logger,
],
components.ProvideDBManager[*AvailabilityStore, *DepositStore, *Logger],
components.ProvideDepositPruner[
Expand All @@ -92,7 +89,9 @@ func DefaultComponents() []any {
],
components.ProvideDepositStore[*Deposit],
components.ProvideDispatcher[
*ConsensusBlock, *BeaconBlock, *BlobSidecars, *Genesis, *Logger,
*ConsensusBlock, *BeaconBlock,
*ConsensusSidecars, *BlobSidecars,
*Genesis, *Logger,
],
components.ProvideEngineClient[
*ExecutionPayload, *ExecutionPayloadHeader, *Logger,
Expand All @@ -111,7 +110,8 @@ func DefaultComponents() []any {
*AvailabilityStore,
*ConsensusBlock, *BeaconBlock, *BeaconBlockBody,
*BeaconBlockHeader, *BlockStore, *BeaconState,
*BeaconStateMarshallable, *BlobSidecar, *BlobSidecars,
*BeaconStateMarshallable,
*ConsensusSidecars, *BlobSidecar, *BlobSidecars,
*Deposit, *DepositStore, *ExecutionPayload, *ExecutionPayloadHeader,
*Genesis, *KVStore, *Logger,
NodeAPIContext,
Expand Down
23 changes: 11 additions & 12 deletions beacond/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ type (
*AvailabilityStore,
*BeaconBlockBody,
*BeaconBlockHeader,
*BlobSidecar,
*BlobSidecars,
]

// BlobVerifier is a type alias for the blob verifier.
BlobVerifier = dablob.Verifier[
*BeaconBlockHeader,
*ConsensusSidecars,
*BlobSidecar,
*BlobSidecars,
]
Expand All @@ -107,7 +101,10 @@ type (
CometBFTService = cometbft.Service[*Logger]

// DAService is a type alias for the DA service.
DAService = da.Service[*AvailabilityStore, *BlobSidecars]
DAService = da.Service[
*AvailabilityStore,
*ConsensusSidecars, *BlobSidecars, *BeaconBlockHeader,
]

// DBManager is a type alias for the database manager.
DBManager = manager.DBManager
Expand Down Expand Up @@ -267,10 +264,12 @@ type (
Validator,
]

// BlobSidecar is a type alias for the blob sidecar.
BlobSidecar = datypes.BlobSidecar

// BlobSidecars is a type alias for the blob sidecars.
// BlobSidecars type aliases.
ConsensusSidecars = consruntimetypes.ConsensusSidecars[
*BlobSidecars,
*BeaconBlockHeader,
]
BlobSidecar = datypes.BlobSidecar
BlobSidecars = datypes.BlobSidecars

// BlockStore is a type alias for the block store.
Expand Down
59 changes: 33 additions & 26 deletions mod/consensus/pkg/cometbft/service/middleware/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ import (
/* -------------------------------------------------------------------------- */

// InitGenesis is called by the base app to initialize the state of the.
func (h *ABCIMiddleware[
_, _, GenesisT, _,
]) InitGenesis(
func (h *ABCIMiddleware[_, _, _, GenesisT, _]) InitGenesis(
ctx sdk.Context,
bz []byte,
) (transition.ValidatorUpdates, error) {
Expand All @@ -65,9 +63,7 @@ func (h *ABCIMiddleware[

// waitForGenesisProcessed waits until the genesis data has been processed and
// returns the validator updates, or err if the context is cancelled.
func (h *ABCIMiddleware[
_, _, _, _,
]) waitForGenesisProcessed(
func (h *ABCIMiddleware[_, _, _, _, _]) waitForGenesisProcessed(
ctx context.Context,
) (transition.ValidatorUpdates, error) {
select {
Expand All @@ -84,8 +80,7 @@ func (h *ABCIMiddleware[

// prepareProposal is the internal handler for preparing proposals.
func (h *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, _, SlotDataT,
]) PrepareProposal(
BeaconBlockT, _, BlobSidecarsT, _, SlotDataT]) PrepareProposal(
ctx sdk.Context,
slotData SlotDataT,
) ([]byte, []byte, error) {
Expand Down Expand Up @@ -133,7 +128,7 @@ func (h *ABCIMiddleware[

// waitForBuiltBeaconBlock waits for the built beacon block to be received.
func (h *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, _, SlotDataT,
BeaconBlockT, _, BlobSidecarsT, _, SlotDataT,
]) waitForBuiltBeaconBlock(
ctx context.Context,
) (BeaconBlockT, error) {
Expand All @@ -147,7 +142,7 @@ func (h *ABCIMiddleware[

// waitForBuiltSidecars waits for the built sidecars to be received.
func (h *ABCIMiddleware[
_, BlobSidecarsT, _, _,
_, _, BlobSidecarsT, _, _,
]) waitForBuiltSidecars(
ctx context.Context,
) (BlobSidecarsT, error) {
Expand All @@ -162,7 +157,7 @@ func (h *ABCIMiddleware[
// handleBuiltBeaconBlockAndSidecars gossips the built beacon block and blob
// sidecars to the network.
func (h *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, _, _,
BeaconBlockT, _, BlobSidecarsT, _, _,
]) handleBuiltBeaconBlockAndSidecars(
bb BeaconBlockT,
sc BlobSidecarsT,
Expand All @@ -185,7 +180,7 @@ func (h *ABCIMiddleware[
// ProcessProposal processes the proposal for the ABCI middleware.
// It handles both the beacon block and blob sidecars concurrently.
func (h *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, _, _,
BeaconBlockT, BeaconBlockHeaderT, BlobSidecarsT, _, _,
]) ProcessProposal(
ctx sdk.Context,
req *cmtabci.ProcessProposalRequest,
Expand All @@ -209,7 +204,7 @@ func (h *ABCIMiddleware[

defer h.metrics.measureProcessProposalDuration(startTime)

// Request the beacon block.
// Decode the beacon block.
blk, err := encoding.
UnmarshalBeaconBlockFromABCIRequest[BeaconBlockT](
req,
Expand All @@ -221,18 +216,18 @@ func (h *ABCIMiddleware[
}

// notify that the beacon block has been received.
var enrichedBlk *types.ConsensusBlock[BeaconBlockT]
enrichedBlk = enrichedBlk.New(
var consensusBlk *types.ConsensusBlock[BeaconBlockT]
consensusBlk = consensusBlk.New(
blk,
req.GetProposerAddress(),
req.GetTime().Add(h.minPayloadDelay),
)
blkEvent := async.NewEvent(ctx, async.BeaconBlockReceived, enrichedBlk)
blkEvent := async.NewEvent(ctx, async.BeaconBlockReceived, consensusBlk)
if err = h.dispatcher.Publish(blkEvent); err != nil {
return h.createProcessProposalResponse(errors.WrapNonFatal(err))
}

// Request the blob sidecars.
// Decode the blob sidecars.
sidecars, err := encoding.
UnmarshalBlobSidecarsFromABCIRequest[BlobSidecarsT](
req,
Expand All @@ -243,7 +238,15 @@ func (h *ABCIMiddleware[
}

// notify that the sidecars have been received.
blobEvent := async.NewEvent(ctx, async.SidecarsReceived, sidecars)
var consensusSidecars *types.ConsensusSidecars[
BlobSidecarsT,
BeaconBlockHeaderT,
]
consensusSidecars = consensusSidecars.New(
sidecars,
blk.GetHeader(),
)
blobEvent := async.NewEvent(ctx, async.SidecarsReceived, consensusSidecars)
if err = h.dispatcher.Publish(blobEvent); err != nil {
return h.createProcessProposalResponse(errors.WrapNonFatal(err))
}
Expand All @@ -263,7 +266,7 @@ func (h *ABCIMiddleware[
// waitForBeaconBlockVerification waits for the built beacon block to be
// verified.
func (h *ABCIMiddleware[
BeaconBlockT, _, _, _,
BeaconBlockT, _, _, _, _,
]) waitForBeaconBlockVerification(
ctx context.Context,
) (BeaconBlockT, error) {
Expand All @@ -277,7 +280,7 @@ func (h *ABCIMiddleware[

// waitForSidecarVerification waits for the built sidecars to be verified.
func (h *ABCIMiddleware[
_, BlobSidecarsT, _, _,
_, _, BlobSidecarsT, _, _,
]) waitForSidecarVerification(
ctx context.Context,
) (BlobSidecarsT, error) {
Expand All @@ -292,7 +295,7 @@ func (h *ABCIMiddleware[
// createResponse generates the appropriate ProcessProposalResponse based on the
// error.
func (*ABCIMiddleware[
BeaconBlockT, _, BlobSidecarsT, _,
BeaconBlockT, _, _, BlobSidecarsT, _,
]) createProcessProposalResponse(
err error,
) (*cmtabci.ProcessProposalResponse, error) {
Expand All @@ -310,7 +313,7 @@ func (*ABCIMiddleware[

// EndBlock returns the validator set updates from the beacon state.
func (h *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, _, _,
BeaconBlockT, _, BlobSidecarsT, _, _,
]) FinalizeBlock(
ctx sdk.Context,
req *cmtabci.FinalizeBlockRequest,
Expand Down Expand Up @@ -339,13 +342,17 @@ func (h *ABCIMiddleware[

// notify that the final beacon block has been received.
// notify that the beacon block has been received.
var enrichedBlk *types.ConsensusBlock[BeaconBlockT]
enrichedBlk = enrichedBlk.New(
var consensusBlk *types.ConsensusBlock[BeaconBlockT]
consensusBlk = consensusBlk.New(
blk,
req.GetProposerAddress(),
req.GetTime().Add(h.minPayloadDelay),
)
blkEvent := async.NewEvent(ctx, async.FinalBeaconBlockReceived, enrichedBlk)
blkEvent := async.NewEvent(
ctx,
async.FinalBeaconBlockReceived,
consensusBlk,
)
if err = h.dispatcher.Publish(blkEvent); err != nil {
return nil, err
}
Expand All @@ -364,7 +371,7 @@ func (h *ABCIMiddleware[
// waitForFinalValidatorUpdates waits for the final validator updates to be
// received.
func (h *ABCIMiddleware[
_, _, _, _,
_, _, _, _, _,
]) waitForFinalValidatorUpdates(
ctx context.Context,
) (transition.ValidatorUpdates, error) {
Expand Down
16 changes: 8 additions & 8 deletions mod/consensus/pkg/cometbft/service/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import (

// ABCIMiddleware is a middleware between ABCI and the validator logic.
type ABCIMiddleware[
BeaconBlockT BeaconBlock[BeaconBlockT],
BeaconBlockT BeaconBlock[BeaconBlockT, BeaconBlockHeaderT],
BeaconBlockHeaderT any,
BlobSidecarsT BlobSidecars[BlobSidecarsT],
GenesisT json.Unmarshaler,
SlotDataT any,
Expand Down Expand Up @@ -67,7 +68,8 @@ type ABCIMiddleware[

// NewABCIMiddleware creates a new instance of the Handler struct.
func NewABCIMiddleware[
BeaconBlockT BeaconBlock[BeaconBlockT],
BeaconBlockT BeaconBlock[BeaconBlockT, BeaconBlockHeaderT],
BeaconBlockHeaderT any,
BlobSidecarsT BlobSidecars[BlobSidecarsT],
GenesisT json.Unmarshaler,
SlotDataT any,
Expand All @@ -78,7 +80,7 @@ func NewABCIMiddleware[
logger log.Logger,
telemetrySink TelemetrySink,
) *ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, GenesisT, SlotDataT,
BeaconBlockT, BeaconBlockHeaderT, BlobSidecarsT, GenesisT, SlotDataT,
] {
// We may build execution payload optimistically (i.e. build the execution
// payload for next block while current block is being verified and not yet
Expand All @@ -100,7 +102,7 @@ func NewABCIMiddleware[
)

return &ABCIMiddleware[
BeaconBlockT, BlobSidecarsT, GenesisT, SlotDataT,
BeaconBlockT, BeaconBlockHeaderT, BlobSidecarsT, GenesisT, SlotDataT,
]{
chainSpec: chainSpec,
minPayloadDelay: minPayloadDelay,
Expand All @@ -117,7 +119,7 @@ func NewABCIMiddleware[
}

// Start subscribes the middleware to the events it needs to listen for.
func (am *ABCIMiddleware[_, _, _, _]) Start(
func (am *ABCIMiddleware[_, _, _, _, _]) Start(
_ context.Context,
) error {
var err error
Expand Down Expand Up @@ -155,8 +157,6 @@ func (am *ABCIMiddleware[_, _, _, _]) Start(
}

// Name returns the name of the middleware.
func (am *ABCIMiddleware[
_, _, _, _,
]) Name() string {
func (am *ABCIMiddleware[_, _, _, _, _]) Name() string {
return "abci-middleware"
}
8 changes: 5 additions & 3 deletions mod/consensus/pkg/cometbft/service/middleware/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ import (
)

// BeaconBlock is an interface for accessing the beacon block.
type BeaconBlock[SelfT any] interface {
type BeaconBlock[BeaconBlockT any, BeaconBlockHeaderT any] interface {
constraints.SSZMarshallable
constraints.Nillable
constraints.Empty[SelfT]
NewFromSSZ([]byte, uint32) (SelfT, error)
constraints.Empty[BeaconBlockT]
NewFromSSZ([]byte, uint32) (BeaconBlockT, error)

GetHeader() BeaconBlockHeaderT
}

// TelemetrySink is an interface for sending metrics to a telemetry backend.
Expand Down
Loading
Loading