From 35d7026fda45092574e8c5a04b5d78552ae8cf2b Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Tue, 2 Apr 2024 13:29:01 +0100 Subject: [PATCH] Host: notify batch subscribers of new batches from any src --- go/host/enclave/guardian.go | 9 +++------ go/host/enclave/state.go | 6 ------ go/host/l2/batchrepository.go | 11 +++++------ 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/go/host/enclave/guardian.go b/go/host/enclave/guardian.go index f1af86d57d..d4d681ca50 100644 --- a/go/host/enclave/guardian.go +++ b/go/host/enclave/guardian.go @@ -186,14 +186,11 @@ func (g *Guardian) HandleBlock(block *types.Block) { // HandleBatch is called by the L2 repository when a new batch arrives // Note: this should only be called for validators, sequencers produce their own batches func (g *Guardian) HandleBatch(batch *common.ExtBatch) { - if g.hostData.IsSequencer { - g.logger.Error("Repo received batch but we are a sequencer, ignoring") - return - } - g.logger.Debug("Received L2 block", log.BatchHashKey, batch.Hash(), log.BatchSeqNoKey, batch.Header.SequencerOrderNo) + g.logger.Debug("Host received L2 batch", log.BatchHashKey, batch.Hash(), log.BatchSeqNoKey, batch.Header.SequencerOrderNo) // record the newest batch we've seen g.state.OnReceivedBatch(batch.Header.SequencerOrderNo) - if !g.state.IsUpToDate() { + // Sequencer enclaves produce batches, they cannot receive them. Also, enclave will reject new batches if it is not up-to-date + if g.hostData.IsSequencer || !g.state.IsUpToDate() { return // ignore batches until we're up-to-date } err := g.submitL2Batch(batch) diff --git a/go/host/enclave/state.go b/go/host/enclave/state.go index 7808676a56..3af6066278 100644 --- a/go/host/enclave/state.go +++ b/go/host/enclave/state.go @@ -97,12 +97,6 @@ func (s *StateTracker) OnReceivedBlock(l1Head gethcommon.Hash) { func (s *StateTracker) OnProcessedBatch(enclL2HeadSeqNo *big.Int) { s.m.Lock() defer s.m.Unlock() - if s.hostL2Head == nil || s.hostL2Head.Cmp(enclL2HeadSeqNo) < 0 { - // we've successfully processed this batch, so the host's head should be at least as high as the enclave's (this shouldn't happen, we want it to be visible if it happens) - s.logger.Trace("host head behind enclave head - updating to match", "hostHead", s.hostL2Head, "enclaveHead", enclL2HeadSeqNo) - s.hostL2Head = enclL2HeadSeqNo - } - s.enclaveL2Head = enclL2HeadSeqNo s.setStatus(s.calculateStatus()) } diff --git a/go/host/l2/batchrepository.go b/go/host/l2/batchrepository.go index 78227310c7..7fbd46f556 100644 --- a/go/host/l2/batchrepository.go +++ b/go/host/l2/batchrepository.go @@ -111,12 +111,6 @@ func (r *Repository) HandleBatches(batches []*common.ExtBatch, isLive bool) { // we've already seen this batch or failed to store it for another reason - do not notify subscribers return } - if isLive { - // notify subscribers if the batch is new - for _, subscriber := range r.subscribers { - go subscriber.HandleBatch(batch) - } - } } } @@ -175,6 +169,7 @@ func (r *Repository) FetchBatchBySeqNo(seqNo *big.Int) (*common.ExtBatch, error) // If the repository already has the batch it returns an AlreadyExists error which is typically ignored. func (r *Repository) AddBatch(batch *common.ExtBatch) error { r.logger.Debug("Saving batch", log.BatchSeqNoKey, batch.Header.SequencerOrderNo, log.BatchHashKey, batch.Hash()) + // this returns an error if the batch already exists in the db err := r.db.AddBatch(batch) if err != nil { return err @@ -184,6 +179,10 @@ func (r *Repository) AddBatch(batch *common.ExtBatch) error { defer r.latestSeqNoMutex.Unlock() if batch.Header.SequencerOrderNo.Cmp(r.latestBatchSeqNo) > 0 { r.latestBatchSeqNo = batch.Header.SequencerOrderNo + // notify subscribers, a new batch has been successfully added to the db + for _, subscriber := range r.subscribers { + go subscriber.HandleBatch(batch) + } } return nil }