From 9c13d47f4c75daffa7d0ceb2f8a578be601e14ad Mon Sep 17 00:00:00 2001 From: Potuz Date: Thu, 25 Jan 2024 21:05:56 -0300 Subject: [PATCH] fix off by one (#13529) --- beacon-chain/blockchain/process_block.go | 5 ++- beacon-chain/cache/proposer_indices.go | 1 + beacon-chain/cache/proposer_indices_test.go | 33 +++++++++---------- beacon-chain/core/helpers/beacon_committee.go | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 152cd19adf80..2b1452faeab2 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -325,7 +325,10 @@ func (s *Service) updateEpochBoundaryCaches(ctx context.Context, st state.Beacon } // The proposer indices cache takes the target root for the previous // epoch as key - target, err := s.cfg.ForkChoiceStore.TargetRootForEpoch(r, e-1) + if e > 0 { + e = e - 1 + } + target, err := s.cfg.ForkChoiceStore.TargetRootForEpoch(r, e) if err != nil { log.WithError(err).Error("could not update proposer index state-root map") return nil diff --git a/beacon-chain/cache/proposer_indices.go b/beacon-chain/cache/proposer_indices.go index 6071e1e462a3..6f7a3a1a9793 100644 --- a/beacon-chain/cache/proposer_indices.go +++ b/beacon-chain/cache/proposer_indices.go @@ -115,6 +115,7 @@ func (p *ProposerIndicesCache) IndicesFromCheckpoint(c forkchoicetypes.Checkpoin root, ok := p.rootMap[c] p.Unlock() if !ok { + ProposerIndicesCacheMiss.Inc() return emptyIndices, ok } return p.ProposerIndices(c.Epoch+1, root) diff --git a/beacon-chain/cache/proposer_indices_test.go b/beacon-chain/cache/proposer_indices_test.go index c3195416e522..ee8d9d6eb9a1 100644 --- a/beacon-chain/cache/proposer_indices_test.go +++ b/beacon-chain/cache/proposer_indices_test.go @@ -37,70 +37,69 @@ func TestProposerCache_Set(t *testing.T) { func TestProposerCache_CheckpointAndPrune(t *testing.T) { cache := NewProposerIndicesCache() indices := [fieldparams.SlotsPerEpoch]primitives.ValidatorIndex{} - root := [32]byte{'a'} - cpRoot := [32]byte{'b'} copy(indices[3:], []primitives.ValidatorIndex{1, 2, 3, 4, 5, 6}) for i := 1; i < 10; i++ { + root := [32]byte{byte(i)} cache.Set(primitives.Epoch(i), root, indices) + cpRoot := [32]byte{byte(i - 1)} cache.SetCheckpoint(forkchoicetypes.Checkpoint{Epoch: primitives.Epoch(i - 1), Root: cpRoot}, root) } - received, ok := cache.ProposerIndices(1, root) + received, ok := cache.ProposerIndices(1, [32]byte{1}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.ProposerIndices(4, root) + received, ok = cache.ProposerIndices(4, [32]byte{4}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.ProposerIndices(9, root) + received, ok = cache.ProposerIndices(9, [32]byte{9}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 0, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 3, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 3, Root: [32]byte{3}}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 4, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 4, Root: [32]byte{4}}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 8, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 8, Root: [32]byte{8}}) require.Equal(t, true, ok) require.Equal(t, indices, received) cache.Prune(5) emptyIndices := [fieldparams.SlotsPerEpoch]primitives.ValidatorIndex{} - received, ok = cache.ProposerIndices(1, root) + received, ok = cache.ProposerIndices(1, [32]byte{1}) require.Equal(t, false, ok) require.Equal(t, emptyIndices, received) - received, ok = cache.ProposerIndices(4, root) + received, ok = cache.ProposerIndices(4, [32]byte{4}) require.Equal(t, false, ok) require.Equal(t, emptyIndices, received) - received, ok = cache.ProposerIndices(9, root) + received, ok = cache.ProposerIndices(9, [32]byte{9}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 0, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 0, Root: [32]byte{0}}) require.Equal(t, false, ok) require.Equal(t, emptyIndices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 3, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 3, Root: [32]byte{3}}) require.Equal(t, false, ok) require.Equal(t, emptyIndices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 4, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 4, Root: [32]byte{4}}) require.Equal(t, true, ok) require.Equal(t, indices, received) - received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 8, Root: cpRoot}) + received, ok = cache.IndicesFromCheckpoint(forkchoicetypes.Checkpoint{Epoch: 8, Root: [32]byte{8}}) require.Equal(t, true, ok) require.Equal(t, indices, received) - } diff --git a/beacon-chain/core/helpers/beacon_committee.go b/beacon-chain/core/helpers/beacon_committee.go index a7e89c4ff9f1..90b9af5d2a51 100644 --- a/beacon-chain/core/helpers/beacon_committee.go +++ b/beacon-chain/core/helpers/beacon_committee.go @@ -379,7 +379,7 @@ func UpdateCachedCheckpointToStateRoot(state state.ReadOnlyBeaconState, cp *fork if cp.Epoch <= params.BeaconConfig().GenesisEpoch+params.BeaconConfig().MinSeedLookahead { return nil } - slot, err := slots.EpochEnd(cp.Epoch - 1) + slot, err := slots.EpochEnd(cp.Epoch) if err != nil { return err }