Skip to content

Commit

Permalink
Populate f.currentGERHash and f.previousGERHash on finalizer start. (#…
Browse files Browse the repository at this point in the history
…3068)

* populate currentGERHash on finalizer start

* refactor

* fix typo

* refactor

* fix test

* fix test
  • Loading branch information
ToniRamirezM authored Jan 15, 2024
1 parent f33c392 commit 6b935a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sequencer/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions sequencer/finalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ func TestFinalizer_syncWithState(t *testing.T) {
now = time.Now
}()
one := uint64(1)
zero := uint64(0)
batches := []*state.Batch{
{
BatchNumber: 1,
Expand All @@ -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
Expand All @@ -630,6 +632,7 @@ func TestFinalizer_syncWithState(t *testing.T) {
{
name: "Success Closed Batch",
lastBatchNum: &one,
prevBatchNum: &zero,
isBatchClosed: true,
ger: oldHash,
batches: batches,
Expand All @@ -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{},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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{},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 6b935a9

Please sign in to comment.