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(consensus, blockchain): set execution payload timestamp based on CometBFT timestamp #2095

Merged
merged 32 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
eea8ead
nits
abi87 Oct 23, 2024
49538dc
wip: extended sdk.Context use to middleware
abi87 Oct 23, 2024
b303196
dropped unnecessary types and file
abi87 Oct 23, 2024
847cd52
some more types cleanup
abi87 Oct 23, 2024
020cb11
some more types cleanup
abi87 Oct 23, 2024
92bbfae
added type for BeaconBlock with relevant consensus data
abi87 Oct 23, 2024
69efa86
nits
abi87 Oct 23, 2024
8b87b65
wip: adding consensus data to blocks to be verified
abi87 Oct 23, 2024
be54ae7
wip: added consensus block to blockchain service
abi87 Oct 23, 2024
196f1ec
wip: used consensus block type in blockchain service
abi87 Oct 23, 2024
55126cb
wip: fixed dispatching of consensus blocks
abi87 Oct 23, 2024
ce0d420
wip: rebased next payload timestamp on top of consensus block time
abi87 Oct 23, 2024
49b31a8
wip: rebased next payload timestamp from finalized block on top of co…
abi87 Oct 23, 2024
7975424
nit
abi87 Oct 23, 2024
70bba14
wip: fixed dispatching of finalized beacon blocks
abi87 Oct 23, 2024
0365941
wip: rebased next payload timestamp for building blocks on top of con…
abi87 Oct 23, 2024
da314f1
nits
abi87 Oct 23, 2024
36041e8
fixed consensus time setting
abi87 Oct 23, 2024
13a93ea
improved SlotData encapsulation
abi87 Oct 24, 2024
f13cdf7
Merge branch 'main' into consensus_decorated_events
abi87 Oct 24, 2024
0c6e82a
nit
abi87 Oct 24, 2024
20c881e
dropped minor code duplication
abi87 Oct 24, 2024
6b106b8
fix(blockchain): Further timestamp simplification (#2097)
abi87 Oct 24, 2024
e447c20
fix(state-transition): Validate execution payload timestamp (#2096)
abi87 Oct 24, 2024
cbc89a4
Merge branch 'main' into consensus_decorated_events
abi87 Oct 25, 2024
699df26
fix(consensus, beacon): let consensus tell SuggestedNextPayloadTimest…
abi87 Oct 25, 2024
6c256d4
fixed comment
abi87 Oct 25, 2024
f386d51
nit to appease rabbit + revert faulty change
abi87 Oct 25, 2024
e66ef6c
hardened check
abi87 Oct 25, 2024
0c0b3d5
Merge branch 'main' into consensus_decorated_events
abi87 Oct 31, 2024
8ca08fb
Merge branch 'main' into consensus_decorated_events
abi87 Nov 6, 2024
a8f2e07
Merge branch 'main' into consensus_decorated_events
abi87 Nov 6, 2024
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
8 changes: 5 additions & 3 deletions beacond/cmd/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func DefaultComponents() []any {
*BeaconBlockHeader, *BlobSidecar, *BlobSidecars,
],
components.ProvideChainService[
*AvailabilityStore, *BeaconBlock, *BeaconBlockBody,
*AvailabilityStore,
*ConsensusBlock, *BeaconBlock, *BeaconBlockBody,
*BeaconBlockHeader, *BeaconState, *BeaconStateMarshallable,
*BlobSidecars, *BlockStore, *Deposit, *DepositStore,
*ExecutionPayload, *ExecutionPayloadHeader, *Genesis,
Expand Down Expand Up @@ -91,7 +92,7 @@ func DefaultComponents() []any {
],
components.ProvideDepositStore[*Deposit],
components.ProvideDispatcher[
*BeaconBlock, *BlobSidecars, *Genesis, *Logger,
*ConsensusBlock, *BeaconBlock, *BlobSidecars, *Genesis, *Logger,
],
components.ProvideEngineClient[
*ExecutionPayload, *ExecutionPayloadHeader, *Logger,
Expand All @@ -107,7 +108,8 @@ func DefaultComponents() []any {
components.ProvideReportingService[*Logger],
components.ProvideCometBFTService[*Logger],
components.ProvideServiceRegistry[
*AvailabilityStore, *BeaconBlock, *BeaconBlockBody,
*AvailabilityStore,
*ConsensusBlock, *BeaconBlock, *BeaconBlockBody,
*BeaconBlockHeader, *BlockStore, *BeaconState,
*BeaconStateMarshallable, *BlobSidecar, *BlobSidecars,
*Deposit, *DepositStore, *ExecutionPayload, *ExecutionPayloadHeader,
Expand Down
14 changes: 2 additions & 12 deletions beacond/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/berachain/beacon-kit/mod/beacon/validator"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
cometbft "github.com/berachain/beacon-kit/mod/consensus/pkg/cometbft/service"
"github.com/berachain/beacon-kit/mod/consensus/pkg/cometbft/service/middleware"
consruntimetypes "github.com/berachain/beacon-kit/mod/consensus/pkg/types"
dablob "github.com/berachain/beacon-kit/mod/da/pkg/blob"
"github.com/berachain/beacon-kit/mod/da/pkg/da"
Expand Down Expand Up @@ -63,14 +62,6 @@ import (
/* -------------------------------------------------------------------------- */

type (
// ABCIMiddleware is a type alias for the ABCIMiddleware.
ABCIMiddleware = middleware.ABCIMiddleware[
*BeaconBlock,
*BlobSidecars,
*Genesis,
*SlotData,
]

Comment on lines -66 to -73
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe unnecessary once Middleware has been encapsulated in consensus service

// AttributesFactory is a type alias for the attributes factory.
AttributesFactory = attributes.Factory[
*BeaconState,
Expand Down Expand Up @@ -100,6 +91,7 @@ type (
// ChainService is a type alias for the chain service.
ChainService = blockchain.Service[
*AvailabilityStore,
*ConsensusBlock,
*BeaconBlock,
*BeaconBlockBody,
*BeaconBlockHeader,
Expand All @@ -114,9 +106,6 @@ type (
// CometBFTService is a type alias for the CometBFT service.
CometBFTService = cometbft.Service[*Logger]

// ConsensusMiddleware is a type alias for the consensus middleware.
ConsensusMiddleware = cometbft.MiddlewareI

Comment on lines -110 to -112
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe unnecessary once Middleware has been encapsulated in consensus service

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

Expand Down Expand Up @@ -245,6 +234,7 @@ type (
AvailabilityStore = dastore.Store[*BeaconBlockBody]

// BeaconBlock type aliases.
ConsensusBlock = consruntimetypes.ConsensusBlock[*BeaconBlock]
BeaconBlock = types.BeaconBlock
BeaconBlockBody = types.BeaconBlockBody
BeaconBlockHeader = types.BeaconBlockHeader
Expand Down
33 changes: 17 additions & 16 deletions mod/beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ package blockchain
import (
"context"

payloadtime "github.com/berachain/beacon-kit/mod/beacon/payload-time"
engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives"
)

// sendPostBlockFCU sends a forkchoice update to the execution client.
func (s *Service[
_, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
_, ConsensusBlockT, _, _, _, BeaconStateT, _, _, _, _, _,
]) sendPostBlockFCU(
ctx context.Context,
st BeaconStateT,
blk BeaconBlockT,
blk ConsensusBlockT,
) {
lph, err := st.GetLatestExecutionPayloadHeader()
if err != nil {
Expand All @@ -54,18 +53,19 @@ func (s *Service[
// sendNextFCUWithAttributes sends a forkchoice update to the execution
// client with attributes.
func (s *Service[
_, BeaconBlockT, _, _, BeaconStateT,
_, ConsensusBlockT, _, _, _, BeaconStateT,
_, _, ExecutionPayloadHeaderT, _, _,
]) sendNextFCUWithAttributes(
ctx context.Context,
st BeaconStateT,
blk BeaconBlockT,
blk ConsensusBlockT,
lph ExecutionPayloadHeaderT,
) {
var err error
beaconBlk := blk.GetBeaconBlock()

stCopy := st.Copy()
if _, err = s.stateProcessor.ProcessSlots(
stCopy, blk.GetSlot()+1,
if _, err := s.stateProcessor.ProcessSlots(
stCopy, beaconBlk.GetSlot()+1,
abi87 marked this conversation as resolved.
Show resolved Hide resolved
); err != nil {
s.logger.Error(
"failed to process slots in non-optimistic payload",
Expand All @@ -74,13 +74,12 @@ func (s *Service[
return
}

prevBlockRoot := blk.HashTreeRoot()
payloadTime := blk.GetBody().GetExecutionPayload().GetTimestamp()
if _, err = s.localBuilder.RequestPayloadAsync(
prevBlockRoot := beaconBlk.HashTreeRoot()
if _, err := s.localBuilder.RequestPayloadAsync(
ctx,
stCopy,
blk.GetSlot()+1,
payloadtime.Next(s.chainSpec, payloadTime),
beaconBlk.GetSlot()+1,
blk.GetNextPayloadTimestamp().Unwrap(),
prevBlockRoot,
lph.GetBlockHash(),
lph.GetParentHash(),
Expand All @@ -96,13 +95,15 @@ func (s *Service[
// sendNextFCUWithoutAttributes sends a forkchoice update to the
// execution client without attributes.
func (s *Service[
_, BeaconBlockT, _, _, _, _, _,
_, ConsensusBlockT, _, _, _, _, _, _,
ExecutionPayloadHeaderT, _, PayloadAttributesT,
]) sendNextFCUWithoutAttributes(
ctx context.Context,
blk BeaconBlockT,
blk ConsensusBlockT,
lph ExecutionPayloadHeaderT,
) {
beaconBlk := blk.GetBeaconBlock()

if _, _, err := s.executionEngine.NotifyForkchoiceUpdate(
ctx,
// TODO: Switch to New().
Expand All @@ -113,7 +114,7 @@ func (s *Service[
SafeBlockHash: lph.GetParentHash(),
FinalizedBlockHash: lph.GetParentHash(),
},
s.chainSpec.ActiveForkVersionForSlot(blk.GetSlot()),
s.chainSpec.ActiveForkVersionForSlot(beaconBlk.GetSlot()),
),
); err != nil {
s.logger.Error(
Expand Down
33 changes: 23 additions & 10 deletions mod/beacon/blockchain/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ package blockchain
import (
"context"

payloadtime "github.com/berachain/beacon-kit/mod/beacon/payload-time"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

// forceStartupHead sends a force head FCU to the execution client.
func (s *Service[
_, _, _, _, BeaconStateT, _, _, _, _, _,
_, _, _, _, _, BeaconStateT, _, _, _, _, _,
]) forceStartupHead(
ctx context.Context,
st BeaconStateT,
Expand Down Expand Up @@ -56,12 +56,17 @@ func (s *Service[
// handleRebuildPayloadForRejectedBlock handles the case where the incoming
// block was rejected and we need to rebuild the payload for the current slot.
func (s *Service[
_, _, _, _, BeaconStateT, _, _, _, _, _,
_, _, _, _, _, BeaconStateT, _, _, _, _, _,
]) handleRebuildPayloadForRejectedBlock(
ctx context.Context,
st BeaconStateT,
nextPayloadTimestamp math.U64,
) {
if err := s.rebuildPayloadForRejectedBlock(ctx, st); err != nil {
if err := s.rebuildPayloadForRejectedBlock(
ctx,
st,
nextPayloadTimestamp,
); err != nil {
s.logger.Error(
"failed to rebuild payload for nil block",
"error", err,
Expand All @@ -77,10 +82,11 @@ func (s *Service[
// rejected the incoming block and it would be unsafe to use any
// information from it.
func (s *Service[
_, _, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT, _, _,
_, _, _, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT, _, _,
]) rebuildPayloadForRejectedBlock(
ctx context.Context,
st BeaconStateT,
nextPayloadTimestamp math.U64,
) error {
s.logger.Info("Rebuilding payload for rejected block ⏳ ")

Expand Down Expand Up @@ -114,7 +120,7 @@ func (s *Service[
st,
// We are rebuilding for the current slot.
stateSlot,
payloadtime.Next(s.chainSpec, lph.GetTimestamp()),
nextPayloadTimestamp.Unwrap(),
// We set the parent root to the previous block root.
latestHeader.HashTreeRoot(),
// We set the head of our chain to the previous finalized block.
Expand All @@ -134,13 +140,19 @@ func (s *Service[
// handleOptimisticPayloadBuild handles optimistically
// building for the next slot.
func (s *Service[
_, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
_, _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
]) handleOptimisticPayloadBuild(
ctx context.Context,
st BeaconStateT,
blk BeaconBlockT,
nextPayloadTimestamp math.U64,
) {
if err := s.optimisticPayloadBuild(ctx, st, blk); err != nil {
if err := s.optimisticPayloadBuild(
ctx,
st,
blk,
nextPayloadTimestamp,
); err != nil {
s.logger.Error(
"Failed to build optimistic payload",
"for_slot", (blk.GetSlot() + 1).Base10(),
Expand All @@ -151,11 +163,12 @@ func (s *Service[

// optimisticPayloadBuild builds a payload for the next slot.
func (s *Service[
_, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
_, _, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
]) optimisticPayloadBuild(
ctx context.Context,
st BeaconStateT,
blk BeaconBlockT,
nextPayloadTimestamp math.U64,
) error {
// We are building for the next slot, so we increment the slot relative
// to the block we just processed.
Expand All @@ -176,7 +189,7 @@ func (s *Service[
if _, err := s.localBuilder.RequestPayloadAsync(
ctx, st,
slot,
payloadtime.Next(s.chainSpec, payload.GetTimestamp()),
nextPayloadTimestamp.Unwrap(),
// The previous block root is simply the root of the block we just
// processed.
blk.HashTreeRoot(),
Expand Down
22 changes: 13 additions & 9 deletions mod/beacon/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// ProcessGenesisData processes the genesis state and initializes the beacon
// state.
func (s *Service[
_, _, _, _, _, _, _, _, GenesisT, _,
_, _, _, _, _, _, _, _, _, GenesisT, _,
]) ProcessGenesisData(
ctx context.Context,
genesisData GenesisT,
Expand All @@ -47,13 +47,15 @@ func (s *Service[
// ProcessBeaconBlock receives an incoming beacon block, it first validates
// and then processes the block.
func (s *Service[
_, BeaconBlockT, _, _, _, _, _, _, _, _,
_, ConsensusBlockT, _, _, _, _, _, _, _, _, _,
]) ProcessBeaconBlock(
ctx context.Context,
blk BeaconBlockT,
blk ConsensusBlockT,
) (transition.ValidatorUpdates, error) {
beaconBlk := blk.GetBeaconBlock()

abi87 marked this conversation as resolved.
Show resolved Hide resolved
// If the block is nil, exit early.
if blk.IsNil() {
if beaconBlk.IsNil() {
return nil, ErrNilBlk
}

Expand All @@ -67,7 +69,7 @@ func (s *Service[
// return an error. It is safe to use the slot off of the beacon block
// since it has been verified as correct already.
if !s.storageBackend.AvailabilityStore().IsDataAvailable(
ctx, blk.GetSlot(), blk.GetBody(),
ctx, beaconBlk.GetSlot(), beaconBlk.GetBody(),
) {
return nil, ErrDataNotAvailable
}
Expand All @@ -79,7 +81,7 @@ func (s *Service[
// via ticker later.
if err = s.dispatcher.Publish(
async.NewEvent(
ctx, async.BeaconBlockFinalized, blk,
ctx, async.BeaconBlockFinalized, beaconBlk,
),
); err != nil {
return nil, err
Expand All @@ -92,11 +94,11 @@ func (s *Service[

// executeStateTransition runs the stf.
func (s *Service[
_, BeaconBlockT, _, _, BeaconStateT, _, _, _, _, _,
_, ConsensusBlockT, _, _, _, BeaconStateT, _, _, _, _, _,
]) executeStateTransition(
ctx context.Context,
st BeaconStateT,
blk BeaconBlockT,
blk ConsensusBlockT,
) (transition.ValidatorUpdates, error) {
startTime := time.Now()
defer s.metrics.measureStateTransitionDuration(startTime)
Expand Down Expand Up @@ -125,9 +127,11 @@ func (s *Service[
// the "verification aspect" of this NewPayload call is
// actually irrelevant at this point.
SkipPayloadVerification: false,

NextPayloadTimestamp: blk.GetNextPayloadTimestamp(),
},
st,
blk,
blk.GetBeaconBlock(),
)
return valUpdates, err
}
Loading
Loading