Skip to content

Commit

Permalink
Merge branch 'main' into replace-berachain-cosmos-sdk-point-local-repo-2
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Dec 19, 2024
2 parents 699fe17 + c08698e commit 19a0ab0
Show file tree
Hide file tree
Showing 157 changed files with 1,926 additions and 2,827 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ site/
contracts/cache
contracts/out
tmp/
cscope.files

##########
# Golang #
Expand Down
6 changes: 3 additions & 3 deletions beacon/blockchain/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
const defaultRetryInterval = 20 * time.Second

func (s *Service[
_, _, ConsensusBlockT, _, _, _, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, _, _, _, _, _,
]) depositFetcher(
ctx context.Context,
blockNum math.U64,
Expand All @@ -52,7 +52,7 @@ func (s *Service[
}

func (s *Service[
_, _, ConsensusBlockT, _, _, _, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, _, _, _, _, _,
]) fetchAndStoreDeposits(
ctx context.Context,
blockNum math.U64,
Expand Down Expand Up @@ -92,7 +92,7 @@ func (s *Service[
}

func (s *Service[
_, _, ConsensusBlockT, _, _, _, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, _, _, _, _, _,
]) depositCatchupFetcher(ctx context.Context) {
ticker := time.NewTicker(defaultRetryInterval)
defer ticker.Stop()
Expand Down
110 changes: 19 additions & 91 deletions beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ package blockchain

import (
"context"
"time"

payloadtime "github.com/berachain/beacon-kit/beacon/payload-time"
"github.com/berachain/beacon-kit/config/spec"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
engineprimitives "github.com/berachain/beacon-kit/engine-primitives/engine-primitives"
)

// sendPostBlockFCU sends a forkchoice update to the execution client.
// sendPostBlockFCU sends a forkchoice update to the execution client after a
// block is finalized.This function should only be used to notify
// the EL client of the new head and should not request optimistic builds, as:
// Optimistic clients already request builds in handleOptimisticPayloadBuild()
// Non-optimistic clients should never request optimistic builds.
func (s *Service[
_, _, ConsensusBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, BeaconStateT, _, _, _, _,
]) sendPostBlockFCU(
ctx context.Context,
st BeaconStateT,
Expand All @@ -46,95 +48,21 @@ func (s *Service[
return
}

if !s.shouldBuildOptimisticPayloads() && s.localBuilder.Enabled() {
s.sendNextFCUWithAttributes(ctx, st, blk, lph)
} else {
s.sendNextFCUWithoutAttributes(ctx, blk, lph)
}
}

// sendNextFCUWithAttributes sends a forkchoice update to the execution
// client with attributes.
func (s *Service[
_, _, ConsensusBlockT, _, _, BeaconStateT, _,
_, ExecutionPayloadHeaderT, _, _, _, _,
]) sendNextFCUWithAttributes(
ctx context.Context,
st BeaconStateT,
blk ConsensusBlockT,
lph ExecutionPayloadHeaderT,
) {
// Send a forkchoice update without payload attributes to notify
// EL of the new head.
beaconBlk := blk.GetBeaconBlock()

stCopy := st.Copy()
if _, err := s.stateProcessor.ProcessSlots(
stCopy, beaconBlk.GetSlot()+1,
); err != nil {
s.logger.Error(
"failed to process slots in non-optimistic payload",
"error", err,
)
return
}

nextPayloadTime := payloadtime.Next(
blk.GetConsensusTime(),
lph.GetTimestamp(),
true, // buildOptimistically
).Unwrap()

// We set timestamp check on Bartio for backward compatibility reasons
// TODO: drop this we drop other Bartio special cases.
if s.chainSpec.DepositEth1ChainID() == spec.BartioChainID {
nextPayloadTime = max(
//#nosec:G701
uint64(time.Now().Unix()+1),
uint64((lph.GetTimestamp() + 1)),
)
}

prevBlockRoot := beaconBlk.HashTreeRoot()
if _, err := s.localBuilder.RequestPayloadAsync(
ctx,
stCopy,
beaconBlk.GetSlot()+1,
nextPayloadTime,
prevBlockRoot,
lph.GetBlockHash(),
lph.GetParentHash(),
); err != nil {
s.logger.Error(
"failed to send forkchoice update with attributes in non-optimistic payload",
"error",
err,
)
}
}

// sendNextFCUWithoutAttributes sends a forkchoice update to the
// execution client without attributes.
func (s *Service[
_, _, ConsensusBlockT, _, _, _, _, _,
ExecutionPayloadHeaderT, _, _, _, PayloadAttributesT,
]) sendNextFCUWithoutAttributes(
ctx context.Context,
blk ConsensusBlockT,
lph ExecutionPayloadHeaderT,
) {
beaconBlk := blk.GetBeaconBlock()

if _, _, err := s.executionEngine.NotifyForkchoiceUpdate(
if _, _, err = s.executionEngine.NotifyForkchoiceUpdate(
ctx,
// TODO: Switch to New().
engineprimitives.
BuildForkchoiceUpdateRequestNoAttrs[PayloadAttributesT](
&engineprimitives.ForkchoiceStateV1{
HeadBlockHash: lph.GetBlockHash(),
SafeBlockHash: lph.GetParentHash(),
FinalizedBlockHash: lph.GetParentHash(),
},
s.chainSpec.ActiveForkVersionForSlot(beaconBlk.GetSlot()),
),
ctypes.
BuildForkchoiceUpdateRequestNoAttrs(
&engineprimitives.ForkchoiceStateV1{
HeadBlockHash: lph.GetBlockHash(),
SafeBlockHash: lph.GetParentHash(),
FinalizedBlockHash: lph.GetParentHash(),
},
s.chainSpec.ActiveForkVersionForSlot(beaconBlk.GetSlot()),
),
); err != nil {
s.logger.Error(
"failed to send forkchoice update without attributes",
Expand Down
8 changes: 4 additions & 4 deletions beacon/blockchain/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
)

func (s *Service[
_, _, ConsensusBlockT, BeaconBlockT, _, _,
_, _, _, GenesisT, ConsensusSidecarsT, BlobSidecarsT, _,
_, _, ConsensusBlockT, BeaconBlockT, _,
_, GenesisT, ConsensusSidecarsT, BlobSidecarsT,
]) FinalizeBlock(
ctx sdk.Context,
req *cmtabci.FinalizeBlockRequest,
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *Service[
// finalizeBeaconBlock receives an incoming beacon block, it first validates
// and then processes the block.
func (s *Service[
_, _, ConsensusBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, BeaconStateT, _, _, _, _,
]) finalizeBeaconBlock(
ctx context.Context,
st BeaconStateT,
Expand Down Expand Up @@ -148,7 +148,7 @@ func (s *Service[

// executeStateTransition runs the stf.
func (s *Service[
_, _, ConsensusBlockT, _, _, BeaconStateT, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, _, BeaconStateT, _, _, _, _,
]) executeStateTransition(
ctx context.Context,
st BeaconStateT,
Expand Down
2 changes: 1 addition & 1 deletion beacon/blockchain/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// ProcessGenesisData processes the genesis state and initializes the beacon
// state.
func (s *Service[
_, _, _, _, _, _, _, _, _, GenesisT, _, _, _,
_, _, _, _, _, _, GenesisT, _, _,
]) ProcessGenesisData(
ctx context.Context,
bytes []byte,
Expand Down
12 changes: 6 additions & 6 deletions beacon/blockchain/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// 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,7 +56,7 @@ 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,
Expand All @@ -82,8 +82,8 @@ func (s *Service[
// rejected the incoming block and it would be unsafe to use any
// information from it.
func (s *Service[
_, _, _, _, _, BeaconStateT, _, _, ExecutionPayloadHeaderT,
_, _, _, _,
_, _, _, _, BeaconStateT, _,
_, _, _,
]) rebuildPayloadForRejectedBlock(
ctx context.Context,
st BeaconStateT,
Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *Service[
// handleOptimisticPayloadBuild handles optimistically
// building for the next slot.
func (s *Service[
_, _, _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _,
_, _, _, BeaconBlockT, BeaconStateT, _, _, _, _,
]) handleOptimisticPayloadBuild(
ctx context.Context,
st BeaconStateT,
Expand All @@ -164,7 +164,7 @@ func (s *Service[

// optimisticPayloadBuild builds a payload for the next slot.
func (s *Service[
_, _, _, BeaconBlockT, _, BeaconStateT, _, _, _, _, _, _, _,
_, _, _, BeaconBlockT, BeaconStateT, _, _, _, _,
]) optimisticPayloadBuild(
ctx context.Context,
st BeaconStateT,
Expand Down
43 changes: 32 additions & 11 deletions beacon/blockchain/process_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

payloadtime "github.com/berachain/beacon-kit/beacon/payload-time"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/consensus/cometbft/service/encoding"
"github.com/berachain/beacon-kit/consensus/types"
engineerrors "github.com/berachain/beacon-kit/engine-primitives/errors"
Expand All @@ -45,8 +46,8 @@ const (
)

func (s *Service[
_, _, ConsensusBlockT, BeaconBlockT, _, _,
_, _, _, GenesisT, ConsensusSidecarsT, BlobSidecarsT, _,
_, _, ConsensusBlockT, BeaconBlockT, _,
_, GenesisT, ConsensusSidecarsT, BlobSidecarsT,
]) ProcessProposal(
ctx sdk.Context,
req *cmtabci.ProcessProposalRequest,
Expand All @@ -72,6 +73,13 @@ func (s *Service[
return createProcessProposalResponse(errors.WrapNonFatal(err))
}

// In theory, swapping the order of verification between the sidecars
// and the incoming block should not introduce any inconsistencies
// in the state on which the sidecar verification depends on (notably
// the currently active fork). ProcessProposal should only
// keep the state changes as candidates (which is what we do in
// VerifyIncomingBlock).

// Process the blob sidecars, if any
if !sidecars.IsNil() && sidecars.Len() > 0 {
var consensusSidecars *types.ConsensusSidecars[BlobSidecarsT]
Expand All @@ -83,13 +91,26 @@ func (s *Service[
s.logger.Info("Received incoming blob sidecars")

// TODO: Clean this up once we remove generics.
c := convertConsensusSidecars[
cs := convertConsensusSidecars[
ConsensusSidecarsT,
BlobSidecarsT,
](consensusSidecars)

// Get the sidecar verification function from the state processor
//nolint:govet // err shadowing
sidecarVerifierFn, err := s.stateProcessor.GetSidecarVerifierFn(
s.storageBackend.StateFromContext(ctx),
)
if err != nil {
s.logger.Error(
"an error incurred while calculating the sidecar verifier",
"reason", err,
)
return createProcessProposalResponse(errors.WrapNonFatal(err))
}

// Verify the blobs and ensure they match the local state.
err = s.blobProcessor.VerifySidecars(c)
err = s.blobProcessor.VerifySidecars(cs, sidecarVerifierFn)
if err != nil {
s.logger.Error(
"rejecting incoming blob sidecars",
Expand Down Expand Up @@ -129,8 +150,8 @@ func (s *Service[
// VerifyIncomingBlock verifies the state root of an incoming block
// and logs the process.
func (s *Service[
_, _, ConsensusBlockT, BeaconBlockT, _, _, _, _,
ExecutionPayloadHeaderT, _, _, _, _,
_, _, ConsensusBlockT, BeaconBlockT, _, _,
_, _, _,
]) VerifyIncomingBlock(
ctx context.Context,
beaconBlk BeaconBlockT,
Expand Down Expand Up @@ -182,7 +203,7 @@ func (s *Service[
)

if s.shouldBuildOptimisticPayloads() {
var lph ExecutionPayloadHeaderT
var lph *ctypes.ExecutionPayloadHeader
lph, err = preState.GetLatestExecutionPayloadHeader()
if err != nil {
return err
Expand All @@ -209,7 +230,7 @@ func (s *Service[
)

if s.shouldBuildOptimisticPayloads() {
var lph ExecutionPayloadHeaderT
var lph *ctypes.ExecutionPayloadHeader
lph, err = postState.GetLatestExecutionPayloadHeader()
if err != nil {
return err
Expand All @@ -232,8 +253,8 @@ func (s *Service[

// verifyStateRoot verifies the state root of an incoming block.
func (s *Service[
_, _, ConsensusBlockT, BeaconBlockT, _, BeaconStateT,
_, _, _, _, _, _, _,
_, _, ConsensusBlockT, BeaconBlockT, BeaconStateT,
_, _, _, _,
]) verifyStateRoot(
ctx context.Context,
st BeaconStateT,
Expand Down Expand Up @@ -272,7 +293,7 @@ func (s *Service[
// shouldBuildOptimisticPayloads returns true if optimistic
// payload builds are enabled.
func (s *Service[
_, _, _, _, _, _, _, _, _, _, _, _, _,
_, _, _, _, _, _, _, _, _,
]) shouldBuildOptimisticPayloads() bool {
return s.optimisticPayloadBuilds && s.localBuilder.Enabled()
}
Expand Down
8 changes: 4 additions & 4 deletions beacon/blockchain/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
package blockchain

import (
"github.com/berachain/beacon-kit/chain-spec/chain"
ctypes "github.com/berachain/beacon-kit/consensus-types/types"
"github.com/berachain/beacon-kit/primitives/common"
"github.com/berachain/beacon-kit/primitives/math"
)

func (s *Service[
_, _, ConsensusBlockT, BeaconBlockT, _, _, _, _, _, _, _, _, _,
_, _, ConsensusBlockT, BeaconBlockT, _, _, _, _, _,
]) processPruning(beaconBlk BeaconBlockT) error {
// prune availability store
start, end := availabilityPruneRangeFn(
Expand All @@ -49,7 +49,7 @@ func (s *Service[
return nil
}

func depositPruneRangeFn(deposits []*ctypes.Deposit, cs common.ChainSpec) (uint64, uint64) {
func depositPruneRangeFn(deposits []*ctypes.Deposit, cs chain.ChainSpec) (uint64, uint64) {
if len(deposits) == 0 || cs.MaxDepositsPerBlock() == 0 {
return 0, 0
}
Expand All @@ -65,7 +65,7 @@ func depositPruneRangeFn(deposits []*ctypes.Deposit, cs common.ChainSpec) (uint6

//nolint:unparam // this is ok
func availabilityPruneRangeFn(
slot uint64, cs common.ChainSpec) (uint64, uint64) {
slot uint64, cs chain.ChainSpec) (uint64, uint64) {
window := cs.MinEpochsForBlobsSidecarsRequest() * cs.SlotsPerEpoch()
if slot < window {
return 0, 0
Expand Down
Loading

0 comments on commit 19a0ab0

Please sign in to comment.