From 6d489eab574d0c964825f2c3188eee46f459514d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= <58293609+ToniRamirezM@users.noreply.github.com> Date: Mon, 9 Sep 2024 16:19:11 +0200 Subject: [PATCH] feat: protect ssender nonce (#67) * feat: protect ssender nonce --- sequencesender/sequencesender.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sequencesender/sequencesender.go b/sequencesender/sequencesender.go index 82e60034..8c96789a 100644 --- a/sequencesender/sequencesender.go +++ b/sequencesender/sequencesender.go @@ -32,6 +32,7 @@ type SequenceSender struct { ethTxManager *ethtxmanager.Client etherman *etherman.Client currentNonce uint64 + nonceMutex sync.Mutex latestVirtualBatch uint64 // Latest virtualized batch obtained from L1 latestVirtualTime time.Time // Latest virtual batch timestamp latestSentToL1Batch uint64 // Latest batch sent to L1 @@ -136,12 +137,14 @@ func (s *SequenceSender) Start(ctx context.Context) { // Get current nonce var err error + s.nonceMutex.Lock() s.currentNonce, err = s.etherman.CurrentNonce(ctx, s.cfg.L2Coinbase) if err != nil { log.Fatalf("failed to get current nonce from %v, error: %v", s.cfg.L2Coinbase, err) } else { log.Infof("current nonce for %v is %d", s.cfg.L2Coinbase, s.currentNonce) } + s.nonceMutex.Unlock() // Get latest virtual state batch from L1 err = s.updateLatestVirtualBatch() @@ -572,8 +575,12 @@ func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *com var valueToAddress common.Address if !resend { + s.nonceMutex.Lock() + nonce := s.currentNonce + s.currentNonce++ + s.nonceMutex.Unlock() + paramNonce = &nonce paramTo = to - paramNonce = &s.currentNonce paramData = data valueFromBatch = fromBatch valueToBatch = toBatch @@ -598,9 +605,6 @@ func (s *SequenceSender) sendTx(ctx context.Context, resend bool, txOldHash *com log.Errorf("error adding sequence to ethtxmanager: %v", err) return err } - if !resend { - s.currentNonce++ - } // Add new eth tx txData := ethTxData{