Skip to content
This repository was archived by the owner on Nov 21, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions rpc/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -107,9 +108,7 @@ func (b *BatchEndpoints) GetL2BlockTimestamp(blockHash string) (uint64, error) {

log.Infof("Getting l2 block timestamp from RPC. Block hash: %s", blockHash)

ctx, cancel := context.WithTimeout(context.Background(), b.readTimeout)
defer cancel()
response, err := rpc.JSONRPCCallWithContext(ctx, b.url, "eth_getBlockByHash", blockHash, false)
response, err := jSONRPCCall(b.url, "eth_getBlockByHash", blockHash, false)
if err != nil {
return 0, err
}
Expand All @@ -119,14 +118,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
37 changes: 29 additions & 8 deletions rpc/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/0xPolygon/cdk-rpc/rpc"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

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

tests := []struct {
name string
batch uint64
Expand Down Expand Up @@ -76,8 +75,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 +197,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 +227,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 +256,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", 30*time.Second)
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", 30*time.Second)
timestamp, err := sut.GetL2BlockTimestamp("0x123456")
require.Error(t, err)
require.Equal(t, uint64(0), timestamp)
require.Contains(t, err.Error(), "is 0")
}