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

Caplin/E3: pre-execute without blobs #12924

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions cl/beacon/handler/block_production.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func (a *ApiHandler) GetEthV1ValidatorAttestationData(
if err != nil {
return nil, beaconhttp.NewEndpointError(http.StatusBadRequest, err)
}
start := time.Now()

// wait until the head state is at the target slot or later
err = a.waitUntilHeadStateAtEpochIsReadyOrCountAsMissed(r.Context(), a.syncedData, *slot/a.beaconChainCfg.SlotsPerEpoch)
if err != nil {
Expand Down Expand Up @@ -179,6 +181,13 @@ func (a *ApiHandler) GetEthV1ValidatorAttestationData(
if err != nil {
log.Warn("Failed to get attestation data", "err", err)
}

defer func() {
a.logger.Debug("Produced Attestation", "slot", *slot,
"committee_index", *committeeIndex, "cached", ok, "beacon_block_root",
attestationData.BeaconBlockRoot, "duration", time.Since(start))
}()

if ok {
return newBeaconResponse(attestationData), nil
}
Expand Down
55 changes: 36 additions & 19 deletions cl/phase1/forkchoice/fork_graph/fork_graph_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ type forkGraphDisk struct {
beaconCfg *clparams.BeaconChainConfig
genesisTime uint64
// highest block seen
highestSeen, anchorSlot uint64
lowestAvailableBlock atomic.Uint64
anchorSlot uint64
lowestAvailableBlock atomic.Uint64

newestLightClientUpdate atomic.Value
// the lightclientUpdates leaks memory, but it's not a big deal since new data is added every 27 hours.
Expand Down Expand Up @@ -169,8 +169,13 @@ func (f *forkGraphDisk) AnchorSlot() uint64 {
return f.anchorSlot
}

func (f *forkGraphDisk) isBlockRootTheCurrentState(blockRoot libcommon.Hash) bool {
blockRootState, _ := f.currentState.BlockRoot()
return blockRoot == blockRootState
}

// Add a new node and edge to the graph
func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, fullValidation bool) (*state.CachingBeaconState, ChainSegmentInsertionResult, error) {
func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, fullValidation, shallowImport bool) (*state.CachingBeaconState, ChainSegmentInsertionResult, error) {
block := signedBlock.Block
blockRoot, err := block.HashSSZ()
if err != nil {
Expand All @@ -187,10 +192,17 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,
return nil, BelowAnchor, nil
}

newState, err := f.getState(block.ParentRoot, false, true)
if err != nil {
return nil, LogisticError, fmt.Errorf("AddChainSegment: %w, parentRoot; %x", err, block.ParentRoot)
isBlockRootTheCurrentState := f.isBlockRootTheCurrentState(blockRoot)
var newState *state.CachingBeaconState
if isBlockRootTheCurrentState {
newState = f.currentState
} else {
newState, err = f.getState(block.ParentRoot, false, true)
if err != nil {
return nil, LogisticError, fmt.Errorf("AddChainSegment: %w, parentRoot; %x", err, block.ParentRoot)
}
}

if newState == nil {
log.Trace("AddChainSegment: missing segment", "block", libcommon.Hash(blockRoot))
return nil, MissingSegment, nil
Expand All @@ -199,7 +211,7 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,
parentBlock, hasParentBlock := f.getBlock(block.ParentRoot)

// Before processing the state: update the newest lightclient update.
if block.Version() >= clparams.AltairVersion && hasParentBlock && fullValidation && hasFinalized && f.rcfg.Beacon {
if block.Version() >= clparams.AltairVersion && hasParentBlock && fullValidation && hasFinalized && f.rcfg.Beacon && !isBlockRootTheCurrentState {
nextSyncCommitteeBranch, err := newState.NextSyncCommitteeBranch()
if err != nil {
return nil, LogisticError, err
Expand Down Expand Up @@ -244,14 +256,22 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,

blockRewardsCollector := &eth2.BlockRewardsCollector{}

// Execute the state
if invalidBlockErr := transition.TransitionState(newState, signedBlock, blockRewardsCollector, fullValidation); invalidBlockErr != nil {
// Add block to list of invalid blocks
log.Warn("Invalid beacon block", "slot", block.Slot, "blockRoot", blockRoot, "reason", invalidBlockErr)
f.badBlocks.Store(libcommon.Hash(blockRoot), struct{}{})
//f.currentState = nil
if !isBlockRootTheCurrentState {
// Execute the state
if invalidBlockErr := transition.TransitionState(newState, signedBlock, blockRewardsCollector, fullValidation); invalidBlockErr != nil {
// Add block to list of invalid blocks
log.Warn("Invalid beacon block", "slot", block.Slot, "blockRoot", blockRoot, "reason", invalidBlockErr)
f.badBlocks.Store(libcommon.Hash(blockRoot), struct{}{})
//f.currentState = nil

return nil, InvalidBlock, invalidBlockErr
return nil, InvalidBlock, invalidBlockErr
}
f.blockRewards.Store(libcommon.Hash(blockRoot), blockRewardsCollector)
}

f.currentState = newState
if shallowImport {
return newState, Success, nil
}

// update diff storages.
Expand All @@ -261,7 +281,6 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,
f.currentIndicies.add(epoch, newState.RawCurrentEpochParticipation())
f.previousIndicies.add(epoch, newState.RawPreviousEpochParticipation())
}
f.blockRewards.Store(libcommon.Hash(blockRoot), blockRewardsCollector)

period := f.beaconCfg.SyncCommitteePeriod(newState.Slot())
f.syncCommittees.Store(period, syncCommittees{
Expand All @@ -277,6 +296,7 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,
f.lightclientBootstraps.Store(libcommon.Hash(blockRoot), lightclientBootstrap)
}
}

f.blocks.Store(libcommon.Hash(blockRoot), signedBlock)
bodyRoot, err := signedBlock.Block.Body.HashSSZ()
if err != nil {
Expand All @@ -294,10 +314,7 @@ func (f *forkGraphDisk) AddChainSegment(signedBlock *cltypes.SignedBeaconBlock,
// Lastly add checkpoints to caches as well.
f.currentJustifiedCheckpoints.Store(libcommon.Hash(blockRoot), newState.CurrentJustifiedCheckpoint())
f.finalizedCheckpoints.Store(libcommon.Hash(blockRoot), newState.FinalizedCheckpoint())
if newState.Slot() > f.highestSeen {
f.highestSeen = newState.Slot()
}
f.currentState = newState

return newState, Success, nil
}

Expand Down
8 changes: 4 additions & 4 deletions cl/phase1/forkchoice/fork_graph/fork_graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ func TestForkGraphInDisk(t *testing.T) {
require.NoError(t, utils.DecodeSSZSnappy(anchorState, anchor, int(clparams.Phase0Version)))
emitter := beaconevents.NewEventEmitter()
graph := NewForkGraphDisk(anchorState, afero.NewMemMapFs(), beacon_router_configuration.RouterConfiguration{}, emitter)
_, status, err := graph.AddChainSegment(blockA, true)
_, status, err := graph.AddChainSegment(blockA, true, false)
require.NoError(t, err)
require.Equal(t, status, Success)
// Now make blockC a bad block
blockC.Block.ProposerIndex = 81214459 // some invalid thing
_, status, err = graph.AddChainSegment(blockC, true)
_, status, err = graph.AddChainSegment(blockC, true, false)
require.Error(t, err)
require.Equal(t, status, InvalidBlock)
// Save current state hash
_, status, err = graph.AddChainSegment(blockB, true)
_, status, err = graph.AddChainSegment(blockB, true, false)
require.NoError(t, err)
require.Equal(t, status, Success)
// Try again with same should yield success
_, status, err = graph.AddChainSegment(blockB, true)
_, status, err = graph.AddChainSegment(blockB, true, false)
require.NoError(t, err)
require.Equal(t, status, PreValidated)
}
2 changes: 1 addition & 1 deletion cl/phase1/forkchoice/fork_graph/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
* to analyze and manipulate the state of the blockchain.
*/
type ForkGraph interface {
AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, fullValidation bool) (*state.CachingBeaconState, ChainSegmentInsertionResult, error)
AddChainSegment(signedBlock *cltypes.SignedBeaconBlock, fullValidation, shallowImport bool) (*state.CachingBeaconState, ChainSegmentInsertionResult, error)
GetHeader(blockRoot libcommon.Hash) (*cltypes.BeaconBlockHeader, bool)
GetState(blockRoot libcommon.Hash, alwaysCopy bool) (*state.CachingBeaconState, error)
GetCurrentJustifiedCheckpoint(blockRoot libcommon.Hash) (solid.Checkpoint, bool)
Expand Down
77 changes: 42 additions & 35 deletions cl/phase1/forkchoice/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ type ForkChoiceStore struct {
unrealizedJustifiedCheckpoint atomic.Value
unrealizedFinalizedCheckpoint atomic.Value

proposerBoostRoot atomic.Value
headHash libcommon.Hash
headSlot uint64
genesisTime uint64
genesisValidatorsRoot libcommon.Hash
weights map[libcommon.Hash]uint64
headSet map[libcommon.Hash]struct{}
hotSidecars map[libcommon.Hash][]*cltypes.BlobSidecar // Set of sidecars that are not yet processed.
proposerBoostRoot atomic.Value
headHash libcommon.Hash
headSlot uint64
genesisTime uint64
genesisValidatorsRoot libcommon.Hash
weights map[libcommon.Hash]uint64
headSet map[libcommon.Hash]struct{}
hotSidecars map[libcommon.Hash][]*cltypes.BlobSidecar // Set of sidecars that are not yet processed.
verifiedExecutionPayload *lru.Cache[libcommon.Hash, struct{}]
// childrens
childrens sync.Map

Expand Down Expand Up @@ -173,6 +174,11 @@ func NewForkChoiceStore(
Epoch: state2.Epoch(anchorState.BeaconState),
}

verifiedExecutionPayload, err := lru.New[libcommon.Hash, struct{}](1024)
if err != nil {
return nil, err
}

eth2Roots, err := lru.New[libcommon.Hash, libcommon.Hash](checkpointsPerCache)
if err != nil {
return nil, err
Expand Down Expand Up @@ -227,33 +233,34 @@ func NewForkChoiceStore(
headSet := make(map[libcommon.Hash]struct{})
headSet[anchorRoot] = struct{}{}
f := &ForkChoiceStore{
forkGraph: forkGraph,
equivocatingIndicies: make([]byte, anchorState.ValidatorLength(), anchorState.ValidatorLength()*2),
latestMessages: newLatestMessagesStore(anchorState.ValidatorLength()),
eth2Roots: eth2Roots,
engine: engine,
operationsPool: operationsPool,
beaconCfg: anchorState.BeaconConfig(),
preverifiedSizes: preverifiedSizes,
finalityCheckpoints: finalityCheckpoints,
totalActiveBalances: totalActiveBalances,
randaoMixesLists: randaoMixesLists,
randaoDeltas: randaoDeltas,
headSet: headSet,
weights: make(map[libcommon.Hash]uint64),
participation: participation,
emitters: emitters,
genesisTime: anchorState.GenesisTime(),
syncedDataManager: syncedDataManager,
nextBlockProposers: nextBlockProposers,
genesisValidatorsRoot: anchorState.GenesisValidatorsRoot(),
hotSidecars: make(map[libcommon.Hash][]*cltypes.BlobSidecar),
blobStorage: blobStorage,
ethClock: ethClock,
optimisticStore: optimistic.NewOptimisticStore(),
validatorMonitor: validatorMonitor,
probabilisticHeadGetter: probabilisticHeadGetter,
publicKeysRegistry: publicKeysRegistry,
forkGraph: forkGraph,
equivocatingIndicies: make([]byte, anchorState.ValidatorLength(), anchorState.ValidatorLength()*2),
latestMessages: newLatestMessagesStore(anchorState.ValidatorLength()),
eth2Roots: eth2Roots,
engine: engine,
operationsPool: operationsPool,
beaconCfg: anchorState.BeaconConfig(),
preverifiedSizes: preverifiedSizes,
finalityCheckpoints: finalityCheckpoints,
totalActiveBalances: totalActiveBalances,
randaoMixesLists: randaoMixesLists,
randaoDeltas: randaoDeltas,
headSet: headSet,
weights: make(map[libcommon.Hash]uint64),
participation: participation,
emitters: emitters,
genesisTime: anchorState.GenesisTime(),
syncedDataManager: syncedDataManager,
nextBlockProposers: nextBlockProposers,
genesisValidatorsRoot: anchorState.GenesisValidatorsRoot(),
hotSidecars: make(map[libcommon.Hash][]*cltypes.BlobSidecar),
blobStorage: blobStorage,
ethClock: ethClock,
optimisticStore: optimistic.NewOptimisticStore(),
validatorMonitor: validatorMonitor,
probabilisticHeadGetter: probabilisticHeadGetter,
publicKeysRegistry: publicKeysRegistry,
verifiedExecutionPayload: verifiedExecutionPayload,
}
f.justifiedCheckpoint.Store(anchorCheckpoint)
f.finalizedCheckpoint.Store(anchorCheckpoint)
Expand Down
3 changes: 3 additions & 0 deletions cl/phase1/forkchoice/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,7 @@ type ForkChoiceStorageWriter interface {
OnTick(time uint64)
SetSynced(synced bool)
ProcessAttestingIndicies(attestation *solid.Attestation, attestionIndicies []uint64)

ProcessBlockExecution(ctx context.Context, block *cltypes.SignedBeaconBlock) error
ProcessBlockConsensus(ctx context.Context, block *cltypes.SignedBeaconBlock) error
}
8 changes: 8 additions & 0 deletions cl/phase1/forkchoice/mock_services/forkchoice_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,11 @@ func (f *ForkChoiceStorageMock) IsRootOptimistic(root common.Hash) bool {
func (f *ForkChoiceStorageMock) IsHeadOptimistic() bool {
return false
}

func (f *ForkChoiceStorageMock) ProcessBlockConsensus(ctx context.Context, block *cltypes.SignedBeaconBlock) error {
return nil
}

func (f *ForkChoiceStorageMock) ProcessBlockExecution(ctx context.Context, block *cltypes.SignedBeaconBlock) error {
return nil
}
Loading
Loading