diff --git a/sequencer/finalizer.go b/sequencer/finalizer.go index 0471eaea0e..a64067f9ac 100644 --- a/sequencer/finalizer.go +++ b/sequencer/finalizer.go @@ -1078,6 +1078,16 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err log.Infof("Initial Batch.InitialStateRoot: %s", f.batch.initialStateRoot.String()) log.Infof("Initial Batch.localExitRoot: %s", f.batch.localExitRoot.String()) + var previousGER = state.ZeroHash + if batchNum >= 1 { + previousBatchNumber := batchNum - 1 + previousBatch, err := f.dbManager.GetBatchByNumber(ctx, previousBatchNumber, nil) + if err != nil { + return fmt.Errorf("failed to get previous batch, err: %w", err) + } + previousGER = previousBatch.GlobalExitRoot + } + f.processRequest = state.ProcessRequest{ BatchNumber: *lastBatchNum, OldStateRoot: f.batch.stateRoot, @@ -1088,6 +1098,9 @@ func (f *finalizer) syncWithState(ctx context.Context, lastBatchNum *uint64) err Caller: stateMetrics.SequencerCallerLabel, } + f.previousGERHash = previousGER + f.currentGERHash = f.batch.globalExitRoot + log.Infof("synced with state, lastBatchNum: %d. State root: %s", *lastBatchNum, f.batch.initialStateRoot.Hex()) return nil diff --git a/sequencer/finalizer_test.go b/sequencer/finalizer_test.go index 0dd0e693c1..32ef8e202c 100644 --- a/sequencer/finalizer_test.go +++ b/sequencer/finalizer_test.go @@ -604,6 +604,7 @@ func TestFinalizer_syncWithState(t *testing.T) { now = time.Now }() one := uint64(1) + zero := uint64(0) batches := []*state.Batch{ { BatchNumber: 1, @@ -615,6 +616,7 @@ func TestFinalizer_syncWithState(t *testing.T) { name string batches []*state.Batch lastBatchNum *uint64 + prevBatchNum *uint64 isBatchClosed bool ger common.Hash getWIPBatchErr error @@ -630,6 +632,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Success Closed Batch", lastBatchNum: &one, + prevBatchNum: &zero, isBatchClosed: true, ger: oldHash, batches: batches, @@ -653,6 +656,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Success Open Batch", lastBatchNum: &one, + prevBatchNum: &zero, isBatchClosed: false, batches: batches, ger: common.Hash{}, @@ -675,6 +679,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to get last batch", lastBatchNum: nil, + prevBatchNum: nil, batches: batches, isBatchClosed: true, ger: oldHash, @@ -684,6 +689,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to check if batch is closed", lastBatchNum: &one, + prevBatchNum: &zero, batches: batches, isBatchClosed: true, ger: oldHash, @@ -693,6 +699,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to get work-in-progress batch", lastBatchNum: &one, + prevBatchNum: &zero, batches: batches, isBatchClosed: false, ger: common.Hash{}, @@ -702,6 +709,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to open new batch", lastBatchNum: &one, + prevBatchNum: &zero, batches: batches, isBatchClosed: true, ger: oldHash, @@ -717,6 +725,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to get batch by number", lastBatchNum: &one, + prevBatchNum: &zero, batches: batches, isBatchClosed: true, ger: oldHash, @@ -732,6 +741,7 @@ func TestFinalizer_syncWithState(t *testing.T) { { name: "Error Failed to get latest GER", lastBatchNum: &one, + prevBatchNum: &zero, batches: batches, isBatchClosed: true, ger: oldHash, @@ -747,6 +757,7 @@ func TestFinalizer_syncWithState(t *testing.T) { dbManagerMock.Mock.On("GetLastBatch", ctx).Return(tc.batches[0], tc.getLastBatchErr).Once() } else { dbManagerMock.On("GetBatchByNumber", ctx, *tc.lastBatchNum, nil).Return(tc.batches[0], tc.getLastBatchByNumberErr).Once() + dbManagerMock.On("GetBatchByNumber", ctx, *tc.prevBatchNum, nil).Return(tc.batches[0], tc.getLastBatchByNumberErr) } if tc.getLastBatchByNumberErr == nil { if tc.getLastBatchErr == nil {