|
4 | 4 | "context" |
5 | 5 |
|
6 | 6 | "github.com/ethereum/go-ethereum" |
| 7 | + "github.com/ethereum/go-ethereum/log" |
7 | 8 |
|
8 | 9 | "github.com/ethereum-optimism/optimism/op-node/rollup/derive" |
9 | 10 | "github.com/ethereum-optimism/optimism/op-service/eth" |
@@ -43,3 +44,45 @@ func (c *confDepth) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1B |
43 | 44 | } |
44 | 45 |
|
45 | 46 | var _ derive.L1Fetcher = (*confDepth)(nil) |
| 47 | + |
| 48 | +// confDepth is an util that wraps the L1 input fetcher used in the pipeline, |
| 49 | +// and hides the part of the L1 chain with insufficient confirmations. |
| 50 | +// |
| 51 | +// At 0 depth the l1 head is completely ignored. |
| 52 | +type confDepthByL1Finalized struct { |
| 53 | + // everything fetched by hash is trusted already, so we implement those by embedding the fetcher |
| 54 | + derive.L1Fetcher |
| 55 | + l1Finalized func() eth.L1BlockRef |
| 56 | + depth uint64 |
| 57 | +} |
| 58 | + |
| 59 | +func NewConfDepthByL1Finalized(depth uint64, l1Finalized func() eth.L1BlockRef, fetcher derive.L1Fetcher) *confDepthByL1Finalized { |
| 60 | + return &confDepthByL1Finalized{L1Fetcher: fetcher, l1Finalized: l1Finalized, depth: depth} |
| 61 | +} |
| 62 | + |
| 63 | +// L1BlockRefByNumber is used for L1 traversal and for finding a safe common point between the L2 engine and L1 chain. |
| 64 | +// Any block numbers that are within confirmation depth of the L1 head are mocked to be "not found", |
| 65 | +// effectively hiding the uncertain part of the L1 chain. |
| 66 | +func (c *confDepthByL1Finalized) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1BlockRef, error) { |
| 67 | + // TODO: performance optimization: buffer the l1Unsafe, invalidate any reorged previous buffer content, |
| 68 | + // and instantly return the origin by number from the buffer if we can. |
| 69 | + |
| 70 | + // Don't apply the conf depth if l1Head is empty (as it is during the startup case before the l1State is initialized). |
| 71 | + l1Finalized := c.l1Finalized() |
| 72 | + if l1Finalized == (eth.L1BlockRef{}) { |
| 73 | + // if l1Finalized is empty, wait for it to be set, temporarily return not found |
| 74 | + log.Warn("Conf depth is waiting for L1 finalized block to be set") |
| 75 | + return eth.L1BlockRef{}, ethereum.NotFound |
| 76 | + } |
| 77 | + if num == l1Finalized.Number { |
| 78 | + log.Info("Conf depth get L1 block number is equal to finalized block number", "num", num, "finalized", l1Finalized) |
| 79 | + return l1Finalized, nil |
| 80 | + } |
| 81 | + if num < l1Finalized.Number { |
| 82 | + return c.L1Fetcher.L1BlockRefByNumber(ctx, num) |
| 83 | + } |
| 84 | + log.Error("Conf depth get L1 block number is greater than finalized block number", "num", num, "finalized", l1Finalized) |
| 85 | + return eth.L1BlockRef{}, ethereum.NotFound |
| 86 | +} |
| 87 | + |
| 88 | +var _ derive.L1Fetcher = (*confDepthByL1Finalized)(nil) |
0 commit comments