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

Caplin: Fixed caplin parent root indexing #11729

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions cl/beacon/handler/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,23 @@ func (a *ApiHandler) getHeaders(w http.ResponseWriter, r *http.Request) (*beacon
}
defer tx.Rollback()
var candidates []libcommon.Hash
var slot *uint64
var potentialRoot libcommon.Hash
// First lets find some good candidates for the query. TODO(Giulio2002): this does not give all the headers.
switch {
case queryParentHash != nil && querySlot != nil:
case queryParentHash != nil:
// get all blocks with this parent
slot, err = beacon_indicies.ReadBlockSlotByBlockRoot(tx, *queryParentHash)
blockRoots, err := beacon_indicies.ReadBlockRootsByParentRoot(tx, *queryParentHash)
if err != nil {
return nil, err
}
if slot == nil {
break
}
if *slot+1 != *querySlot {
break
}
potentialRoot, err = beacon_indicies.ReadCanonicalBlockRoot(tx, *slot+1)
if err != nil {
return nil, err
}
candidates = append(candidates, potentialRoot)
case queryParentHash == nil && querySlot != nil:
candidates = append(candidates, blockRoots...)
case querySlot != nil:
potentialRoot, err = beacon_indicies.ReadCanonicalBlockRoot(tx, *querySlot)
if err != nil {
return nil, err
}
candidates = append(candidates, potentialRoot)
case queryParentHash == nil && querySlot == nil:
default:
headSlot := a.syncedData.HeadSlot()
if headSlot == 0 {
break
Expand Down
36 changes: 36 additions & 0 deletions cl/persistence/beacon_indicies/indicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ func WriteBeaconBlockHeaderAndIndicies(ctx context.Context, tx kv.RwTx, signedHe
if err := WriteParentBlockRoot(ctx, tx, blockRoot, signedHeader.Header.ParentRoot); err != nil {
return err
}
if err := AddBlockRootToParentRootsIndex(tx, signedHeader.Header.ParentRoot, blockRoot); err != nil {
return err
}
if forceCanonical {
if err := MarkRootCanonical(ctx, tx, signedHeader.Header.Slot, blockRoot); err != nil {
return err
Expand Down Expand Up @@ -407,3 +410,36 @@ func ReadSignedHeaderByBlockRoot(ctx context.Context, tx kv.Tx, blockRoot libcom
}
return h, canonical == blockRoot, nil
}

func ReadBlockRootsByParentRoot(tx kv.Tx, parentRoot libcommon.Hash) ([]libcommon.Hash, error) {
roots, err := tx.GetOne(kv.ParentRootToBlockRoots, parentRoot[:])
if err != nil {
return nil, err
}
if len(roots) == 0 {
return nil, nil
}
blockRoots := make([]libcommon.Hash, 0, len(roots)/32)
for i := 0; i < len(roots); i += 32 {
var blockRoot libcommon.Hash
copy(blockRoot[:], roots[i:i+32])
blockRoots = append(blockRoots, blockRoot)
}
return blockRoots, nil
}

func AddBlockRootToParentRootsIndex(tx kv.RwTx, parentRoot, blockRoot libcommon.Hash) error {
roots, err := tx.GetOne(kv.ParentRootToBlockRoots, parentRoot[:])
if err != nil {
return err
}
// check if it is already there
for i := 0; i < len(roots); i += 32 {
if bytes.Equal(roots[i:i+32], blockRoot[:]) {
return nil
}
}

roots = append(libcommon.Copy(roots), blockRoot[:]...)
return tx.Put(kv.ParentRootToBlockRoots, parentRoot[:], roots)
}
4 changes: 3 additions & 1 deletion erigon-lib/kv/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ const (
BlockRootToKzgCommitments = "BlockRootToKzgCommitments"

// [Block Root] => [Parent Root]
BlockRootToParentRoot = "BlockRootToParentRoot"
BlockRootToParentRoot = "BlockRootToParentRoot"
ParentRootToBlockRoots = "ParentRootToBlockRoots"

HighestFinalized = "HighestFinalized" // hash -> transaction/receipt lookup metadata

Expand Down Expand Up @@ -598,6 +599,7 @@ var ChaindataTables = []string{
BlockRootToBlockHash,
BlockRootToBlockNumber,
LastBeaconSnapshot,
ParentRootToBlockRoots,
// Blob Storage
BlockRootToKzgCommitments,
// State Reconstitution
Expand Down
Loading