Skip to content

Commit

Permalink
Feature/#1506 forced batch (#1523)
Browse files Browse the repository at this point in the history
* ForceBatch unit test

* SequenceForceBatch unit test

* Fix forcedBatch check

* linter

* Fix reset forcedBatch

* batch and forcedbatch table modified

* fix
  • Loading branch information
ARR552 committed Jan 11, 2023
1 parent dae5060 commit c5f5f21
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 183 deletions.
15 changes: 15 additions & 0 deletions db/migrations/state/0003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +migrate Up
ALTER TABLE state.forced_batch
DROP COLUMN IF EXISTS batch_num;

ALTER TABLE state.batch
ADD COLUMN forced_batch_num BIGINT;
ALTER TABLE state.batch
ADD FOREIGN KEY (forced_batch_num) REFERENCES state.forced_batch(forced_batch_num);

-- +migrate Down
ALTER TABLE state.batch
DROP COLUMN IF EXISTS forced_batch_num;

ALTER TABLE state.forced_batch
ADD COLUMN batch_num BIGINT;
1 change: 0 additions & 1 deletion sequencer/broadcast/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ type stateInterface interface {
GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*state.Batch, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
GetEncodedTransactionsByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (encoded []string, err error)
GetForcedBatchByBatchNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.ForcedBatch, error)
GetExitRootByGlobalExitRoot(ctx context.Context, ger common.Hash, dbTx pgx.Tx) (*state.GlobalExitRoot, error)
}
25 changes: 1 addition & 24 deletions sequencer/broadcast/mocks/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions sequencer/broadcast/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ func (s *Server) genericGetBatch(ctx context.Context, batch *state.Batch) (*pb.G
}
}

var forcedBatchNum uint64
forcedBatch, err := s.state.GetForcedBatchByBatchNumber(ctx, batch.BatchNumber, nil)
if err == nil {
forcedBatchNum = forcedBatch.ForcedBatchNumber
} else if err != state.ErrNotFound {
return nil, err
}

var mainnetExitRoot, rollupExitRoot string
ger, err := s.state.GetExitRootByGlobalExitRoot(ctx, batch.GlobalExitRoot, nil)
if err == nil {
Expand All @@ -108,6 +100,10 @@ func (s *Server) genericGetBatch(ctx context.Context, batch *state.Batch) (*pb.G
} else if err != state.ErrNotFound {
return nil, err
}
var fb uint64
if batch.ForcedBatchNum != nil {
fb = *batch.ForcedBatchNum
}

return &pb.GetBatchResponse{
BatchNumber: batch.BatchNumber,
Expand All @@ -119,7 +115,7 @@ func (s *Server) genericGetBatch(ctx context.Context, batch *state.Batch) (*pb.G
RollupExitRoot: rollupExitRoot,
Timestamp: uint64(batch.Timestamp.Unix()),
Transactions: transactions,
ForcedBatchNumber: forcedBatchNum,
ForcedBatchNumber: fb,
}, nil
}

Expand Down
2 changes: 0 additions & 2 deletions sequencer/broadcast/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func TestBroadcastServerGetBatch(t *testing.T) {
}
st.On("GetBatchByNumber", mock.AnythingOfType("*context.valueCtx"), tc.inputBatchNumber, nil).Return(tc.expectedBatch, err)
st.On("GetEncodedTransactionsByBatchNumber", mock.AnythingOfType("*context.valueCtx"), tc.inputBatchNumber, nil).Return(tc.expectedEncodedTxs, err)
st.On("GetForcedBatchByBatchNumber", mock.AnythingOfType("*context.valueCtx"), tc.inputBatchNumber, nil).Return(tc.expectedForcedBatch, err)
if tc.expectedBatch != nil {
st.On("GetExitRootByGlobalExitRoot", mock.AnythingOfType("*context.valueCtx"), tc.expectedBatch.GlobalExitRoot, nil).Return(tc.expectedGER, err)
}
Expand Down Expand Up @@ -217,7 +216,6 @@ func TestBroadcastServerGetLastBatch(t *testing.T) {
st.On("GetLastBatch", mock.AnythingOfType("*context.valueCtx"), nil).Return(tc.expectedBatch, err)
if tc.expectedBatch != nil {
st.On("GetEncodedTransactionsByBatchNumber", mock.AnythingOfType("*context.valueCtx"), tc.expectedBatch.BatchNumber, nil).Return(tc.expectedEncodedTxs, err)
st.On("GetForcedBatchByBatchNumber", mock.AnythingOfType("*context.valueCtx"), tc.expectedBatch.BatchNumber, nil).Return(tc.expectedForcedBatch, err)
st.On("GetExitRootByGlobalExitRoot", mock.AnythingOfType("*context.valueCtx"), tc.expectedBatch.GlobalExitRoot, nil).Return(tc.expectedGER, err)
}

Expand Down
2 changes: 2 additions & 0 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Batch struct {
Timestamp time.Time
Transactions []types.Transaction
GlobalExitRoot common.Hash
ForcedBatchNum *uint64
}

// ProcessingContext is the necessary data that a batch needs to provide to the runtime,
Expand All @@ -27,6 +28,7 @@ type ProcessingContext struct {
Coinbase common.Address
Timestamp time.Time
GlobalExitRoot common.Hash
ForcedBatchNum *uint64
}

// ProcessingReceipt indicates the outcome (StateRoot, AccInputHash) of processing a batch
Expand Down
1 change: 0 additions & 1 deletion state/forcedbatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// ForcedBatch represents a ForcedBatch
type ForcedBatch struct {
BlockNumber uint64
BatchNumber *uint64
ForcedBatchNumber uint64
Sequencer common.Address
GlobalExitRoot common.Hash
Expand Down
Loading

0 comments on commit c5f5f21

Please sign in to comment.