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

interop: Handle ErrParentToFirst from PreviousDerivedFrom #12833

Merged
merged 1 commit into from
Nov 6, 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
3 changes: 2 additions & 1 deletion op-supervisor/supervisor/backend/db/fromda/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ func (db *DB) PreviousDerivedFrom(derivedFrom eth.BlockID) (prevDerivedFrom type
if self.derivedFrom.Number == 0 {
return types.BlockSeal{}, nil
} else {
return types.BlockSeal{}, fmt.Errorf("cannot find previous derived before start of database: %s", derivedFrom)
return types.BlockSeal{},
fmt.Errorf("cannot find previous derived before start of database: %s (%w)", derivedFrom, types.ErrPreviousToFirst)
}
}
prev, err := db.readAt(selfIndex - 1)
Expand Down
26 changes: 20 additions & 6 deletions op-supervisor/supervisor/backend/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ func (db *ChainsDB) CrossDerivedFromBlockRef(chainID types.ChainID, derived eth.
return eth.BlockRef{}, err
}
parent, err := xdb.PreviousDerivedFrom(res.ID())
if err != nil {
// if we are working with the first item in the database, PreviousDerivedFrom will return ErrPreviousToFirst
// in which case we can attach a zero parent to the cross-derived-from block, as the parent block is unknown
if errors.Is(err, types.ErrPreviousToFirst) {
return res.ForceWithParent(eth.BlockID{}), nil
} else if err != nil {
return eth.BlockRef{}, err
}
return res.MustWithParent(parent.ID()), nil
Expand Down Expand Up @@ -266,7 +270,11 @@ func (db *ChainsDB) CandidateCrossSafe(chain types.ChainID) (derivedFromScope, c
candidateRef := candidate.MustWithParent(crossDerived.ID())

parentDerivedFrom, err := lDB.PreviousDerivedFrom(candidateFrom.ID())
if err != nil {
// if we are working with the first item in the database, PreviousDerivedFrom will return ErrPreviousToFirst
// in which case we can attach a zero parent to the cross-derived-from block, as the parent block is unknown
if errors.Is(err, types.ErrPreviousToFirst) {
parentDerivedFrom = types.BlockSeal{}
} else if err != nil {
return eth.BlockRef{}, eth.BlockRef{}, fmt.Errorf("failed to find parent-block of derived-from %s: %w", candidateFrom, err)
}
candidateFromRef := candidateFrom.MustWithParent(parentDerivedFrom.ID())
Expand All @@ -275,12 +283,18 @@ func (db *ChainsDB) CandidateCrossSafe(chain types.ChainID) (derivedFromScope, c
if candidateFrom.Number > crossDerivedFrom.Number+1 {
// If we are not ready to process the candidate block,
// then we need to stick to the current scope, so the caller can bump up from there.
var crossDerivedFromRef eth.BlockRef
parent, err := lDB.PreviousDerivedFrom(crossDerivedFrom.ID())
if err != nil {
return eth.BlockRef{}, eth.BlockRef{}, fmt.Errorf("failed to find parent-block of cross-derived-from %s: %w",
crossDerivedFrom, err)
// if we are working with the first item in the database, PreviousDerivedFrom will return ErrPreviousToFirst
// in which case we can attach a zero parent to the cross-derived-from block, as the parent block is unknown
if errors.Is(err, types.ErrPreviousToFirst) {
crossDerivedFromRef = crossDerivedFrom.ForceWithParent(eth.BlockID{})
} else if err != nil {
return eth.BlockRef{}, eth.BlockRef{},
fmt.Errorf("failed to find parent-block of cross-derived-from %s: %w", crossDerivedFrom, err)
} else {
crossDerivedFromRef = crossDerivedFrom.MustWithParent(parent.ID())
}
crossDerivedFromRef := crossDerivedFrom.MustWithParent(parent.ID())
return crossDerivedFromRef, eth.BlockRef{},
fmt.Errorf("candidate is from %s, while current scope is %s: %w",
candidateFrom, crossDerivedFrom, types.ErrOutOfScope)
Expand Down
3 changes: 3 additions & 0 deletions op-supervisor/supervisor/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ var (
// ErrOutOfScope is when data is accessed, but access is not allowed, because of a limited scope.
// E.g. when limiting scope to L2 blocks derived from a specific subset of the L1 chain.
ErrOutOfScope = errors.New("out of scope")
// ErrPreviousToFirst is when you try to get the previous block of the first block
// E.g. when calling PreviousDerivedFrom on the first L1 block in the DB.
ErrPreviousToFirst = errors.New("cannot get parent of first block in the database")
// ErrUnknownChain is when a chain is unknown, not in the dependency set.
ErrUnknownChain = errors.New("unknown chain")
// ErrNoRPCSource happens when a sub-service needs an RPC data source, but is not configured with one.
Expand Down