Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have l2 blocks unblock preconf when there are gaps #132

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion optimism/op-e2e/actions/helpers/l2_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func NewL2Verifier(t Testing, log log.Logger, l1 derive.L1Fetcher,
Drain: executor.Drain,
}, opts)

sys.Register("engine", engine.NewEngDeriver(log, ctx, cfg, metrics, ec), opts)
channels := engine.StartPreconf(ctx, eng)
sys.Register("engine", engine.NewEngDeriver(log, ctx, cfg, metrics, ec, channels), opts)

rollupNode := &L2Verifier{
eventSys: sys,
Expand Down
5 changes: 3 additions & 2 deletions optimism/op-node/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (n *OpNode) init(ctx context.Context, cfg *Config) error {
if err := n.initMetricsServer(cfg); err != nil {
return fmt.Errorf("failed to init the metrics server: %w", err)
}
n.preconfChannels = engine.StartPreconf(ctx, n.l2Source)
n.metrics.RecordInfo(n.appVersion)
n.metrics.RecordUp()
if err := n.initPProf(cfg); err != nil {
Expand Down Expand Up @@ -399,6 +398,8 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config) error {
return err
}

n.preconfChannels = engine.StartPreconf(ctx, n.l2Source)

if cfg.Rollup.InteropTime != nil {
cl, err := cfg.Supervisor.SupervisorClient(ctx, n.log)
if err != nil {
Expand Down Expand Up @@ -429,7 +430,7 @@ func (n *OpNode) initL2(ctx context.Context, cfg *Config) error {
n.safeDB = safedb.Disabled
}
n.l2Driver = driver.NewDriver(n.eventSys, n.eventDrain, &cfg.Driver, &cfg.Rollup, n.l2Source, n.l1Source,
n.supervisor, n.beacon, n, n, n.log, n.metrics, cfg.ConfigPersistence, n.safeDB, &cfg.Sync, sequencerConductor, altDA)
n.supervisor, n.beacon, n, n, n.log, n.metrics, cfg.ConfigPersistence, n.safeDB, &cfg.Sync, sequencerConductor, altDA, n.preconfChannels)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion optimism/op-node/rollup/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func NewDriver(
syncCfg *sync.Config,
sequencerConductor conductor.SequencerConductor,
altDA AltDAIface,
preconfChannels engine.PreconfChannels,
) *Driver {
driverCtx, driverCancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -239,7 +240,7 @@ func NewDriver(
}
sys.Register("sync", syncDeriver, opts)

sys.Register("engine", engine.NewEngDeriver(log, driverCtx, cfg, metrics, ec), opts)
sys.Register("engine", engine.NewEngDeriver(log, driverCtx, cfg, metrics, ec, preconfChannels), opts)

schedDeriv := NewStepSchedulingDeriver(log)
sys.Register("step-scheduler", schedDeriv, opts)
Expand Down
25 changes: 14 additions & 11 deletions optimism/op-node/rollup/engine/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,24 +263,26 @@ func (ev CrossUpdateRequestEvent) String() string {
type EngDeriver struct {
metrics Metrics

log log.Logger
cfg *rollup.Config
ec *EngineController
ctx context.Context
emitter event.Emitter
log log.Logger
cfg *rollup.Config
ec *EngineController
ctx context.Context
emitter event.Emitter
preconfChannels PreconfChannels
}

var _ event.Deriver = (*EngDeriver)(nil)

func NewEngDeriver(log log.Logger, ctx context.Context, cfg *rollup.Config,
metrics Metrics, ec *EngineController,
metrics Metrics, ec *EngineController, preconfChannels PreconfChannels,
) *EngDeriver {
return &EngDeriver{
log: log,
cfg: cfg,
ec: ec,
ctx: ctx,
metrics: metrics,
log: log,
cfg: cfg,
ec: ec,
ctx: ctx,
metrics: metrics,
preconfChannels: preconfChannels,
}
}

Expand Down Expand Up @@ -429,6 +431,7 @@ func (d *EngDeriver) OnEvent(ev event.Event) bool {
d.emitter.Emit(PromoteSafeEvent(x))
}
case PromoteSafeEvent:
d.preconfChannels.SendL2Block(&x.Ref)
d.log.Debug("Updating safe", "safe", x.Ref, "unsafe", d.ec.UnsafeL2Head())
d.ec.SetSafeHead(x.Ref)
// Finalizer can pick up this safe cross-block now
Expand Down
19 changes: 18 additions & 1 deletion optimism/op-node/rollup/engine/preconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ type PreconfChannels struct {
EnvCh chan *eth.SignedEnv
NewFragCh chan *eth.SignedNewFrag
SealCh chan *eth.SignedSeal
l2BlockCh chan *eth.L2BlockRef
}

func NewPreconfChannels() PreconfChannels {
return PreconfChannels{
EnvCh: make(chan *eth.SignedEnv),
NewFragCh: make(chan *eth.SignedNewFrag),
SealCh: make(chan *eth.SignedSeal),
l2BlockCh: make(chan *eth.L2BlockRef),
}
}

func (c *PreconfChannels) SendEnv(e *eth.SignedEnv) { c.EnvCh <- e }
func (c *PreconfChannels) SendFrag(f *eth.SignedNewFrag) { c.NewFragCh <- f }
func (c *PreconfChannels) SendSeal(s *eth.SignedSeal) { c.SealCh <- s }
func (c *PreconfChannels) SendL2Block(b *eth.L2BlockRef) { c.l2BlockCh <- b }

type FragIndex struct {
BlockNumber uint64
Expand Down Expand Up @@ -54,6 +57,7 @@ type PreconfState struct {
sentFrags map[FragIndex]bool
pendingSeals map[uint64]eth.SignedSeal
sentSeals map[uint64]bool
sentL2Blocks map[uint64]bool
ctx context.Context
e ExecEngine
}
Expand All @@ -68,6 +72,7 @@ func NewPreconfState(ctx context.Context, e ExecEngine) PreconfState {
sentFrags: make(map[FragIndex]bool),
pendingSeals: make(map[uint64]eth.SignedSeal),
sentSeals: make(map[uint64]bool),
sentL2Blocks: make(map[uint64]bool),
ctx: ctx,
e: e,
}
Expand All @@ -83,7 +88,7 @@ func StartPreconf(ctx context.Context, e ExecEngine) PreconfChannels {
// Checks if the state is new or if the previous block is sealed.
func (s *PreconfState) putEnv(sEnv *eth.SignedEnv) {
env := sEnv.Env
if s.JustStarted || s.sentSeals[env.Number-1] {
if s.JustStarted || s.sentSeals[env.Number-1] || s.sentL2Blocks[env.Number] {
s.sentEnvs[env.Number] = true
s.JustStarted = false
s.e.Env(s.ctx, sEnv)
Expand Down Expand Up @@ -148,6 +153,16 @@ func (s *PreconfState) putSeal(sSeal *eth.SignedSeal) {
}
}

// Checks if there's envs blocked because of gaps and sends them over.
func (s *PreconfState) putL2Block(block *eth.L2BlockRef) {
s.sentL2Blocks[block.Number] = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this always-growing hashmap be a problem after some time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the same with all the rest. I'll add pruning for all in a separate thread.

nextEnv, ok := s.pendingEnvs[block.Number]
if ok {
delete(s.pendingEnvs, block.Number)
s.putEnv(&nextEnv)
}
}

// Listens for env, frag and seal events and updates the local state.
// If the events are ready, they are sent to the engine api. If not, they are
// saved in the local state as pending until they are.
Expand All @@ -162,6 +177,8 @@ func preconfHandler(ctx context.Context, c PreconfChannels, e ExecEngine) {
state.putFrag(frag)
case seal := <-c.SealCh:
state.putSeal(seal)
case l2Block := <-c.l2BlockCh:
state.putL2Block(l2Block)
}
}
}
70 changes: 70 additions & 0 deletions optimism/op-node/rollup/engine/preconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,73 @@ func TestEverythingOutOfOrderAllAtOnce(t *testing.T) {
t.Fatalf("The first seal was not sent to the engine api")
}
}

func TestPreconfGapSavedByL2Block(t *testing.T) {
var m MockEngine
state := NewPreconfState(context.Background(), &m)

// Data for the first block
e := env()
f := frag()
f2 := f
f2.Frag.Seq = 1
f2.Frag.IsLast = true
s := seal()

// Data for the third block, this test case assumes the second is missing.
e3 := e
e3.Env.Number += 2
f3 := f
f3.Frag.BlockNumber += 2
f3.Frag.IsLast = true
s3 := s
s3.Seal.BlockNumber += 2

state.putEnv(&e)
if !cmp.Equal(m.SeenEnvs[0], e, cmp.AllowUnexported(big.Int{})) {
t.Fatalf("The first env was not sent to the engine api.")
}

state.putFrag(&f)
if !cmp.Equal(m.SeenNewFrags[0], f) {
t.Fatalf("The first frag was not pushed correctly")
}
state.putFrag(&f2)
if !cmp.Equal(m.SeenNewFrags[1], f2) {
t.Fatalf("The second frag was not pushed correctly")
}
state.putSeal(&s)
if !cmp.Equal(m.SeenSeals[0], s) {
t.Fatalf("The first seal was not pushed correctly")
}

// Now when we receive the third block it should not be pushed.
state.putEnv(&e3)
if !cmp.Equal(len(m.SeenEnvs), 1, cmp.AllowUnexported(big.Int{})) {
t.Fatalf("The env for the third block shouldn't have been sent")
}
state.putFrag(&f3)
if !cmp.Equal(len(m.SeenNewFrags), 2) {
t.Fatalf("The frag for the third block shouldn't have been sent")
}
state.putSeal(&s3)
if !cmp.Equal(len(m.SeenSeals), 1) {
t.Fatalf("The seal for the third block shouldn't have been sent")
}

// Now we send an l2 block ref and everything from the third block should unblock.

b := eth.L2BlockRef{
Number: 3,
}
state.putL2Block(&b)
if !cmp.Equal(m.SeenEnvs[1], e3, cmp.AllowUnexported(big.Int{})) {
t.Fatalf("The env for the third block was not pushed correctly")
}
if !cmp.Equal(m.SeenNewFrags[2], f3) {
t.Fatalf("The frag for the third block was not pushed correctly")
}
if !cmp.Equal(m.SeenSeals[1], s3) {
t.Fatalf("The seal for the third block was not pushed correctly")
}
}
3 changes: 2 additions & 1 deletion optimism/op-program/client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func NewDriver(logger log.Logger, cfg *rollup.Config, l1Source derive.L1Fetcher,
pipelineDeriver.AttachEmitter(d)

ec := engine.NewEngineController(l2Source, logger, metrics.NoopMetrics, cfg, &sync.Config{SyncMode: sync.CLSync}, d)
engineDeriv := engine.NewEngDeriver(logger, context.Background(), cfg, metrics.NoopMetrics, ec)
channels := engine.StartPreconf(context.Background(), l2Source)
engineDeriv := engine.NewEngDeriver(logger, context.Background(), cfg, metrics.NoopMetrics, ec, channels)
engineDeriv.AttachEmitter(d)
syncCfg := &sync.Config{SyncMode: sync.CLSync}
engResetDeriv := engine.NewEngineResetDeriver(context.Background(), logger, cfg, l1Source, l2Source, syncCfg)
Expand Down
Loading