Skip to content

Commit

Permalink
feat: protect ssender nonce (#67)
Browse files Browse the repository at this point in the history
* feat: protect ssender nonce
  • Loading branch information
ToniRamirezM authored Sep 9, 2024
1 parent 2a5f311 commit 10fee93
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sequencesender/sequencesender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down

0 comments on commit 10fee93

Please sign in to comment.