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

Get lastbatch from data base #699

Merged
merged 3 commits into from
Jun 9, 2022
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
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