Skip to content

Commit

Permalink
Merge pull request #104 from ethpandaops/pk910/update-fork-parents
Browse files Browse the repository at this point in the history
update parent ids of forks when parent fork gets finalized
  • Loading branch information
pk910 authored Aug 17, 2024
2 parents 1d09057 + fe0fdc7 commit ca2eafc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
35 changes: 35 additions & 0 deletions db/forks.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,40 @@ func DeleteFinalizedForks(finalizedRoots [][]byte, tx *sqlx.Tx) error {

fmt.Fprint(&sql, ")")

_, err = tx.Exec(sql.String(), args...)
if err != nil {
return err
}

return nil
}

func UpdateFinalizedForkParents(finalizedRoots [][]byte, tx *sqlx.Tx) error {
var sql strings.Builder
args := []any{}

fmt.Fprint(&sql, `
UPDATE forks
SET parent_fork = 0
WHERE fork_id IN (
SELECT fork_id FROM forks WHERE leaf_root IN (
`)

for i, root := range finalizedRoots {
if i > 0 {
fmt.Fprint(&sql, ",")
}

args = append(args, root)
fmt.Fprintf(&sql, "$%v", len(args))
}

fmt.Fprint(&sql, "))")

_, err := tx.Exec(sql.String(), args...)
if err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions indexer/beacon/finalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ func (indexer *Indexer) finalizeEpoch(epoch phase0.Epoch, justifiedRoot phase0.R
}

// delete unfinalized forks for canonical roots
if err := db.UpdateFinalizedForkParents(canonicalRoots, tx); err != nil {
return fmt.Errorf("failed updating finalized fork parents: %v", err)
}
if err := db.DeleteFinalizedForks(canonicalRoots, tx); err != nil {
return fmt.Errorf("failed deleting finalized forks: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion indexer/beacon/forkcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (cache *forkCache) setFinalizedEpoch(finalizedSlot phase0.Slot, justifiedRo
closestDistance := uint64(0)

for _, fork := range cache.forkMap {
if fork.baseSlot >= finalizedSlot {
if fork.leafSlot >= finalizedSlot {
continue
}

Expand Down
3 changes: 3 additions & 0 deletions indexer/beacon/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ func (sync *synchronizer) syncEpoch(syncEpoch phase0.Epoch, client *Client, last
}

// delete unfinalized forks for canonical roots
if err := db.UpdateFinalizedForkParents(canonicalBlockRoots, tx); err != nil {
return fmt.Errorf("failed updating finalized fork parents: %v", err)
}
if err := db.DeleteFinalizedForks(canonicalBlockRoots, tx); err != nil {
return fmt.Errorf("failed deleting finalized forks: %v", err)
}
Expand Down

0 comments on commit ca2eafc

Please sign in to comment.