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

Fix tx OOC (node level) when first empty L2 block in batch #3744

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,26 @@ func (f *finalizer) finalizeBatches(ctx context.Context) {
f.finalizeWIPL2Block(ctx)
}

tx, err := f.workerIntf.GetBestFittingTx(f.wipBatch.imRemainingResources, f.wipBatch.imHighReservedZKCounters)
tx, oocTxs, err := f.workerIntf.GetBestFittingTx(f.wipBatch.imRemainingResources, f.wipBatch.imHighReservedZKCounters, (f.wipBatch.countOfL2Blocks == 0 && f.wipL2Block.isEmpty()))

// If we have txs pending to process but none of them fits into the wip batch, we close the wip batch and open a new one
// Set as invalid txs in the worker pool that will never fit into an empty batch
for _, oocTx := range oocTxs {
log.Infof("tx %s doesn't fits in empty batch %d (node OOC), setting tx as invalid in the pool", oocTx.HashStr, f.wipL2Block.trackingNum, f.wipBatch.batchNumber)

f.LogEvent(ctx, event.Level_Info, event.EventID_NodeOOC,
fmt.Sprintf("tx %s doesn't fits in empty batch %d (node OOC), from: %s, IP: %s", oocTx.HashStr, f.wipBatch.batchNumber, oocTx.FromStr, oocTx.IP), nil)

// Delete the transaction from the worker
f.workerIntf.DeleteTx(oocTx.Hash, oocTx.From)

errMsg := "node OOC"
err = f.poolIntf.UpdateTxStatus(ctx, oocTx.Hash, pool.TxStatusInvalid, false, &errMsg)
if err != nil {
log.Errorf("failed to update status to invalid in the pool for tx %s, error: %v", oocTx.Hash.String(), err)
}
}

// We have txs pending to process but none of them fits into the wip batch we close the wip batch and open a new one
if err == ErrNoFittingTransaction {
f.finalizeWIPBatch(ctx, state.NoTxFitsClosingReason)
continue
Expand Down
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type stateInterface interface {
}

type workerInterface interface {
GetBestFittingTx(remainingResources state.BatchResources, highReservedCounters state.ZKCounters) (*TxTracker, error)
GetBestFittingTx(remainingResources state.BatchResources, highReservedCounters state.ZKCounters, fistL2Block bool) (*TxTracker, []*TxTracker, error)
UpdateAfterSingleSuccessfulTxExecution(from common.Address, touchedAddresses map[common.Address]*state.InfoReadWrite) []*TxTracker
UpdateTxZKCounters(txHash common.Hash, from common.Address, usedZKCounters state.ZKCounters, reservedZKCounters state.ZKCounters)
AddTxTracker(ctx context.Context, txTracker *TxTracker) (replacedTx *TxTracker, dropReason error)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/l2block.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func (f *finalizer) openNewWIPL2Block(ctx context.Context, prevTimestamp uint64,

f.wipBatch.imHighReservedZKCounters = newHighZKCounters
} else {
log.Infof("new wip L2 block [%d] reserved resources exceeds the remaining batch resources, overflow resource: %s, closing WIP batch and creating new one. counters: {batch: %s, used: %s, reserved: %s, needed: %s, high: %s}",
log.Infof("new wip L2 block [%d] needed resources exceeds the remaining batch resources, overflow resource: %s, closing WIP batch and creating new one. counters: {batch: %s, used: %s, reserved: %s, needed: %s, high: %s}",
f.wipL2Block.trackingNum, overflowResource,
f.logZKCounters(f.wipBatch.imRemainingResources.ZKCounters), f.logZKCounters(f.wipL2Block.usedZKCountersOnNew), f.logZKCounters(f.wipL2Block.reservedZKCountersOnNew), f.logZKCounters(neededZKCounters), f.logZKCounters(f.wipBatch.imHighReservedZKCounters))
}
Expand Down
33 changes: 21 additions & 12 deletions sequencer/mock_worker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions sequencer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func (w *Worker) DeleteTxPendingToStore(txHash common.Hash, addr common.Address)
}

// GetBestFittingTx gets the most efficient tx that fits in the available batch resources
func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highReservedCounters state.ZKCounters) (*TxTracker, error) {
func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highReservedCounters state.ZKCounters, isFistL2BlockAndEmpty bool) (*TxTracker, []*TxTracker, error) {
w.workerMutex.Lock()
defer w.workerMutex.Unlock()

Expand All @@ -417,7 +417,7 @@ func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highR
w.reorgedTxs = w.reorgedTxs[1:]
if addrQueue, found := w.pool[reorgedTx.FromStr]; found {
if addrQueue.readyTx != nil && addrQueue.readyTx.Hash == reorgedTx.Hash {
return reorgedTx, nil
return reorgedTx, nil, nil
} else {
log.Warnf("reorged tx %s is not the ready tx for addrQueue %s, this shouldn't happen", reorgedTx.Hash, reorgedTx.From)
}
Expand All @@ -427,12 +427,14 @@ func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highR
}

if w.txSortedList.len() == 0 {
return nil, ErrTransactionsListEmpty
return nil, nil, ErrTransactionsListEmpty
}

var (
tx *TxTracker
foundMutex sync.RWMutex
tx *TxTracker
foundMutex sync.RWMutex
oocTxs []*TxTracker
oocTxsMutex sync.Mutex
)

nGoRoutines := runtime.NumCPU()
Expand All @@ -457,7 +459,14 @@ func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highR
needed, _ := getNeededZKCounters(highReservedCounters, txCandidate.UsedZKCounters, txCandidate.ReservedZKCounters)
fits, _ := bresources.Fits(state.BatchResources{ZKCounters: needed, Bytes: txCandidate.Bytes})
if !fits {
// We don't add this Tx
// If we are looking for a tx for the first empty L2 block in the batch and this tx doesn't fits in the batch, then this tx will never fit in any batch.
// We add the tx to the oocTxs slice. That slice will be returned to set these txs as invalid (and delete them from the worker) from the finalizer code
if isFistL2BlockAndEmpty {
oocTxsMutex.Lock()
oocTxs = append(oocTxs, txCandidate)
oocTxsMutex.Unlock()
}
// We continue looking for a tx that fits in the batch
continue
}

Expand All @@ -477,9 +486,15 @@ func (w *Worker) GetBestFittingTx(remainingResources state.BatchResources, highR
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
return tx, oocTxs, nil
} else {
return nil, ErrNoFittingTransaction
// If the length of the oocTxs slice is equal to the length of the txSortedList this means that all the txs are ooc,
// therefore we need to return an error indicating that the list is empty
if w.txSortedList.len() == len(oocTxs) {
return nil, oocTxs, ErrTransactionsListEmpty
} else {
return nil, oocTxs, ErrNoFittingTransaction
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion sequencer/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestWorkerGetBestTx(t *testing.T) {
ct := 0

for {
tx, _ := worker.GetBestFittingTx(rc, state.ZKCounters{})
tx, _, _ := worker.GetBestFittingTx(rc, state.ZKCounters{}, true)
if tx != nil {
if ct >= len(expectedGetBestTx) {
t.Fatalf("Error getting more best tx than expected. Expected=%d, Actual=%d", len(expectedGetBestTx), ct+1)
Expand Down
Loading