prysm-pulse/beacon-chain/rpc/debug/block.go
Nishant Das caf9bdbc6f
Use Block Interface Across Prysm (#8918)
* commit initial work

* checkpoint current work

* gaz

* checkpoint

* req/resp changes

* initial-sync

* finally works

* fix error

* fix bugs

* fix issue

* fix issues

* fix refs

* tests

* more text fixes

* more text fixes

* more text fixes

* fix tests

* fix tests

* tests

* finally fix builds

* finally

* comments

* fix fuzz

* share common library

* fix

* fix

* add in more defensive nil checks

* add in more defensive nil checks

* imports

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Apply suggestions from code review

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_wrapper.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* Update shared/interfaces/block_interface.go

Co-authored-by: terence tsao <terence@prysmaticlabs.com>

* imports

* fix bad changes

* fix

* terence's review

* terence's review

* fmt

* Update beacon-chain/rpc/beacon/blocks.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* fix tests

* fix

* fix all tests

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2021-05-26 16:19:54 +00:00

96 lines
3.2 KiB
Go

package debug
import (
"context"
"fmt"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/db/filters"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
pbrpc "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/attestationutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GetBlock in an ssz-encoded format by block root.
func (ds *Server) GetBlock(
ctx context.Context,
req *pbrpc.BlockRequest,
) (*pbrpc.SSZResponse, error) {
root := bytesutil.ToBytes32(req.BlockRoot)
signedBlock, err := ds.BeaconDB.Block(ctx, root)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve block by root: %v", err)
}
if signedBlock == nil || signedBlock.IsNil() {
return &pbrpc.SSZResponse{Encoded: make([]byte, 0)}, nil
}
encoded, err := signedBlock.MarshalSSZ()
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not marshal block: %v", err)
}
return &pbrpc.SSZResponse{
Encoded: encoded,
}, nil
}
// GetInclusionSlot of an attestation in block.
func (ds *Server) GetInclusionSlot(ctx context.Context, req *pbrpc.InclusionSlotRequest) (*pbrpc.InclusionSlotResponse, error) {
ds.GenesisTimeFetcher.CurrentSlot()
// Attestation has one epoch to get included in the chain. This blocks users from requesting too soon.
epochBack := types.Slot(0)
if ds.GenesisTimeFetcher.CurrentSlot() > params.BeaconConfig().SlotsPerEpoch {
epochBack = ds.GenesisTimeFetcher.CurrentSlot() - params.BeaconConfig().SlotsPerEpoch
}
if epochBack < req.Slot {
return nil, fmt.Errorf("attestation has one epoch window, please request slot older than %d", epochBack)
}
// Attestation could be in blocks between slot + 1 to slot + epoch_duration.
startSlot := req.Slot + params.BeaconConfig().MinAttestationInclusionDelay
endSlot := req.Slot + params.BeaconConfig().SlotsPerEpoch
filter := filters.NewFilter().SetStartSlot(startSlot).SetEndSlot(endSlot)
blks, _, err := ds.BeaconDB.Blocks(ctx, filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve blocks: %v", err)
}
inclusionSlot := types.Slot(1<<64 - 1)
targetStates := make(map[[32]byte]iface.ReadOnlyBeaconState)
for _, blk := range blks {
for _, a := range blk.Block().Body().Attestations() {
tr := bytesutil.ToBytes32(a.Data.Target.Root)
s, ok := targetStates[tr]
if !ok {
s, err = ds.StateGen.StateByRoot(ctx, tr)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve state: %v", err)
}
targetStates[tr] = s
}
c, err := helpers.BeaconCommitteeFromState(s, a.Data.Slot, a.Data.CommitteeIndex)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not get committee: %v", err)
}
indices, err := attestationutil.AttestingIndices(a.AggregationBits, c)
if err != nil {
return nil, err
}
for _, i := range indices {
if req.Id == i && req.Slot == a.Data.Slot {
inclusionSlot = blk.Block().Slot()
break
}
}
}
}
return &pbrpc.InclusionSlotResponse{Slot: inclusionSlot}, nil
}