Skip to content
Merged
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
16 changes: 7 additions & 9 deletions sidecar/rpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ type NodeStatus struct {
func (c *StatusClient) Endpoint() string { return c.endpoint }

type statusResponse struct {
Result struct {
SyncInfo struct {
LatestBlockHeight string `json:"latest_block_height"`
CatchingUp bool `json:"catching_up"`
} `json:"sync_info"`
} `json:"result"`
SyncInfo struct {
LatestBlockHeight string `json:"latest_block_height"`
CatchingUp bool `json:"catching_up"`
} `json:"sync_info"`
}

// NewStatusClient creates a client targeting the given RPC endpoint.
Expand Down Expand Up @@ -79,14 +77,14 @@ func (c *StatusClient) Status(ctx context.Context) (*NodeStatus, error) {
return nil, fmt.Errorf("decoding /status: %w", err)
}

h, err := strconv.ParseInt(rpcResp.Result.SyncInfo.LatestBlockHeight, 10, 64)
h, err := strconv.ParseInt(rpcResp.SyncInfo.LatestBlockHeight, 10, 64)
if err != nil {
return nil, fmt.Errorf("parsing latest_block_height %q: %w", rpcResp.Result.SyncInfo.LatestBlockHeight, err)
return nil, fmt.Errorf("parsing latest_block_height %q: %w", rpcResp.SyncInfo.LatestBlockHeight, err)
}

return &NodeStatus{
LatestBlockHeight: h,
CatchingUp: rpcResp.Result.SyncInfo.CatchingUp,
CatchingUp: rpcResp.SyncInfo.CatchingUp,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions sidecar/rpc/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestStatusClient_LatestHeight(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"result":{"sync_info":{"latest_block_height":"12345","catching_up":false}}}`))
_, _ = w.Write([]byte(`{"sync_info":{"latest_block_height":"12345","catching_up":false}}`))
}))
defer srv.Close()

Expand All @@ -25,7 +25,7 @@ func TestStatusClient_LatestHeight(t *testing.T) {

func TestStatusClient_CatchingUp(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"result":{"sync_info":{"latest_block_height":"100","catching_up":true}}}`))
_, _ = w.Write([]byte(`{"sync_info":{"latest_block_height":"100","catching_up":true}}`))
}))
defer srv.Close()

Expand Down Expand Up @@ -79,8 +79,8 @@ func TestStatusClient_InvalidBlockHeight(t *testing.T) {
name string
payload string
}{
{"empty height", `{"result":{"sync_info":{"latest_block_height":"","catching_up":false}}}`},
{"non-numeric height", `{"result":{"sync_info":{"latest_block_height":"abc","catching_up":false}}}`},
{"empty height", `{"sync_info":{"latest_block_height":"","catching_up":false}}`},
{"non-numeric height", `{"sync_info":{"latest_block_height":"abc","catching_up":false}}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion sidecar/tasks/await_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func heightServer(heights ...int64) *httptest.Server {
idx++
}
mu.Unlock()
fmt.Fprintf(w, `{"result":{"sync_info":{"latest_block_height":"%d","catching_up":false}}}`, heights[i])
fmt.Fprintf(w, `{"sync_info":{"latest_block_height":"%d","catching_up":false}}`, heights[i])
}))
}

Expand Down
22 changes: 9 additions & 13 deletions sidecar/tasks/result_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,17 @@ func queryLatestHeight(ctx context.Context, rpcEndpoint string) (int64, error) {
}

var rpcResp struct {
Result struct {
SyncInfo struct {
LatestBlockHeight string `json:"latest_block_height"`
} `json:"sync_info"`
} `json:"result"`
SyncInfo struct {
LatestBlockHeight string `json:"latest_block_height"`
} `json:"sync_info"`
}
if err := json.NewDecoder(resp.Body).Decode(&rpcResp); err != nil {
return 0, fmt.Errorf("decoding /status response: %w", err)
}

h, err := strconv.ParseInt(rpcResp.Result.SyncInfo.LatestBlockHeight, 10, 64)
h, err := strconv.ParseInt(rpcResp.SyncInfo.LatestBlockHeight, 10, 64)
if err != nil {
return 0, fmt.Errorf("parsing latest_block_height %q: %w", rpcResp.Result.SyncInfo.LatestBlockHeight, err)
return 0, fmt.Errorf("parsing latest_block_height %q: %w", rpcResp.SyncInfo.LatestBlockHeight, err)
}
if h <= 0 {
return 0, fmt.Errorf("latest_block_height is %d, node may still be syncing", h)
Expand Down Expand Up @@ -285,14 +283,12 @@ func queryBlockResults(ctx context.Context, rpcEndpoint string, height int64) (j
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, bytes.TrimSpace(body))
}

var rpcResp struct {
Result json.RawMessage `json:"result"`
}
if err := json.NewDecoder(resp.Body).Decode(&rpcResp); err != nil {
return nil, fmt.Errorf("decoding response: %w", err)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response body: %w", err)
}

return rpcResp.Result, nil
return json.RawMessage(body), nil
}

func parseExportConfig(params map[string]any) (ResultExportConfig, error) {
Expand Down
6 changes: 3 additions & 3 deletions sidecar/tasks/result_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func fakeRPCServer(latestHeight int64) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/status":
fmt.Fprintf(w, `{"result":{"sync_info":{"latest_block_height":"%d"}}}`, latestHeight)
fmt.Fprintf(w, `{"sync_info":{"latest_block_height":"%d"}}`, latestHeight)
case r.URL.Path == "/block_results":
fmt.Fprint(w, `{"result":{}}`)
fmt.Fprint(w, `{}`)
default:
http.NotFound(w, r)
}
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestExportRPCNon200Status(t *testing.T) {

func TestQueryLatestHeight_ZeroHeight(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, `{"result":{"sync_info":{"latest_block_height":"0"}}}`)
fmt.Fprint(w, `{"sync_info":{"latest_block_height":"0"}}`)
}))
defer srv.Close()

Expand Down
Loading