Skip to content

Commit

Permalink
fix: case of eth_getBlockByHash response null or tstamp zero (#275) (#…
Browse files Browse the repository at this point in the history
…277)

* cherry-pick #275
* fix: case of eth_getBlockByHash response null or tstamp zero
---------

Co-authored-by: Toni Ramírez <[email protected]>
  • Loading branch information
joanestebanr and ToniRamirezM authored Jan 23, 2025
1 parent 57b4b15 commit 8902419
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
17 changes: 13 additions & 4 deletions rpc/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down
36 changes: 28 additions & 8 deletions rpc/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
)

func Test_getBatchFromRPC(t *testing.T) {
t.Parallel()

tests := []struct {
name string
batch uint64
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -200,8 +196,6 @@ func Test_getBatchWitnessRPC(t *testing.T) {
}

func Test_getGetL2BlockTimestamp(t *testing.T) {
t.Parallel()

tests := []struct {
name string
blockHash []byte
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

0 comments on commit 8902419

Please sign in to comment.