Skip to content

Commit

Permalink
Get lastbatch from data base (#699)
Browse files Browse the repository at this point in the history
* Get lastbach from data base

* Delete comment
  • Loading branch information
ToniRamirezM authored Jun 9, 2022
1 parent f7a72f7 commit 7b61286
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 11 additions & 5 deletions state/batchprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -560,8 +568,6 @@ func (b *BatchProcessor) commit(ctx context.Context, batch *Batch) error {
}
}

b.LastBatch = batch

return nil
}

Expand Down
7 changes: 1 addition & 6 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down

0 comments on commit 7b61286

Please sign in to comment.