Skip to content

Commit

Permalink
Fix adding tx that matches with tx that is being processed (0xPolygon…
Browse files Browse the repository at this point in the history
…Hermez#3559)

* fix adding  tx that matches (same addr and nonce) tx that is being processing

* fix generate mocks

* fix updateCurrentNonceBalance
  • Loading branch information
agnusmor authored and Stefan-Ethernal committed May 21, 2024
1 parent d9ec8f0 commit cfeb68d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions sequencer/addrqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (a *addrQueue) updateCurrentNonceBalance(nonce *uint64, balance *big.Int) (
if oldReadyTx != nil && oldReadyTx.Nonce > a.currentNonce {
log.Infof("set readyTx %s as notReadyTx from addrQueue %s", oldReadyTx.HashStr, a.fromStr)
a.notReadyTxs[oldReadyTx.Nonce] = oldReadyTx
} else if oldReadyTx != nil { // if oldReadyTx doesn't have a valid nonce then we add it to the txsToDelete
reason := runtime.ErrIntrinsicInvalidNonce.Error()
oldReadyTx.FailedReason = &reason
txsToDelete = append(txsToDelete, oldReadyTx)
}

return a.readyTx, oldReadyTx, txsToDelete
Expand Down
4 changes: 2 additions & 2 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ func (f *finalizer) checkL1InfoTreeUpdate(ctx context.Context) {
if firstL1InfoRootUpdate || l1InfoRoot.L1InfoTreeIndex > f.lastL1InfoTree.L1InfoTreeIndex {
log.Infof("received new l1InfoRoot %s, index: %d, l1Block: %d", l1InfoRoot.L1InfoTreeRoot, l1InfoRoot.L1InfoTreeIndex, l1InfoRoot.BlockNumber)

// Check if new l1InfoRoot is valid. We skip it if l1InfoRoot.BlockNumber == 0 (empty tree)
if l1InfoRoot.BlockNumber > 0 {
// Check if new l1InfoRoot is valid. We skip it if l1InfoTreeIndex is 0 (it's a special case)
if l1InfoRoot.L1InfoTreeIndex > 0 {
valid, err := f.checkValidL1InfoRoot(ctx, l1InfoRoot)
if err != nil {
log.Errorf("error validating new l1InfoRoot, index: %d, error: %v", l1InfoRoot.L1InfoTreeIndex, err)
Expand Down
20 changes: 20 additions & 0 deletions sequencer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Worker struct {
state stateInterface
batchConstraints state.BatchConstraintsCfg
readyTxsCond *timeoutCond
wipTx *TxTracker
}

// NewWorker creates an init a worker
Expand Down Expand Up @@ -60,6 +61,12 @@ func (w *Worker) AddTxTracker(ctx context.Context, tx *TxTracker) (replacedTx *T
return nil, pool.ErrOutOfCounters
}

if (w.wipTx != nil) && (w.wipTx.FromStr == tx.FromStr) && (w.wipTx.Nonce == tx.Nonce) {
log.Infof("adding tx %s (nonce %d) from address %s that matches current processing tx %s (nonce %d), rejecting it as duplicated nonce", tx.Hash, tx.Nonce, tx.From, w.wipTx.Hash, w.wipTx.Nonce)
w.workerMutex.Unlock()
return nil, ErrDuplicatedNonce
}

addr, found := w.pool[tx.FromStr]
if !found {
// Unlock the worker to let execute other worker functions while creating the new AddrQueue
Expand Down Expand Up @@ -174,6 +181,8 @@ func (w *Worker) MoveTxToNotReady(txHash common.Hash, from common.Address, actua
defer w.workerMutex.Unlock()
log.Debugf("move tx %s to notReady (from: %s, actualNonce: %d, actualBalance: %s)", txHash.String(), from.String(), actualNonce, actualBalance.String())

w.resetWipTx(txHash)

addrQueue, found := w.pool[from.String()]
if found {
// Sanity check. The txHash must be the readyTx
Expand All @@ -195,6 +204,8 @@ func (w *Worker) DeleteTx(txHash common.Hash, addr common.Address) {
w.workerMutex.Lock()
defer w.workerMutex.Unlock()

w.resetWipTx(txHash)

addrQueue, found := w.pool[addr.String()]
if found {
deletedReadyTx := addrQueue.deleteTx(txHash)
Expand Down Expand Up @@ -293,6 +304,8 @@ func (w *Worker) GetBestFittingTx(resources state.BatchResources) (*TxTracker, e
w.workerMutex.Lock()
defer w.workerMutex.Unlock()

w.wipTx = nil

if w.txSortedList.len() == 0 {
return nil, ErrTransactionsListEmpty
}
Expand Down Expand Up @@ -342,6 +355,7 @@ func (w *Worker) GetBestFittingTx(resources state.BatchResources) (*TxTracker, e

if foundAt != -1 {
log.Debugf("best fitting tx %s found at index %d with gasPrice %d", tx.HashStr, foundAt, tx.GasPrice)
w.wipTx = tx
return tx, nil
} else {
return nil, ErrNoFittingTransaction
Expand Down Expand Up @@ -382,3 +396,9 @@ func (w *Worker) addTxToSortedList(readyTx *TxTracker) {
w.readyTxsCond.L.Unlock()
}
}

func (w *Worker) resetWipTx(txHash common.Hash) {
if (w.wipTx != nil) && (w.wipTx.Hash == txHash) {
w.wipTx = nil
}
}
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ generate-mocks-sequencer: ## Generates mocks for sequencer , using mockery tool
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=stateInterface --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=StateMock --filename=mock_state.go
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=txPool --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=PoolMock --filename=mock_pool.go
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=Tx --srcpkg=github.com/jackc/pgx/v4 --output=../sequencer --outpkg=sequencer --structname=DbTxMock --filename=mock_dbtx.go
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=etherman --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=EthermanMock --filename=mock_etherman.go
export "GOROOT=$$(go env GOROOT)" && $$(go env GOPATH)/bin/mockery --name=ethermanInterface --dir=../sequencer --output=../sequencer --outpkg=sequencer --inpackage --structname=EthermanMock --filename=mock_etherman.go

.PHONY: generate-mocks-sequencesender
generate-mocks-sequencesender: ## Generates mocks for sequencesender , using mockery tool
Expand Down

0 comments on commit cfeb68d

Please sign in to comment.