github.com/ethereum-optimism/optimism@v1.7.2/op-node/rollup/driver/conf_depth.go (about) 1 package driver 2 3 import ( 4 "context" 5 6 "github.com/ethereum/go-ethereum" 7 8 "github.com/ethereum-optimism/optimism/op-node/rollup/derive" 9 "github.com/ethereum-optimism/optimism/op-service/eth" 10 ) 11 12 // confDepth is an util that wraps the L1 input fetcher used in the pipeline, 13 // and hides the part of the L1 chain with insufficient confirmations. 14 // 15 // At 0 depth the l1 head is completely ignored. 16 type confDepth struct { 17 // everything fetched by hash is trusted already, so we implement those by embedding the fetcher 18 derive.L1Fetcher 19 l1Head func() eth.L1BlockRef 20 depth uint64 21 } 22 23 func NewConfDepth(depth uint64, l1Head func() eth.L1BlockRef, fetcher derive.L1Fetcher) *confDepth { 24 return &confDepth{L1Fetcher: fetcher, l1Head: l1Head, depth: depth} 25 } 26 27 // L1BlockRefByNumber is used for L1 traversal and for finding a safe common point between the L2 engine and L1 chain. 28 // Any block numbers that are within confirmation depth of the L1 head are mocked to be "not found", 29 // effectively hiding the uncertain part of the L1 chain. 30 func (c *confDepth) L1BlockRefByNumber(ctx context.Context, num uint64) (eth.L1BlockRef, error) { 31 // TODO: performance optimization: buffer the l1Unsafe, invalidate any reorged previous buffer content, 32 // and instantly return the origin by number from the buffer if we can. 33 34 // Don't apply the conf depth is l1Head is empty (as it is during the startup case before the l1State is initialized). 35 l1Head := c.l1Head() 36 if l1Head == (eth.L1BlockRef{}) { 37 return c.L1Fetcher.L1BlockRefByNumber(ctx, num) 38 } 39 if num == 0 || c.depth == 0 || num+c.depth <= l1Head.Number { 40 return c.L1Fetcher.L1BlockRefByNumber(ctx, num) 41 } 42 return eth.L1BlockRef{}, ethereum.NotFound 43 } 44 45 var _ derive.L1Fetcher = (*confDepth)(nil)