From 7b612868e71449c95852cd134077a9c05ded2a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20Ram=C3=ADrez?= <58293609+ToniRamirezM@users.noreply.github.com> Date: Thu, 9 Jun 2022 09:42:01 +0200 Subject: [PATCH] Get lastbatch from data base (#699) * Get lastbach from data base * Delete comment --- state/batchprocessor.go | 16 +++++++++++----- state/state.go | 7 +------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/state/batchprocessor.go b/state/batchprocessor.go index 0dc4fa14a4..a68ad9b40f 100644 --- a/state/batchprocessor.go +++ b/state/batchprocessor.go @@ -60,7 +60,6 @@ var InvalidTxErrors = map[string]bool{ type BatchProcessor struct { SequencerAddress common.Address SequencerChainID uint64 - LastBatch *Batch CumulativeGasUsed uint64 MaxCumulativeGasUsed uint64 Host Host @@ -234,8 +233,16 @@ func (b *BatchProcessor) processTransaction(ctx context.Context, tx *types.Trans func (b *BatchProcessor) populateBatchHeader(batch *Batch) { parentHash := common.Hash{} - if b.LastBatch != nil { - parentHash = b.LastBatch.Hash() + + // Get last batch from data base to ensure consistency + ctx := context.Background() + lastBatch, err := b.Host.State.GetLastBatchByStateRoot(ctx, b.Host.stateRoot, "") + if err != nil { + log.Errorf("failed to get last batch to populate batch header, err: %v", err) + } + + if lastBatch != nil { + parentHash = lastBatch.Hash() } rr := make([]*types.Receipt, 0, len(batch.Receipts)) @@ -526,6 +533,7 @@ func (b *BatchProcessor) commit(ctx context.Context, batch *Batch) error { if err != nil { return err } + log.Debugf("successfully stored batch %v into data base", batch.Header.Number) // store transactions for i, tx := range batch.Transactions { @@ -560,8 +568,6 @@ func (b *BatchProcessor) commit(ctx context.Context, batch *Batch) error { } } - b.LastBatch = batch - return nil } diff --git a/state/state.go b/state/state.go index 1e8770de87..8f1479a474 100644 --- a/state/state.go +++ b/state/state.go @@ -143,11 +143,6 @@ func (s *State) NewBatchProcessor(ctx context.Context, sequencerAddress common.A chainID = sq.ChainID.Uint64() } - lastBatch, err := s.GetLastBatchByStateRoot(ctx, stateRoot, txBundleID) - if err != ErrNotFound && err != nil { - return nil, err - } - logs := make(map[common.Hash][]*types.Log) host := Host{State: s, stateRoot: stateRoot, txBundleID: txBundleID, logs: logs} host.setRuntime(evm.NewEVM()) @@ -157,7 +152,7 @@ func (s *State) NewBatchProcessor(ctx context.Context, sequencerAddress common.A } host.forks = runtime.AllForksEnabled.At(blockNumber) - batchProcessor := &BatchProcessor{SequencerAddress: sequencerAddress, SequencerChainID: chainID, LastBatch: lastBatch, MaxCumulativeGasUsed: s.cfg.MaxCumulativeGasUsed, Host: host} + batchProcessor := &BatchProcessor{SequencerAddress: sequencerAddress, SequencerChainID: chainID, MaxCumulativeGasUsed: s.cfg.MaxCumulativeGasUsed, Host: host} batchProcessor.Host.setRuntime(evm.NewEVM()) return batchProcessor, nil