diff --git a/rpc/batch.go b/rpc/batch.go index 59e10b20..bd75a374 100644 --- a/rpc/batch.go +++ b/rpc/batch.go @@ -15,7 +15,8 @@ import ( var ( // ErrBusy is returned when the witness server is busy - ErrBusy = errors.New("witness server is busy") + ErrBusy = errors.New("witness server is busy") + jSONRPCCall = rpc.JSONRPCCall ) const busyResponse = "busy" @@ -92,7 +93,7 @@ func (b *BatchEndpoints) GetL2BlockTimestamp(blockHash string) (uint64, error) { log.Infof("Getting l2 block timestamp from RPC. Block hash: %s", blockHash) - response, err := rpc.JSONRPCCall(b.url, "eth_getBlockByHash", blockHash, false) + response, err := jSONRPCCall(b.url, "eth_getBlockByHash", blockHash, false) if err != nil { return 0, err } @@ -102,14 +103,22 @@ func (b *BatchEndpoints) GetL2BlockTimestamp(blockHash string) (uint64, error) { return 0, fmt.Errorf("error in the response calling eth_getBlockByHash: %v", response.Error) } + if string(response.Result) == "null" { + log.Errorf("eth_getBlockByHash response is null. Block hash %s not found", blockHash) + return 0, fmt.Errorf("error response of eth_getBlockByHash is null. Block hash: %s. err: Not Found", blockHash) + } + // Get the l2 block from the response l2Block := zkeEVML2Block{} err = json.Unmarshal(response.Result, &l2Block) if err != nil { return 0, fmt.Errorf("error unmarshalling the l2 block from the response calling eth_getBlockByHash: %w", err) } - - return new(big.Int).SetBytes(common.FromHex(l2Block.Timestamp)).Uint64(), nil + timestamp := new(big.Int).SetBytes(common.FromHex(l2Block.Timestamp)).Uint64() + if timestamp == 0 { + return 0, fmt.Errorf("timestamp str '%s' from block hash %s is 0", l2Block.Timestamp, blockHash) + } + return timestamp, nil } func (b *BatchEndpoints) GetWitness(batchNumber uint64, fullWitness bool) ([]byte, error) { diff --git a/rpc/batch_test.go b/rpc/batch_test.go index d6940bf3..e3ce7699 100644 --- a/rpc/batch_test.go +++ b/rpc/batch_test.go @@ -14,8 +14,6 @@ import ( ) func Test_getBatchFromRPC(t *testing.T) { - t.Parallel() - tests := []struct { name string batch uint64 @@ -76,8 +74,6 @@ func Test_getBatchFromRPC(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { - t.Parallel() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var req rpc.Request err := json.NewDecoder(r.Body).Decode(&req) @@ -200,8 +196,6 @@ func Test_getBatchWitnessRPC(t *testing.T) { } func Test_getGetL2BlockTimestamp(t *testing.T) { - t.Parallel() - tests := []struct { name string blockHash []byte @@ -232,8 +226,6 @@ func Test_getGetL2BlockTimestamp(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { - t.Parallel() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var req rpc.Request err := json.NewDecoder(r.Body).Decode(&req) @@ -263,3 +255,31 @@ func Test_getGetL2BlockTimestamp(t *testing.T) { }) } } + +func Test_getGetL2BlockTimestampNull(t *testing.T) { + response := rpc.Response{ + Result: []byte(`null`), + } + jSONRPCCall = func(_, _ string, _ ...interface{}) (rpc.Response, error) { + return response, nil + } + sut := NewBatchEndpoints("http://localhost:8080") + timestamp, err := sut.GetL2BlockTimestamp("0x123456") + require.Error(t, err) + require.Equal(t, uint64(0), timestamp) + require.Contains(t, err.Error(), "error response of eth_getBlockByHash is null. Block hash: 0x123456. err: Not Found") +} + +func Test_getGetL2BlockTimestampZero(t *testing.T) { + response := rpc.Response{ + Result: []byte(`{"timestamp": "0x0"}`), + } + jSONRPCCall = func(_, _ string, _ ...interface{}) (rpc.Response, error) { + return response, nil + } + sut := NewBatchEndpoints("http://localhost:8080") + timestamp, err := sut.GetL2BlockTimestamp("0x123456") + require.Error(t, err) + require.Equal(t, uint64(0), timestamp) + require.Contains(t, err.Error(), "is 0") +}