Skip to content

Commit

Permalink
Host: notify batch subscribers of new batches from any src (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
BedrockSquirrel authored Apr 2, 2024
1 parent d3bf8ec commit 7190f55
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
9 changes: 3 additions & 6 deletions go/host/enclave/guardian.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions go/host/enclave/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
11 changes: 5 additions & 6 deletions go/host/l2/batchrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 7190f55

Please sign in to comment.