Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(rpc): improve L2ParentByBlockID (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Apr 12, 2024
1 parent 1f9f063 commit 036f8e6
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions pkg/rpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ func (c *Client) L2ParentByBlockID(ctx context.Context, blockID *big.Int) (*type
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

parentBlockID := new(big.Int).Sub(blockID, common.Big1)
var (
parentHash common.Hash
parentBlockID = new(big.Int).Sub(blockID, common.Big1)
)

log.Debug("Get parent block by block ID", "parentBlockID", parentBlockID)

Expand All @@ -177,12 +180,27 @@ func (c *Client) L2ParentByBlockID(ctx context.Context, blockID *big.Int) (*type

l1Origin, err := c.L2.L1OriginByID(ctxWithTimeout, parentBlockID)
if err != nil {
return nil, err
if err.Error() != ethereum.NotFound.Error() {
return nil, err
}

// In some cases, the L1Origin data is not found in the L2 execution engine, we will try to fetch the parent
// by the parent block ID.
log.Warn("L1Origin not found, try to fetch parent by ID", "blockID", parentBlockID)

parent, err := c.L2.BlockByNumber(ctxWithTimeout, parentBlockID)
if err != nil {
return nil, err
}

parentHash = parent.Hash()
} else {
parentHash = l1Origin.L2BlockHash
}

log.Debug("Parent block L1 origin", "l1Origin", l1Origin, "parentBlockID", parentBlockID)

return c.L2.HeaderByHash(ctxWithTimeout, l1Origin.L2BlockHash)
return c.L2.HeaderByHash(ctxWithTimeout, parentHash)
}

// WaitL1Origin keeps waiting until the L1Origin with given block ID appears on the L2 execution engine.
Expand Down

0 comments on commit 036f8e6

Please sign in to comment.