Skip to content

Commit

Permalink
fix stateroot on first run (#1740)
Browse files Browse the repository at this point in the history
* fix stateroot on first run

* add check stateroot in reprocessfull

* fix
  • Loading branch information
ToniRamirezM authored Mar 6, 2023
1 parent c3ca539 commit 51275b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
14 changes: 7 additions & 7 deletions sequencer/dbmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ func (d *dbManager) DeleteTransactionFromPool(ctx context.Context, txHash common
func (d *dbManager) storeProcessedTxAndDeleteFromPool() {
// TODO: Finish the retry mechanism and error handling
for {
txToStore := <-d.txsStore.Ch
log.Debugf("Storing tx %v", txToStore.txResponse.TxHash)
dbTx, err := d.BeginStateTransaction(d.ctx)
if err != nil {
log.Errorf("StoreProcessedTxAndDeleteFromPool: %v", err)
}

numberOfReorgs, err := d.state.CountReorgs(d.ctx, nil)
if err != nil {
log.Error("failed to get number of reorgs: %v", err)
Expand All @@ -180,6 +173,13 @@ func (d *dbManager) storeProcessedTxAndDeleteFromPool() {
continue
}

txToStore := <-d.txsStore.Ch
log.Debugf("Storing tx %v", txToStore.txResponse.TxHash)
dbTx, err := d.BeginStateTransaction(d.ctx)
if err != nil {
log.Errorf("StoreProcessedTxAndDeleteFromPool: %v", err)
}

err = d.StoreProcessedTransaction(d.ctx, txToStore.batchNumber, txToStore.txResponse, txToStore.coinbase, txToStore.timestamp, dbTx)
if err != nil {
err = dbTx.Rollback(d.ctx)
Expand Down
24 changes: 16 additions & 8 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,19 +485,27 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err
defer f.sharedResourcesMux.Unlock()
f.txsStore.Wg.Wait()

var lastBatch *state.Batch
var err error
for !f.isSynced(ctx) {
log.Info("wait for synchronizer to sync last batch")
time.Sleep(time.Second)
}
if lastBatchNum == nil {
batchNum, err := f.dbManager.GetLastBatchNumber(ctx)
for err != nil {
return fmt.Errorf("failed to get last batch number, err: %w", err)
lastBatch, err = f.dbManager.GetLastBatch(ctx)
if err != nil {
return fmt.Errorf("failed to get last batch, err: %w", err)
}
} else {
lastBatch, err = f.dbManager.GetBatchByNumber(ctx, *lastBatchNum, nil)
if err != nil {
return fmt.Errorf("failed to get last batch, err: %w", err)
}
lastBatchNum = &batchNum
}

batchNum := lastBatch.BatchNumber
lastBatchNum = &batchNum

isClosed, err := f.dbManager.IsBatchClosed(ctx, *lastBatchNum)
if err != nil {
return fmt.Errorf("failed to check if batch is closed, err: %w", err)
Expand All @@ -507,10 +515,8 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err
if err != nil {
return fmt.Errorf("failed to get latest ger, err: %w", err)
}
_, oldStateRoot, err := f.getLastBatchNumAndOldStateRoot(ctx)
if err != nil {
return fmt.Errorf("failed to get old state root, err: %w", err)
}

oldStateRoot := lastBatch.StateRoot
f.batch, err = f.openWIPBatch(ctx, *lastBatchNum+1, ger.GlobalExitRoot, oldStateRoot)
if err != nil {
return err
Expand All @@ -532,6 +538,8 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err
Caller: state.SequencerCallerLabel,
}

log.Info("synced with state", "lastBatchNum", *lastBatchNum, "stateRoot", f.batch.initialStateRoot.Hex())

return nil
}

Expand Down

0 comments on commit 51275b4

Please sign in to comment.