-
Notifications
You must be signed in to change notification settings - Fork 180
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
[Chunk Data Pack Pruner] Add block view index #6933
Open
zhangchiqing
wants to merge
7
commits into
master
Choose a base branch
from
leo/add-block-view-index
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
700b677
add block view index
zhangchiqing 92ac8d1
add headers.BlockIDByView methods
zhangchiqing 0ddf04e
refactor index block by view
zhangchiqing 0173275
Apply suggestions from code review
zhangchiqing c901bc4
fix tests
zhangchiqing 98f6014
fix lint
zhangchiqing 9f55bdb
skip duplicates
zhangchiqing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ type Headers struct { | |
db *badger.DB | ||
cache *Cache[flow.Identifier, *flow.Header] | ||
heightCache *Cache[uint64, flow.Identifier] | ||
viewCache *Cache[uint64, flow.Identifier] | ||
} | ||
|
||
func NewHeaders(collector module.CacheMetrics, db *badger.DB) *Headers { | ||
|
@@ -48,6 +49,14 @@ func NewHeaders(collector module.CacheMetrics, db *badger.DB) *Headers { | |
} | ||
} | ||
|
||
retrieveView := func(view uint64) func(tx *badger.Txn) (flow.Identifier, error) { | ||
return func(tx *badger.Txn) (flow.Identifier, error) { | ||
var id flow.Identifier | ||
err := operation.LookupCertifiedBlockByView(view, &id)(tx) | ||
return id, err | ||
} | ||
} | ||
|
||
h := &Headers{ | ||
db: db, | ||
cache: newCache(collector, metrics.ResourceHeader, | ||
|
@@ -59,6 +68,10 @@ func NewHeaders(collector module.CacheMetrics, db *badger.DB) *Headers { | |
withLimit[uint64, flow.Identifier](4*flow.DefaultTransactionExpiry), | ||
withStore(storeHeight), | ||
withRetrieve(retrieveHeight)), | ||
|
||
viewCache: newCache(collector, metrics.ResourceCertifiedView, | ||
withLimit[uint64, flow.Identifier](4*flow.DefaultTransactionExpiry), | ||
withRetrieve(retrieveView)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cache is only filled by retrieval op. |
||
} | ||
|
||
return h | ||
|
@@ -110,6 +123,19 @@ func (h *Headers) ByHeight(height uint64) (*flow.Header, error) { | |
return h.retrieveTx(blockID)(tx) | ||
} | ||
|
||
// ByView returns block header for the given view. It is only available for certified blocks. | ||
// Note: this method is not available until next spork or a migration that builds the index. | ||
func (h *Headers) ByView(view uint64) (*flow.Header, error) { | ||
tx := h.db.NewTransaction(false) | ||
defer tx.Discard() | ||
|
||
blockID, err := h.viewCache.Get(view)(tx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return h.retrieveTx(blockID)(tx) | ||
} | ||
|
||
// Exists returns true if a header with the given ID has been stored. | ||
// No errors are expected during normal operation. | ||
func (h *Headers) Exists(blockID flow.Identifier) (bool, error) { | ||
|
@@ -140,6 +166,22 @@ func (h *Headers) BlockIDByHeight(height uint64) (flow.Identifier, error) { | |
return blockID, nil | ||
} | ||
|
||
// BlockIDByView returns the block ID that is certified at the given view. It is an optimized | ||
// version of `ByView` that skips retrieving the block. Expected errors during normal operations: | ||
// - `storage.ErrNotFound` if no certified block is known at given view. | ||
// | ||
// NOTE: this method is not available until next spork (mainnet27) or a migration that builds the index. | ||
func (h *Headers) BlockIDByView(view uint64) (flow.Identifier, error) { | ||
tx := h.db.NewTransaction(false) | ||
defer tx.Discard() | ||
|
||
blockID, err := h.viewCache.Get(view)(tx) | ||
if err != nil { | ||
return flow.ZeroID, fmt.Errorf("could not lookup block id by view %d: %w", view, err) | ||
} | ||
return blockID, nil | ||
} | ||
|
||
func (h *Headers) ByParentID(parentID flow.Identifier) ([]*flow.Header, error) { | ||
var blockIDs flow.IdentifierList | ||
err := h.db.View(procedure.LookupBlockChildren(parentID, &blockIDs)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,14 @@ type Headers interface { | |
// ByHeight returns the block with the given number. It is only available for finalized blocks. | ||
ByHeight(height uint64) (*flow.Header, error) | ||
|
||
// ByView returns the block with the given view. It is only available for certified blocks. | ||
// Certified blocks are the blocks that have received QC. Hotstuff guarantees that for each view, | ||
// at most one block is certified. Hence, the return value of `ByView` is guaranteed to be unique | ||
// oven for non-finalized blocks. | ||
// | ||
// TODO: this method is not available until next spork (mainnet27) or a migration that builds the index. | ||
// ByView(view uint64) (*flow.Header, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will uncomment in next spork. |
||
|
||
// Exists returns true if a header with the given ID has been stored. | ||
// No errors are expected during normal operation. | ||
Exists(blockID flow.Identifier) (bool, error) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlexHentschel I was using the db operation directly, for 2 reasons: