Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: protect ssender nonce #67

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading