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

feat(rpc): improve L2ParentByBlockID #715

Merged
merged 2 commits into from
Apr 12, 2024
Merged
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
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
Loading