Skip to content

Commit

Permalink
chore: remove the mutex from updateLatestVirtualBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Oct 10, 2024
1 parent c14d806 commit 1bf2dd3
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ type SequenceSender struct {
validStream bool // Not valid while receiving data before the desired batch
seqSendingStopped uint32 // If there is a critical error
TxBuilder txbuilder.TxBuilder
latestVirtualBatchLock sync.Mutex
}

type sequenceData struct {
Expand Down Expand Up @@ -124,7 +123,7 @@ func (s *SequenceSender) Start(ctx context.Context) {
go s.ethTxManager.Start()

// Get latest virtual state batch from L1
err := s.getLatestVirtualBatch()
err := s.updateLatestVirtualBatch()
if err != nil {
s.logger.Fatalf("error getting latest sequenced batch, error: %v", err)
}
Expand Down Expand Up @@ -279,24 +278,24 @@ func (s *SequenceSender) purgeSequences() {
func (s *SequenceSender) tryToSendSequence(ctx context.Context) {
// Update latest virtual batch
s.logger.Infof("updating virtual batch")
err := s.getLatestVirtualBatch()
err := s.updateLatestVirtualBatch()
if err != nil {
return
}

// Check if the sequence sending is stopped
if s.IsStopped() {
s.logger.Warnf("sending is stopped!")
return
}

// Update state of transactions
s.logger.Infof("updating tx results")
pendingTxsCount, err := s.syncEthTxResults(ctx)
if err != nil {
return
}

// Check if the sequence sending is stopped
if s.IsStopped() {
s.logger.Warnf("sending is stopped!")
return
}

// Check if reached the maximum number of pending transactions
if pendingTxsCount >= s.cfg.MaxPendingTx {
s.logger.Infof("max number of pending txs (%d) reached. Waiting for some to be completed", pendingTxsCount)
Expand Down Expand Up @@ -386,7 +385,7 @@ func (s *SequenceSender) tryToSendSequence(ctx context.Context) {
}

// Get latest virtual state batch from L1
err = s.getLatestVirtualBatch()
err = s.updateLatestVirtualBatch()
if err != nil {
s.logger.Fatalf("error getting latest sequenced batch, error: %v", err)
}
Expand Down Expand Up @@ -481,22 +480,16 @@ func (s *SequenceSender) getSequencesToSend(ctx context.Context) (seqsendertypes
return nil, nil
}

// getLatestVirtualBatch queries the value in L1 and updates the latest virtual batch field
func (s *SequenceSender) getLatestVirtualBatch() error {
s.latestVirtualBatchLock.Lock()
defer s.latestVirtualBatchLock.Unlock()

// updateLatestVirtualBatch queries the value in L1 and updates the latest virtual batch field
func (s *SequenceSender) updateLatestVirtualBatch() error {
// Get latest virtual state batch from L1
var err error

latestVirtualBatchNumber, err := s.etherman.GetLatestBatchNumber()
if err != nil {
s.logger.Errorf("error getting latest virtual batch, error: %v", err)
return errors.New("fail to get latest virtual batch")
}

atomic.StoreUint64(&s.latestVirtualBatchNumber, latestVirtualBatchNumber)

s.logger.Infof("latest virtual batch is %d", latestVirtualBatchNumber)

return nil
Expand Down

0 comments on commit 1bf2dd3

Please sign in to comment.