driver.go 3.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package driver

import (
	"context"

	"github.com/ethereum-optimism/optimism/op-node/eth"
	"github.com/ethereum-optimism/optimism/op-node/rollup"
	"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/log"
)

type Driver struct {
	s *state
}

18 19 20 21 22 23 24 25 26 27 28
type Metrics interface {
	RecordPipelineReset()
	RecordSequencingError()
	RecordPublishingError()
	RecordDerivationError()

	RecordReceivedUnsafePayload(payload *eth.ExecutionPayload)

	RecordL1Ref(name string, ref eth.L1BlockRef)
	RecordL2Ref(name string, ref eth.L2BlockRef)

29 30
	RecordUnsafePayloadsBuffer(length uint64, memSize uint64, next eth.BlockID)

31 32 33 34 35 36
	SetDerivationIdle(idle bool)

	RecordL1ReorgDepth(d uint64)
	CountSequencedTxs(count int)
}

37
type Downloader interface {
38 39
	InfoByHash(ctx context.Context, hash common.Hash) (eth.BlockInfo, error)
	Fetch(ctx context.Context, blockHash common.Hash) (eth.BlockInfo, types.Transactions, eth.ReceiptsFetcher, error)
40 41 42
}

type L1Chain interface {
protolambda's avatar
protolambda committed
43
	derive.L1Fetcher
44
	L1BlockRefByLabel(context.Context, eth.BlockLabel) (eth.L1BlockRef, error)
45 46 47
}

type L2Chain interface {
protolambda's avatar
protolambda committed
48
	derive.Engine
49
	L2BlockRefByLabel(ctx context.Context, label eth.BlockLabel) (eth.L2BlockRef, error)
50 51 52
	L2BlockRefByHash(ctx context.Context, l2Hash common.Hash) (eth.L2BlockRef, error)
}

protolambda's avatar
protolambda committed
53 54 55 56 57 58 59 60 61 62
type DerivationPipeline interface {
	Reset()
	Step(ctx context.Context) error
	SetUnsafeHead(head eth.L2BlockRef)
	AddUnsafePayload(payload *eth.ExecutionPayload)
	Finalized() eth.L2BlockRef
	SafeL2Head() eth.L2BlockRef
	UnsafeL2Head() eth.L2BlockRef
	Progress() derive.Progress
}
63

protolambda's avatar
protolambda committed
64
type outputInterface interface {
65
	// createNewBlock builds a new block based on the L2 Head, L1 Origin, and the current mempool.
66
	createNewBlock(ctx context.Context, l2Head eth.L2BlockRef, l2SafeHead eth.BlockID, l2Finalized eth.BlockID, l1Origin eth.L1BlockRef) (eth.L2BlockRef, *eth.ExecutionPayload, error)
67 68 69 70
}

type Network interface {
	// PublishL2Payload is called by the driver whenever there is a new payload to publish, synchronously with the driver main loop.
71
	PublishL2Payload(ctx context.Context, payload *eth.ExecutionPayload) error
72 73
}

74
func NewDriver(driverCfg *Config, cfg *rollup.Config, l2 L2Chain, l1 L1Chain, network Network, log log.Logger, snapshotLog log.Logger, metrics Metrics) *Driver {
75 76 77 78 79 80
	output := &outputImpl{
		Config: cfg,
		dl:     l1,
		l2:     l2,
		log:    log,
	}
protolambda's avatar
protolambda committed
81 82 83

	var state *state
	verifConfDepth := NewConfDepth(driverCfg.VerifierConfDepth, func() eth.L1BlockRef { return state.l1Head }, l1)
84
	derivationPipeline := derive.NewDerivationPipeline(log, cfg, verifConfDepth, l2, metrics)
85
	state = NewState(driverCfg, log, snapshotLog, cfg, l1, l2, output, derivationPipeline, network, metrics)
protolambda's avatar
protolambda committed
86
	return &Driver{s: state}
87 88 89 90 91 92
}

func (d *Driver) OnL1Head(ctx context.Context, head eth.L1BlockRef) error {
	return d.s.OnL1Head(ctx, head)
}

93 94 95 96 97 98 99 100
func (d *Driver) OnL1Safe(ctx context.Context, safe eth.L1BlockRef) error {
	return d.s.OnL1Safe(ctx, safe)
}

func (d *Driver) OnL1Finalized(ctx context.Context, finalized eth.L1BlockRef) error {
	return d.s.OnL1Finalized(ctx, finalized)
}

101
func (d *Driver) OnUnsafeL2Payload(ctx context.Context, payload *eth.ExecutionPayload) error {
102 103 104
	return d.s.OnUnsafeL2Payload(ctx, payload)
}

105 106 107 108
func (d *Driver) ResetDerivationPipeline(ctx context.Context) error {
	return d.s.ResetDerivationPipeline(ctx)
}

109 110 111 112
func (d *Driver) SyncStatus(ctx context.Context) (*SyncStatus, error) {
	return d.s.SyncStatus(ctx)
}

113 114 115 116 117 118
func (d *Driver) Start(ctx context.Context) error {
	return d.s.Start(ctx)
}
func (d *Driver) Close() error {
	return d.s.Close()
}