Skip to content

Commit

Permalink
Feature/UpdateHeadTimely: cache finalized checkpoint when initial sync (
Browse files Browse the repository at this point in the history
#9244)

(cherry picked from commit b667d30)
  • Loading branch information
terencechain authored and prestonvanloon committed Aug 27, 2021
1 parent 6f4d8a9 commit 73c3272
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions beacon-chain/blockchain/process_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ func (s *Service) handleBlockAfterBatchVerify(ctx context.Context, signed interf
if err := s.updateFinalized(ctx, fCheckpoint); err != nil {
return err
}
if featureconfig.Get().UpdateHeadTimely {
s.prevFinalizedCheckpt = s.finalizedCheckpt
s.finalizedCheckpt = fCheckpoint
}
}
return nil
}
Expand Down
58 changes: 58 additions & 0 deletions beacon-chain/blockchain/receive_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

types "github.com/prysmaticlabs/eth2-types"
blockchainTesting "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/forkchoice/protoarray"
"github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper"
"github.com/prysmaticlabs/prysm/proto/interfaces"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
Expand Down Expand Up @@ -283,6 +285,62 @@ func TestService_ReceiveBlockBatch(t *testing.T) {
}
}

func TestService_ReceiveBlockBatch_UpdateFinalizedCheckpoint(t *testing.T) {
// Must enable head timely feature flag to test this.
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{
UpdateHeadTimely: true,
})
defer resetCfg()

ctx := context.Background()
genesis, keys := testutil.DeterministicGenesisState(t, 64)

// Generate 5 epochs worth of blocks.
var blks []interfaces.SignedBeaconBlock
var roots [][32]byte
copied := genesis.Copy()
for i := types.Slot(1); i < params.BeaconConfig().SlotsPerEpoch*5; i++ {
b, err := testutil.GenerateFullBlock(copied, keys, testutil.DefaultBlockGenConfig(), i)
assert.NoError(t, err)
copied, err = state.ExecuteStateTransition(context.Background(), copied, wrapper.WrappedPhase0SignedBeaconBlock(b))
assert.NoError(t, err)
r, err := b.Block.HashTreeRoot()
require.NoError(t, err)
blks = append(blks, wrapper.WrappedPhase0SignedBeaconBlock(b))
roots = append(roots, r)
}

beaconDB := testDB.SetupDB(t)
genesisBlockRoot, err := genesis.HashTreeRoot(ctx)
require.NoError(t, err)
cfg := &Config{
BeaconDB: beaconDB,
ForkChoiceStore: protoarray.New(
0, // justifiedEpoch
0, // finalizedEpoch
genesisBlockRoot,
),
StateNotifier: &blockchainTesting.MockStateNotifier{RecordEvents: false},
StateGen: stategen.New(beaconDB),
}
s, err := NewService(ctx, cfg)
require.NoError(t, err)
err = s.saveGenesisData(ctx, genesis)
require.NoError(t, err)
gBlk, err := s.cfg.BeaconDB.GenesisBlock(ctx)
require.NoError(t, err)

gRoot, err := gBlk.Block().HashTreeRoot()
require.NoError(t, err)
s.finalizedCheckpt = &ethpb.Checkpoint{Root: gRoot[:]}

// Process 5 epochs worth of blocks.
require.NoError(t, s.ReceiveBlockBatch(ctx, blks, roots))

// Finalized epoch must be updated.
require.Equal(t, types.Epoch(2), s.finalizedCheckpt.Epoch)
}

func TestService_HasInitSyncBlock(t *testing.T) {
s, err := NewService(context.Background(), &Config{StateNotifier: &blockchainTesting.MockStateNotifier{}})
require.NoError(t, err)
Expand Down

0 comments on commit 73c3272

Please sign in to comment.