Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can retrieve cached initial sync block and db block #10568

Merged
merged 20 commits into from
Apr 29, 2022

Conversation

terencechain
Copy link
Member

@terencechain terencechain commented Apr 27, 2022

This fixes #10570

To improve on previous behavior, we adopted a new getBlock method which retrieves blocks in the initial syncing cacher DB and returns an error not found if the block is nil. We apply getBlock to more than one occurrence which adopted similar patterns. This is a much nicer clean-up than before.

Ideally, the initial syncing cache really should belong inside DB package but that's a much more heavily refactor and will induce more risk. I'm incline to push this to the later

@terencechain terencechain marked this pull request as ready for review April 27, 2022 17:55
@terencechain terencechain requested a review from a team as a code owner April 27, 2022 17:55
@terencechain terencechain self-assigned this Apr 27, 2022
@terencechain terencechain added Ready For Review Priority: High High priority item Bug Something isn't working labels Apr 27, 2022
@terencechain terencechain changed the title Wip: Save cached initial sync blocks before getting head block Save cached initial sync blocks before getting head block Apr 27, 2022
@terencechain terencechain changed the title Save cached initial sync blocks before getting head block Can retrieve cached initial sync block and db block Apr 27, 2022
@@ -182,9 +182,14 @@ func (s *Service) notifyEngineIfChangedHead(ctx context.Context, newHeadRoot [32
log.WithError(errNilFinalizedInStore).Error("could not get finalized checkpoint")
return
}
newHeadBlock, err := s.cfg.BeaconDB.Block(ctx, newHeadRoot)
if err := s.cfg.BeaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we pass in 0 blocks, do we still write to the db ? I think SaveBlocks should exit early if there is nothing to write.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I will just remove it. This was from the previous solution. Now getBlock should take care of it. Doesn't seem right to Save cached blocks here

nisdas
nisdas previously approved these changes Apr 28, 2022
Comment on lines 33 to 36
defer s.initSyncBlocksLock.RUnlock()
b := s.initSyncBlocks[r]
return b

// Check cache first because it's faster.
b, ok := s.initSyncBlocks[r]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can release the read lock immediately after the map read. In the case that there is a cache miss, you are holding the read lock for longer than necessary

@@ -438,7 +438,7 @@ func TestVerifyBeaconBlock_NoBlock(t *testing.T) {
require.NoError(t, err)

d := util.HydrateAttestationData(&ethpb.AttestationData{})
assert.ErrorContains(t, "signed beacon block can't be nil", service.verifyBeaconBlock(ctx, d))
assert.ErrorContains(t, "block not found in cache or db", service.verifyBeaconBlock(ctx, d))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use errBlockNotFoundInCacheOrDB here

terencechain and others added 2 commits April 28, 2022 09:20
nisdas
nisdas previously approved these changes Apr 28, 2022
@prylabs-bulldozer prylabs-bulldozer bot merged commit b7a82d0 into develop Apr 29, 2022
@delete-merged-branch delete-merged-branch bot deleted the save-cached-init-blks branch April 29, 2022 07:56
prestonvanloon pushed a commit that referenced this pull request Apr 29, 2022
* Save cached initial sync blocks before getting head block

* Add better abstraction to get block

* Move unlock read to a better location

* Feedbacks

* Add head changed logging

* Harder hasBlock requirement

* Update beacon-chain/blockchain/service.go

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>

* Update receive_attestation.go

* Don't process head if the block is unknown

* Use a helper method

* Fix test

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
(cherry picked from commit b7a82d0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Priority: High High priority item
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No block in DB when update head
4 participants