From 0449cd34507756f799b82846b4d9a9fdffbfbced Mon Sep 17 00:00:00 2001 From: terence tsao Date: Tue, 15 Dec 2020 13:19:02 -0800 Subject: [PATCH] Allow update cache at last slot of the epoch (#8094) * Epoch boundary updates at the last slot of the epoch * Report metric on first slot * Remove comment * Add locks to param config * Remove lock for copy * Revert "Add locks to param config" This reverts commit 79d5130b58ae7f80c72109e29cc9ea775c91b063. Co-authored-by: Raul Jordan --- beacon-chain/blockchain/process_block.go | 15 +++++++++++++-- beacon-chain/blockchain/process_block_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index e7069bc314..ef0d18fc72 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -330,7 +330,15 @@ func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed *ethpb // Epoch boundary bookkeeping such as logging epoch summaries. func (s *Service) handleEpochBoundary(ctx context.Context, postState *stateTrie.BeaconState) error { - if postState.Slot() >= s.nextEpochBoundarySlot { + if postState.Slot() == s.nextEpochBoundarySlot-1 { + // Update caches for the next epoch at epoch boundary slot - 1. + if err := helpers.UpdateCommitteeCache(postState, helpers.NextEpoch(postState)); err != nil { + return err + } + if err := helpers.UpdateProposerIndicesInCache(postState, helpers.NextEpoch(postState)); err != nil { + return err + } + } else if postState.Slot() >= s.nextEpochBoundarySlot { if err := reportEpochMetrics(ctx, postState, s.head.state); err != nil { return err } @@ -339,7 +347,9 @@ func (s *Service) handleEpochBoundary(ctx context.Context, postState *stateTrie. if err != nil { return err } - // Update committees cache at epoch boundary slot. + + // Update caches at epoch boundary slot. + // The following updates have short cut to return nil cheaply if fulfilled during boundary slot - 1. if err := helpers.UpdateCommitteeCache(postState, helpers.CurrentEpoch(postState)); err != nil { return err } @@ -347,6 +357,7 @@ func (s *Service) handleEpochBoundary(ctx context.Context, postState *stateTrie. return err } } + return nil } diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index d8ea2e51ca..485616b674 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -888,3 +888,16 @@ func TestHandleEpochBoundary_BadMetrics(t *testing.T) { service.head = &head{} require.ErrorContains(t, "failed to initialize precompute: nil inner state", service.handleEpochBoundary(ctx, s)) } + +func TestHandleEpochBoundary_UpdateFirstSlot(t *testing.T) { + ctx := context.Background() + cfg := &Config{} + service, err := NewService(ctx, cfg) + require.NoError(t, err) + + s, _ := testutil.DeterministicGenesisState(t, 1024) + service.head = &head{state: s} + require.NoError(t, s.SetSlot(2*params.BeaconConfig().SlotsPerEpoch)) + require.NoError(t, service.handleEpochBoundary(ctx, s)) + require.Equal(t, 3*params.BeaconConfig().SlotsPerEpoch, service.nextEpochBoundarySlot) +}