From 6ac2ddb3ec162d791a8f5e2b2c15d43a2e0c7fc2 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 28 Apr 2020 15:30:26 -0700 Subject: [PATCH 1/3] Use `BlockRootAtSlot` to look up historical head root --- beacon-chain/rpc/validator/attester.go | 36 ++++++++++++++------------ 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/beacon-chain/rpc/validator/attester.go b/beacon-chain/rpc/validator/attester.go index cf38894246a3..85272607bd54 100644 --- a/beacon-chain/rpc/validator/attester.go +++ b/beacon-chain/rpc/validator/attester.go @@ -2,6 +2,7 @@ package validator import ( "context" + "fmt" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -78,27 +79,28 @@ func (vs *Server) GetAttestationData(ctx context.Context, req *ethpb.Attestation return nil, status.Errorf(codes.Internal, "Could not retrieve head root: %v", err) } - // In the case that we receive an attestation request after a newer state/block has been - // processed, we walk up the chain until state.Slot <= req.Slot to prevent producing an - // attestation that violates processing constraints. - fetchState := vs.BeaconDB.State - if featureconfig.Get().NewStateMgmt { - fetchState = vs.StateGen.StateByRoot - } - for headState.Slot() > req.Slot { - if ctx.Err() != nil { - return nil, status.Errorf(codes.Aborted, ctx.Err().Error()) - } - parent := headState.ParentRoot() - headRoot = parent[:] - headState, err = fetchState(ctx, parent) + // In the case that we receive an attestation request after a newer state/block has been processed. + if headState.Slot() > req.Slot { + fmt.Println(headState.Slot(), req.Slot) + headRoot, err = helpers.BlockRootAtSlot(headState, req.Slot) if err != nil { - return nil, status.Error(codes.Internal, err.Error()) + return nil, status.Errorf(codes.Internal, "Could not get historical head root: %v", err) } - if headState == nil { - return nil, status.Error(codes.Internal, "Failed to lookup parent state from head.") + if featureconfig.Get().NewStateMgmt { + headState, err = vs.StateGen.StateByRoot(ctx, bytesutil.ToBytes32(headRoot)) + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get historical head state: %v", err) + } + } else { + headState, err = vs.BeaconDB.State(ctx, bytesutil.ToBytes32(headRoot)) + if err != nil { + return nil, status.Errorf(codes.Internal, "Could not get historical head state: %v", err) + } } } + if headState == nil { + return nil, status.Error(codes.Internal, "Failed to lookup parent state from head.") + } if helpers.CurrentEpoch(headState) < helpers.SlotToEpoch(req.Slot) { headState, err = state.ProcessSlots(ctx, headState, helpers.StartSlot(helpers.SlotToEpoch(req.Slot))) From a7759283f0d148a43cba2ff62975bdb57915c47b Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 28 Apr 2020 15:30:36 -0700 Subject: [PATCH 2/3] Update test --- beacon-chain/rpc/validator/attester_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/rpc/validator/attester_test.go b/beacon-chain/rpc/validator/attester_test.go index fb45fefffb6f..bb6b92de7429 100644 --- a/beacon-chain/rpc/validator/attester_test.go +++ b/beacon-chain/rpc/validator/attester_test.go @@ -488,6 +488,7 @@ func TestServer_GetAttestationData_HeadStateSlotGreaterThanRequestSlot(t *testin blockRoots[1] = blockRoot[:] blockRoots[1*params.BeaconConfig().SlotsPerEpoch] = targetRoot[:] blockRoots[2*params.BeaconConfig().SlotsPerEpoch] = justifiedRoot[:] + blockRoots[3*params.BeaconConfig().SlotsPerEpoch] = blockRoot2[:] if err := beaconState.SetBlockRoots(blockRoots); err != nil { t.Fatal(err) } From 5ec101617f3306c6f729ba85333cd0c695123bcf Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 28 Apr 2020 15:37:02 -0700 Subject: [PATCH 3/3] Typo --- beacon-chain/rpc/validator/attester.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/beacon-chain/rpc/validator/attester.go b/beacon-chain/rpc/validator/attester.go index 85272607bd54..50e258b40082 100644 --- a/beacon-chain/rpc/validator/attester.go +++ b/beacon-chain/rpc/validator/attester.go @@ -2,7 +2,6 @@ package validator import ( "context" - "fmt" ptypes "github.com/gogo/protobuf/types" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" @@ -81,7 +80,6 @@ func (vs *Server) GetAttestationData(ctx context.Context, req *ethpb.Attestation // In the case that we receive an attestation request after a newer state/block has been processed. if headState.Slot() > req.Slot { - fmt.Println(headState.Slot(), req.Slot) headRoot, err = helpers.BlockRootAtSlot(headState, req.Slot) if err != nil { return nil, status.Errorf(codes.Internal, "Could not get historical head root: %v", err)