From 519cfcf0b136b28359029ba96ac6a23c3f1ca9cc Mon Sep 17 00:00:00 2001 From: Alonso Rodriguez Date: Fri, 29 Sep 2023 18:55:09 +0400 Subject: [PATCH] race condition: reset trusted state memory var (#2602) * race condition: reset trusted state memory var * linter * doc * comment --- docs/config-file/node-config-doc.md | 6 +++--- synchronizer/synchronizer.go | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/docs/config-file/node-config-doc.md b/docs/config-file/node-config-doc.md index 1943d1e2d5..319c14e0f9 100644 --- a/docs/config-file/node-config-doc.md +++ b/docs/config-file/node-config-doc.md @@ -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 | #### 6.3.1. [EthTxManager.PrivateKeys.PrivateKeys items] diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index a6fc5f05dc..17de5f6d47 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -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) } @@ -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) @@ -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 @@ -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 {