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
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Apr 12, 2024
1 parent 1f9f063 commit 36a2208
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: "Push docker image to GCR"

on:
push:
branches: [main]
branches: [main,update-L2ParentByBlockID]
tags:
- "v*"

Expand Down
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 36a2208

Please sign in to comment.