Skip to content

Commit

Permalink
review l2 block and batch tags: earliest, latest, pending, safe and f…
Browse files Browse the repository at this point in the history
…inalized (#2673)

* add batch pending, safe and finalized tags for jRPC queries

* fix linter issues

* review l2 block and batch tags to handle earliest, latest, pending, safe and finalized properly

* fix unit tests
  • Loading branch information
tclemos authored Oct 30, 2023
1 parent 836c456 commit 3335bdf
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 160 deletions.
2 changes: 1 addition & 1 deletion jsonrpc/endpoints_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (d *DebugEndpoints) TraceBatchByNumber(httpRequest *http.Request, number ty
const bufferSize = 10

return d.txMan.NewDbTxScope(d.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
batchNumber, rpcErr := number.GetNumericBatchNumber(ctx, d.state, dbTx)
batchNumber, rpcErr := number.GetNumericBatchNumber(ctx, d.state, d.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/endpoints_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (z *ZKEVMEndpoints) VerifiedBatchNumber() (interface{}, types.Error) {
func (z *ZKEVMEndpoints) GetBatchByNumber(batchNumber types.BatchNumber, fullTx bool) (interface{}, types.Error) {
return z.txMan.NewDbTxScope(z.state, func(ctx context.Context, dbTx pgx.Tx) (interface{}, types.Error) {
var err error
batchNumber, rpcErr := batchNumber.GetNumericBatchNumber(ctx, z.state, dbTx)
batchNumber, rpcErr := batchNumber.GetNumericBatchNumber(ctx, z.state, z.etherman, dbTx)
if rpcErr != nil {
return nil, rpcErr
}
Expand Down
6 changes: 3 additions & 3 deletions jsonrpc/endpoints_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ func TestGetBatchByNumber(t *testing.T) {
Once()

m.State.
On("GetLastBatchNumber", context.Background(), m.DbTx).
On("GetLastClosedBatchNumber", context.Background(), m.DbTx).
Return(uint64(tc.ExpectedResult.Number), nil).
Once()

Expand Down Expand Up @@ -1063,7 +1063,7 @@ func TestGetBatchByNumber(t *testing.T) {
Once()

m.State.
On("GetLastBatchNumber", context.Background(), m.DbTx).
On("GetLastClosedBatchNumber", context.Background(), m.DbTx).
Return(uint64(0), errors.New("failed to get last batch number")).
Once()
},
Expand All @@ -1085,7 +1085,7 @@ func TestGetBatchByNumber(t *testing.T) {
Once()

m.State.
On("GetLastBatchNumber", context.Background(), m.DbTx).
On("GetLastClosedBatchNumber", context.Background(), m.DbTx).
Return(uint64(1), nil).
Once()

Expand Down
120 changes: 72 additions & 48 deletions jsonrpc/mocks/mock_state.go

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

82 changes: 63 additions & 19 deletions jsonrpc/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ import (
)

const (
// PendingBlockNumber represents the pending block number
PendingBlockNumber = BlockNumber(-3)
// EarliestBlockNumber represents the earliest block number, always 0
EarliestBlockNumber = BlockNumber(-1)
// LatestBlockNumber represents the latest block number
LatestBlockNumber = BlockNumber(-2)
// EarliestBlockNumber represents the earliest block number
EarliestBlockNumber = BlockNumber(-1)
// SafeBlockNumber represents the last virtualized block number
// PendingBlockNumber represents the pending block number
PendingBlockNumber = BlockNumber(-3)
// SafeBlockNumber represents the last verified block number that is safe on Ethereum
SafeBlockNumber = BlockNumber(-4)
// FinalizedBlockNumber represents the last verified block number
// FinalizedBlockNumber represents the last verified block number that is finalized on Ethereum
FinalizedBlockNumber = BlockNumber(-5)

// LatestBatchNumber represents the latest batch number
LatestBatchNumber = BatchNumber(-2)
// EarliestBatchNumber represents the earliest batch number
// EarliestBatchNumber represents the earliest batch number, always 0
EarliestBatchNumber = BatchNumber(-1)
// LatestBatchNumber represents the last closed batch number
LatestBatchNumber = BatchNumber(-2)
// PendingBatchNumber represents the last batch in the trusted state
PendingBatchNumber = BatchNumber(-3)
// SafeBatchNumber represents the last batch verified in a block that is safe on Ethereum
SafeBatchNumber = BatchNumber(-4)
// FinalizedBatchNumber represents the last batch verified in a block that has been finalized on Ethereum
FinalizedBatchNumber = BatchNumber(-5)

// Earliest contains the string to represent the earliest block known.
Earliest = "earliest"
Expand Down Expand Up @@ -170,6 +176,9 @@ func (b *BlockNumber) GetNumericBlockNumber(ctx context.Context, s StateInterfac
}

switch bValue {
case EarliestBlockNumber:
return 0, nil

case LatestBlockNumber, PendingBlockNumber:
lastBlockNumber, err := s.GetLastL2BlockNumber(ctx, dbTx)
if err != nil {
Expand All @@ -178,16 +187,13 @@ func (b *BlockNumber) GetNumericBlockNumber(ctx context.Context, s StateInterfac

return lastBlockNumber, nil

case EarliestBlockNumber:
return 0, nil

case SafeBlockNumber:
l1SafeBlockNumber, err := e.GetSafeBlockNumber(ctx)
if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the safe block number from ethereum")
}

lastBlockNumber, err := s.GetSafeL2BlockNumber(ctx, l1SafeBlockNumber, dbTx)
lastBlockNumber, err := s.GetLastVerifiedL2BlockNumberUntilL1Block(ctx, l1SafeBlockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return 0, nil
} else if err != nil {
Expand All @@ -202,7 +208,7 @@ func (b *BlockNumber) GetNumericBlockNumber(ctx context.Context, s StateInterfac
return 0, NewRPCError(DefaultErrorCode, "failed to get the finalized block number from ethereum")
}

lastBlockNumber, err := s.GetFinalizedL2BlockNumber(ctx, l1FinalizedBlockNumber, dbTx)
lastBlockNumber, err := s.GetLastVerifiedL2BlockNumberUntilL1Block(ctx, l1FinalizedBlockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return 0, nil
} else if err != nil {
Expand Down Expand Up @@ -407,23 +413,61 @@ func (b *BatchNumber) UnmarshalJSON(buffer []byte) error {
}

// GetNumericBatchNumber returns a numeric batch number based on the BatchNumber instance
func (b *BatchNumber) GetNumericBatchNumber(ctx context.Context, s StateInterface, dbTx pgx.Tx) (uint64, Error) {
func (b *BatchNumber) GetNumericBatchNumber(ctx context.Context, s StateInterface, e EthermanInterface, dbTx pgx.Tx) (uint64, Error) {
bValue := LatestBatchNumber
if b != nil {
bValue = *b
}

switch bValue {
case EarliestBatchNumber:
return 0, nil

case LatestBatchNumber:
lastBatchNumber, err := s.GetLastBatchNumber(ctx, dbTx)
batchNumber, err := s.GetLastClosedBatchNumber(ctx, dbTx)
if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the last batch number from state")
}

return lastBatchNumber, nil
return batchNumber, nil

case EarliestBatchNumber:
return 0, nil
case PendingBatchNumber:
batchNumber, err := s.GetLastBatchNumber(ctx, dbTx)
if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the pending batch number from state")
}

return batchNumber, nil

case SafeBatchNumber:
l1SafeBlockNumber, err := e.GetSafeBlockNumber(ctx)
if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the safe batch number from ethereum")
}

batchNumber, err := s.GetLastVerifiedBatchNumberUntilL1Block(ctx, l1SafeBlockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return 0, nil
} else if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the safe batch number from state")
}

return batchNumber, nil

case FinalizedBatchNumber:
l1FinalizedBlockNumber, err := e.GetFinalizedBlockNumber(ctx)
if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the finalized batch number from ethereum")
}

batchNumber, err := s.GetLastVerifiedBatchNumberUntilL1Block(ctx, l1FinalizedBlockNumber, dbTx)
if errors.Is(err, state.ErrNotFound) {
return 0, nil
} else if err != nil {
return 0, NewRPCError(DefaultErrorCode, "failed to get the finalized batch number from state")
}

return batchNumber, nil

default:
if bValue < 0 {
Expand Down
Loading

0 comments on commit 3335bdf

Please sign in to comment.