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

race condition: reset trusted state memory var (#2602) #2603

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ to be read in order to provide the private keys to sign the L1 txs
| **Additional items** | False |
| **Tuple validation** | See below |

| Each item of this array must be | Description |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------ |
| [PrivateKeys items](#EthTxManager_PrivateKeys_items) | KeystoreFileConfig has all the information needed to load a private key from a k ... |
| Each item of this array must be | Description |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| [PrivateKeys items](#EthTxManager_PrivateKeys_items) | KeystoreFileConfig has all the information needed to load a private key from a key store file |

#### <a name="autogenerated_heading_2"></a>6.3.1. [EthTxManager.PrivateKeys.PrivateKeys items]

Expand Down
19 changes: 16 additions & 3 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,15 @@ func (s *ClientSynchronizer) processSequenceBatches(sequencedBatches []etherman.
return err
}

// Clean trustedState sync variables to avoid sync the trusted state from the wrong starting point.
// This wrong starting point would force the trusted sync to clean the virtualization of the batch reaching an inconsistency.
s.trustedState.lastTrustedBatches = nil
s.trustedState.lastStateRoot = nil

// Reset trusted state
previousBatchNumber := batch.BatchNumber - 1
if tBatch.StateRoot == (common.Hash{}) {
log.Warn("cleaning state before inserting batch from L1. Clean until batch: %d", previousBatchNumber)
log.Warnf("cleaning state before inserting batch from L1. Clean until batch: %d", previousBatchNumber)
} else {
log.Warnf("missmatch in trusted state detected, discarding batches until batchNum %d", previousBatchNumber)
}
Expand Down Expand Up @@ -1086,7 +1091,12 @@ func (s *ClientSynchronizer) processSequenceForceBatch(sequenceForceBatch []ethe
log.Errorf("error getting lastVirtualBatchNumber. BlockNumber: %d, error: %v", block.BlockNumber, err)
return err
}
// Second, reset trusted state
// Clean trustedState sync variables to avoid sync the trusted state from the wrong starting point.
// This wrong starting point would force the trusted sync to clean the virtualization of the batch reaching an inconsistency.
s.trustedState.lastTrustedBatches = nil
s.trustedState.lastStateRoot = nil

// Reset trusted state
err = s.state.ResetTrustedState(s.ctx, lastVirtualizedBatchNumber, dbTx) // This method has to reset the forced batches deleting the batchNumber for higher batchNumbers
if err != nil {
log.Errorf("error resetting trusted state. BatchNumber: %d, BlockNumber: %d, error: %v", lastVirtualizedBatchNumber, block.BlockNumber, err)
Expand Down Expand Up @@ -1354,8 +1364,10 @@ func (s *ClientSynchronizer) processTrustedBatch(trustedBatch *types.Batch, dbTx

// Find txs to be processed and included in the trusted state
if *s.trustedState.lastStateRoot == batches[1].StateRoot {
prevBatch := uint64(trustedBatch.Number) - 1
log.Info("Cleaning state until batch: ", prevBatch)
// Delete txs that were stored before restart. We need to reprocess all txs because the intermediary stateRoot is only stored in memory
err := s.state.ResetTrustedState(s.ctx, uint64(trustedBatch.Number)-1, dbTx)
err := s.state.ResetTrustedState(s.ctx, prevBatch, dbTx)
if err != nil {
log.Error("error resetting trusted state. Error: ", err)
return nil, nil, err
Expand All @@ -1382,6 +1394,7 @@ func (s *ClientSynchronizer) processTrustedBatch(trustedBatch *types.Batch, dbTx
if forkID >= forkID5 {
syncedEfficiencyPercentages = syncedEfficiencyPercentages[len(storedTxs):]
}
log.Infof("Processing %d new txs with forkID: %d", len(txsToBeAdded), forkID)

request.Transactions, err = state.EncodeTransactions(txsToBeAdded, syncedEfficiencyPercentages, forkID)
if err != nil {
Expand Down