diff --git a/beacon-chain/blockchain/chain_info.go b/beacon-chain/blockchain/chain_info.go index 62372572a..f54782c2b 100644 --- a/beacon-chain/blockchain/chain_info.go +++ b/beacon-chain/blockchain/chain_info.go @@ -464,6 +464,13 @@ func (s *Service) IsOptimisticForRoot(ctx context.Context, root [32]byte) (bool, return !isCanonical, nil } +// TargetRoot wraps the corresponding method in forkchoice +func (s *Service) TargetRoot(root [32]byte) ([32]byte, error) { + s.cfg.ForkChoiceStore.RLock() + defer s.cfg.ForkChoiceStore.RUnlock() + return s.cfg.ForkChoiceStore.TargetRoot(root) +} + // Ancestor returns the block root of an ancestry block from the input block root. // // Spec pseudocode definition: diff --git a/beacon-chain/blockchain/receive_attestation.go b/beacon-chain/blockchain/receive_attestation.go index da57f0d7a..2115cd689 100644 --- a/beacon-chain/blockchain/receive_attestation.go +++ b/beacon-chain/blockchain/receive_attestation.go @@ -53,15 +53,11 @@ func (s *Service) AttestationTargetState(ctx context.Context, target *ethpb.Chec // VerifyLmdFfgConsistency verifies that attestation's LMD and FFG votes are consistency to each other. func (s *Service) VerifyLmdFfgConsistency(ctx context.Context, a *ethpb.Attestation) error { - targetSlot, err := slots.EpochStart(a.Data.Target.Epoch) + r, err := s.TargetRoot([32]byte(a.Data.BeaconBlockRoot)) if err != nil { return err } - r, err := s.Ancestor(ctx, a.Data.BeaconBlockRoot, targetSlot) - if err != nil { - return err - } - if !bytes.Equal(a.Data.Target.Root, r) { + if !bytes.Equal(a.Data.Target.Root, r[:]) { return fmt.Errorf("FFG and LMD votes are not consistent, block root: %#x, target root: %#x, canonical target root: %#x", a.Data.BeaconBlockRoot, a.Data.Target.Root, r) } return nil diff --git a/beacon-chain/blockchain/receive_attestation_test.go b/beacon-chain/blockchain/receive_attestation_test.go index 500c0418a..7ebe332ef 100644 --- a/beacon-chain/blockchain/receive_attestation_test.go +++ b/beacon-chain/blockchain/receive_attestation_test.go @@ -36,50 +36,28 @@ func TestAttestationCheckPtState_FarFutureSlot(t *testing.T) { require.ErrorContains(t, "exceeds max allowed value relative to the local clock", err) } -func TestVerifyLMDFFGConsistent_NotOK(t *testing.T) { +func TestVerifyLMDFFGConsistent(t *testing.T) { service, tr := minimalTestService(t) ctx := tr.ctx - b32 := util.NewBeaconBlock() - b32.Block.Slot = 32 - util.SaveBlock(t, ctx, service.cfg.BeaconDB, b32) - r32, err := b32.Block.HashTreeRoot() + f := service.cfg.ForkChoiceStore + fc := ðpb.Checkpoint{Root: params.BeaconConfig().ZeroHash[:]} + state, r32, err := prepareForkchoiceState(ctx, 32, [32]byte{'a'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, fc, fc) require.NoError(t, err) - b33 := util.NewBeaconBlock() - b33.Block.Slot = 33 - b33.Block.ParentRoot = r32[:] - util.SaveBlock(t, ctx, service.cfg.BeaconDB, b33) - r33, err := b33.Block.HashTreeRoot() + require.NoError(t, f.InsertNode(ctx, state, r32)) + + state, r33, err := prepareForkchoiceState(ctx, 33, [32]byte{'b'}, r32, params.BeaconConfig().ZeroHash, fc, fc) require.NoError(t, err) + require.NoError(t, f.InsertNode(ctx, state, r33)) wanted := "FFG and LMD votes are not consistent" a := util.NewAttestation() a.Data.Target.Epoch = 1 - a.Data.Target.Root = []byte{'a'} + a.Data.Target.Root = []byte{'c'} a.Data.BeaconBlockRoot = r33[:] require.ErrorContains(t, wanted, service.VerifyLmdFfgConsistency(context.Background(), a)) -} -func TestVerifyLMDFFGConsistent_OK(t *testing.T) { - service, tr := minimalTestService(t) - ctx := tr.ctx - - b32 := util.NewBeaconBlock() - b32.Block.Slot = 32 - util.SaveBlock(t, ctx, service.cfg.BeaconDB, b32) - r32, err := b32.Block.HashTreeRoot() - require.NoError(t, err) - b33 := util.NewBeaconBlock() - b33.Block.Slot = 33 - b33.Block.ParentRoot = r32[:] - util.SaveBlock(t, ctx, service.cfg.BeaconDB, b33) - r33, err := b33.Block.HashTreeRoot() - require.NoError(t, err) - - a := util.NewAttestation() - a.Data.Target.Epoch = 1 a.Data.Target.Root = r32[:] - a.Data.BeaconBlockRoot = r33[:] err = service.VerifyLmdFfgConsistency(context.Background(), a) require.NoError(t, err, "Could not verify LMD and FFG votes to be consistent") }