From 7e62149f4a16fa72c2d43f68b23632b9a67f362f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Thu, 3 Oct 2024 14:01:51 +0200 Subject: [PATCH 1/2] feat: proof cleaning --- aggregator/aggregator.go | 67 +++++++++++++++------------------------- aggregator/interfaces.go | 1 + 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index 359021eb..dd3be1a9 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -31,6 +31,7 @@ import ( synclog "github.com/0xPolygonHermez/zkevm-synchronizer-l1/log" "github.com/0xPolygonHermez/zkevm-synchronizer-l1/state/entities" "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer" + "github.com/0xPolygonHermez/zkevm-synchronizer-l1/synchronizer/l1_check_block" "github.com/ethereum/go-ethereum/common" "go.uber.org/zap/zapcore" "google.golang.org/grpc" @@ -990,7 +991,7 @@ func (a *Aggregator) settleDirect( // process monitored batch verifications before starting a next cycle a.ethTxManager.ProcessPendingMonitoredTxs(ctx, func(result ethtxmanager.MonitoredTxResult) { - a.handleMonitoredTxResult(result) + a.handleMonitoredTxResult(result, proof.BatchNumber, proof.BatchNumberFinal) }) return true @@ -1928,57 +1929,39 @@ func (hc *healthChecker) Watch(req *grpchealth.HealthCheckRequest, server grpche }) } -func (a *Aggregator) handleMonitoredTxResult(result ethtxmanager.MonitoredTxResult) { +func (a *Aggregator) handleMonitoredTxResult(result ethtxmanager.MonitoredTxResult, firstBatch, lastBatch uint64) { mTxResultLogger := ethtxmanager.CreateMonitoredTxResultLogger(result) if result.Status == ethtxmanager.MonitoredTxStatusFailed { mTxResultLogger.Fatal("failed to send batch verification, TODO: review this fatal and define what to do in this case") } - // TODO: REVIEW THIS + // Wait for the transaction to be finalized, then we can safely delete all recursive + // proofs up to the last batch in this proof - /* - // monitoredIDFormat: "proof-from-%v-to-%v" - idSlice := strings.Split(result.ID, "-") - proofBatchNumberStr := idSlice[2] - proofBatchNumber, err := strconv.ParseUint(proofBatchNumberStr, encoding.Base10, 0) - - if err != nil { - mTxResultLogger.Errorf("failed to read final proof batch number from monitored tx: %v", err) - } - - proofBatchNumberFinalStr := idSlice[4] - proofBatchNumberFinal, err := strconv.ParseUint(proofBatchNumberFinalStr, encoding.Base10, 0) - - if err != nil { - mTxResultLogger.Errorf("failed to read final proof batch number final from monitored tx: %v", err) - } - - log := log.WithFields("txId", result.ID, "batches", fmt.Sprintf("%d-%d", proofBatchNumber, proofBatchNumberFinal)) - log.Info("Final proof verified") - - // wait for the synchronizer to catch up the verified batches - log.Debug("A final proof has been sent, waiting for the network to be synced") - - for !a.isSynced(a.ctx, &proofBatchNumberFinal) { - log.Info("Waiting for synchronizer to sync...") - time.Sleep(a.cfg.RetryTime.Duration) - } + finaLizedBlockNumber, err := l1_check_block.L1FinalizedFetch.BlockNumber(a.ctx, a.etherman) + if err != nil { + mTxResultLogger.Errorf("failed to get finalized block number: %v", err) + } - // network is synced with the final proof, we can safely delete all recursive - // proofs up to the last synced batch - err = a.State.CleanupGeneratedProofs(a.ctx, proofBatchNumberFinal, nil) + for result.MinedAtBlockNumber.Uint64() > finaLizedBlockNumber { + select { + case <-a.ctx.Done(): + return + case <-time.After(a.cfg.RetryTime.Duration): + finaLizedBlockNumber, err = l1_check_block.L1FinalizedFetch.BlockNumber(a.ctx, a.etherman) + if err != nil { + mTxResultLogger.Errorf("failed to get finalized block number: %v", err) + } + } + } - if err != nil { - log.Errorf("Failed to store proof aggregation result: %v", err) - } - */ -} + err = a.state.DeleteGeneratedProofs(a.ctx, firstBatch, lastBatch, nil) + if err != nil { + mTxResultLogger.Errorf("failed to delete generated proofs, err: %v", err) + } -/* -func buildMonitoredTxID(batchNumber, batchNumberFinal uint64) string { - return fmt.Sprintf(monitoredIDFormat, batchNumber, batchNumberFinal) + mTxResultLogger.Debugf("deleted generated proofs from %d to %d", firstBatch, lastBatch) } -*/ func (a *Aggregator) cleanupLockedProofs() { for { diff --git a/aggregator/interfaces.go b/aggregator/interfaces.go index b231de35..85676f69 100644 --- a/aggregator/interfaces.go +++ b/aggregator/interfaces.go @@ -35,6 +35,7 @@ type etherman interface { ) (to *common.Address, data []byte, err error) GetLatestBlockHeader(ctx context.Context) (*types.Header, error) GetBatchAccInputHash(ctx context.Context, batchNumber uint64) (common.Hash, error) + HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) } // aggregatorTxProfitabilityChecker interface for different profitability From 78e508003581336c2abe0489a6368465a81a0acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= Date: Fri, 4 Oct 2024 11:47:34 +0200 Subject: [PATCH 2/2] feat: improve log --- aggregator/aggregator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aggregator/aggregator.go b/aggregator/aggregator.go index dd3be1a9..b1f5be3e 100644 --- a/aggregator/aggregator.go +++ b/aggregator/aggregator.go @@ -1957,7 +1957,7 @@ func (a *Aggregator) handleMonitoredTxResult(result ethtxmanager.MonitoredTxResu err = a.state.DeleteGeneratedProofs(a.ctx, firstBatch, lastBatch, nil) if err != nil { - mTxResultLogger.Errorf("failed to delete generated proofs, err: %v", err) + mTxResultLogger.Errorf("failed to delete generated proofs from %d to %d: %v", firstBatch, lastBatch, err) } mTxResultLogger.Debugf("deleted generated proofs from %d to %d", firstBatch, lastBatch)