-
Notifications
You must be signed in to change notification settings - Fork 1k
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
New prysm get block RPC #11834
New prysm get block RPC #11834
Changes from 19 commits
5bfb952
b557ee1
f5aad1d
ab8b0d3
b72323f
6d9df33
a14b667
a8986b9
2851145
a48b75c
9a0aa6e
e298d35
aa4ac54
144b800
a29bef8
a1e86bf
2e038fa
933e920
8f65d2c
c84b525
7ad424a
45b31b5
f380656
edc24be
4891145
4756bbe
d608353
3ad693e
daa1104
d0d5cde
a40c2f9
13baabf
dd883df
dd591bb
9361785
e2c59cc
8291ed8
455197d
44b6099
a2f1137
ac95af3
142cf8d
575e523
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,10 @@ import ( | |
"github.com/prysmaticlabs/prysm/v3/beacon-chain/builder" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/feed" | ||
blockfeed "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/feed/block" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/helpers" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/core/transition" | ||
"github.com/prysmaticlabs/prysm/v3/beacon-chain/db/kv" | ||
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams" | ||
"github.com/prysmaticlabs/prysm/v3/config/params" | ||
"github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" | ||
"github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces" | ||
|
@@ -41,26 +43,153 @@ func (vs *Server) GetBeaconBlock(ctx context.Context, req *ethpb.BlockRequest) ( | |
ctx, span := trace.StartSpan(ctx, "ProposerServer.GetBeaconBlock") | ||
defer span.End() | ||
span.AddAttributes(trace.Int64Attribute("slot", int64(req.Slot))) | ||
if slots.ToEpoch(req.Slot) < params.BeaconConfig().AltairForkEpoch { | ||
blk, err := vs.getPhase0BeaconBlock(ctx, req) | ||
|
||
// A syncing validator should not produce a block. | ||
if vs.SyncChecker.Syncing() { | ||
return nil, status.Error(codes.Unavailable, "Syncing to latest head, not ready to respond") | ||
} | ||
|
||
// An optimistic validator MUST NOT produce a block (i.e., sign across the DOMAIN_BEACON_PROPOSER domain). | ||
if err := vs.optimisticStatus(ctx); err != nil { | ||
return nil, status.Errorf(codes.Unavailable, "Validator is not ready to propose: %v", err) | ||
} | ||
|
||
sBlk, err := getEmptyBlock(req.Slot) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not prepare block: %v", err) | ||
} | ||
|
||
parentRoot, err := vs.HeadFetcher.HeadRoot(ctx) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not get head root: %v", err) | ||
} | ||
head, err := vs.HeadFetcher.HeadState(ctx) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not get head state: %v", err) | ||
} | ||
head, err = transition.ProcessSlotsUsingNextSlotCache(ctx, head, parentRoot, req.Slot) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not process slots up to %d: %v", req.Slot, err) | ||
} | ||
|
||
blk := sBlk.Block() | ||
// Set slot, graffiti, randao reveal, and parent root. | ||
blk.SetSlot(req.Slot) | ||
blk.Body().SetGraffiti(req.Graffiti) | ||
blk.Body().SetRandaoReveal(req.RandaoReveal) | ||
blk.SetParentRoot(parentRoot) | ||
|
||
// Set eth1 data. | ||
eth1Data, err := vs.eth1DataMajorityVote(ctx, head) | ||
if err != nil { | ||
log.WithError(err).Error("Could not get eth1data") | ||
potuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
blk.Body().SetEth1Data(eth1Data) | ||
|
||
// Set deposit and attestation. | ||
deposits, atts, err := vs.packDepositsAndAttestations(ctx, head, eth1Data) // TODO: split attestations and deposits | ||
if err != nil { | ||
log.WithError(err).Error("Could not pack deposits and attestations") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, do we need to set the empty deposits and attestations here or the marshaller takes care of it automatically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this one is fine since those are not vectors but I added anyway |
||
} else { | ||
blk.Body().SetDeposits(deposits) | ||
blk.Body().SetAttestations(atts) | ||
} | ||
} | ||
|
||
// Set proposer index | ||
idx, err := helpers.BeaconProposerIndex(ctx, head) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not calculate proposer index %v", err) | ||
} | ||
blk.SetProposerIndex(idx) | ||
|
||
// Set slashings | ||
validProposerSlashings, validAttSlashings := vs.getSlashings(ctx, head) | ||
blk.Body().SetProposerSlashings(validProposerSlashings) | ||
blk.Body().SetAttesterSlashings(validAttSlashings) | ||
|
||
// Set exits | ||
blk.Body().SetVoluntaryExits(vs.getExits(head, req.Slot)) | ||
|
||
// Set sync aggregate. New in Altair. | ||
if req.Slot > 0 && slots.ToEpoch(req.Slot) >= params.BeaconConfig().AltairForkEpoch { | ||
syncAggregate, err := vs.getSyncAggregate(ctx, req.Slot-1, bytesutil.ToBytes32(parentRoot)) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not fetch phase0 beacon block: %v", err) | ||
log.WithError(err).Error("Could not get sync aggregate") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to return right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to return here. There's no reason to miss a block for this error |
||
} else { | ||
if err := blk.Body().SetSyncAggregate(syncAggregate); err != nil { | ||
log.WithError(err).Error("Could not set sync aggregate") | ||
if err := blk.Body().SetSyncAggregate(ðpb.SyncAggregate{ | ||
SyncCommitteeBits: make([]byte, params.BeaconConfig().SyncCommitteeSize), | ||
SyncCommitteeSignature: make([]byte, fieldparams.BLSSignatureLength), | ||
}); err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not set default sync aggregate: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we return here ? or let it propose? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
} | ||
} | ||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Phase0{Phase0: blk}}, nil | ||
} else if slots.ToEpoch(req.Slot) < params.BeaconConfig().BellatrixForkEpoch { | ||
blk, err := vs.getAltairBeaconBlock(ctx, req) | ||
} | ||
|
||
// Set execution data. New in Bellatrix | ||
if slots.ToEpoch(req.Slot) >= params.BeaconConfig().BellatrixForkEpoch { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these nested conditionals and control variables make me nervous. I would prefer we have a small function that does these different tasks and has almost full coverage. It should help with reducing nesting. It could be cleaner if we have small functions that apply altair things, apply bellatrix things, and apply capella things. Each one thoroughly tested and should be small (less than 50 loc) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, I'll be adding tests over this later |
||
fallBackToLocal := true | ||
canUseBuilder, err := vs.canUseBuilder(ctx, req.Slot, idx) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not fetch Altair beacon block: %v", err) | ||
log.WithError(err).Warn("Proposer: failed to check if builder can be used") | ||
} else if canUseBuilder { | ||
h, err := vs.getPayloadHeaderFromBuilder(ctx, req.Slot, idx) | ||
if err != nil { | ||
builderGetPayloadMissCount.Inc() | ||
log.WithError(err).Warn("Proposer: failed to get payload header from builder") | ||
} else { | ||
blk.SetBlinded(true) | ||
if err := blk.Body().SetExecution(h); err != nil { | ||
log.WithError(err).Warn("Proposer: failed to set execution payload") | ||
} else { | ||
fallBackToLocal = false | ||
} | ||
} | ||
} | ||
if fallBackToLocal { | ||
executionData, err := vs.getExecutionPayload(ctx, req.Slot, idx, bytesutil.ToBytes32(parentRoot), head) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not get execution payload: %v", err) | ||
} | ||
if err := blk.Body().SetExecution(executionData); err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not set execution payload: %v", err) | ||
} | ||
} | ||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Altair{Altair: blk}}, nil | ||
} | ||
|
||
// An optimistic validator MUST NOT produce a block (i.e., sign across the DOMAIN_BEACON_PROPOSER domain). | ||
if err := vs.optimisticStatus(ctx); err != nil { | ||
return nil, err | ||
// Set bls to execution change. New in Capella | ||
if slots.ToEpoch(req.Slot) >= params.BeaconConfig().CapellaForkEpoch { | ||
changes, err := vs.BLSChangesPool.BLSToExecChangesForInclusion(head) | ||
if err != nil { | ||
log.WithError(err).Error("Could not get bls to execution changes") | ||
} else { | ||
if err := blk.Body().SetBLSToExecutionChanges(changes); err != nil { | ||
log.WithError(err).Error("Could not set bls to execution changes") | ||
} | ||
} | ||
} | ||
|
||
return vs.getBellatrixBeaconBlock(ctx, req) | ||
sr, err := vs.computeStateRoot(ctx, sBlk) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not compute state root: %v", err) | ||
} | ||
blk.SetStateRoot(sr) | ||
|
||
pb, err := blk.Proto() | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not convert block to proto: %v", err) | ||
} | ||
if slots.ToEpoch(req.Slot) >= params.BeaconConfig().CapellaForkEpoch { | ||
potuz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Capella{Capella: pb.(*ethpb.BeaconBlockCapella)}}, nil | ||
} else if slots.ToEpoch(req.Slot) >= params.BeaconConfig().BellatrixForkEpoch { | ||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Bellatrix{Bellatrix: pb.(*ethpb.BeaconBlockBellatrix)}}, nil | ||
} else if slots.ToEpoch(req.Slot) >= params.BeaconConfig().AltairForkEpoch { | ||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Altair{Altair: pb.(*ethpb.BeaconBlockAltair)}}, nil | ||
} | ||
return ðpb.GenericBeaconBlock{Block: ðpb.GenericBeaconBlock_Phase0{Phase0: pb.(*ethpb.BeaconBlock)}}, nil | ||
} | ||
|
||
// ProposeBeaconBlock is called by a proposer during its assigned slot to create a block in an attempt | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this error right now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a beacon API for retrieving blind blocks (only). By the beacon API definition, you must return a valid blind block or fail. Very different than our current implementation used by Prysm API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean are you sure that the only failures are miss-config or circuit breaker in this path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error msg is wrong. I'll update